diff --git a/packages/kbn-plugin-generator/index.js b/packages/kbn-plugin-generator/index.js index 15adce7f01c8e..5417649db1a39 100644 --- a/packages/kbn-plugin-generator/index.js +++ b/packages/kbn-plugin-generator/index.js @@ -54,9 +54,8 @@ exports.run = function run(argv) { } const name = options._[0]; - const isKibanaPlugin = options.internal; const template = resolve(__dirname, './sao_template'); - const kibanaPlugins = resolve(__dirname, isKibanaPlugin ? '../../src/plugins' : '../../plugins'); + const kibanaPlugins = resolve(process.cwd(), 'plugins'); const targetPath = resolve(kibanaPlugins, snakeCase(name)); sao({ @@ -64,7 +63,6 @@ exports.run = function run(argv) { targetPath: targetPath, configOptions: { name, - isKibanaPlugin, targetPath, }, }).catch(error => { diff --git a/packages/kbn-plugin-generator/sao_template/sao.js b/packages/kbn-plugin-generator/sao_template/sao.js index aed4b9a02838f..f8d13b1229b7b 100755 --- a/packages/kbn-plugin-generator/sao_template/sao.js +++ b/packages/kbn-plugin-generator/sao_template/sao.js @@ -17,7 +17,8 @@ * under the License. */ -const { relative } = require('path'); +const { relative, resolve } = require('path'); +const fs = require('fs'); const startCase = require('lodash.startcase'); const camelCase = require('lodash.camelcase'); @@ -29,9 +30,54 @@ const pkg = require('../package.json'); const kibanaPkgPath = require.resolve('../../../package.json'); const kibanaPkg = require(kibanaPkgPath); // eslint-disable-line import/no-dynamic-require -module.exports = function({ name, targetPath, isKibanaPlugin }) { +async function gitInit(dir) { + // Only plugins in /plugins get git init + try { + await execa('git', ['init', dir]); + console.log(`Git repo initialized in ${dir}`); + } catch (error) { + console.error(error); + throw new Error(`Failure to git init ${dir}: ${error.all || error}`); + } +} + +async function moveToCustomFolder(from, to) { + try { + await execa('mv', [from, to]); + } catch (error) { + console.error(error); + throw new Error(`Failure to move plugin to ${to}: ${error.all || error}`); + } +} + +async function eslintPlugin(dir) { + try { + await execa('yarn', ['lint:es', `./${dir}/**/*.ts*`, '--no-ignore', '--fix']); + } catch (error) { + console.error(error); + throw new Error(`Failure when running prettier on the generated output: ${error.all || error}`); + } +} + +module.exports = function({ name, targetPath }) { return { prompts: { + customPath: { + message: 'Would you like to create the plugin in a different folder?', + default: '/plugins', + filter(value) { + // Keep default value empty + if (value === '/plugins') return ''; + return value; + }, + validate(customPath) { + const p = resolve(process.cwd(), customPath); + const exists = fs.existsSync(p); + if (!exists) + return `Folder should exist relative to the kibana root folder. Consider /src/plugins or /x-pack/plugins.`; + return true; + }, + }, description: { message: 'Provide a short description', default: 'An awesome Kibana plugin', @@ -64,7 +110,9 @@ module.exports = function({ name, targetPath, isKibanaPlugin }) { generateEslint: { type: 'confirm', message: 'Would you like to use a custom eslint file?', - default: !isKibanaPlugin, + default({ customPath }) { + return !customPath; + }, }, }, filters: { @@ -86,31 +134,32 @@ module.exports = function({ name, targetPath, isKibanaPlugin }) { camelCase, snakeCase, name, - isKibanaPlugin, + // kibana plugins are placed in a the non default path + isKibanaPlugin: !answers.customPath, kbnVersion: answers.kbnVersion, upperCamelCaseName: name.charAt(0).toUpperCase() + camelCase(name).slice(1), hasUi: !!answers.generateApp, hasServer: !!answers.generateApi, hasScss: !!answers.generateScss, - relRoot: isKibanaPlugin ? '../../../..' : '../../..', + relRoot: relative(resolve(answers.customPath || targetPath, 'public'), process.cwd()), }, answers ), enforceNewFolder: true, installDependencies: false, - gitInit: !isKibanaPlugin, - async post({ log }) { - const dir = relative(process.cwd(), targetPath); + async post({ log, answers }) { + let dir = relative(process.cwd(), targetPath); + if (answers.customPath) { + // Move to custom path + moveToCustomFolder(targetPath, answers.customPath); + dir = relative(process.cwd(), resolve(answers.customPath, snakeCase(name))); + } else { + // Init git only in the default path + await gitInit(dir); + } // Apply eslint to the generated plugin - try { - await execa('yarn', ['lint:es', `./${dir}/**/*.ts*`, '--no-ignore', '--fix']); - } catch (error) { - console.error(error); - throw new Error( - `Failure when running prettier on the generated output: ${error.all || error}` - ); - } + eslintPlugin(dir); log.success(chalk`🎉