Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
use a promise instead of a callback to track task completion
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Nov 17, 2016
1 parent e42d134 commit c6dacf3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 75 deletions.
140 changes: 70 additions & 70 deletions tasks/build/build_action.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
module.exports = function (plugin, callback) {
if (!callback) callback = function () {};
module.exports = function (plugin) {
return new Promise(function (resolve, reject) {
var vfs = require('vinyl-fs');
var zip = require('gulp-zip');
var map = require('through2-map').obj;
var rename = require('gulp-rename');
var join = require('path').join;
var inquirer = require('inquirer');

var vfs = require('vinyl-fs');
var zip = require('gulp-zip');
var map = require('through2-map').obj;
var rename = require('gulp-rename');
var join = require('path').join;
var inquirer = require('inquirer');
function main() {
var kibanaVersion = (plugin.pkg.kibana && plugin.pkg.kibana.version) || plugin.pkg.version;
var deps = Object.keys(plugin.pkg.dependencies || {});
var buildId = `${plugin.id}-${plugin.version}`;

function main() {
var kibanaVersion = (plugin.pkg.kibana && plugin.pkg.kibana.version) || plugin.pkg.version;
var deps = Object.keys(plugin.pkg.dependencies || {});
var buildId = `${plugin.id}-${plugin.version}`;
if (kibanaVersion === 'kibana') {
askForKibanaVersion(function (customKibanaVersion) {
build(buildId, deps, customKibanaVersion);
});
} else {
build(buildId, deps, kibanaVersion);
}
}

if (kibanaVersion === 'kibana') {
askForKibanaVersion(function (customKibanaVersion) {
build(buildId, deps, customKibanaVersion);
function askForKibanaVersion(cb) {
inquirer.prompt([
{
type: 'input',
name: 'kibanaVersion',
message: 'What version of Kibana are you building for?'
}
]).then(function (answers) {
cb(answers.kibanaVersion);
});
} else {
build(buildId, deps, kibanaVersion);
}
}

function askForKibanaVersion(cb) {
inquirer.prompt([
{
type: 'input',
name: 'kibanaVersion',
message: 'What version of Kibana are you building for?'
function toBuffer(string) {
if (typeof Buffer.from === 'function') {
return Buffer.from(string, 'utf8');
} else {
// this was deprecated in node v5 in favor
// of Buffer.from(string, encoding)
return new Buffer(string, 'utf8');
}
]).then(function (answers) {
cb(answers.kibanaVersion);
});
}

function toBuffer(string) {
if (typeof Buffer.from === 'function') {
return Buffer.from(string, 'utf8');
} else {
// this was deprecated in node v5 in favor
// of Buffer.from(string, encoding)
return new Buffer(string, 'utf8');
}
}

function build(buildId, deps, kibanaVersion) {
var files = [
'package.json',
'index.js',
'{lib,public,server,webpackShims}/**/*'
];
function build(buildId, deps, kibanaVersion) {
var files = [
'package.json',
'index.js',
'{lib,public,server,webpackShims}/**/*'
];

if (deps.length === 1) {
files.push(`node_modules/${ deps[0] }/**/*`);
} else if (deps.length) {
files.push(`node_modules/{${ deps.join(',') }}/**/*`);
}
if (deps.length === 1) {
files.push(`node_modules/${ deps[0] }/**/*`);
} else if (deps.length) {
files.push(`node_modules/{${ deps.join(',') }}/**/*`);
}

vfs
.src(files, { cwd: plugin.root, base: plugin.root })
vfs
.src(files, { cwd: plugin.root, base: plugin.root })

// rewrite the target kibana version while the
// file is on it's way to the archive
.pipe(map(function (file) {
if (file.basename === 'package.json') {
const pkg = JSON.parse(file.contents.toString('utf8'));
if (!pkg.kibana) pkg.kibana = {};
pkg.kibana.version = kibanaVersion;
file.contents = toBuffer(JSON.stringify(pkg));
}
// rewrite the target kibana version while the
// file is on it's way to the archive
.pipe(map(function (file) {
if (file.basename === 'package.json') {
const pkg = JSON.parse(file.contents.toString('utf8'));
if (!pkg.kibana) pkg.kibana = {};
pkg.kibana.version = kibanaVersion;
file.contents = toBuffer(JSON.stringify(pkg));
}

return file;
}))
return file;
}))

// put all files inside the correct directoried
.pipe(rename(function nestFileInDir(path) {
path.dirname = join('kibana', plugin.id, path.dirname);
}))
// put all files inside the correct directoried
.pipe(rename(function nestFileInDir(path) {
path.dirname = join('kibana', plugin.id, path.dirname);
}))

.pipe(zip(`${buildId}.zip`))
.pipe(vfs.dest(join(plugin.root, 'build')))
.on('end', callback);
}
.pipe(zip(`${buildId}.zip`))
.pipe(vfs.dest(join(plugin.root, 'build')))
.on('end', resolve);
}

main();
main();
});
};
8 changes: 3 additions & 5 deletions tasks/build/build_action.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ describe('build_action', () => {
beforeEach(() => del(PLUGIN_BUILD_DIR));
afterEach(() => del(PLUGIN_BUILD_DIR));

it('creates a zip in the build directory', (done) => {
buildAction(PLUGIN, () => {
it('creates a zip in the build directory', () => {
return buildAction(PLUGIN).then(() => {
if (!fs.existsSync(resolve(PLUGIN_BUILD_DIR, PLUGIN.id + '-' + PLUGIN.version + '.zip'))) {
done(new Error('expected the plugin to build a zip file'));
} else {
done();
throw new Error('expected the plugin to build a zip file');
}
});
});
Expand Down

0 comments on commit c6dacf3

Please sign in to comment.