Skip to content

Commit

Permalink
Merge branch 'MathSciNet' of github.com:adam3smith/translators; branc…
Browse files Browse the repository at this point in the history
…h 'master' of github.com:zotero/translators into master
  • Loading branch information
adam3smith committed Dec 12, 2023
2 parents e7dddcf + 3ad35cf commit 976c853
Show file tree
Hide file tree
Showing 69 changed files with 13,693 additions and 3,488 deletions.
13 changes: 10 additions & 3 deletions .ci/eslint-plugin-zotero-translator/lib/rules/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ module.exports = {
}
else if (testCase.type === 'search') {
// console.log(JSON.stringify(testCase.input))
const expected = ['DOI', 'ISBN', 'PMID', 'identifiers', 'contextObject', 'adsBibcode'];
if (!Object.keys(testCase.input).every(key => expected.includes(key))) {
let invalidKey = Object.keys(testCase.input).find(key => !expected.includes(key));
const expected = ['DOI', 'ISBN', 'PMID', 'identifiers', 'contextObject', 'adsBibcode', 'ericNumber'];
let keys;
if (Array.isArray(testCase.input)) {
keys = testCase.input.flatMap(Object.keys);
}
else {
keys = Object.keys(testCase.input);
}
if (!keys.every(key => expected.includes(key))) {
let invalidKey = keys.find(key => !expected.includes(key));
context.report({
message: `${prefix} of type "${testCase.type}" has invalid search term '${invalidKey}' - expected one of ${expected.join(', ')}`,
loc,
Expand Down
10 changes: 9 additions & 1 deletion .ci/eslint-plugin-zotero-translator/lib/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ const path = require('path');
const findRoot = require('find-root');
const childProcess = require('child_process');

const repo = path.resolve(findRoot(__dirname, dir => fs.existsSync(path.resolve(dir, '.git'))));
let repo;
try {
repo = path.resolve(findRoot(__dirname, dir => fs.existsSync(path.resolve(dir, '.git'))));
}
catch (e) {
console.error('ERROR: Translators can only be linted inside a clone of the zotero/translators repo (not a ZIP downloaded from GitHub)');
console.error(' git clone https://github.com/zotero/translators.git');
process.exit(1);
}

const metaDataRules = [
'zotero-translator/header-valid-json',
Expand Down
75 changes: 75 additions & 0 deletions .ci/updateTypes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env node

import { readFile, writeFile } from 'fs/promises';

const INDEX_D_TS_URL = new URL('../index.d.ts', import.meta.url);
const SCHEMA_JSON_URL = new URL('../../zotero-client/resource/schema/global/schema.json', import.meta.url);

const BEGIN_MARKER = '\t/* *** BEGIN GENERATED TYPES *** */';
const END_MARKER = '\t/* *** END GENERATED TYPES *** */';

async function updateIndexDTS() {
let indexDTS = await readFile(INDEX_D_TS_URL, { encoding: 'utf8' });
let schema = JSON.parse(await readFile(SCHEMA_JSON_URL));

let typeItemTypes = '\ttype ItemTypes = {';
let itemTypeTypes = '';
let creatorTypes = new Set();

for (let typeSchema of schema.itemTypes) {
let itemType = typeSchema.itemType;
if (['annotation', 'attachment', 'note'].includes(itemType)) {
continue;
}

let itemTypeUppercase = itemType[0].toUpperCase() + itemType.substring(1) + 'Item';
if (itemTypeUppercase == 'TvBroadcastItem') {
itemTypeUppercase = 'TVBroadcastItem';
}

typeItemTypes += `\n\t\t"${itemType}": ${itemTypeUppercase},`;
itemTypeTypes += `\n\n\ttype ${itemTypeUppercase} = {`;
itemTypeTypes += `\n\t\titemType: "${itemType}";`;
for (let { field } of typeSchema.fields) {
itemTypeTypes += `\n\t\t${field}?: string;`
}

let creatorTypesJoined = typeSchema.creatorTypes.map(typeSchema => '"' + typeSchema.creatorType + '"').join(' | ');
itemTypeTypes += `\n\n\t\tcreators: Creator<${creatorTypesJoined}>[];`;
itemTypeTypes += '\n\t\tattachments: Attachment[];';
itemTypeTypes += '\n\t\ttags: Tag[];';
itemTypeTypes += '\n\t\tnotes: Note[];';
itemTypeTypes += '\n\t\tseeAlso: string[];';
itemTypeTypes += '\n\t\tcomplete(): void;';
itemTypeTypes += '\n\n\t\t[key: string]: string;';
itemTypeTypes += '\n\t};';

for (let { creatorType } of typeSchema.creatorTypes) {
creatorTypes.add(creatorType);
}
}
typeItemTypes += '\n\t};'

let typeCreatorType = '\n\ttype CreatorType =';
for (let creatorType of Array.from(creatorTypes).sort()) {
typeCreatorType += `\n\t\t| "${creatorType}"`;
}
typeCreatorType += ';';

let beginIdx = indexDTS.indexOf(BEGIN_MARKER);
let endIdx = indexDTS.indexOf(END_MARKER);
if (beginIdx == -1 || endIdx == -1) {
throw new Error('Could not find generated types section in index.d.ts');
}

indexDTS = indexDTS.substring(0, beginIdx) + BEGIN_MARKER + '\n'
+ typeItemTypes
+ itemTypeTypes
+ '\n' + typeCreatorType
+ '\n'
+ indexDTS.substring(endIdx);

await writeFile(INDEX_D_TS_URL, indexDTS);
}

updateIndexDTS();
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"env": {
"browser": true,
"es2017": true
"es2018": true
},
"extends": [
"@zotero"
],
"parserOptions": {
"ecmaVersion": 2018
},
"globals": {
"Zotero": "readonly",
"Z": "readonly",
Expand Down
Loading

0 comments on commit 976c853

Please sign in to comment.