Skip to content

Commit

Permalink
Allow custom paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Feb 16, 2020
1 parent 36599fd commit 2de8d5a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
4 changes: 1 addition & 3 deletions packages/kbn-plugin-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ 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({
template: template,
targetPath: targetPath,
configOptions: {
name,
isKibanaPlugin,
targetPath,
},
}).catch(error => {
Expand Down
81 changes: 65 additions & 16 deletions packages/kbn-plugin-generator/sao_template/sao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
Expand Down Expand Up @@ -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: {
Expand All @@ -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`🎉
Expand Down

0 comments on commit 2de8d5a

Please sign in to comment.