Skip to content

Commit

Permalink
[build][m]: refactor schema build and tests to new vuepress dir layou…
Browse files Browse the repository at this point in the history
…t - refs frictionlessdata/frictionlessdata.io#413.

* Remove specs tests as kind of pointless now
* Remove specs build from build.js as vuepress now does that!
  • Loading branch information
rufuspollock committed Feb 16, 2020
1 parent 9c68162 commit 539393b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 122 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
build/*
.*.swp
TAGS
.DS_Store
Expand Down
46 changes: 6 additions & 40 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
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'))
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#',
Expand All @@ -41,63 +32,38 @@ 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)
if (excludeOnOutput.includes(basename)) continue
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) => {
Expand Down
45 changes: 21 additions & 24 deletions test/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
})

})
57 changes: 0 additions & 57 deletions test/specs.js

This file was deleted.

0 comments on commit 539393b

Please sign in to comment.