Skip to content

Commit

Permalink
Update plugin generator to generate NP plugins (elastic#55281)
Browse files Browse the repository at this point in the history
* Generate NP plugin

* Added tsconfig

* tsconfig

* Adjust sao test

* Add server side to plugin gen

* Added navigation

* add empty element

* eslint

* platform team CR

* design CR improvements

* text updates

* temp disable plugin gen tests

* eslint

* Code review fixes

* Add scss support - requires elastic#53976 to be merged to work

* CR fixes

* comment fixes

* Don't generate eslint for internal plugins by default

* Update tests

* reenable jest test for sao

* Fix regex

* review comments

* code review

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
Liza Katz and elasticmachine committed Feb 18, 2020
1 parent beab1d0 commit a430d7a
Show file tree
Hide file tree
Showing 33 changed files with 411 additions and 614 deletions.
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:server'], {
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 Down Expand Up @@ -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

0 comments on commit a430d7a

Please sign in to comment.