Skip to content

Commit

Permalink
Convert cli.js to an ECMAScript Module
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Jun 10, 2024
1 parent f3bebe6 commit 4a7bfa8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
},
"env": {
"es2021": true,
"node": true
Expand Down
66 changes: 35 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
/// <binding AfterBuild='build' />
const path = require('node:path')
const fs = require('node:fs')
const readline = require('node:readline/promises')
const addFormats = require('ajv-formats')
const ajvFormatsDraft2019 = require('ajv-formats-draft2019')
const AjvDraft04 = require('ajv-draft-04')
const AjvDraft06And07 = require('ajv')
const Ajv2019 = require('ajv/dist/2019')
const Ajv2020 = require('ajv/dist/2020')
const AjvDraft06SchemaJson = require('ajv/dist/refs/json-schema-draft-06.json')
const AjvStandalone = require('ajv/dist/standalone').default
const TOML = require('@ltd/j-toml')
const YAML = require('yaml')
const schemasafe = require('@exodus/schemasafe')
const prettier = require('prettier')
const axios = require('axios').default
const jsonlint = require('@prantlf/jsonlint')
const jsoncParser = require('jsonc-parser')
const chalk = require('chalk')
const minimist = require('minimist')

;('use strict')
import path from 'node:path'
import fs from 'node:fs'
import readline from 'node:readline/promises'
import addFormats from 'ajv-formats'
import ajvFormatsDraft2019 from 'ajv-formats-draft2019'
import AjvDraft04 from 'ajv-draft-04'
import AjvDraft06And07 from 'ajv'
import Ajv2019 from 'ajv/dist/2019.js'
import Ajv2020 from 'ajv/dist/2020.js'
import AjvStandalone from 'ajv/dist/standalone/index.js'
import TOML from '@ltd/j-toml'
import YAML from 'yaml'
import schemasafe from '@exodus/schemasafe'
import prettier from 'prettier'
import axios from 'axios'
import jsonlint from '@prantlf/jsonlint'
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 temporaryCoverageDir = './temp'
const schemaDir = './src/schemas/json'
const testPositiveDir = './src/test'
const testNegativeDir = './src/negative_test'
const urlSchemaStore = 'https://json.schemastore.org/'
const catalog = require('./src/api/json/catalog.json')
const catalog = JSON.parse(fs.readFileSync('./src/api/json/catalog.json'))
const schemaValidation = jsoncParser.parse(
fs.readFileSync('./src/schema-validation.json', 'utf-8'),
)
Expand Down Expand Up @@ -59,6 +60,10 @@ const argv = minimist(process.argv.slice(2), {
boolean: ['help', 'lint'],
})

function readJsonFile(/** @type {string} */ filename) {
return JSON.parse(fs.readFileSync(filename, 'utf-8'))
}

function skipThisFileName(/** @type {string} */ name) {
// This macOS file must always be ignored.
return name === '.DS_Store'
Expand Down Expand Up @@ -599,7 +604,7 @@ function ajv() {

// Add external schema to AJV
externalSchemaWithPathList.forEach((x) => {
ajvSelected.addSchema(require(x.toString()))
ajvSelected.addSchema(readJsonFile(x.toString()))
})

// What schema draft version is it?
Expand Down Expand Up @@ -1041,7 +1046,7 @@ function assertCatalogJsonPassesJsonLint() {
}

function assertCatalogJsonValidatesAgainstJsonSchema() {
const catalogSchema = require(
const catalogSchema = readJsonFile(
path.resolve('.', schemaDir, 'schema-catalog.json'),
)
const ajvInstance = factoryAJV({ schemaName: 'draft-04' })
Expand Down Expand Up @@ -1336,7 +1341,7 @@ function printDowngradableSchemaVersions() {

// Add external schema to AJV
option.externalSchemaWithPathList.forEach((x) => {
ajvSelected.addSchema(require(x.toString()))
ajvSelected.addSchema(readJsonFile(x.toString()))
})

ajvSelected.compile(schemaJson)
Expand Down Expand Up @@ -1799,7 +1804,6 @@ function assertSchemaValidationJsonHasValidSkipTest() {
function taskCoverage() {
const javaScriptCoverageName = 'schema.json.translated.to.js'
const javaScriptCoverageNameWithPath = path.join(
__dirname,
`${temporaryCoverageDir}/${javaScriptCoverageName}`,
)

Expand Down Expand Up @@ -1836,7 +1840,7 @@ function taskCoverage() {
if (isThisWithExternalSchema) {
// There is an external schema that need to be included.
externalSchemaWithPathList.forEach((x) => {
multipleSchema.push(require(x.toString()))
multipleSchema.push(readJsonFile(x.toString()))
})
// Also add the 'root' schema
multipleSchema.push(mainSchema)
Expand Down Expand Up @@ -1886,7 +1890,7 @@ function taskCoverage() {
}),
)
// Now use this JavaScript as validation in the positive and negative test
validations = require(javaScriptCoverageNameWithPath)
validations = readJsonFile(javaScriptCoverageNameWithPath)
}

// Load the Javascript file from the disk and run it with the JSON test file.
Expand Down Expand Up @@ -1955,7 +1959,7 @@ function printStrictAndNotStrictAjvValidatedSchemas() {

// Add external schema to AJV
externalSchemaWithPathList.forEach((x) => {
ajvSelected.addSchema(require(x.toString()))
ajvSelected.addSchema(readJsonFile(x.toString()))
})

try {
Expand Down Expand Up @@ -2033,7 +2037,7 @@ EXAMPLES:
}
const taskOrFn = argv._[0]
if (taskOrFn in taskMapping) {
taskMapping[taskOrFn]()
await taskMapping[taskOrFn]()
} else {
eval(`${taskOrFn}()`)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/schemastore/SchemaStore"
},
"type": "commonjs",
"type": "module",
"scripts": {
"prettier": "prettier --config .prettierrc.cjs --ignore-path .gitignore --check .",
"prettier:fix": "prettier --config .prettierrc.cjs --ignore-path .gitignore --write .",
Expand Down

0 comments on commit 4a7bfa8

Please sign in to comment.