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.
* only modify root package.json file previously this was modifying every package.json from node_modules as well * refactor the build task export the build, define helpers outside of the task * allow custom kibana and build versions both can be passed via flags * allow custom build globs to be specified * move build into its own module * add simple option parsing tests * update readme * move dependency file appending into the action * put source and target into variables * move config file loading into a module * refactor test_server_action slightly be more explicit about the files option overwriting the plugin settings * move default build patterns to plugin config allows the setting to be overridden via the config file * fix dirname on relative includes trim any leading '../' off the path when moving it into the build target * move node_module dirs into plugin_config module, use existing promises * rename file_config => config_file
- Loading branch information
Showing
8 changed files
with
251 additions
and
152 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
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var resolve = require('path').resolve; | ||
var readFileSync = require('fs').readFileSync; | ||
|
||
var configFiles = [ '.kibana-plugin-helpers.json', '.kibana-plugin-helpers.dev.json' ]; | ||
var configCache = {}; | ||
|
||
module.exports = function (root) { | ||
if (!root) root = process.cwd(); | ||
|
||
if (configCache[root]) { | ||
return configCache[root]; | ||
} | ||
|
||
// config files to read from, in the order they are merged together | ||
var config = configCache[root] = {}; | ||
|
||
configFiles.forEach(function (configFile) { | ||
try { | ||
var content = JSON.parse(readFileSync(resolve(root, configFile))); | ||
config = Object.assign(config, content); | ||
} catch (e) { | ||
// noop | ||
} | ||
}); | ||
|
||
// if the kibanaRoot is set, use resolve to ensure correct resolution | ||
if (config.kibanaRoot) config.kibanaRoot = resolve(root, config.kibanaRoot); | ||
|
||
return config; | ||
}; |
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
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,122 +1,39 @@ | ||
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 execFileSync = require('child_process').execFileSync; | ||
|
||
module.exports = function (plugin) { | ||
return new Promise(function (resolve, reject) { | ||
|
||
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); | ||
} | ||
} | ||
|
||
function askForKibanaVersion(cb) { | ||
inquirer.prompt([ | ||
{ | ||
type: 'input', | ||
name: 'kibanaVersion', | ||
message: 'What version of Kibana are you building for?' | ||
} | ||
]).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 gitInfo() { | ||
try { | ||
var LOG_SEPARATOR = '||'; | ||
var commitCount = execFileSync('git', ['rev-list', '--count', 'HEAD'], { | ||
cwd: plugin.root, | ||
stdio: ['ignore', 'pipe', 'ignore'], | ||
encoding: 'utf8', | ||
}); | ||
var logLine = execFileSync('git', ['log', '--pretty=%h' + LOG_SEPARATOR + '%cD', '-n', '1'], { | ||
cwd: plugin.root, | ||
stdio: ['ignore', 'pipe', 'ignore'], | ||
encoding: 'utf8', | ||
}).split(LOG_SEPARATOR); | ||
|
||
return { | ||
count: commitCount.trim(), | ||
sha: logLine[0].trim(), | ||
date: logLine[1].trim(), | ||
}; | ||
} catch (e) { | ||
return {}; | ||
} | ||
} | ||
|
||
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(',') }}/**/*`); | ||
} | ||
|
||
vfs | ||
.src(files, { cwd: plugin.root, base: plugin.root }) | ||
|
||
// modify the package.json file | ||
.pipe(map(function (file) { | ||
if (file.basename === 'package.json') { | ||
var pkg = JSON.parse(file.contents.toString('utf8')); | ||
|
||
// rewrite the target kibana version while the | ||
// file is on it's way to the archive | ||
if (!pkg.kibana) pkg.kibana = {}; | ||
pkg.kibana.version = kibanaVersion; | ||
|
||
// append build info | ||
pkg.build = { | ||
git: gitInfo(), | ||
date: new Date().toString() | ||
}; | ||
|
||
file.contents = toBuffer(JSON.stringify(pkg, null, 2)); | ||
} | ||
|
||
return file; | ||
})) | ||
|
||
// put all files inside the correct directoried | ||
.pipe(rename(function nestFileInDir(path) { | ||
path.dirname = join('kibana', plugin.id, path.dirname); | ||
})) | ||
var createBuild = require('./create_build'); | ||
|
||
module.exports = function (plugin, run, options) { | ||
options = options || {}; | ||
var buildVersion = plugin.version; | ||
var kibanaVersion = (plugin.pkg.kibana && plugin.pkg.kibana.version) || plugin.pkg.version; | ||
var buildFiles = plugin.buildSourcePatterns; | ||
|
||
// allow source files to be overridden | ||
if (options.files && options.files.length) { | ||
buildFiles = options.files; | ||
} | ||
|
||
// allow options to override plugin info | ||
if (options.buildVersion) buildVersion = options.buildVersion; | ||
if (options.kibanaVersion) kibanaVersion = options.kibanaVersion; | ||
|
||
if (kibanaVersion === 'kibana') { | ||
return askForKibanaVersion().then(function (customKibanaVersion) { | ||
return createBuild(plugin, buildVersion, customKibanaVersion, buildFiles); | ||
}); | ||
} else { | ||
return createBuild(plugin, buildVersion, kibanaVersion, buildFiles); | ||
} | ||
}; | ||
|
||
.pipe(zip(`${buildId}.zip`)) | ||
.pipe(vfs.dest(join(plugin.root, 'build'))) | ||
.on('end', resolve); | ||
function askForKibanaVersion(cb) { | ||
return inquirer.prompt([ | ||
{ | ||
type: 'input', | ||
name: 'kibanaVersion', | ||
message: 'What version of Kibana are you building for?' | ||
} | ||
|
||
main(); | ||
]).then(function (answers) { | ||
return answers.kibanaVersion; | ||
}); | ||
}; | ||
} |
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,24 +1,78 @@ | ||
/*eslint-env jest*/ | ||
const resolve = require('path').resolve; | ||
const fs = require('fs'); | ||
|
||
const del = require('del'); | ||
|
||
const buildAction = require('./build_action'); | ||
|
||
const PLUGIN_FIXTURE = resolve(__dirname, '__fixtures__/test_plugin'); | ||
const PLUGIN_BUILD_DIR = resolve(PLUGIN_FIXTURE, 'build'); | ||
const PLUGIN = require('../../lib/plugin_config')(PLUGIN_FIXTURE); | ||
const noop = function () {}; | ||
|
||
describe('build_action', () => { | ||
beforeEach(() => del(PLUGIN_BUILD_DIR)); | ||
afterEach(() => del(PLUGIN_BUILD_DIR)); | ||
|
||
it('creates a zip in the build directory', () => { | ||
return buildAction(PLUGIN).then(() => { | ||
if (!fs.existsSync(resolve(PLUGIN_BUILD_DIR, PLUGIN.id + '-' + PLUGIN.version + '.zip'))) { | ||
throw new Error('expected the plugin to build a zip file'); | ||
} | ||
describe('creating build zip', function () { | ||
const buildAction = require('./build_action'); | ||
|
||
beforeEach(() => del(PLUGIN_BUILD_DIR)); | ||
afterEach(() => del(PLUGIN_BUILD_DIR)); | ||
|
||
it('creates a zip in the build directory', () => { | ||
return buildAction(PLUGIN).then(() => { | ||
var buildFile = resolve(PLUGIN_BUILD_DIR, PLUGIN.id + '-' + PLUGIN.version + '.zip'); | ||
if (!fs.existsSync(buildFile)) { | ||
throw new Error('Build file not found: ' + buildFile); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('calling create_build', () => { | ||
let mockBuild; | ||
let buildAction; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
mockBuild = jest.fn(() => Promise.resolve()); | ||
jest.mock('./create_build', () => mockBuild); | ||
buildAction = require('./build_action'); | ||
}); | ||
|
||
it('takes optional build version', function () { | ||
const options = { | ||
buildVersion: '1.2.3', | ||
kibanaVersion: '4.5.6', | ||
}; | ||
|
||
return buildAction(PLUGIN, noop, options).then(() => { | ||
expect(mockBuild.mock.calls).toHaveLength(1); | ||
const [ plugin, buildVersion, kibanaVersion, files ] = mockBuild.mock.calls[0]; | ||
expect(buildVersion).toBe('1.2.3'); | ||
expect(kibanaVersion).toBe('4.5.6'); | ||
}); | ||
}); | ||
|
||
it('uses default file list without files option', function () { | ||
return buildAction(PLUGIN).then(() => { | ||
expect(mockBuild.mock.calls).toHaveLength(1); | ||
const [ plugin, buildVersion, kibanaVersion, files ] = mockBuild.mock.calls[0]; | ||
PLUGIN.buildSourcePatterns.forEach(file => expect(files).toContain(file)); | ||
}); | ||
}); | ||
|
||
it('uses only files passed in', function () { | ||
const options = { | ||
files: [ | ||
'index.js', | ||
'LICENSE.txt', | ||
'plugins/**/*', | ||
'{server,public}/**/*' | ||
] | ||
}; | ||
|
||
return buildAction(PLUGIN, noop, options).then(() => { | ||
expect(mockBuild.mock.calls).toHaveLength(1); | ||
const [ plugin, buildVersion, kibanaVersion, files ] = mockBuild.mock.calls[0]; | ||
options.files.forEach(file => expect(files).toContain(file)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.