Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ that directory called `constants.yaml`. You may override these if desired.
// Whether or not to push your generated schemas out to the world at large.
publish: process.env.NODE_ENV == 'production'

// Whether or not to write your generated schemas to local files.
writeFile: false

// Whether or not to write your generated schemas to the console.
preview: false

// What the root of all of your schemas is. This will make up the first part of
// the key of your schemas. The default should be correct.
baseUrl: 'http://schema.taskcluster.net/'
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
"dependencies": {
"ajv": "^4.0.4",
"app-root-dir": "^1.0.2",
"aws-sdk": "^2.2.38",
"aws-sdk": "^2.5.3",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how this crept in, but i don't see it being a problem...

"babel-runtime": "^6.0.0",
"debug": "^2.2.0",
"js-yaml": "^3.5.3",
"lodash": "^4.5.1",
"rimraf": "^2.5.2",
"promise": "^7.0.4",
"url-join": "^1.1.0",
"walk": "^2.3.9"
Expand All @@ -35,7 +36,6 @@
"mocha": "2.5.3",
"mocha-eslint": "^2.0.1",
"mock-aws-s3": "^2.1.0",
"rimraf": "^2.5.2",
"source-map-support": "^0.4.0"
},
"main": "./lib/validate.js"
Expand Down
58 changes: 56 additions & 2 deletions src/publish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
let debug = require('debug')('taskcluster-lib-validate');
let Promise = require('promise');
let fs = require('fs');
let path = require('path');

function publish(s3, bucket, prefix, name, content) {
function s3(s3, bucket, prefix, name, content) {
return new Promise((accept, reject) => {
debug('Publishing schema %s', name);
s3.putObject({
Expand All @@ -20,4 +22,56 @@ function publish(s3, bucket, prefix, name, content) {
});
}

module.exports = publish;
/**
* Write the schema to a local file. This is useful for debugging purposes
* mainly.
*/
function writeFile(name, content) {
let toPrint;
// We want something that's pretty-printable, so let's parse and reserialise
// the JSON in a nice way. If this fails, let's just write out whatever was
// there.
try {
toPrint = JSON.stringify(JSON.parse(content), null, 2);
} catch (err) {
toPrint = content;
}
return new Promise((resolve, reject) => {
fs.writeFile(path.join('rendered_schemas', name), toPrint, (err) => {
if (err) {
reject(err);
}
console.log('Wrote ' + name);
resolve();
});
});
}

/**
* Write the generated schema to the console as pretty-json output. This is
* useful for debugging purposes
*/
function preview(name, content) {
let toPrint;
// We want something that's pretty-printable, so let's parse and reserialise
// the JSON in a nice way. If this fails, let's just write out whatever was
// there.
try {
toPrint = JSON.stringify(JSON.parse(content), null, 2);
} catch (err) {
toPrint = content;
}
console.log('=======');
console.log('JSON SCHEMA PREVIEW BEGIN: ' + name);
console.log('=======');
console.log(toPrint);
console.log('=======');
console.log('JSON SCHEMA PREVIEW END: ' + name);
return Promise.resolve();
}

module.exports = {
s3: s3,
writeFile: writeFile,
preview: preview,
};
28 changes: 27 additions & 1 deletion src/validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let debug = require('debug')('taskcluster-lib-validate');
let _ = require('lodash');
let fs = require('fs');
let rimraf = require('rimraf');
let path = require('path');
let walk = require('walk');
let yaml = require('js-yaml');
Expand All @@ -23,6 +24,8 @@ async function validator(options) {
publish: process.env.NODE_ENV == 'production',
baseUrl: 'http://schemas.taskcluster.net/',
bucket: 'schemas.taskcluster.net',
preview: process.env.PREVIEW_JSON_SCHEMA_FILES,
writeFile: process.env.WRITE_JSON_SCHEMA_FILES,
});

if (_.isString(cfg.constants)) {
Expand Down Expand Up @@ -87,7 +90,7 @@ async function validator(options) {
s3Provider = new aws.S3(cfg.aws);
}
await Promise.all(_.map(schemas, (content, name) => {
return publish(
return publish.s3(
s3Provider,
cfg.bucket,
cfg.prefix,
Expand All @@ -97,6 +100,29 @@ async function validator(options) {
}));
}

if (cfg.writeFile) {
debug('Writing schema to local file');
let dir = 'rendered_schemas';
rimraf.sync(dir);
fs.mkdirSync(dir);
await Promise.all(_.map(schemas, (content, name) => {
return publish.writeFile(
name,
content
);
}));
}

if (cfg.preview) {
debug('Writing schema to console');
await Promise.all(_.map(schemas, (content, name) => {
return publish.preview(
name,
content
);
}));
}

let validate = (obj, id) => {
id = id.replace(/#$/, '');
id = id.replace(/\.ya?ml$/, '.json');
Expand Down
2 changes: 2 additions & 0 deletions test/publish_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ suite('Publish Tests', () => {
secretAccessKey: 'thesearentused',
},
publish: true,
preview: true,
writeFile: true,
s3Provider: s3,
});
});
Expand Down