cli/new: automatically inject package.json input

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-10 11:35:15 +01:00
parent 472c0d3d5c
commit c993e428f9
11 changed files with 49 additions and 78 deletions
@@ -92,7 +92,7 @@ describe('writeTemplateContents', () => {
out: {
'test.txt': 'test',
'plugin.txt': 'id=test',
'test.json': '{\n "x": 1\n}',
'test.json': '{"x":1}',
},
});
});
@@ -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);
}
@@ -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"
@@ -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"
@@ -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",
@@ -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"
@@ -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"
@@ -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"
@@ -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"
@@ -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"
@@ -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"