This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a promise instead of a callback to track task completion
- Loading branch information
spalger
committed
Nov 17, 2016
1 parent
e42d134
commit c6dacf3
Showing
2 changed files
with
73 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters