diff --git a/.changeset/unlucky-plums-join.md b/.changeset/unlucky-plums-join.md new file mode 100644 index 0000000..d61db0c --- /dev/null +++ b/.changeset/unlucky-plums-join.md @@ -0,0 +1,5 @@ +--- +'@sketch-hq/sketch-file-format': patch +--- + +Reorganised source code and simplified build process diff --git a/package.json b/package.json index e508ea3..8d3a8b2 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@types/node": "14.14.35", "jest": "26.6.3", "prettier": "2.2.1", + "ts-node": "9.1.1", "ts-jest": "26.5.4", "typescript": "4.2.3", "webpack": "5.28.0", diff --git a/packages/file-format-ts/package.json b/packages/file-format-ts/package.json index b06fc46..d7b3a8b 100644 --- a/packages/file-format-ts/package.json +++ b/packages/file-format-ts/package.json @@ -22,13 +22,12 @@ "devDependencies": { "@sketch-hq/sketch-file-format": "5.2.3", "@types/humps": "2.0.0", - "humps": "2.0.1", - "ts-node": "9.1.1" + "humps": "2.0.1" }, "scripts": { "generate": "ts-node ./scripts/generate.ts ./src/types.ts", "build": "rm -rf dist && yarn generate && tsc --project tsconfig.json && tsc --project tsconfig.esm.json", "test": "jest", - "format-check": "prettier --check {**/,}*.{ts,md,json} --ignore-path ../../.prettierignore" + "format-check": "prettier --check **.{ts,md,json} --ignore-path ../../.prettierignore" } } diff --git a/packages/file-format/package.json b/packages/file-format/package.json index fa3cdaf..c1901f7 100644 --- a/packages/file-format/package.json +++ b/packages/file-format/package.json @@ -36,14 +36,13 @@ "node-stream-zip": "1.13.2" }, "scripts": { - "build": "yarn build:schemas && yarn build:pkg", - "build:schemas": "tsc --project tsconfig.schemas.json && yarn n src/build-schemas.js", - "build:pkg": "rm -rf dist && mkdir -p dist && tsc --project tsconfig.pkg.json", - "validate-schemas": "tsc && yarn n src/validate-schemas.js", - "validate-reference-files": "yarn build:schemas && tsc && yarn n src/validate-reference-files.js", - "validate-file": "yarn build:schemas && tsc && yarn n src/validate-file.js", - "n": "node --experimental-json-modules --es-module-specifier-resolution=node", + "build": "yarn build:schemas ./schema ./src && yarn build:pkg", + "build:schemas": "ts-node ./scripts/build-schemas.ts", + "build:pkg": "rm -rf dist && mkdir -p dist && tsc", + "validate-schemas": "ts-node ./scripts/validate-schemas.ts", + "validate-reference-files": "yarn build:schemas && ts-node ./scripts/validate-reference-files.ts", + "validate-file": "yarn build:schemas && ts-node ./scripts/validate-file.ts", "test": "echo \"No test specified\"", - "format-check": "prettier --check {schema/{**/,}*.yaml,*.md,src/*.ts} --ignore-path ../../.prettierignore" + "format-check": "prettier --check {**.{ts,md,json},schema/**/*.yaml} --ignore-path ../../.prettierignore" } } diff --git a/packages/file-format/src/assemble.ts b/packages/file-format/scripts/assemble.ts similarity index 97% rename from packages/file-format/src/assemble.ts rename to packages/file-format/scripts/assemble.ts index c017cb5..c8096e5 100644 --- a/packages/file-format/src/assemble.ts +++ b/packages/file-format/scripts/assemble.ts @@ -13,14 +13,16 @@ * - Prune any un-used definitions */ +/// + import yaml from 'js-yaml' import fs from 'fs' import globby from 'globby' -import changeCase from 'change-case' +import { pascalCase } from 'change-case' import { basename } from 'path' import deepdash from 'deepdash' import _ from 'lodash' -import jsont from 'json-transforms' +import * as jsont from 'json-transforms' import mergeAllOf from 'json-schema-merge-allof' import { JSONSchema7 } from 'json-schema' @@ -32,8 +34,7 @@ deepdash(_) * Input: ./schemas/foo-bar.schema.yaml * Output: FooBar */ -const pathToId = (path: string) => - changeCase.pascalCase(basename(path, '.schema.yaml')) +const pathToId = (path: string) => pascalCase(basename(path, '.schema.yaml')) /** * Returns a Promise that resolves with the POJO representation of diff --git a/packages/file-format/scripts/build-schemas.ts b/packages/file-format/scripts/build-schemas.ts new file mode 100644 index 0000000..d1b4cfa --- /dev/null +++ b/packages/file-format/scripts/build-schemas.ts @@ -0,0 +1,55 @@ +/** + * Build all distributable schemas into the `dist` folder. + */ + +import process from 'process' + +import { writeFileSync } from 'fs' +import { execSync } from 'child_process' +import { basename } from 'path' + +import { assemble } from './assemble' + +const build = async (entry: string) => { + const schema = await assemble(entry) + writeFileSync( + `src/${basename(entry, '.yaml')}.json`, + JSON.stringify(schema, null, 2), + { encoding: 'utf8' }, + ) +} + +/** + * Both paths for the directory containing the schemas, as well as the directory + * the generated JSON Schema should be written to must be provided as a + * command-line argument. + * + * ts-node build-schemas.ts + */ +const main = async (args: string[]) => { + const [schemaDir, outDir] = args.slice(2, 4) // user defined args start at index 2 + if (!schemaDir) { + console.error('missing path to directory containing schemas') + process.exit(1) + } + if (!outDir) { + console.error('missing path to output directory for generated JSON Schema') + process.exit(1) + } + + try { + await Promise.all([ + build(`${schemaDir}/file-format.schema.yaml`), + build(`${schemaDir}/document.schema.yaml`), + build(`${schemaDir}/meta.schema.yaml`), + build(`${schemaDir}/user.schema.yaml`), + build(`${schemaDir}/layers/page.schema.yaml`), + ]) + execSync(`yarn prettier --write "${outDir}/*.json"`) + } catch (e) { + console.error(e) + process.exit(1) + } +} + +main(process.argv) diff --git a/packages/file-format/src/global.d.ts b/packages/file-format/scripts/global.d.ts similarity index 100% rename from packages/file-format/src/global.d.ts rename to packages/file-format/scripts/global.d.ts diff --git a/packages/file-format/src/utils.ts b/packages/file-format/scripts/utils.ts similarity index 100% rename from packages/file-format/src/utils.ts rename to packages/file-format/scripts/utils.ts diff --git a/packages/file-format/src/validate-file.ts b/packages/file-format/scripts/validate-file.ts similarity index 100% rename from packages/file-format/src/validate-file.ts rename to packages/file-format/scripts/validate-file.ts diff --git a/packages/file-format/src/validate-reference-files.ts b/packages/file-format/scripts/validate-reference-files.ts similarity index 100% rename from packages/file-format/src/validate-reference-files.ts rename to packages/file-format/scripts/validate-reference-files.ts diff --git a/packages/file-format/src/validate-schemas.ts b/packages/file-format/scripts/validate-schemas.ts similarity index 100% rename from packages/file-format/src/validate-schemas.ts rename to packages/file-format/scripts/validate-schemas.ts diff --git a/packages/file-format/src/build-schemas.ts b/packages/file-format/src/build-schemas.ts deleted file mode 100644 index 1f91035..0000000 --- a/packages/file-format/src/build-schemas.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Build all distributable schemas into the `dist` folder. - */ - -import { writeFileSync } from 'fs' -import { execSync } from 'child_process' -import { basename } from 'path' - -import { assemble } from './assemble' - -const build = async (entry: string) => { - const schema = await assemble(entry) - writeFileSync( - `src/${basename(entry, '.yaml')}.json`, - JSON.stringify(schema, null, 2), - { encoding: 'utf8' }, - ) -} - -const main = async () => { - try { - await Promise.all([ - build('schema/file-format.schema.yaml'), - build('schema/document.schema.yaml'), - build('schema/meta.schema.yaml'), - build('schema/user.schema.yaml'), - build('schema/layers/page.schema.yaml'), - ]) - execSync('yarn prettier --write "src/*.json"') - } catch (e) { - console.error(e) - process.exit(1) - } -} - -main() diff --git a/packages/file-format/src/index.ts b/packages/file-format/src/index.ts index cd03189..33ed794 100644 --- a/packages/file-format/src/index.ts +++ b/packages/file-format/src/index.ts @@ -1,9 +1,11 @@ /** - * This file is the entry point to the published npm module. + * This file is the entry point to {@link @sketch-hq/sketch-file-format}. */ import { JSONSchema7 } from 'json-schema' +/// All *.schema.json are generated from the schema YAML source by +/// the `build:schemas` script defined in package.json. import fileFormatSchema from './file-format.schema.json' import docSchema from './document.schema.json' import metaSchema from './meta.schema.json' diff --git a/packages/file-format/src/package.json b/packages/file-format/src/package.json deleted file mode 100644 index 3dbc1ca..0000000 --- a/packages/file-format/src/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/packages/file-format/tsconfig.json b/packages/file-format/tsconfig.json index 03a34be..6f5d1d5 100644 --- a/packages/file-format/tsconfig.json +++ b/packages/file-format/tsconfig.json @@ -1,10 +1,12 @@ { "extends": "@tsconfig/recommended/tsconfig.json", - "include": ["src/*.ts"], - "exclude": ["node_modules", "dist"], + "include": ["src/index.ts"], "compilerOptions": { - "module": "ESNext", + "outDir": "dist", + "baseUrl": "src", "moduleResolution": "node", - "resolveJsonModule": true + "resolveJsonModule": true, + "declaration": true, + "declarationMap": true } } diff --git a/packages/file-format/tsconfig.pkg.json b/packages/file-format/tsconfig.pkg.json deleted file mode 100644 index 0e61f54..0000000 --- a/packages/file-format/tsconfig.pkg.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "extends": "@tsconfig/recommended/tsconfig.json", - "include": ["src/index.ts"], - "exclude": ["node_modules", "dist"], - "compilerOptions": { - "outDir": "dist", - "baseUrl": "src", - "moduleResolution": "node", - "resolveJsonModule": true, - "declaration": true, - "declarationMap": true - } -} diff --git a/packages/file-format/tsconfig.schemas.json b/packages/file-format/tsconfig.schemas.json deleted file mode 100644 index 755eb94..0000000 --- a/packages/file-format/tsconfig.schemas.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "extends": "@tsconfig/recommended/tsconfig.json", - "include": ["src/*.ts"], - "exclude": [ - "node_modules", - "dist", - "src/validate-reference-files.ts", - "src/validate-schemas.ts", - "src/validate-file.ts", - "src/index.ts" - ], - "compilerOptions": { - "module": "ESNext", - "moduleResolution": "node", - "resolveJsonModule": true - } -} diff --git a/yarn.lock b/yarn.lock index 0303587..4f5f1bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -735,16 +735,6 @@ dependencies: "@sinonjs/commons" "^1.7.0" -"@sketch-hq/sketch-file-format-1@npm:@sketch-hq/sketch-file-format@1.1.7": - version "1.1.7" - resolved "https://registry.yarnpkg.com/@sketch-hq/sketch-file-format/-/sketch-file-format-1.1.7.tgz#7de3cdfda10c97d21bbe261e0544ca581d27366e" - integrity sha512-tEQxuWgXIFn2fNgAiGDszrEGF/sR16O2kOjkB49PJcI7NWq+rwghr6Fx6nSL8dPNXOpYeSQOHGqTvGlVuo9YEA== - -"@sketch-hq/sketch-file-format-2@npm:@sketch-hq/sketch-file-format@2.0.3": - version "2.0.3" - resolved "https://registry.yarnpkg.com/@sketch-hq/sketch-file-format/-/sketch-file-format-2.0.3.tgz#ed3077b6e146c947b4fe144ef0cacfc67b7e507e" - integrity sha512-l/sLUhHayJEair70vQEGBAldpR3K8Xb+WqZGlrVtJT3aZVGGnEAT8i8n8qNMECWBdEojTxC3gKqgiJu9U98nDw== - "@sketch-hq/sketch-reference-files@2.3.0": version "2.3.0" resolved "https://registry.yarnpkg.com/@sketch-hq/sketch-reference-files/-/sketch-reference-files-2.3.0.tgz#2e8e186ae2b69d3db483f14bd3393d0f8a952b70"