From 539393ba493a576c77f467ba918b97e4fbf29cad Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 16 Feb 2020 21:31:11 +0800 Subject: [PATCH] [build][m]: refactor schema build and tests to new vuepress dir layout - refs https://github.com/frictionlessdata/project/issues/413. * Remove specs tests as kind of pointless now * Remove specs build from build.js as vuepress now does that! --- .gitignore | 1 - build.js | 46 ++++++--------------------------------- test/schemas.js | 45 ++++++++++++++++++-------------------- test/specs.js | 57 ------------------------------------------------- 4 files changed, 27 insertions(+), 122 deletions(-) delete mode 100644 test/specs.js diff --git a/.gitignore b/.gitignore index 1769c85f..76d15aaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -build/* .*.swp TAGS .DS_Store diff --git a/build.js b/build.js index 73de829c..cf7ddb77 100644 --- a/build.js +++ b/build.js @@ -1,5 +1,6 @@ const util = require('util') const fs = require('fs-extra') +const path = require('path') const yaml = require('js-yaml') const nodePath = require('path') const glob = util.promisify(require('glob')) @@ -7,25 +8,15 @@ const $RefParser = require('json-schema-ref-parser') const csvParseSync = require('csv-parse/lib/sync'); const excludeOnOutput = ['dictionary.json'] +BUILD_DIR = '.vuepress/dist/schemas' // Helpers - function prepareDirectories() { - const dirs = [ - 'build/schemas', - 'build/specs', - ] - - // Ensure directories - for (const dir of dirs) { - fs.emptyDirSync(dir) - } - + fs.emptyDirSync(BUILD_DIR) } function compileDictionary() { - // Init dictionary const dictionary = { '$schema': 'http://json-schema.org/draft-04/schema#', @@ -41,12 +32,10 @@ function compileDictionary() { // Save dictionary const contents = JSON.stringify(dictionary, null, 2) fs.writeFileSync('schemas/dictionary.json', contents) - } async function buildSchemas() { - // Dereference and save every schema for (const file of glob.sync('schemas/*.json')) { const basename = nodePath.basename(file) @@ -54,50 +43,27 @@ async function buildSchemas() { const rawSchema = JSON.parse(fs.readFileSync(file)) const schema = await $RefParser.dereference(rawSchema) const contents = JSON.stringify(schema, null, 2) - fs.writeFileSync(`build/schemas/${basename}`, contents) - console.log(`[+] build/schemas/${basename}.json`) + fs.writeFileSync(`${BUILD_DIR}/${basename}`, contents) } - } function buildRegistry() { - // Copy csv registry - fs.copyFileSync('schemas/registry.csv', 'build/schemas/registry.csv') - console.log(`[+] build/schemas/registry.csv`) - + fs.copyFileSync('schemas/registry.csv', BUILD_DIR + '/registry.csv') // Convert csv registy to json const registry = csvParseSync(fs.readFileSync('schemas/registry.csv'), {columns: true}) const contents = JSON.stringify(registry, null, 2) - fs.writeFileSync(`build/schemas/registry.json`, contents) - console.log(`[+] build/schemas/registry.json`) - -} - - -function buildSpecs() { - - // Convert specs files for `lektor` - for (const file of glob.sync('specs/*.md')) { - let name = nodePath.parse(file).name - if (name === 'index') name = '' - fs.ensureDirSync(`build/specs/${name}`) - fs.copyFileSync(file, `build/specs/${name}/contents.lr`) - console.log(`[+] build/schemas/specs/${name}/contents.lr`) - } - + fs.writeFileSync(`${BUILD_DIR}/registry.json`, contents) } // Main script - async function main() { prepareDirectories() compileDictionary() await buildSchemas() buildRegistry() - buildSpecs() } process.on('warning', (warning) => { diff --git a/test/schemas.js b/test/schemas.js index a73ef5b4..52b335cb 100644 --- a/test/schemas.js +++ b/test/schemas.js @@ -4,43 +4,41 @@ const glob = util.promisify(require('glob')) const readFile = util.promisify(require('fs').readFile) const tv4 = require('tv4') +const BUILD_DIR = '.vuepress/dist/schemas' // Tests - describe('schemas', () => { - it('[all]', async () => { - const files = await glob('build/schemas/*.*') - assert.include(files, 'build/schemas/csv-dialect.json') - assert.include(files, 'build/schemas/data-package.json') - assert.include(files, 'build/schemas/data-resource.json') - assert.include(files, 'build/schemas/fiscal-data-package.json') - assert.include(files, 'build/schemas/registry.csv') - assert.include(files, 'build/schemas/registry.json') - assert.include(files, 'build/schemas/table-schema.json') - assert.include(files, 'build/schemas/tabular-data-package.json') - assert.include(files, 'build/schemas/tabular-data-resource.json') + const files = await glob(`${BUILD_DIR}/*.*`) + assert.include(files, `${BUILD_DIR}/csv-dialect.json`) + assert.include(files, `${BUILD_DIR}/data-package.json`) + assert.include(files, `${BUILD_DIR}/data-resource.json`) + assert.include(files, `${BUILD_DIR}/fiscal-data-package.json`) + assert.include(files, `${BUILD_DIR}/registry.csv`) + assert.include(files, `${BUILD_DIR}/registry.json`) + assert.include(files, `${BUILD_DIR}/table-schema.json`) + assert.include(files, `${BUILD_DIR}/tabular-data-package.json`) + assert.include(files, `${BUILD_DIR}/tabular-data-resource.json`) }) it('csv-dialect', async () => { - const schema = require('../build/schemas/csv-dialect.json') + const schema = require(`../${BUILD_DIR}/csv-dialect.json`) assert.deepEqual(schema.title, 'CSV Dialect') }) it('data-package', async () => { - const schema = require('../build/schemas/data-package.json') + const schema = require(`../${BUILD_DIR}/data-package.json`) assert.deepEqual(schema.title, 'Data Package') }) it('data-resource', async () => { - const schema = require('../build/schemas/data-resource.json') + const schema = require(`../${BUILD_DIR}/data-resource.json`) assert.deepEqual(schema.title, 'Data Resource') }) it('data-resource path property', async () => { - // Valid paths - const schema = require('../build/schemas/data-resource.json') + const schema = require(`../${BUILD_DIR}/data-resource.json`) const invalidPaths = [ '../dest', './dest', @@ -78,12 +76,12 @@ describe('schemas', () => { }) it('fiscal-data-package', async () => { - const schema = require('../build/schemas/fiscal-data-package.json') + const schema = require(`../${BUILD_DIR}/fiscal-data-package.json`) assert.deepEqual(schema.title, 'Fiscal Data Package') }) it('registry.csv', async () => { - const contents = await readFile('build/schemas/registry.csv', 'utf-8') + const contents = await readFile(`${BUILD_DIR}/registry.csv`, 'utf-8') assert.include(contents, 'Data Package') assert.include(contents, 'Tabular Data Package') assert.include(contents, 'Fiscal Data Package') @@ -93,7 +91,7 @@ describe('schemas', () => { }) it('registry.json', async () => { - const registry = require('../build/schemas/registry.json') + const registry = require(`../${BUILD_DIR}/registry.json`) const titles = registry.map(item => item.title) assert.include(titles, 'Data Package') assert.include(titles, 'Tabular Data Package') @@ -104,18 +102,17 @@ describe('schemas', () => { }) it('table-schema', async () => { - const schema = require('../build/schemas/table-schema.json') + const schema = require(`../${BUILD_DIR}/table-schema.json`) assert.deepEqual(schema.title, 'Table Schema') }) it('tabular-data-package', async () => { - const schema = require('../build/schemas/tabular-data-package.json') + const schema = require(`../${BUILD_DIR}/tabular-data-package.json`) assert.deepEqual(schema.title, 'Tabular Data Package') }) it('tabular-data-resource', async () => { - const schema = require('../build/schemas/tabular-data-resource.json') + const schema = require(`../${BUILD_DIR}/tabular-data-resource.json`) assert.deepEqual(schema.title, 'Tabular Data Resource') }) - }) diff --git a/test/specs.js b/test/specs.js deleted file mode 100644 index 2b144ba6..00000000 --- a/test/specs.js +++ /dev/null @@ -1,57 +0,0 @@ -const util = require('util') -const {assert} = require('chai') -const glob = util.promisify(require('glob')) -const readFile = util.promisify(require('fs').readFile) - - -// Tests - -describe('specs', () => { - - it('[all]', async () => { - const files = await glob('build/specs/**/contents.lr') - assert.include(files, 'build/specs/csv-dialect/contents.lr') - assert.include(files, 'build/specs/data-package/contents.lr') - assert.include(files, 'build/specs/data-resource/contents.lr') - assert.include(files, 'build/specs/fiscal-data-package/contents.lr') - assert.include(files, 'build/specs/table-schema/contents.lr') - assert.include(files, 'build/specs/tabular-data-package/contents.lr') - assert.include(files, 'build/specs/tabular-data-resource/contents.lr') - }) - - it('csv-dialect', async () => { - const contents = await readFile('build/specs/csv-dialect/contents.lr', 'utf-8') - assert.include(contents, 'CSV Dialect') - }) - - it('data-package', async () => { - const contents = await readFile('build/specs/data-package/contents.lr', 'utf-8') - assert.include(contents, 'Data Package') - }) - - it('data-resource', async () => { - const contents = await readFile('build/specs/data-resource/contents.lr', 'utf-8') - assert.include(contents, 'Data Resource') - }) - - it('fiscal-data-package', async () => { - const contents = await readFile('build/specs/fiscal-data-package/contents.lr', 'utf-8') - assert.include(contents, 'Fiscal Data Package') - }) - - it('table-schema', async () => { - const contents = await readFile('build/specs/table-schema/contents.lr', 'utf-8') - assert.include(contents, 'Data Package') - }) - - it('tabular-data-package', async () => { - const contents = await readFile('build/specs/tabular-data-package/contents.lr', 'utf-8') - assert.include(contents, 'Tabular Data Package') - }) - - it('tabular-data-resource', async () => { - const contents = await readFile('build/specs/tabular-data-resource/contents.lr', 'utf-8') - assert.include(contents, 'Tabular Data Resource') - }) - -})