Skip to content

Commit

Permalink
fix: generate schema.json files in built dir
Browse files Browse the repository at this point in the history
This is necessary fr distributing as a package.  The file `util.ts` needs to load the JSON Schema files at run time. So when distributed as a package the path for these can't be `path.join(__dirname, 'dist', ...)` as it currently is.
  • Loading branch information
nokome committed Apr 14, 2019
1 parent 067074e commit c97c759
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/built
/coverage
/dist
/node_modules
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.cache
built
coverage
dist
node_modules
Expand Down
40 changes: 25 additions & 15 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function processSchema(schemas, aliases, schema) {
}

/**
* Generate `dist/*.schema.json` files from `schema/*.schema.{yaml,json}` files.
* Generate `built/*.schema.json` files from `schema/*.schema.{yaml,json}` files.
*
* This function does not use Gulp file streams because it needs to load all the schemas
* into memory at once. It does
Expand All @@ -150,13 +150,13 @@ async function jsonschema() {
const aliases = {}
for (let schema of schemas.values()) processSchema(schemas, aliases, schema)

// Do final processing and write schema objects to `dist/*.schema.json` files
await fs.ensureDir('dist')
// Do final processing and write schema objects to file
await fs.ensureDir('built')
for (let schema of schemas.values()) {
// Generate the destination path from the source and then
// rewrite source so that it can be use for a "Edit this schema" link in docs.
const destPath = path.join(
'dist',
'built',
path.basename(schema.source).replace('.yaml', '.json')
)
schema.source = `https://github.com/stencila/schema/blob/master/schema/${
Expand Down Expand Up @@ -188,7 +188,7 @@ async function jsonschema() {
}

// Output `aliases.json`
await fs.writeJSON(path.join('dist', 'aliases.json'), aliases, { spaces: 2 })
await fs.writeJSON(path.join('built', 'aliases.json'), aliases, { spaces: 2 })

// Output `types.schema.json`
// This 'meta' schema provides a list of type schemas as:
Expand All @@ -212,11 +212,19 @@ async function jsonschema() {
properties,
required
}
await fs.writeJSON('dist/types.schema.json', types, { spaces: 2 })
await fs.writeJSON('built/types.schema.json', types, { spaces: 2 })

// Copy the built files into `dist` for publishing package
await fs.ensureDir('dist')
await Promise.all(
(await globby('built/**/*')).map(
async file => await fs.copy(file, path.join('dist', file))
)
)
}

/**
* Generate a JSON-LD `@context` from the `dist/*.schema.json` files
* Generate a JSON-LD `@context` from the `built/*.schema.json` files
*
* Generates a `@context` similar to https://github.com/codemeta/codemeta/blob/master/codemeta.jsonld
* but with any extension class or properties defined using `Class` or `Property` (see using https://meta.schema.org/).
Expand All @@ -227,7 +235,7 @@ function jsonld() {

// Process each JSON file
return (
src('dist/*.schema.json')
src('built/*.schema.json')
.pipe(
through2.obj((file, enc, cb) => {
const schema = JSON.parse(file.contents)
Expand Down Expand Up @@ -321,6 +329,8 @@ function jsonld() {
...[...Object.entries(properties)].sort()
])
jsonld[key] = value

fs.ensureDirSync('dist')
fs.writeJSONSync(path.join('dist', 'stencila.jsonld'), jsonld, {
spaces: 2
})
Expand All @@ -329,10 +339,10 @@ function jsonld() {
}

/**
* Generate `types.d.ts` from `schema/types.schema.json`
* Generate `types.ts` from `built/types.schema.json`
*/
async function ts() {
const src = 'dist/types.schema.json'
const src = 'built/types.schema.json'
const dest = 'types.ts'
const options = {
bannerComment: `/* tslint:disable */
Expand All @@ -348,10 +358,10 @@ async function ts() {
}

/**
* Check the generated JSON Schemas in `dist/types.schema.json` are valid.
* Check the generated JSON Schemas in `built/types.schema.json` are valid.
*/
async function check() {
const schema = await fs.readJSON('dist/types.schema.json')
const schema = await fs.readJSON('built/types.schema.json')
const metaSchema = require('ajv/lib/refs/json-schema-draft-07.json')
const ajv = new Ajv({ jsonPointers: true })
const validate = ajv.compile(metaSchema)
Expand All @@ -375,7 +385,7 @@ function test() {
loadSchema: uri => {
const match = uri.match(/https:\/\/stencila.github.com\/schema\/(.+)$/)
if (!match) throw new Error(`Not able to get schema from URI "${uri}"`)
return fs.readJSON(path.join('dist', match[1]))
return fs.readJSON(path.join('built', match[1]))
}
})

Expand All @@ -395,7 +405,7 @@ function test() {
)
if (!validator) {
let schema = await fs.readJSON(
path.join('dist', type + '.schema.json')
path.join('built', type + '.schema.json')
)
validator = await ajv.compileAsync(schema)
}
Expand Down Expand Up @@ -464,7 +474,7 @@ function test() {
* Clean up!
*/
function clean() {
return del('dist')
return del('dist', 'built', 'types.ts')
}

exports.jsonschema = jsonschema
Expand Down
4 changes: 2 additions & 2 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function cast<Key extends keyof stencila.Types>(

// Load all schemas for use by Ajv
const schemas = globby
.sync(path.join(__dirname, 'dist', '*.schema.json'))
.sync(path.join(__dirname, 'built', '*.schema.json'))
.map(file => fs.readJSONSync(file))

// Cached JSON Schema validation functions
Expand Down Expand Up @@ -154,7 +154,7 @@ const mutators = new Ajv({
})

// Read in aliases for use in mutate function
const aliases = fs.readJSONSync(path.join(__dirname, 'dist', 'aliases.json'))
const aliases = fs.readJSONSync(path.join(__dirname, 'built', 'aliases.json'))

/**
* Mutate a node so it conforms to a type's schema
Expand Down

0 comments on commit c97c759

Please sign in to comment.