Skip to content
Closed
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
14,976 changes: 13,765 additions & 1,211 deletions packages/kbn-pm/dist/index.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/kbn-pm/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ import { BootstrapCommand } from './bootstrap';
import { CleanCommand } from './clean';
import { RunCommand } from './run';
import { WatchCommand } from './watch';
import { PluginCommand } from './plugin';

export const commands: { [key: string]: ICommand } = {
bootstrap: BootstrapCommand,
clean: CleanCommand,
run: RunCommand,
watch: WatchCommand,
plugin: PluginCommand,
};
145 changes: 145 additions & 0 deletions packages/kbn-pm/src/commands/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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.
*/

import chalk from 'chalk';
import { join } from 'path';
import { capitalize, camelCase } from 'lodash';
import { ICommand } from './';
import {
mkdirp,
readFile,
writeFile,
isDirectory,
unlink,
deleteFolderRecursive,
} from '../utils/fs';
import { log } from '../utils/log';

const PLUGINS_BASE_PATH = 'src/plugins';

function replacePluginName(template: string, pluginName: string) {
return template.replace(/\[PLUGIN_CLASS_NAME\]/g, pluginName);
}

async function addFotmattedTemplate(
rootPath: string,
templateType: 'public' | 'server' | 'common',
fileName: string,
pluginName: string
) {
const templateContent = await readFile(
join(__dirname, '..', `templates/${templateType}/${fileName}.tmpl`)
);
const pluginClassName = `${capitalize(pluginName)}`;
const formattedTemplate = replacePluginName(templateContent.toString(), pluginClassName);
return await writeFile(
join(rootPath, PLUGINS_BASE_PATH, pluginName, templateType, fileName),
formattedTemplate
);
}

async function pluginExists(pluginPath: string) {
return isDirectory(pluginPath);
}

export const PluginCommand: ICommand = {
description: 'Initialize a new plugin.',
name: 'plugin',

async run(projects, projectGraph, { options, rootPath }) {
const pluginName = options._[1];
const pluginPath = join(rootPath, PLUGINS_BASE_PATH, pluginName);
if (await pluginExists(pluginPath)) {
log.write(chalk.red(`\nPlugin ${pluginName} already exists in ${rootPath}`));
return;
}

const hasUi = options.ui === true;
const hasServer = options.server === true;

if (!hasUi && !hasServer) {
log.write(
chalk.red(`\nPlugins should have either a server or a ui component. Run yarn kbn for help.`)
);
return;
}

log.write(
chalk.bold(
`\nInitializing plugin ${pluginName} (server ${hasServer ? 'enabled' : 'disabled'}, ui ${
hasUi ? 'enabled' : 'disabled'
})`
)
);

try {
await mkdirp(pluginPath);

const pluginKibanaJson = {
id: pluginName,
version: 'kibana',
server: hasServer,
ui: hasUi,
requiredPlugins: [],
};

await writeFile(join(pluginPath, 'kibana.json'), JSON.stringify(pluginKibanaJson, null, 2));

await writeFile(join(pluginPath, 'README.md'), `# ${pluginName}\n`);

if (hasUi || hasServer) {
const commonPath = join(pluginPath, 'common');
await mkdirp(commonPath);

await addFotmattedTemplate(rootPath, 'common', 'index.ts', pluginName);
}

if (hasUi) {
await mkdirp(join(pluginPath, 'public'));

await addFotmattedTemplate(rootPath, 'public', 'index.ts', pluginName);
await addFotmattedTemplate(rootPath, 'public', 'plugin.ts', pluginName);
await addFotmattedTemplate(rootPath, 'public', 'types.ts', pluginName);
}

if (hasServer) {
await mkdirp(join(pluginPath, 'server'));
await addFotmattedTemplate(rootPath, 'server', 'index.ts', pluginName);
await addFotmattedTemplate(rootPath, 'server', 'plugin.ts', pluginName);
}

// Update .i18nrc.json
const i18nPath = join(rootPath, '.i18nrc.json');
const i18nrcContent = await readFile(i18nPath);
const i18nrcJson = JSON.parse(i18nrcContent.toString());
i18nrcJson.paths[camelCase(pluginName)] = join(PLUGINS_BASE_PATH, pluginName);
await writeFile(i18nPath, JSON.stringify(i18nrcJson, null, 2));

log.write(chalk.green(`\nInitialized plugin ${pluginName} successfully`));
} catch (e) {
log.write(chalk.red(`\Failed to initialize plugin ${pluginName}. Cleaning up.`));
try {
deleteFolderRecursive(pluginPath);
log.write(chalk.white(`\Cleanup ${pluginName} complete.`));
} catch (cleanUpErr) {
log.write(chalk.red(`\Failed to cleanup plugin ${pluginName}.`));
}
}
},
};
16 changes: 16 additions & 0 deletions packages/kbn-pm/src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import fs from 'fs';
import { ncp } from 'ncp';
import { dirname, relative } from 'path';
import { promisify } from 'util';
import { join } from 'path';

