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

feat(examples): replace pnpm in readme #6747

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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: 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,50 @@
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, example } = args;

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

// 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(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