From fff76c65f327647ca995dca4df9eaee481ff8c40 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Sat, 13 Jul 2024 10:12:05 -0700 Subject: [PATCH] Tweak comments, async, hide node deprecations for `new-schema` --- cli.js | 77 ++++++++++++++++++++++++++++------------------------ package.json | 2 +- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/cli.js b/cli.js index 622ef23b1e4..fe2ac034ae1 100644 --- a/cli.js +++ b/cli.js @@ -19,21 +19,25 @@ import * as jsoncParser from 'jsonc-parser' import chalk from 'chalk' import minimist from 'minimist' -const AjvDraft06SchemaJson = JSON.parse( - fs.readFileSync('node_modules/ajv/dist/refs/json-schema-draft-06.json'), +const AjvDraft06SchemaJson = readJsonFile( + 'node_modules/ajv/dist/refs/json-schema-draft-06.json', ) const temporaryCoverageDir = './temp' const schemaDir = './src/schemas/json' const testPositiveDir = './src/test' const testNegativeDir = './src/negative_test' const urlSchemaStore = 'https://json.schemastore.org/' -const catalog = JSON.parse(fs.readFileSync('./src/api/json/catalog.json')) +const catalog = readJsonFile('./src/api/json/catalog.json') const schemaValidation = jsoncParser.parse( - fs.readFileSync('./src/schema-validation.json', 'utf-8'), + await fs.promises.readFile('./src/schema-validation.json', 'utf-8'), ) -const schemasToBeTested = fs.readdirSync(schemaDir) -const foldersPositiveTest = fs.readdirSync(testPositiveDir) -const foldersNegativeTest = fs.readdirSync(testNegativeDir) +const [schemasToBeTested, foldersPositiveTest, foldersNegativeTest] = + await Promise.all([ + fs.promises.readdir(schemaDir), + fs.promises.readdir(testPositiveDir), + fs.promises.readdir(testNegativeDir), + ]) + // prettier-ignore const SCHEMA_DIALECTS = [ { schemaName: '2020-12', url: 'https://json-schema.org/draft/2020-12/schema', isActive: true, isTooHigh: true }, @@ -56,14 +60,14 @@ const log = { }, } -const argv = minimist(process.argv.slice(2), { - boolean: ['help', 'lint'], -}) - function readJsonFile(/** @type {string} */ filename) { return JSON.parse(fs.readFileSync(filename, 'utf-8')) } +const argv = minimist(process.argv.slice(2), { + boolean: ['help', 'lint'], +}) + function skipThisFileName(/** @type {string} */ name) { // This macOS file must always be ignored. return name === '.DS_Store' @@ -80,9 +84,8 @@ function getUrlFromCatalog(catalogUrl) { } /** - * @summary Create an exception with error text - * Make sure that the user see this error message. - * And not only the error message generated by npm after this message. + * @summary Calling this will terminate the process and show the text + * of each error message, in addition to npm's error message. * @param {string[]} errorText */ function throwWithErrorText(errorText) { @@ -102,9 +105,10 @@ function throwWithErrorText(errorText) { async function remoteSchemaFile(schemaOnlyScan, showLog = true) { for (const { url } of catalog.schemas) { if (url.startsWith(urlSchemaStore)) { - // Skip local schema + // Skip local schemas continue } + try { const res = await fetch(url) const resText = await res.text() @@ -407,7 +411,6 @@ async function localSchemaFileAndTestFile( * @param {Schema} schema */ function testSchemaFileForBOM(schema) { - // JSON schema file must not have any BOM type const buffer = schema.rawFile const bomTypes = [ { name: 'UTF-8', signature: [0xef, 0xbb, 0xbf] }, @@ -441,8 +444,8 @@ function testSchemaFileForBOM(schema) { */ /** - * There are multiple AJV version for each $schema version. - * return the correct AJV instance + * @summary There are multiple AJV versions for each $schema version. This returns + * the correct AJV instance * @param {FactoryAJVParameter} schemaName * @returns {Object} */ @@ -523,7 +526,7 @@ function factoryAJV({ */ /** - * Get the option items for this specific jsonName + * @summary Gets the option items for a particular `jsonName` * @param {string} jsonName * @returns {getOptionReturn} */ @@ -544,7 +547,6 @@ function getOption(jsonName) { }, ) - // return all the collected values return { unknownFormatsList, unknownKeywordsList, @@ -704,8 +706,9 @@ function showSchemaVersions() { try { obj = getObj_(schema.jsonObj) } catch { - // suppress possible JSON.parse exception. It will be processed as obj = undefined + // Suppress `JSON.parse` exceptions, leaving obj with value of `undefined` } + if (obj) { schemaDialectCounts.set(obj.url, schemaDialectCounts.get(obj.url) + 1) } else { @@ -788,7 +791,7 @@ function taskLint() { lintSchemaNoSmartQuotes() } -function taskCheck() { +async function taskCheck() { // Check filesystem assertDirectoryStructureIsValid() assertFilenamesHaveCorrectExtensions() @@ -801,7 +804,7 @@ function taskCheck() { assertSchemaValidationJsonHasValidSkipTest() // Check catalog.json - assertCatalogJsonPassesJsonLint() + await assertCatalogJsonPassesJsonLint() assertCatalogJsonValidatesAgainstJsonSchema() assertCatalogJsonHasNoDuplicateNames() assertCatalogJsonHasNoPoorlyWordedFields() @@ -1039,19 +1042,22 @@ async function remoteAssertSchemaHasNoBom() { await remoteSchemaFile(testSchemaFileForBOM, false) } -function assertCatalogJsonPassesJsonLint() { - jsonlint.parse(fs.readFileSync('./src/api/json/catalog.json', 'utf-8'), { - ignoreBOM: false, - ignoreComments: false, - ignoreTrailingCommas: false, - allowSingleQuotedStrings: false, - allowDuplicateObjectKeys: false, - }) +async function assertCatalogJsonPassesJsonLint() { + jsonlint.parse( + await fs.promises.readFile('./src/api/json/catalog.json', 'utf-8'), + { + ignoreBOM: false, + ignoreComments: false, + ignoreTrailingCommas: false, + allowSingleQuotedStrings: false, + allowDuplicateObjectKeys: false, + }, + ) } function assertCatalogJsonValidatesAgainstJsonSchema() { const catalogSchema = readJsonFile( - path.resolve('.', schemaDir, 'schema-catalog.json'), + path.join(schemaDir, 'schema-catalog.json'), ) const ajvInstance = factoryAJV({ schemaName: 'draft-04' }) if (ajvInstance.validate(catalogSchema, catalog)) { @@ -1142,7 +1148,7 @@ function assertCatalogJsonLocalUrlsMustRefFile() { return } countScan++ - // Check if local URL have .json extension + // Check if local URLs have .json extension const filenameMustBeAtThisUrlDepthPosition = 3 const filename = catalogUrl.split('/')[filenameMustBeAtThisUrlDepthPosition] if (!filename?.endsWith('.json')) { @@ -1168,7 +1174,7 @@ function assertCatalogJsonIncludesAllSchemas() { // Read all the JSON file name from catalog and add it to allCatalogLocalJsonFiles[] getUrlFromCatalog((catalogUrl) => { - // No need to validate the local URL correctness. It is al ready done in "local_assert_catalog.json_local_url_must_ref_file" + // No need to validate the local URL correctness. It is already done in "local_assert_catalog.json_local_url_must_ref_file" // Only scan for local schema. if (catalogUrl.startsWith(urlSchemaStore)) { const filename = catalogUrl.split('/').pop() @@ -1190,7 +1196,7 @@ function assertCatalogJsonIncludesAllSchemas() { } } } - // Get all the json file for AJV + // Get all the JSON files for AJV localSchemaFileAndTestFile( { schemaOnlyScan: schemaFileCompare }, { fullScanAllFiles: true }, @@ -1929,7 +1935,6 @@ async function taskCoverage() { } function printStrictAndNotStrictAjvValidatedSchemas() { - // this is only for AJV schemas const schemaVersion = showSchemaVersions() const schemaInFullStrictMode = [] const schemaInNotStrictMode = [] diff --git a/package.json b/package.json index 57dbca02d58..4689c245751 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "prettier:fix": "prettier --config .prettierrc.cjs --ignore-path .gitignore --write .", "eslint": "eslint ./cli.js", "eslint:fix": "eslint --fix ./cli.js", - "new-schema": "node ./cli.js new-schema", + "new-schema": "node --no-deprecation ./cli.js new-schema", "check": "node ./cli.js check", "check-remote": "node ./cli.js check-remote", "maintenance": "node ./cli.js maintenance",