Skip to content

Commit

Permalink
Include the Zotero schema in the build. Resolve #592
Browse files Browse the repository at this point in the history
Also, as part of the build, automatically fetch
the latest version of the zotero-schema repo
  • Loading branch information
tnajdek committed Jan 27, 2025
1 parent baaca3f commit 9a5495e
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
[submodule "modules/web-common"]
path = modules/web-common
url = https://github.com/tnajdek/web-common.git
[submodule "modules/zotero-schema"]
path = modules/zotero-schema
url = https://github.com/zotero/zotero-schema.git
1 change: 1 addition & 0 deletions modules/zotero-schema
Submodule zotero-schema added at a605cc
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"clean:static": "rimraf tmp src/static/fonts src/static/note-editor src/static/pdf-reader src/static/pdf-worker",
"clean:all": "run-p clean clean:data clean:static",
"purge:submodules": "rimraf modules && git submodule update --init --recursive",
"build": "npm run build:prepare && NODE_ENV=production npm run build:all && npm run build:postprocess",
"build": "NODE_ENV=production npm run build:prepare && NODE_ENV=production npm run build:all && npm run build:postprocess",
"build:tests": "NODE_ENV=testing run-s \"build:prepare\" \"build:all\" \"build:postprocess\" \"build:layout-only-css\"",
"build:all": "run-p 'build:js -s' 'build:scss -s' 'build:html -s' 'build:static -s'",
"build:js": "mkdir -p build/static && rollup -c",
Expand All @@ -35,8 +35,9 @@
"build:static": "mkdir -p build/static && rsync -vazL src/static/* build/static/",
"build:postprocess": "postcss build/static/zotero-web-library.css --use autoprefixer --no-map -r",
"build:check-icons": "node scripts/check-icons.js",
"build:prepare": "run-p \"build:version\" \"build:locale\" \"build:date-formats\" \"build:fetch-or-build-modules\" \"build:fonts\" \"build:styles-json\" \"build:check-icons\"",
"build:prepare": "run-p \"build:version\" \"build:locale\" \"build:date-formats\" \"build:fetch-or-build-modules\" \"build:fonts\" \"build:styles-json\" \"build:check-icons\" \"build:update-schema-submodule\"",
"build:layout-only-css": "node scripts/build-layout-only-css.js",
"build:update-schema-submodule": "node scripts/update-schema.mjs",
"devel": "run-p 'devel:js -s' 'devel:scss -s' 'devel:html -s' 'devel:static -s'",
"devel:js": "rollup -c -w",
"devel:scss": "nodemon -q -w src/scss --ext 'scss' --exec 'npm run devbuild:scss'",
Expand Down
38 changes: 38 additions & 0 deletions scripts/update-schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from "fs-extra";
import { dirname, join } from "path";
import util from "util";
import child_process from "child_process";
import { fileURLToPath } from "url";


const exec = util.promisify(child_process.exec);
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
const modulePath = join(ROOT, "modules", "zotero-schema");
const schemaPath = join(modulePath, "schema.json");
let currentVersion = 0;

try {
const schema = await fs.readJson(schemaPath);
currentVersion = parseInt(schema.version);
} catch (e) {
// ignore
}

(async () => {
if (process.env.NODE_ENV !== 'production' || process.env.SKIP_SCHEMA_UPDATE) {
console.log(`Skipping schema update check (using version ${currentVersion})`);
return
}
try {
await exec("git pull origin master --ff-only", { cwd: modulePath });
const schema = await fs.readJson(schemaPath);
if (parseInt(schema.version) > currentVersion) {
console.log(`Updated schema repository (${currentVersion} -> ${schema.version})`);
}
console.log(`Using latest schema version ${schema.version}`);
} catch (e) {
console.error("Failed to pull zotero-schema repository");
console.error(e);
process.exit(1);
}
})();
20 changes: 5 additions & 15 deletions src/js/actions/init.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import api from 'zotero-api-client';
import { REQUEST_SCHEMA, RECEIVE_SCHEMA, ERROR_SCHEMA, CONFIGURE } from '../constants/actions';
import schema from 'zotero-schema/schema.json';
import { REQUEST_SCHEMA, RECEIVE_SCHEMA, CONFIGURE } from '../constants/actions';

