diff --git a/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts b/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts index c71ff17d4e..86d5e5f4f7 100644 --- a/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts +++ b/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts @@ -92,7 +92,7 @@ describe('writeTemplateContents', () => { out: { 'test.txt': 'test', 'plugin.txt': 'id=test', - 'test.json': '{\n "x": 1\n}', + 'test.json': '{"x":1}', }, }); }); diff --git a/packages/cli/src/lib/new/execution/writeTemplateContents.ts b/packages/cli/src/lib/new/execution/writeTemplateContents.ts index e99507fd6b..ddd94d40ae 100644 --- a/packages/cli/src/lib/new/execution/writeTemplateContents.ts +++ b/packages/cli/src/lib/new/execution/writeTemplateContents.ts @@ -62,12 +62,15 @@ export async function writeTemplateContents( ? templater.template(file.content) : file.content; - // Try to format JSON files - if (file.path.endsWith('.json')) { + // Automatically inject input values into package.json + if (file.path === 'package.json') { try { - content = JSON.stringify(JSON.parse(content), null, 2); - } catch { - /* ignore */ + content = injectPackageJsonInput(input, content); + } catch (error) { + throw new ForwardedError( + 'Failed to transform templated package.json', + error, + ); } } @@ -82,3 +85,43 @@ export async function writeTemplateContents( throw error; } } + +export function injectPackageJsonInput( + input: PortableTemplateInput, + content: string, +) { + const pkgJson = JSON.parse(content); + + const toAdd = new Array<[name: string, value: unknown]>(); + + if (pkgJson.version) { + pkgJson.version = input.version; + } else { + toAdd.push(['version', input.version]); + } + if (pkgJson.license) { + pkgJson.license = input.license; + } else { + toAdd.push(['license', input.license]); + } + if (input.private) { + if (pkgJson.private === false) { + pkgJson.private = true; + } else if (!pkgJson.private) { + toAdd.push(['private', true]); + } + } else { + delete pkgJson.private; + } + + const entries = Object.entries(pkgJson); + + const nameIndex = entries.findIndex(([name]) => name === 'name'); + if (nameIndex === -1) { + throw new Error('templated package.json does not contain a name field'); + } + + entries.splice(nameIndex + 1, 0, ...toAdd); + + return JSON.stringify(Object.fromEntries(entries), null, 2); +} diff --git a/packages/cli/templates/default-backend-module/package.json.hbs b/packages/cli/templates/default-backend-module/package.json.hbs index 2aa648fbc0..20d78380ca 100644 --- a/packages/cli/templates/default-backend-module/package.json.hbs +++ b/packages/cli/templates/default-backend-module/package.json.hbs @@ -1,17 +1,9 @@ { "name": "{{packageName}}", "description": "The {{moduleId}} backend module for the {{pluginId}} plugin.", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/default-backend-plugin/package.json.hbs b/packages/cli/templates/default-backend-plugin/package.json.hbs index 3acb1b2f44..ad3810dff3 100644 --- a/packages/cli/templates/default-backend-plugin/package.json.hbs +++ b/packages/cli/templates/default-backend-plugin/package.json.hbs @@ -1,16 +1,8 @@ { "name": "{{packageName}}", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/default-common-plugin-package/package.json.hbs b/packages/cli/templates/default-common-plugin-package/package.json.hbs index 93c51a9f97..085ccb40dd 100644 --- a/packages/cli/templates/default-common-plugin-package/package.json.hbs +++ b/packages/cli/templates/default-common-plugin-package/package.json.hbs @@ -1,17 +1,9 @@ { "name": "{{packageName}}", "description": "Common functionalities for the {{pluginId}} plugin", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/packages/cli/templates/default-node-plugin-package/package.json.hbs b/packages/cli/templates/default-node-plugin-package/package.json.hbs index 82dc0aef12..182ef1c93f 100644 --- a/packages/cli/templates/default-node-plugin-package/package.json.hbs +++ b/packages/cli/templates/default-node-plugin-package/package.json.hbs @@ -1,17 +1,9 @@ { "name": "{{packageName}}", "description": "Node.js library for the {{pluginId}} plugin", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/default-plugin/package.json.hbs b/packages/cli/templates/default-plugin/package.json.hbs index c19763afad..64888c2d37 100644 --- a/packages/cli/templates/default-plugin/package.json.hbs +++ b/packages/cli/templates/default-plugin/package.json.hbs @@ -1,16 +1,8 @@ { "name": "{{packageName}}", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.esm.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/default-react-plugin-package/package.json.hbs b/packages/cli/templates/default-react-plugin-package/package.json.hbs index f133fe289e..d104a29305 100644 --- a/packages/cli/templates/default-react-plugin-package/package.json.hbs +++ b/packages/cli/templates/default-react-plugin-package/package.json.hbs @@ -1,17 +1,9 @@ { "name": "{{packageName}}", "description": "Web library for the {{pluginId}} plugin", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.esm.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/node-library-package/package.json.hbs b/packages/cli/templates/node-library-package/package.json.hbs index 15e88daeb5..941bf309fd 100644 --- a/packages/cli/templates/node-library-package/package.json.hbs +++ b/packages/cli/templates/node-library-package/package.json.hbs @@ -1,16 +1,8 @@ { "name": "{{packageName}}", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/scaffolder-module/package.json.hbs b/packages/cli/templates/scaffolder-module/package.json.hbs index fd6b1a4c7f..3866a83672 100644 --- a/packages/cli/templates/scaffolder-module/package.json.hbs +++ b/packages/cli/templates/scaffolder-module/package.json.hbs @@ -1,17 +1,9 @@ { "name": "{{packageName}}", "description": "The {{moduleId}} module for @backstage/plugin-scaffolder-backend", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.cjs.js", "types": "dist/index.d.ts" diff --git a/packages/cli/templates/web-library-package/package.json.hbs b/packages/cli/templates/web-library-package/package.json.hbs index e63fe5f218..52e54b1783 100644 --- a/packages/cli/templates/web-library-package/package.json.hbs +++ b/packages/cli/templates/web-library-package/package.json.hbs @@ -1,16 +1,8 @@ { "name": "{{packageName}}", - "version": "{{packageVersion}}", "main": "src/index.ts", "types": "src/index.ts", - "license": "{{license}}", -{{#if privatePackage}} - "private": {{privatePackage}}, -{{/if}} "publishConfig": { -{{#if npmRegistry}} - "registry": "{{npmRegistry}}", -{{/if}} "access": "public", "main": "dist/index.esm.js", "types": "dist/index.d.ts"