Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip replacing existing tooling in --mode migrate #1223

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions script/__snapshots__/migrate-test-e2e.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,43 +101,9 @@ exports[`expected file changes > README.md 1`] = `
-\`create-typescript-app\` is a one-stop-shop solution to set up a new or existing repository with the latest and greatest TypeScript tooling.
-It includes options not just for building and testing but also GitHub repository templates, contributor recognition, automated release management, and more.
-
-## Getting Started
-
-First make sure you have the following installed:
-
-- [GitHub CLI](https://cli.github.com) _(you'll need to be logged in)_
-- [Node.js](https://nodejs.org)
-- [pnpm](https://pnpm.io)
-
-Then in an existing repository or in your directory where you'd like to make a new repository:
-
-\`\`\`shell
-npx create-typescript-app
-\`\`\`
-
-That setup script will walk you through using the template.
-You can read more about the supported setup modes in their docs pages:
-
-- [**Creating from the terminal**](./docs/Creation.md): creating a new repository locally on the command-line _(recommended)_
-- [**Initializing from the template**](./docs/Initialization.md): creating a new repository with the [_Use this template_](https://github.com/JoshuaKGoldberg/create-typescript-app/generate) button on GitHub
-- [**Migrating an existing repository**](./docs/Migration.md): adding this template's tooling on top of an existing repository
-
-## Documentation
-
-You can read more about \`create-typescript-app\` and the tooling it supports:
-
-1. [**Tooling**](./docs/Tooling.md): a breakdown of all the pieces this template can set up.
-2. [**Options**](./docs/Options.md): granular options to customize how the template is run.
-3. [**FAQs**](./docs/FAQs.md): frequently asked questions
-
-> [!NOTE]
-> This template is early stage, opinionated, and not endorsed by the TypeScript team.
-> It can be configured to set up a _lot_ of tooling out of the box.
-> If you don't want to use any particular tool, you can always remove it manually.
-
## Development
## Getting Started

See [\`.github/CONTRIBUTING.md\`](./.github/CONTRIBUTING.md), then [\`.github/DEVELOPMENT.md\`](./.github/DEVELOPMENT.md).
First make sure you have the following installed:
@@ ... @@ Thanks! 💖

<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down
12 changes: 0 additions & 12 deletions script/migrate-test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const logoAlt = `Project logo: the TypeScript blue square with rounded corners,
const owner = "JoshuaKGoldberg";
const title = "Create TypeScript App";

const originalDevelopment = (
await fs.readFile(".github/DEVELOPMENT.md")
).toString();

const originalReadme = (await fs.readFile("README.md")).toString();

const originalSnapshots = (
Expand All @@ -61,14 +57,6 @@ await fs.writeFile(
) + "\n",
);

// The development setup scripts docs can be ignored from snapshots.
// We manually add them back after hydration to clear them from Git diffs.
await fs.appendFile(
".github/DEVELOPMENT.md",
"\n" +
originalDevelopment.slice(originalDevelopment.indexOf("## Setup Scripts")),
);

