create plugin adds newly created plugin as a dependency in app package. adds to the plugins list in the app
This commit is contained in:
@@ -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<any> => {
|
||||
|
||||
const questions: Question[] = [
|
||||
{
|
||||
type: 'input',
|
||||
@@ -210,6 +263,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