Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Update plugin generator to generate NP plugins (#55281) #57831

Merged
merged 3 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/kbn-plugin-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports.run = function run(argv) {
const options = getopts(argv, {
alias: {
h: 'help',
i: 'internal',
},
});

Expand All @@ -40,24 +41,31 @@ exports.run = function run(argv) {
if (options.help) {
console.log(
dedent(chalk`
{dim usage:} node scripts/generate-plugin {bold [name]}

generate a fresh Kibana plugin in the plugins/ directory
# {dim Usage:}
node scripts/generate-plugin {bold [name]}
Generate a fresh Kibana plugin in the plugins/ directory

# {dim Core Kibana plugins:}
node scripts/generate-plugin {bold [name]} -i
To generate a core Kibana plugin inside the src/plugins/ directory, add the -i flag.
`) + '\n'
);
process.exit(1);
}

const name = options._[0];
const isKibanaPlugin = options.internal;
const template = resolve(__dirname, './sao_template');
const kibanaPlugins = resolve(__dirname, '../../plugins');
const kibanaPlugins = resolve(__dirname, isKibanaPlugin ? '../../src/plugins' : '../../plugins');
const targetPath = resolve(kibanaPlugins, snakeCase(name));

sao({
template: template,
targetPath: targetPath,
configOptions: {
name,
isKibanaPlugin,
targetPath,
},
}).catch(error => {
console.error(chalk`{red fatal error}!`);
Expand Down
24 changes: 24 additions & 0 deletions packages/kbn-plugin-generator/index.js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
interface PluginGenerator {
/**
* Run plugin generator.
*/
run: (...args: any[]) => any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ describe(`running the plugin-generator via 'node scripts/generate_plugin.js plug
expect(stats.isDirectory()).toBe(true);
});

it(`should create an internationalization config file with a blank line appended to satisfy the parser`, async () => {
// skipped until internationalization is re-introduced
it.skip(`should create an internationalization config file with a blank line appended to satisfy the parser`, async () => {
// Link to the error that happens when the blank line is not there:
// https://github.com/elastic/kibana/pull/45044#issuecomment-530092627
const intlFile = `${generatedPath}/.i18nrc.json`;
Expand All @@ -78,16 +79,7 @@ describe(`running the plugin-generator via 'node scripts/generate_plugin.js plug
});
});

it(`'yarn test:server' should exit 0`, async () => {
await execa('yarn', ['test:mocha'], {
cwd: generatedPath,
env: {
DISABLE_JUNIT_REPORTER: '1',
},
});
});

it(`'yarn build' should exit 0`, async () => {
it.skip(`'yarn build' should exit 0`, async () => {
await execa('yarn', ['build'], { cwd: generatedPath });
});

Expand All @@ -113,7 +105,7 @@ describe(`running the plugin-generator via 'node scripts/generate_plugin.js plug
'--migrations.skip=true',
],
cwd: generatedPath,
wait: /ispec_plugin.+Status changed from uninitialized to green - Ready/,
wait: new RegExp('\\[ispecPlugin\\]\\[plugins\\] Setting up plugin'),
});
await pr.stop('kibana');
});
Expand All @@ -123,7 +115,7 @@ describe(`running the plugin-generator via 'node scripts/generate_plugin.js plug
await execa('yarn', ['preinstall'], { cwd: generatedPath });
});

it(`'yarn lint' should exit 0`, async () => {
it.skip(`'yarn lint' should exit 0`, async () => {
await execa('yarn', ['lint'], { cwd: generatedPath });
});

Expand Down
69 changes: 33 additions & 36 deletions packages/kbn-plugin-generator/sao_template/sao.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
* under the License.
*/

const { resolve, relative, dirname } = require('path');
const { relative } = require('path');

const startCase = require('lodash.startcase');
const camelCase = require('lodash.camelcase');
const snakeCase = require('lodash.snakecase');
const execa = require('execa');
const chalk = require('chalk');
const execa = require('execa');

const pkg = require('../package.json');
const kibanaPkgPath = require.resolve('../../../package.json');
const kibanaPkg = require(kibanaPkgPath); // eslint-disable-line import/no-dynamic-require

const KBN_DIR = dirname(kibanaPkgPath);

module.exports = function({ name }) {
module.exports = function({ name, targetPath, isKibanaPlugin }) {
return {
prompts: {
description: {
Expand All @@ -47,41 +45,38 @@ module.exports = function({ name }) {
message: 'Should an app component be generated?',
default: true,
},
generateTranslations: {
type: 'confirm',
message: 'Should translation files be generated?',
default: true,
},
generateHack: {
type: 'confirm',
message: 'Should a hack component be generated?',
default: true,
},
generateApi: {
type: 'confirm',
message: 'Should a server API be generated?',
default: true,
},
// generateTranslations: {
// type: 'confirm',
// message: 'Should translation files be generated?',
// default: true,
// },
generateScss: {
type: 'confirm',
message: 'Should SCSS be used?',
when: answers => answers.generateApp,
default: true,
},
generateEslint: {
type: 'confirm',
message: 'Would you like to use a custom eslint file?',
default: !isKibanaPlugin,
},
},
filters: {
'public/**/index.scss': 'generateScss',
'public/**/*': 'generateApp',
'translations/**/*': 'generateTranslations',
'.i18nrc.json': 'generateTranslations',
'public/hack.js': 'generateHack',
'server/**/*': 'generateApi',
'public/app.scss': 'generateScss',
'.kibana-plugin-helpers.json': 'generateScss',
// 'translations/**/*': 'generateTranslations',
// '.i18nrc.json': 'generateTranslations',
'eslintrc.js': 'generateEslint',
},
move: {
gitignore: '.gitignore',
'eslintrc.js': '.eslintrc.js',
'package_template.json': 'package.json',
},
data: answers =>
Object.assign(
Expand All @@ -91,34 +86,36 @@ module.exports = function({ name }) {
camelCase,
snakeCase,
name,
isKibanaPlugin,
kbnVersion: answers.kbnVersion,
upperCamelCaseName: name.charAt(0).toUpperCase() + camelCase(name).slice(1),
hasUi: !!answers.generateApp,
hasServer: !!answers.generateApi,
hasScss: !!answers.generateScss,
relRoot: isKibanaPlugin ? '../../../..' : '../../..',
},
answers
),
enforceNewFolder: true,
installDependencies: false,
gitInit: true,
gitInit: !isKibanaPlugin,
async post({ log }) {
await execa('yarn', ['kbn', 'bootstrap'], {
cwd: KBN_DIR,
stdio: 'inherit',
});

const dir = relative(process.cwd(), resolve(KBN_DIR, 'plugins', snakeCase(name)));
const dir = relative(process.cwd(), targetPath);

// Apply eslint to the generated plugin
try {
await execa('yarn', ['lint', '--fix'], {
cwd: dir,
all: true,
});
await execa('yarn', ['lint:es', `./${dir}/**/*.ts*`, '--no-ignore', '--fix']);
} catch (error) {
throw new Error(`Failure when running prettier on the generated output: ${error.all}`);
console.error(error);
throw new Error(
`Failure when running prettier on the generated output: ${error.all || error}`
);
}

log.success(chalk`🎉

Your plugin has been created in {bold ${dir}}. Move into that directory to run it:
Your plugin has been created in {bold ${dir}}.

{bold cd "${dir}"}
{bold yarn start}
`);
},
Expand Down
Loading