这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/create-turbo/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe("create-turbo", () => {
telemetry,
});

expect(mockConsole.error).toHaveBeenCalledTimes(2);
expect(mockConsole.error).toHaveBeenCalledTimes(3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this error increasing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to do with https://github.com/vercel/turborepo/pull/6747/commits/870f012b5b67f96fa11aec0a8aafbfeaad287d3b#diff-87964d57d03186688cb5[…]bf2993f85483c259c7R31-R33. It made complete sense to me at the time that I wrote it - but now I'm struggling to remember why. 🤔

expect(mockConsole.error).toHaveBeenNthCalledWith(
1,
logger.turboRed(bold(">>>")),
Expand Down
2 changes: 2 additions & 0 deletions packages/create-turbo/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { transform as packageManagerTransform } from "./package-manager";
import { transform as officialStarter } from "./official-starter";
import { transform as gitIgnoreTransform } from "./git-ignore";
import { transform as pnpmEslintTransform } from "./pnpm-eslint";
import { transform as updateCommandsInREADME } from "./update-commands-in-readme";
import type { TransformInput, TransformResult } from "./types";

/**
Expand All @@ -12,4 +13,5 @@ export const transforms: Array<(args: TransformInput) => TransformResult> = [
gitIgnoreTransform,
packageManagerTransform,
pnpmEslintTransform,
updateCommandsInREADME,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import path from "node:path";
import fs from "node:fs/promises";
import { TransformError } from "./errors";
import type { TransformInput, TransformResult } from "./types";

const meta = {
name: "update-commands-in-readme",
};

// an array of all the possible replacement strings.
const PACKAGE_MANAGER_REPLACEMENTS = [
"pnpm run",
"npm run",
"yarn run",
"bun run",
"pnpm",
"npm",
"yarn",
"bun",
];

export async function transform(args: TransformInput): TransformResult {
const { prompts } = args;

const selectedPackageManager = prompts.packageManager;
const readmeFilePath = path.join(prompts.root, "README.md");
try {
// Read the content of the file
const data = await fs.readFile(readmeFilePath, "utf8");

if (!selectedPackageManager) {
throw new Error("A package manager was not selected.");
}

// replace package manager
const updatedReadmeData = replacePackageManager(
selectedPackageManager,
data
);

// Write the updated content back to the file
await fs.writeFile(readmeFilePath, updatedReadmeData, "utf8");
} catch (err) {
throw new TransformError("Unable to update README.md", {
transform: meta.name,
fatal: false,
});
}
return { result: "success", ...meta };
}

function replacePackageManager(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some test cases for this function just to make sure it's working as we expect. You can use it.each to make this simple. Something like:

const testCases = [...]
  
it.each(testCases)(
    'should replace placeholders correctly for packageManager: %p and text: "%s"',
    (packageManager, text, expected) => {
      const result = replacePackageManager(packageManager, text);
      expect(result).toBe(expected);
    }
  );

packageManager: { name: string },
text: string
): string {
// regex to search for a pattern enclosed in single backticks (` `), double backticks (`` ``) or
// triple backticks (``` ```) considering there might be newlines in between backticks and commands.
const searchRegex = new RegExp(
`\`\`\`[\\s\\S]*?\\b(?:${PACKAGE_MANAGER_REPLACEMENTS.join(
"|"
)})\\b[\\s\\S]*?\`\`\`|\`\`[\\s\\S]*?\\b(?:${PACKAGE_MANAGER_REPLACEMENTS.join(
"|"
)})\\b[\\s\\S]*?\`\`|\`[\\s\\S]*?\\b(?:${PACKAGE_MANAGER_REPLACEMENTS.join(
"|"
)})\\b[\\s\\S]*?\``,
"g"
);

// Replace all occurrences of regex with selectedPackageManager
const finalText = text.replace(searchRegex, (match) => {
// replacement regex => the regex required to replace the package manager.
const replacementRegex = new RegExp(
`\\b(?:${PACKAGE_MANAGER_REPLACEMENTS.join("|")})\\b`,
"g"
);
const updatedText = match.replace(
replacementRegex,
`${packageManager.name} run`
);
return updatedText;
});
return finalText;
}
Loading