// Ignore changes to the README.md all-contributor count and contributors table...
const updatedReadme = (await fs.readFile("README.md")).toString();
await fs.writeFile(
Expand Down
70 changes: 70 additions & 0 deletions src/steps/updateLocalFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,75 @@ describe("updateLocalFiles", () => {
`);
});

it("doesn't remove existing tooling when mode is migrate", async () => {
mockReadFileSafeAsJson.mockResolvedValue({});
mockReplaceInFile.mockResolvedValue([]);

await updateLocalFiles({ ...options, mode: "migrate" });

expect(mockReplaceInFile.mock.calls).toMatchInlineSnapshot(`
[
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
],
"from": /Create TypeScript App/g,
"to": "Stub Title",
},
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
],
"from": /JoshuaKGoldberg\\(\\?:\\\\/\\(\\.\\+\\)\\)\\?/g,
"to": [Function],
},
],
[
{
"allowEmptyPaths": true,
"files": "package.json",
"from": /JoshuaKGoldberg/g,
"to": "StubOwner",
},
],
[
{
"allowEmptyPaths": true,
"files": [
"./.github/**/*",
"./*.*",
],
"from": /create-typescript-app/g,
"to": "stub-repository",
},
],
[
{
"allowEmptyPaths": true,
"files": ".eslintrc.cjs",
"from": /\\\\/\\\\\\*\\\\n\\.\\+\\\\\\*\\\\/\\\\n\\\\n/gs,
"to": "",
},
],
[
{
"allowEmptyPaths": true,
"files": "./package.json",
"from": /"author": "\\.\\+"/g,
"to": ""author": "undefined"",
},
],
]
`);
});

it("does not replace an existing description when it does not exist", async () => {
mockReadFileSafeAsJson.mockResolvedValue({});
mockReplaceInFile.mockResolvedValue([]);
Expand All @@ -396,6 +465,7 @@ describe("updateLocalFiles", () => {
to: options.description,
});
});

it("replaces an existing description when it exists", async () => {
const existingDescription = "Existing description.";

Expand Down
54 changes: 34 additions & 20 deletions src/steps/updateLocalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,40 @@ export async function updateLocalFiles(options: Options) {
[/create-typescript-app/g, options.repository],
[/\/\*\n.+\*\/\n\n/gs, ``, ".eslintrc.cjs"],
[/"author": ".+"/g, `"author": "${options.author}"`, "./package.json"],
[/"test:create": ".+\n/g, ``, "./package.json"],
[/"test:initialize": ".*/g, ``, "./package.json"],
[/"initialize": ".*/g, ``, "./package.json"],
[/"test:migrate": ".+\n/g, ``, "./package.json"],
[/## Getting Started.*## Development/gs, `## Development`, "./README.md"],
[/\n## Setup Scripts.*$/gs, "", "./.github/DEVELOPMENT.md"],
[`\t\t"src/initialize/index.ts",\n`, ``, "./knip.jsonc"],
[`\t\t"src/migrate/index.ts",\n`, ``, "./knip.jsonc"],
[
`["src/index.ts!", "script/initialize*.js"]`,
`"src/index.ts!"`,
"./knip.jsonc",
],
[`["src/**/*.ts!", "script/**/*.js"]`, `"src/**/*.ts!"`, "./knip.jsonc"],
// Edge case: migration scripts will rewrite README.md attribution
[
/> 💙 This package was templated with .+\./g,
endOfReadmeTemplateLine,
"./README.md",
],
...(options.mode === "migrate"
? []
: ([
// Only creating a new repository should remove these parts.
// Existing ones might coincidentally have them too.
[/"test:create": ".+\n/g, ``, "./package.json"],
[/"test:initialize": ".*/g, ``, "./package.json"],
[/"initialize": ".*/g, ``, "./package.json"],
[/"test:migrate": ".+\n/g, ``, "./package.json"],
[
/## Getting Started.*## Development/gs,
`## Development`,
"./README.md",
],
[/\n## Setup Scripts.*$/gs, "", "./.github/DEVELOPMENT.md"],
[`\t\t"src/initialize/index.ts",\n`, ``, "./knip.jsonc"],
[`\t\t"src/migrate/index.ts",\n`, ``, "./knip.jsonc"],
[
`["src/index.ts!", "script/initialize*.js"]`,
`"src/index.ts!"`,
"./knip.jsonc",
],
[
`["src/**/*.ts!", "script/**/*.js"]`,
`"src/**/*.ts!"`,
"./knip.jsonc",
],
// Edge case: migration scripts will rewrite README.md attribution
[
/> 💙 This package was templated with .+\./g,
endOfReadmeTemplateLine,
"./README.md",
],
] as typeof replacements)),
];

if (existingPackage.description) {
Expand Down