Skip to content

Commit

Permalink
fix: preserve existing package.json scripts (#1252)
Browse files Browse the repository at this point in the history
## 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`.
  • Loading branch information
JoshuaKGoldberg committed Jan 16, 2024
1 parent 7793c80 commit fc4e7e5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
23 changes: 0 additions & 23 deletions script/__snapshots__/migrate-test-e2e.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {"
`;
1 change: 0 additions & 1 deletion script/migrate-test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/steps/writing/creation/writePackageJson.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fc4e7e5

Please sign in to comment.