309 lines
8.4 KiB
JavaScript
309 lines
8.4 KiB
JavaScript
/* eslint-disable filenames/match-exported */
|
|
/* eslint-disable no-console */
|
|
|
|
const config = require('./gulp.config')();
|
|
const gulp = require('gulp');
|
|
const mocha = require('gulp-spawn-mocha');
|
|
const $ = require('gulp-load-plugins')({lazy: true});
|
|
const exec = require('child_process').exec;
|
|
const args = require('yargs').argv;
|
|
|
|
const DocGen = require('./tools/docgen/docgen.js');
|
|
const WikiGen = require('./tools/wikidocs/wikidocs.js');
|
|
const IndexGen = require('./tools/alldocs/alldocs.js');
|
|
const SchemaGen = require('./tools/wikiToSchema/wikiToSchema.js');
|
|
|
|
/**
|
|
* List the available gulp tasks
|
|
*/
|
|
gulp.task('help', $.taskListing);
|
|
gulp.task('default', ['help']);
|
|
|
|
/**
|
|
* Mocha test reporter can be passed on the command line
|
|
*
|
|
* Defaults to 'spec' as that is the most human-friendly, but e.g.
|
|
* can be changed to 'tap' as used for the arcanist integration
|
|
*/
|
|
let testReporter = 'spec';
|
|
if (args.reporter) {
|
|
log('Custom unit test reporter: ' + $.util.colors.red(args.reporter));
|
|
testReporter = args.reporter;
|
|
}
|
|
|
|
/**
|
|
* Build the docs from the Swagger file
|
|
*/
|
|
gulp.task('swagger2asciidoc', (callback) => {
|
|
log('Generating the asciidoc from the swagger definition');
|
|
|
|
const options = config.api.options;
|
|
const promise = DocGen.Swagger2AsciiDoc(config.api.src, options);
|
|
promise.then(() => {
|
|
// All good
|
|
callback();
|
|
return null;
|
|
}).catch((error) => {
|
|
// Failed somewhere
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
gulp.task('swagger-asciidoc2html', ['swagger2asciidoc'], (callback) => {
|
|
log('Building the asciidoc into output formats');
|
|
|
|
const cmdLine = 'asciidoctor -d book ' + config.api.indexPath;
|
|
const options = {
|
|
cwd: config.api.dest
|
|
};
|
|
exec(cmdLine, options, (err, stdout, stderr) => {
|
|
console.log(stdout);
|
|
console.log(stderr);
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Watch the docs source files, and regenerate the docs if it changes
|
|
*/
|
|
gulp.task('swagger-watcher', ['swagger-asciidoc2html'], () => {
|
|
gulp.watch([config.api.watch], ['swagger-asciidoc2html']);
|
|
});
|
|
|
|
/**
|
|
* Build the docs from the Swagger file
|
|
*/
|
|
gulp.task('integration2asciidoc', (callback) => {
|
|
log('Generating the asciidoc from the integration API swagger definition');
|
|
|
|
const options = config.intApi.options;
|
|
const promise = DocGen.Swagger2AsciiDoc(config.intApi.src, options);
|
|
promise.then(() => {
|
|
// All good
|
|
callback();
|
|
return null;
|
|
}).catch((error) => {
|
|
// Failed somewhere
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
gulp.task('integration-asciidoc2html', ['integration2asciidoc'], (callback) => {
|
|
log('Building the asciidoc into output formats');
|
|
|
|
const cmdLine = 'asciidoctor -a toc -a toclevels=3 -d book ' + config.intApi.indexPath;
|
|
const options = {
|
|
cwd: config.intApi.dest
|
|
};
|
|
exec(cmdLine, options, (err, stdout, stderr) => {
|
|
console.log(stdout);
|
|
console.log(stderr);
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Watch the docs source files, and regenerate the docs if it changes
|
|
*/
|
|
gulp.task('integration-watcher', ['integration-asciidoc2html'], () => {
|
|
gulp.watch([config.intApi.watch], ['integration-asciidoc2html']);
|
|
});
|
|
|
|
/**
|
|
* Build the asciidoc files from the wiki pages
|
|
*/
|
|
gulp.task('wiki2asciidoc', (callback) => {
|
|
log('Generating the asciidoc from the wiki');
|
|
|
|
const options = config.wikidocs;
|
|
const promise = WikiGen.Wiki2AsciiDoc(options);
|
|
promise.then(() => {
|
|
// All good
|
|
callback();
|
|
return null;
|
|
}).catch((error) => {
|
|
// Failed somewhere
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Build the html from the asciidoc files
|
|
*/
|
|
gulp.task('wiki-asciidoc2html', ['wiki2asciidoc'], (callback) => {
|
|
log('Building the wiki-based asciidoc into output formats');
|
|
|
|
let cmdLine = 'asciidoctor';
|
|
cmdLine += ' --attribute imagesdir=' + config.wikidocs.fileDestRelative;
|
|
cmdLine += ' --attribute data-uri'; // Write images inline in the html
|
|
cmdLine += ' *.adoc';
|
|
const options = {
|
|
cwd: config.wikidocs.dest
|
|
};
|
|
exec(cmdLine, options, (err, stdout, stderr) => {
|
|
console.log(stdout);
|
|
console.log(stderr);
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Watch the asciidocs files, and regenerate the html if they change
|
|
*/
|
|
gulp.task('wiki-watcher', ['wiki-asciidoc2html'], () => {
|
|
gulp.watch([config.wikidocs.watch], ['wiki-asciidoc2html']);
|
|
});
|
|
|
|
/**
|
|
* Build the index file from the configuration
|
|
*/
|
|
gulp.task('index2asciidoc', ['swagger2asciidoc', 'wiki2asciidoc'], (callback) => {
|
|
log('Generating the asciidoc for the index');
|
|
|
|
const options = config;
|
|
const promise = IndexGen.GenerateIndex(options);
|
|
promise.then(() => {
|
|
// All good
|
|
callback();
|
|
return null;
|
|
}).catch((error) => {
|
|
// Failed somewhere
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Build the html from the index asciidoc files
|
|
*/
|
|
gulp.task('index-asciidoc2html', ['index2asciidoc'], (callback) => {
|
|
log('Building the index asciidoc into output formats');
|
|
|
|
let cmdLine = 'asciidoctor';
|
|
cmdLine += ' --attribute imagesdir=' + config.indexdocs.fileDestRelative;
|
|
cmdLine += ' --attribute data-uri'; // Write images inline in the html
|
|
cmdLine += ' -d book';
|
|
cmdLine += ' ' + config.indexdocs.indexPath;
|
|
const options = {
|
|
cwd: config.indexdocs.dest
|
|
};
|
|
exec(cmdLine, options, (err, stdout, stderr) => {
|
|
console.log(stdout);
|
|
console.log(stderr);
|
|
callback(err);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Watch the asciidocs files, and regenerate the html if they change
|
|
*/
|
|
gulp.task('index-watcher', ['index-asciidoc2html'], () => {
|
|
gulp.watch([config.indexdocs.watch], ['index-asciidoc2Html']);
|
|
});
|
|
|
|
/**
|
|
* Task to build sample schemas from the wiki page
|
|
*/
|
|
gulp.task('wiki2schema', (callback) => {
|
|
log('Generating the schema samples from the wiki');
|
|
|
|
const options = config.wikidocs;
|
|
const promise = SchemaGen.Wiki2Schema(options);
|
|
promise.then(() => {
|
|
// All good
|
|
callback();
|
|
return null;
|
|
}).catch((error) => {
|
|
// Failed somewhere
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Task to run moch tests on all files call *.spec.js
|
|
* This task allows errors to exit the program so that they can be picked up externally.
|
|
*/
|
|
gulp.task('test', () => {
|
|
console.log('Running tests...');
|
|
gulp.src(config.test.testpaths)
|
|
.pipe(mocha({
|
|
reporter: testReporter
|
|
}));
|
|
});
|
|
|
|
/**
|
|
* Task to run moch tests on all files call *.spec.js
|
|
* This differs from the above task in that it swallows all errors so that gulp.watch
|
|
* will not end prematurely when the tests fail.
|
|
*/
|
|
gulp.task('test-watched', () => {
|
|
console.log('Running tests...');
|
|
gulp.src(config.test.testpaths)
|
|
.pipe($.plumber())
|
|
.pipe(mocha({
|
|
reporter: testReporter
|
|
}));
|
|
});
|
|
|
|
/**
|
|
* A watcher to automatically re-run unit tests when a watched file changes.
|
|
*/
|
|
gulp.task('test-watcher', ['test-watched'], () => {
|
|
gulp.watch(config.test.watchpaths, ['test-watched']);
|
|
});
|
|
|
|
/**
|
|
* Log a message or series of messages using chalk's blue color.
|
|
* Can pass in a string, object or array.
|
|
*
|
|
* @param {String|Object} msg - the message to log
|
|
*/
|
|
function log(msg) {
|
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
if (typeof (msg) === 'object') {
|
|
for (const item in msg) {
|
|
if (msg.hasOwnProperty(item)) {
|
|
$.util.log($.util.colors.blue(msg[item]));
|
|
}
|
|
}
|
|
} else {
|
|
$.util.log($.util.colors.blue(msg));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bump the version
|
|
* --type=pre will bump the prerelease version *.*.*-x
|
|
* --type=patch or no flag will bump the patch version *.*.x
|
|
* --type=minor will bump the minor version *.x.*
|
|
* --type=major will bump the major version x.*.*
|
|
* --version=1.2.3 will bump to a specific version and ignore other flags
|
|
*/
|
|
gulp.task('bump', () => {
|
|
let msg = 'Bumping versions';
|
|
const type = args.type;
|
|
const version = args.ver;
|
|
const options = {};
|
|
if (version) {
|
|
options.version = version;
|
|
msg += ' to ' + version;
|
|
} else {
|
|
options.type = type;
|
|
msg += ' for a ' + type;
|
|
}
|
|
log(msg);
|
|
|
|
gulp
|
|
.src('./package.json')
|
|
.pipe($.print())
|
|
.pipe($.bump(options))
|
|
.pipe(gulp.dest('./'));
|
|
|
|
return gulp
|
|
.src('../package.json')
|
|
.pipe($.print())
|
|
.pipe($.bump(options))
|
|
.pipe(gulp.dest('../'));
|
|
});
|
|
|
|
module.exports = gulp;
|