From 111fbb9e0fcea84532e63838a9d09232478c357d Mon Sep 17 00:00:00 2001 From: Fabian Chong Date: Tue, 15 Sep 2020 13:30:18 +0800 Subject: [PATCH] fix plugin:diff --- .../commands/create-plugin/createPlugin.ts | 8 ++++---- packages/cli/src/commands/plugin/diff.ts | 20 +++++++++++++------ .../templates/default-plugin/package.json.hbs | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/commands/create-plugin/createPlugin.ts b/packages/cli/src/commands/create-plugin/createPlugin.ts index 8c2a9eeb98..8241f68b8c 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.ts @@ -228,10 +228,10 @@ export default async (cmd: Command) => { ? `@${cmd.scope.replace(/^@/, '')}/plugin-${answers.id}` : `plugin-${answers.id}`; const scopeName = cmd.scope ? `@${cmd.scope.replace(/^@/, '')}` : ''; - const registryURL = + const npmRegistry = cmd.npmRegistry && cmd.scope - ? `"${scopeName}:registry": "${cmd.npmRegistry}",` - : null; + ? `"${scopeName}:registry": "${cmd.npmRegistry}"` + : ''; const privatePackage = cmd.private === false ? false : true; const appPackage = paths.resolveTargetRoot('packages/app'); @@ -261,7 +261,7 @@ export default async (cmd: Command) => { scopeName, name, privatePackage, - registryURL, + npmRegistry, }); Task.section('Moving to final location'); diff --git a/packages/cli/src/commands/plugin/diff.ts b/packages/cli/src/commands/plugin/diff.ts index 064c72c448..f5dfa670b7 100644 --- a/packages/cli/src/commands/plugin/diff.ts +++ b/packages/cli/src/commands/plugin/diff.ts @@ -30,6 +30,9 @@ import { version as backstageVersion } from '../../lib/version'; export type PluginData = { id: string; name: string; + privatePackage: string; + version: string; + npmRegistry: string; }; const fileHandlers = [ @@ -62,14 +65,9 @@ export default async (cmd: Command) => { promptFunc = yesPromptFunc; } - const { version } = await fs.readJson(paths.resolveTargetRoot('lerna.json')); - const data = await readPluginData(); - const privatePackage = false; const templateFiles = await diffTemplateFiles('default-plugin', { - version, backstageVersion, - privatePackage, ...data, }); await handleAllFiles(fileHandlers, templateFiles, promptFunc); @@ -79,9 +77,19 @@ export default async (cmd: Command) => { // Reads templating data from the existing plugin async function readPluginData(): Promise { let name: string; + let privatePackage: string; + let version: string; + let npmRegistry: string; try { const pkg = require(paths.resolveTarget('package.json')); name = pkg.name; + privatePackage = pkg.private; + version = pkg.version; + const scope = name.split('/')[0]; + if (`${scope}:registry` in pkg.publishConfig) { + const registryURL = pkg.publishConfig[`${scope}:registry`]; + npmRegistry = `"${scope}:registry" : "${registryURL}"`; + } else npmRegistry = ''; } catch (error) { throw new Error(`Failed to read target package, ${error}`); } @@ -98,5 +106,5 @@ async function readPluginData(): Promise { const id = pluginIdMatch[1]; - return { id, name }; + return { id, name, privatePackage, version, npmRegistry }; } diff --git a/packages/cli/templates/default-plugin/package.json.hbs b/packages/cli/templates/default-plugin/package.json.hbs index ed1a56d605..1a969f9712 100644 --- a/packages/cli/templates/default-plugin/package.json.hbs +++ b/packages/cli/templates/default-plugin/package.json.hbs @@ -6,7 +6,8 @@ "license": "Apache-2.0", "private": {{privatePackage}}, "publishConfig": { - {{{registryURL}}} +{{#if npmRegistry}} {{{npmRegistry}}}, +{{/if}} "access": "public", "main": "dist/index.esm.js", "types": "dist/index.d.ts"