From fc4e7e518ffd39706fde670e390711338527ed18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 16 Jan 2024 16:32:23 -0500 Subject: [PATCH] fix: preserve existing package.json scripts (#1252) ## PR Checklist - [x] Addresses an existing open issue: fixes #1226 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Changes `package.json` creation to preserve any existing `scripts`. --- script/__snapshots__/migrate-test-e2e.js.snap | 23 ------------------- script/migrate-test-e2e.js | 1 - .../writing/creation/writePackageJson.ts | 7 ++++-- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/script/__snapshots__/migrate-test-e2e.js.snap b/script/__snapshots__/migrate-test-e2e.js.snap index 43b5b7c6..8a090261 100644 --- a/script/__snapshots__/migrate-test-e2e.js.snap +++ b/script/__snapshots__/migrate-test-e2e.js.snap @@ -160,26 +160,3 @@ exports[`expected file changes > knip.json 1`] = ` + "project": ["src/**/*.ts!"] }" `; - -exports[`expected file changes > package.json 1`] = ` -"--- a/package.json -+++ b/package.json -@@ ... @@ - "scripts": { - "build": "tsup", - "format": "prettier .", -- "initialize": "tsx ./bin/index.js --mode initialize", - "lint": "eslint . .*js --max-warnings 0", - "lint:knip": "knip", - "lint:md": "markdownlint \\"**/*.md\\" \\".github/**/*.md\\" --rules sentences-per-line", -@@ ... @@ - "prepare": "husky install", - "should-semantic-release": "should-semantic-release --verbose", - "test": "vitest", -- "test:create": "node script/create-test-e2e.js", -- "test:initialize": "node script/initialize-test-e2e.js", -- "test:migrate": "vitest run -r script/", - "tsc": "tsc" - }, - "lint-staged": {" -`; diff --git a/script/migrate-test-e2e.js b/script/migrate-test-e2e.js index f55049a0..97c646a3 100644 --- a/script/migrate-test-e2e.js +++ b/script/migrate-test-e2e.js @@ -9,7 +9,6 @@ import packageData from "../package.json" assert { type: "json" }; const filesExpectedToBeChanged = [ "README.md", "knip.json", - "package.json", ".eslintignore", ".eslintrc.cjs", ".github/workflows/test.yml", diff --git a/src/steps/writing/creation/writePackageJson.ts b/src/steps/writing/creation/writePackageJson.ts index c1ce7714..01cd9971 100644 --- a/src/steps/writing/creation/writePackageJson.ts +++ b/src/steps/writing/creation/writePackageJson.ts @@ -1,5 +1,5 @@ import { readFileSafeAsJson } from "../../../shared/readFileSafeAsJson.js"; -import { Options } from "../../../shared/types.js"; +import { Options, PartialPackageData } from "../../../shared/types.js"; import { formatJson } from "./formatters/formatJson.js"; const devDependenciesToRemove = [ @@ -31,7 +31,9 @@ const devDependenciesToRemove = [ export async function writePackageJson(options: Options) { const existingPackageJson = - ((await readFileSafeAsJson("./package.json")) as null | object) ?? {}; + ((await readFileSafeAsJson( + "./package.json", + )) as PartialPackageData | null) ?? {}; return await formatJson({ // If we didn't already have a version, set it to 0.0.0 @@ -82,6 +84,7 @@ export async function writePackageJson(options: Options) { url: `https://github.com/${options.owner}/${options.repository}`, }, scripts: { + ...existingPackageJson.scripts, build: "tsup", format: "prettier .", lint: "eslint . .*js --max-warnings 0",