const initialize = () => {
return async (dispatch, getState) => {
const { config: { apiConfig } } = getState();
dispatch({
type: REQUEST_SCHEMA
});

try {
const schema = (await api(null, apiConfig).schema().get()).getData();
dispatch({ type: RECEIVE_SCHEMA, schema });
} catch(error) {
dispatch({ type: ERROR_SCHEMA, error });
throw error;
}
return async (dispatch) => {
dispatch({ type: REQUEST_SCHEMA });
dispatch({ type: RECEIVE_SCHEMA, schema, embedded: true });
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/js/reducers/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const defaultState = {
itemTypeCreatorTypes: {},
itemTypeFields: {},
itemTemplates: {},
invalidated: false
invalidated: false,
isUsingEmbeddedSchema: null,
}

// TODO: localization
Expand All @@ -34,6 +35,7 @@ const meta = (state = { ...defaultState }, action) => {
case RECEIVE_SCHEMA:
return {
...state,
isUsingEmbeddedSchema: !!action?.embedded,
itemTypes: action.schema.itemTypes
.filter(({ itemType }) => !ignoredItemTypes.includes(itemType))
.map(({ itemType }) => ({
Expand Down
7 changes: 0 additions & 7 deletions test/config.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { renderWithProviders } from './utils/render';
import { MainZotero } from '../src/js/component/main';
import { applyAdditionalJestTweaks } from './utils/common';
import minState from './fixtures/state/minimal.json';
import schema from './fixtures/response/schema';

const minStateWithApiAuthorityPart = {
...minState,
Expand Down Expand Up @@ -44,16 +43,11 @@ describe('config', () => {
afterAll(() => server.close());

test(`Use apiAuthorityPart from config`, async () => {
let schemaRequested = false;
let settingsRequested = false;
let collectionsRequested = false;
let itemsRequested = false;
let tagsRequested = false;
server.use(
http.get('https://bazinga.zotero.org/schema', () => {
schemaRequested = true;
return HttpResponse.json(schema);
}),
http.get('https://bazinga.zotero.org/users/475425/settings/tagColors', () => {
settingsRequested = true;
return HttpResponse.json({});
Expand All @@ -79,7 +73,6 @@ describe('config', () => {
);
renderWithProviders(<MainZotero />, { preloadedState: minStateWithApiAuthorityPart });
expect(screen.getByRole('img', { name: 'Loading' })).toBeInTheDocument();
await waitFor(() => expect(schemaRequested).toBe(true));
await waitFor(() => expect(settingsRequested).toBe(true));
await waitFor(() => expect(collectionsRequested).toBe(true));
await waitFor(() => expect(itemsRequested).toBe(true));
Expand Down
8 changes: 0 additions & 8 deletions test/loading-screen.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ import { renderWithProviders } from './utils/render';
import { MainZotero } from '../src/js/component/main';
import { applyAdditionalJestTweaks } from './utils/common';
import minState from './fixtures/state/minimal.json';
import schema from './fixtures/response/schema';

applyAdditionalJestTweaks();

describe('Loading Screen', () => {
let schemaRequested = false;
let settingsRequested = false;
let collectionsRequestCounter = 0;
let server;

beforeAll(() => {
const handlers = [
http.get('https://api.zotero.org/schema', () => {
schemaRequested = true;
return HttpResponse.json(schema);
}),
http.get('https://api.zotero.org/users/475425/settings/tagColors', () => {
settingsRequested = true;
return HttpResponse.json({});
Expand Down Expand Up @@ -65,7 +59,6 @@ describe('Loading Screen', () => {
beforeEach(() => {
delete window.location;
window.location = new URL('http://localhost/');
schemaRequested = false;
settingsRequested = false;
collectionsRequestCounter = 0;
});
Expand All @@ -77,7 +70,6 @@ describe('Loading Screen', () => {
test('Shows Z while fetching data', async () => {
renderWithProviders(<MainZotero />, { preloadedState: minState });
expect(screen.getByRole('img', { name: 'Loading' })).toBeInTheDocument();
await waitFor(() => expect(schemaRequested).toBe(true));
await waitFor(() => expect(settingsRequested).toBe(true));
});

Expand Down
4 changes: 0 additions & 4 deletions test/unexpected-item-type.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { renderWithProviders } from './utils/render';
import { MainZotero } from '../src/js/component/main';
import { applyAdditionalJestTweaks } from './utils/common';
import minState from './fixtures/state/minimal.json';
import schema from './fixtures/response/schema';
import annotationItems from './fixtures/response/annotation-item.json';

applyAdditionalJestTweaks();
Expand All @@ -30,9 +29,6 @@ describe('Unexpected Item Types', () => {
delete window.location;
window.location = new URL('http://localhost/');
server.use(
http.get('https://api.zotero.org/schema', () => {
return HttpResponse.json(schema);
}),
http.get('https://api.zotero.org/users/475425/settings/tagColors', () => {
return HttpResponse.json({});
}),
Expand Down
4 changes: 0 additions & 4 deletions test/unexpected-url.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { renderWithProviders } from './utils/render';
import { MainZotero } from '../src/js/component/main';
import { applyAdditionalJestTweaks } from './utils/common';
import minState from './fixtures/state/minimal.json';
import schema from './fixtures/response/schema';
// import searchResults from './fixtures/response/zotero-user-search-results.json';
// import searchResultsTags from './fixtures/response/zotero-user-search-results-tags.json';
import groups from './fixtures/response/min-user-groups.json';
Expand All @@ -22,9 +21,6 @@ applyAdditionalJestTweaks();

describe('Unexpected URL', () => {
const handlers = [
http.get('https://api.zotero.org/schema', () => {
return HttpResponse.json(schema);
}),
http.get('https://api.zotero.org/users/475425/settings/tagColors', () => {
return HttpResponse.json({});
}),
Expand Down

0 comments on commit 9a5495e

Please sign in to comment.