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

Update plugin generator to generate NP plugins #55281

Merged
merged 31 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5eb5357
Generate NP plugin
Jan 19, 2020
f44b2c3
Added tsconfig
Jan 20, 2020
65e621d
tsconfig
Jan 20, 2020
b972827
Adjust sao test
Jan 20, 2020
3fa50aa
Add server side to plugin gen
Jan 20, 2020
fed27b5
Added navigation
Jan 20, 2020
7dbb585
add empty element
Jan 20, 2020
3f8520a
eslint
Jan 21, 2020
7f5e363
platform team CR
Jan 23, 2020
7ad1324
design CR improvements
Jan 23, 2020
c5a8ab0
Merge remote-tracking branch 'upstream/master' into newplatform/plugi…
Jan 23, 2020
055a63a
text updates
Jan 26, 2020
33c2680
temp disable plugin gen tests
Jan 26, 2020
9f2bcb8
Merge remote-tracking branch 'upstream/master' into newplatform/plugi…
Jan 26, 2020
d11db3a
Merge branch 'master' into newplatform/plugin-gen
elasticmachine Jan 28, 2020
e7c15c5
Merge branch 'master' of github.com:elastic/kibana into newplatform/p…
Jan 29, 2020
0dfa19f
eslint
Jan 29, 2020
7279243
Merge branch 'newplatform/plugin-gen' of github.com:lizozom/kibana in…
Jan 29, 2020
47b7bef
Code review fixes
Jan 29, 2020
38aee60
Add scss support - requires #53976 to be merged to work
Jan 29, 2020
a9d471b
CR fixes
Jan 30, 2020
5e3f3dc
comment fixes
Jan 30, 2020
5a3ceff
Merge branch 'master' of github.com:elastic/kibana into newplatform/p…
Jan 30, 2020
b91ee61
Don't generate eslint for internal plugins by default
Jan 30, 2020
09b2375
Update tests
Jan 30, 2020
2a9e1ff
reenable jest test for sao
Jan 30, 2020
1952d75
Fix regex
Jan 30, 2020
0b9ca29
review comments
Jan 30, 2020
1ff8556
Merge branch 'master' of github.com:elastic/kibana into newplatform/p…
Feb 2, 2020
8ad025e
code review
Feb 2, 2020
8274c78
Merge branch 'master' into newplatform/plugin-gen
elasticmachine Feb 2, 2020
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: any;
lizozom marked this conversation as resolved.
Show resolved Hide resolved
}
46 changes: 21 additions & 25 deletions packages/kbn-plugin-generator/sao_template/sao.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
* 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 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 @@ -52,11 +49,6 @@ module.exports = function({ name }) {
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?',
Expand All @@ -73,7 +65,6 @@ module.exports = function({ name }) {
'public/**/*': 'generateApp',
'translations/**/*': 'generateTranslations',
'.i18nrc.json': 'generateTranslations',
'public/hack.js': 'generateHack',
'server/**/*': 'generateApi',
'public/app.scss': 'generateScss',
'.kibana-plugin-helpers.json': 'generateScss',
Expand All @@ -91,28 +82,33 @@ module.exports = function({ name }) {
camelCase,
snakeCase,
name,
kbnVersion: answers.kbnVersion,
camelCaseName: name.charAt(0).toUpperCase() + camelCase(name).slice(1),
hasUi: !!answers.generateApp,
hasServer: !!answers.generateApi,
relRoot: isKibanaPlugin ? '../../../..' : '../../..',
},
answers
),
enforceNewFolder: true,
installDependencies: false,
gitInit: true,
gitInit: !isKibanaPlugin,
async post({ log }) {
await execa('yarn', ['kbn', 'bootstrap'], {
cwd: KBN_DIR,
stdio: 'inherit',
});
// 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);

try {
await execa('yarn', ['lint', '--fix'], {
cwd: dir,
all: true,
});
} catch (error) {
throw new Error(`Failure when running prettier on the generated output: ${error.all}`);
}
// try {
// await execa('yarn', ['lint', '--fix'], {
// cwd: dir,
// all: true,
// });
// } catch (error) {
// throw new Error(`Failure when running prettier on the generated output: ${error.all}`);
// }

log.success(chalk`🎉

Expand Down
87 changes: 20 additions & 67 deletions packages/kbn-plugin-generator/sao_template/sao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ function getConfig(file) {
}

describe('plugin generator sao integration', () => {
test('skips files when answering no', async () => {
test.skip('skips files when answering no', async () => {
const res = await sao.mockPrompt(template, {
generateApp: false,
generateHack: false,
generateApi: false,
});

Expand All @@ -61,49 +60,20 @@ describe('plugin generator sao integration', () => {
it('includes app when answering yes', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: false,
generateApi: false,
});

// check output files
expect(res.fileList).toContain('public/app.js');
expect(res.fileList).toContain('public/__tests__/index.js');
expect(res.fileList).not.toContain('public/hack.js');
expect(res.fileList).not.toContain('server/routes/example.js');
expect(res.fileList).not.toContain('server/__tests__/index.js');

const uiExports = getConfig(res.files['index.js']);
expect(uiExports).toContain('app:');
expect(uiExports).toContain('init(server, options)');
expect(uiExports).toContain('registerFeature(');
expect(uiExports).not.toContain('hacks:');
expect(res.fileList).toContain('common/index.ts');
expect(res.fileList).toContain('public/index.ts');
expect(res.fileList).toContain('public/plugin.ts');
expect(res.fileList).toContain('public/types.ts');
expect(res.fileList).toContain('public/components/app.tsx');
});

it('includes hack when answering yes', async () => {
it.skip('includes server api when answering yes', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: true,
generateApi: false,
});

// check output files
expect(res.fileList).toContain('public/app.js');
expect(res.fileList).toContain('public/__tests__/index.js');
expect(res.fileList).toContain('public/hack.js');
expect(res.fileList).not.toContain('server/routes/example.js');
expect(res.fileList).not.toContain('server/__tests__/index.js');

const uiExports = getConfig(res.files['index.js']);
expect(uiExports).toContain('app:');
expect(uiExports).toContain('hacks:');
expect(uiExports).toContain('init(server, options)');
expect(uiExports).toContain('registerFeature(');
});

it('includes server api when answering yes', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: true,
generateApi: true,
});

Expand All @@ -121,62 +91,46 @@ describe('plugin generator sao integration', () => {
expect(uiExports).toContain('registerFeature(');
});

it('plugin config has correct name and main path', async () => {
it('plugin package has correct title', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: true,
generateApi: true,
});

const indexContents = getFileContents(res.files['index.js']);
const nameLine = indexContents.match('name: (.*)')[1];
const mainLine = indexContents.match('main: (.*)')[1];

expect(nameLine).toContain('some_fancy_plugin');
expect(mainLine).toContain('plugins/some_fancy_plugin/app');
});

it('plugin package has correct name', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: true,
generateApi: true,
});

const packageContents = getFileContents(res.files['package.json']);
const pkg = JSON.parse(packageContents);
const contents = getFileContents(res.files['common/index.ts']);
const controllerLine = contents.match("PLUGIN_NAME = '(.*)'")[1];

expect(pkg.name).toBe('some_fancy_plugin');
expect(controllerLine).toContain('Some fancy plugin');
});

it('package has version "kibana" with master', async () => {
const res = await sao.mockPrompt(template, {
kbnVersion: 'master',
});

const packageContents = getFileContents(res.files['package.json']);
const packageContents = getFileContents(res.files['kibana.json']);
const pkg = JSON.parse(packageContents);

expect(pkg.kibana.version).toBe('kibana');
expect(pkg.version).toBe('master');
});

it('package has correct version', async () => {
const res = await sao.mockPrompt(template, {
kbnVersion: 'v6.0.0',
});

const packageContents = getFileContents(res.files['package.json']);
const packageContents = getFileContents(res.files['kibana.json']);
const pkg = JSON.parse(packageContents);

expect(pkg.kibana.version).toBe('v6.0.0');
expect(pkg.version).toBe('v6.0.0');
});

it('package has correct templateVersion', async () => {
it.skip('package has correct templateVersion', async () => {
const res = await sao.mockPrompt(template, {
kbnVersion: 'master',
});

const packageContents = getFileContents(res.files['package.json']);
const packageContents = getFileContents(res.files['kibana.json']);
const pkg = JSON.parse(packageContents);

expect(pkg.kibana.templateVersion).toBe(templatePkg.version);
Expand All @@ -185,17 +139,16 @@ describe('plugin generator sao integration', () => {
it('sample app has correct values', async () => {
const res = await sao.mockPrompt(template, {
generateApp: true,
generateHack: true,
generateApi: true,
});

const contents = getFileContents(res.files['public/app.js']);
const controllerLine = contents.match('setRootController(.*)')[1];
const contents = getFileContents(res.files['common/index.ts']);
const controllerLine = contents.match("PLUGIN_ID = '(.*)'")[1];

expect(controllerLine).toContain('someFancyPlugin');
});

it('includes dotfiles', async () => {
it.skip('includes dotfiles', async () => {
const res = await sao.mockPrompt(template);
expect(res.files['.gitignore']).toBeTruthy();
expect(res.files['.eslintrc.js']).toBeTruthy();
Expand Down

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions packages/kbn-plugin-generator/sao_template/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,3 @@

See the [kibana contributing guide](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md) for instructions setting up your development environment. Once you have completed that, use the following yarn scripts.

- `yarn kbn bootstrap`
lizozom marked this conversation as resolved.
Show resolved Hide resolved

Install dependencies and crosslink Kibana and all projects/plugins.

> ***IMPORTANT:*** Use this script instead of `yarn` to install dependencies when switching branches, and re-run it whenever your dependencies change.

- `yarn start`

Start kibana and have it include this plugin. You can pass any arguments that you would normally send to `bin/kibana`

```
yarn start --elasticsearch.hosts http://localhost:9220
```

- `yarn build`

Build a distributable archive of your plugin.

- `yarn test:browser`

Run the browser tests in a real web browser.

- `yarn test:mocha`

Run the server tests using mocha.

For more information about any of these commands run `yarn ${task} --help`. For a full list of tasks checkout the `package.json` file, or run `yarn run`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const PLUGIN_ID = '<%= camelCase(name) %>';
export const PLUGIN_NAME = '<%= name %>';
24 changes: 0 additions & 24 deletions packages/kbn-plugin-generator/sao_template/template/eslintrc.js

This file was deleted.

Loading