From 56ddf44d404176e0e46c33cb95ab5e290814d1ad Mon Sep 17 00:00:00 2001 From: Raghunandan Date: Tue, 3 Mar 2020 15:16:29 +0100 Subject: [PATCH 1/2] create plugin adds newly created plugin as a dependency in app package. adds to the plugins list in the app --- .../packages/cli/src/commands/createPlugin.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/frontend/packages/cli/src/commands/createPlugin.ts b/frontend/packages/cli/src/commands/createPlugin.ts index e5808ec6ea..18a3f9566c 100644 --- a/frontend/packages/cli/src/commands/createPlugin.ts +++ b/frontend/packages/cli/src/commands/createPlugin.ts @@ -61,6 +61,58 @@ export const createFileFromTemplate = ( } }; +const sortObjectByKeys = (obj: {[name in string]: string}) => { + return Object.keys(obj) + .sort((a: string, b: string): number => a > b ? 1 : 0) + .reduce((result:{[name in string]: string}, key: string) => { + result[key] = obj[key]; + return result; + }, {}); +} + +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), 'utf-8'); + fs.appendFileSync(packageFile, '\n', 'utf-8'); // new line at end of file + 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 = `\nexport { 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 +231,7 @@ const buildPlugin = async (pluginFolder: string) => { }; const createPlugin = async (): Promise => { + const questions: Question[] = [ { type: 'input', @@ -210,6 +263,9 @@ const createPlugin = async (): Promise => { await createFromTemplateDir(templateFolder, destinationFolder, answers); await buildPlugin(destinationFolder); + addPluginDependencyToApp(rootDir, answers.id); + addPluginToApp(rootDir, answers.id); + console.log(); console.log( chalk.green( From b544b11a689aab4b577ada1583f27c605dedd318 Mon Sep 17 00:00:00 2001 From: Raghunandan Date: Wed, 4 Mar 2020 11:37:39 +0100 Subject: [PATCH 2/2] Fix PR review --- frontend/packages/cli/src/commands/createPlugin.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/frontend/packages/cli/src/commands/createPlugin.ts b/frontend/packages/cli/src/commands/createPlugin.ts index 18a3f9566c..9eb46eebff 100644 --- a/frontend/packages/cli/src/commands/createPlugin.ts +++ b/frontend/packages/cli/src/commands/createPlugin.ts @@ -63,11 +63,11 @@ export const createFileFromTemplate = ( const sortObjectByKeys = (obj: {[name in string]: string}) => { return Object.keys(obj) - .sort((a: string, b: string): number => a > b ? 1 : 0) - .reduce((result:{[name in string]: string}, key: string) => { + .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); @@ -99,15 +99,14 @@ export const addPluginDependencyToApp = (rootDir: string, pluginName: string): s dependencies[pluginPackage] = pluginPackageVersion; packageFileJson.dependencies = sortObjectByKeys(dependencies); - fs.writeFileSync(packageFile, JSON.stringify(packageFileJson, null, 2), 'utf-8'); - fs.appendFileSync(packageFile, '\n', 'utf-8'); // new line at end of file + 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 = `\nexport { default as ${pluginNameCapitalized} } from '${pluginPackage}';`; + const pluginExport = `export { default as ${pluginNameCapitalized} } from '${pluginPackage}';`; const pluginsFile = path.join(rootDir, 'packages', 'app', 'src', 'plugins.ts'); addExportStatement(pluginsFile, pluginExport); @@ -265,7 +264,7 @@ const createPlugin = async (): Promise => { addPluginDependencyToApp(rootDir, answers.id); addPluginToApp(rootDir, answers.id); - + console.log(); console.log( chalk.green(