Merge pull request #151 from spotify/raghub/cli-imports-plugin
Automate manual steps to include a new plugin in the app
This commit is contained in:
@@ -61,6 +61,57 @@ export const createFileFromTemplate = (
|
||||
}
|
||||
};
|
||||
|
||||
const sortObjectByKeys = (obj: {[name in string]: string}) => {
|
||||
return Object.keys(obj)
|
||||
.sort()
|
||||
.reduce((result, key: string) => {
|
||||
result[key] = obj[key];
|
||||
return result;
|
||||
}, {} as { [name in string]: string });
|
||||
}
|
||||
|
||||
const capitalize = (str: string): string => str.charAt(0).toUpperCase() + str.slice(1);
|
||||
|
||||
const addExportStatement = (file: string, exportStatement: string) => {
|
||||
const newContents = fs
|
||||
.readFileSync(file, 'utf8')
|
||||
.split('\n')
|
||||
.filter(Boolean) // get rid of empty lines
|
||||
.concat([exportStatement])
|
||||
.sort()
|
||||
.concat(['']) // newline at end of file
|
||||
.join('\n');
|
||||
|
||||
fs.writeFileSync(file, newContents, 'utf8');
|
||||
};
|
||||
|
||||
export const addPluginDependencyToApp = (rootDir: string, pluginName: string): string => {
|
||||
const pluginPackage = `@spotify-backstage/plugin-${pluginName}`;
|
||||
const pluginPackageVersion = "0.0.0";
|
||||
const packageFile = path.join(rootDir, 'packages', 'app', 'package.json');
|
||||
const packageFileContent = fs.readFileSync(packageFile, 'utf-8').toString();
|
||||
const packageFileJson = JSON.parse(packageFileContent);
|
||||
const dependencies = packageFileJson.dependencies;
|
||||
|
||||
if (typeof dependencies[pluginPackage] !== 'undefined') {
|
||||
throw new Error(`Plugin ${pluginPackage} already exists in ${packageFile}`);
|
||||
}
|
||||
|
||||
dependencies[pluginPackage] = pluginPackageVersion;
|
||||
packageFileJson.dependencies = sortObjectByKeys(dependencies);
|
||||
fs.writeFileSync(packageFile, `${JSON.stringify(packageFileJson, null, 2)}\n`, 'utf-8');
|
||||
return pluginPackage;
|
||||
}
|
||||
|
||||
export const addPluginToApp = (rootDir: string, pluginName: string) => {
|
||||
const pluginPackage = `@spotify-backstage/plugin-${pluginName}`;
|
||||
const pluginNameCapitalized = pluginName.split('-').map(name => capitalize(name)).join('');
|
||||
const pluginExport = `export { default as ${pluginNameCapitalized} } from '${pluginPackage}';`;
|
||||
const pluginsFile = path.join(rootDir, 'packages', 'app', 'src', 'plugins.ts');
|
||||
|
||||
addExportStatement(pluginsFile, pluginExport);
|
||||
}
|
||||
|
||||
export const createFromTemplateDir = async (
|
||||
templateFolder: string,
|
||||
destinationFolder: string,
|
||||
@@ -179,6 +230,7 @@ const buildPlugin = async (pluginFolder: string) => {
|
||||
};
|
||||
|
||||
const createPlugin = async (): Promise<any> => {
|
||||
|
||||
const questions: Question[] = [
|
||||
{
|
||||
type: 'input',
|
||||
@@ -210,6 +262,9 @@ const createPlugin = async (): Promise<any> => {
|
||||
await createFromTemplateDir(templateFolder, destinationFolder, answers);
|
||||
await buildPlugin(destinationFolder);
|
||||
|
||||
addPluginDependencyToApp(rootDir, answers.id);
|
||||
addPluginToApp(rootDir, answers.id);
|
||||
|
||||
console.log();
|
||||
console.log(
|
||||
chalk.green(
|
||||
|
||||
Reference in New Issue
Block a user