const lstat = promisify(fs.lstat);
export const readFile = promisify(fs.readFile);
Expand All @@ -32,6 +33,7 @@ const mkdir = promisify(fs.mkdir);
export const mkdirp = async (path: string) => await mkdir(path, { recursive: true });
export const unlink = promisify(fs.unlink);
export const copyDirectory = promisify(ncp);
export const writeFile = promisify(fs.writeFile);

async function statTest(path: string, block: (stats: fs.Stats) => boolean) {
try {
Expand Down Expand Up @@ -104,3 +106,17 @@ async function forceCreate(src: string, dest: string, type: string) {

await symlink(src, dest, type);
}

export function deleteFolderRecursive(path: string) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(file => {
const curPath = join(path, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
18 changes: 18 additions & 0 deletions packages/kbn-pm/templates/common/index.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.
*/
30 changes: 30 additions & 0 deletions packages/kbn-pm/templates/public/index.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { PluginInitializerContext } from '../../../core/public';
export function plugin(initializerContext: PluginInitializerContext) {
return new [PLUGIN_CLASS_NAME]PublicPlugin(initializerContext);
}

export * from '../common';
export * from './types';

// Export plugin after all other imports
import { [PLUGIN_CLASS_NAME]PublicPlugin } from './plugin';
export { [PLUGIN_CLASS_NAME]PublicPlugin as Plugin };
39 changes: 39 additions & 0 deletions packages/kbn-pm/templates/public/plugin.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public';
import {
[PLUGIN_CLASS_NAME]PublicPluginSetup,
[PLUGIN_CLASS_NAME]PublicPluginStart,
} from './types';

export class [PLUGIN_CLASS_NAME]PublicPlugin implements Plugin<[PLUGIN_CLASS_NAME]PublicPluginSetup, [PLUGIN_CLASS_NAME]PublicPluginStart> {
constructor(initializerContext: PluginInitializerContext) {
}

public setup(core: CoreSetup): [PLUGIN_CLASS_NAME]PublicPluginSetup {
return {};
}

public start(core: CoreStart): [PLUGIN_CLASS_NAME]PublicPluginStart {
return {}
}

public stop() {}
}
22 changes: 22 additions & 0 deletions packages/kbn-pm/templates/public/types.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

export interface [PLUGIN_CLASS_NAME]PublicPluginSetup {}

export interface [PLUGIN_CLASS_NAME]PublicPluginStart {}
28 changes: 28 additions & 0 deletions packages/kbn-pm/templates/server/index.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

import { PluginInitializerContext } from '../../../core/server';
import { [PLUGIN_CLASS_NAME]ServerPlugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new [PLUGIN_CLASS_NAME]ServerPlugin(initializerContext);
}

export { [PLUGIN_CLASS_NAME]ServerPlugin as Plugin };
export * from '../common';
38 changes: 38 additions & 0 deletions packages/kbn-pm/templates/server/plugin.ts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/server';

export interface [PLUGIN_CLASS_NAME]PluginSetup {};
export interface [PLUGIN_CLASS_NAME]PluginStart {};

export class [PLUGIN_CLASS_NAME]ServerPlugin implements Plugin<[PLUGIN_CLASS_NAME]PluginSetup, [PLUGIN_CLASS_NAME]PluginStart> {
constructor(initializerContext: PluginInitializerContext) {}

public setup(core: CoreSetup) {
return {};
}
public start(core: CoreStart) {
return {};
}

public stop() {}
}

export { [PLUGIN_CLASS_NAME]ServerPlugin as Plugin };