Skip to content

Commit

Permalink
Merge pull request #161 from sketch-hq/chore/update-schema-build-process
Browse files Browse the repository at this point in the history
Simplify schema build process
  • Loading branch information
christianklotz authored Apr 14, 2021
2 parents 87fb2a6 + 16dedc7 commit 84693ad
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 99 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-plums-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sketch-hq/sketch-file-format': patch
---

Reorganised source code and simplified build process
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions packages/file-format-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
15 changes: 7 additions & 8 deletions packages/file-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
* - Prune any un-used definitions
*/

/// <reference types="./global" />

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'

Expand All @@ -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
Expand Down
55 changes: 55 additions & 0 deletions packages/file-format/scripts/build-schemas.ts
Original file line number Diff line number Diff line change
@@ -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 <schema-dir> <output-dir>
*/
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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 0 additions & 36 deletions packages/file-format/src/build-schemas.ts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/file-format/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 0 additions & 3 deletions packages/file-format/src/package.json

This file was deleted.

10 changes: 6 additions & 4 deletions packages/file-format/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 0 additions & 13 deletions packages/file-format/tsconfig.pkg.json

This file was deleted.

17 changes: 0 additions & 17 deletions packages/file-format/tsconfig.schemas.json

This file was deleted.

10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 84693ad

Please sign in to comment.