From 335c7129924d25dc200d69c9b29c0b734634701b Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 16 Feb 2020 18:13:22 +0200 Subject: [PATCH 1/2] allow using any path to generate --- packages/kbn-plugin-generator/index.js | 4 +- .../kbn-plugin-generator/sao_template/sao.js | 81 +++++++++++++++---- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/packages/kbn-plugin-generator/index.js b/packages/kbn-plugin-generator/index.js index 15adce7f01c8e5..5417649db1a39b 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 aed4b9a02838f5..f8d13b1229b7b1 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`🎉 From 36599fdfac18ddf6ecd475f0de461e4d5e87900f Mon Sep 17 00:00:00 2001 From: Liza K Date: Sun, 16 Feb 2020 18:19:37 +0200 Subject: [PATCH 2/2] revert --- packages/kbn-plugin-generator/index.js | 4 +- .../kbn-plugin-generator/sao_template/sao.js | 81 ++++--------------- 2 files changed, 19 insertions(+), 66 deletions(-) diff --git a/packages/kbn-plugin-generator/index.js b/packages/kbn-plugin-generator/index.js index 5417649db1a39b..15adce7f01c8e5 100644 --- a/packages/kbn-plugin-generator/index.js +++ b/packages/kbn-plugin-generator/index.js @@ -54,8 +54,9 @@ exports.run = function run(argv) { } const name = options._[0]; + const isKibanaPlugin = options.internal; const template = resolve(__dirname, './sao_template'); - const kibanaPlugins = resolve(process.cwd(), 'plugins'); + const kibanaPlugins = resolve(__dirname, isKibanaPlugin ? '../../src/plugins' : '../../plugins'); const targetPath = resolve(kibanaPlugins, snakeCase(name)); sao({ @@ -63,6 +64,7 @@ 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 f8d13b1229b7b1..aed4b9a02838f5 100755 --- a/packages/kbn-plugin-generator/sao_template/sao.js +++ b/packages/kbn-plugin-generator/sao_template/sao.js @@ -17,8 +17,7 @@ * under the License. */ -const { relative, resolve } = require('path'); -const fs = require('fs'); +const { relative } = require('path'); const startCase = require('lodash.startcase'); const camelCase = require('lodash.camelcase'); @@ -30,54 +29,9 @@ const pkg = require('../package.json'); const kibanaPkgPath = require.resolve('../../../package.json'); const kibanaPkg = require(kibanaPkgPath); // eslint-disable-line import/no-dynamic-require -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 }) { +module.exports = function({ name, targetPath, isKibanaPlugin }) { 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', @@ -110,9 +64,7 @@ module.exports = function({ name, targetPath }) { generateEslint: { type: 'confirm', message: 'Would you like to use a custom eslint file?', - default({ customPath }) { - return !customPath; - }, + default: !isKibanaPlugin, }, }, filters: { @@ -134,32 +86,31 @@ module.exports = function({ name, targetPath }) { camelCase, snakeCase, name, - // kibana plugins are placed in a the non default path - isKibanaPlugin: !answers.customPath, + isKibanaPlugin, kbnVersion: answers.kbnVersion, upperCamelCaseName: name.charAt(0).toUpperCase() + camelCase(name).slice(1), hasUi: !!answers.generateApp, hasServer: !!answers.generateApi, hasScss: !!answers.generateScss, - relRoot: relative(resolve(answers.customPath || targetPath, 'public'), process.cwd()), + relRoot: isKibanaPlugin ? '../../../..' : '../../..', }, answers ), enforceNewFolder: true, installDependencies: false, - 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); - } + gitInit: !isKibanaPlugin, + async post({ log }) { + const dir = relative(process.cwd(), targetPath); // Apply eslint to the generated plugin - 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}` + ); + } log.success(chalk`🎉