-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(examples): replace pnpm in readme #6747
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
Open
DependerKumarSoni
wants to merge
15
commits into
vercel:main
Choose a base branch
from
DependerKumarSoni:fix/replace-pnpm-in-README
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−1
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0b8e215
added a script 'updateCommandsInREADME.js' to update pnpm commands as…
DependerKumarSoni c7253b5
updated with regex based solution
DependerKumarSoni b5927af
Merge branch 'main' into fix/replace-pnpm-in-README
DependerKumarSoni 420a9fe
changes after 1st PR review
theletude 5827c18
revert
theletude 1e38615
Merge branch 'main' into fix/replace-pnpm-in-README
DependerKumarSoni 8f42522
codes modified as per legacy formatting and PR review
DependerKumarSoni 438a713
Merge branch 'main' into fix/replace-pnpm-in-README
DependerKumarSoni 7ed0507
[MINOR] Removed officialStarter check + updated readmeFilePath
DependerKumarSoni d193b36
Updated regex + refactoring
DependerKumarSoni 94ae468
Merge branch 'main' into fix/replace-pnpm-in-README
tknickman 07652e8
update based on review suggestions except the unit test
DependerKumarSoni 64e0351
Merge branch 'fix/replace-pnpm-in-README' of https://github.com/Depen…
DependerKumarSoni 329e44e
Merge branch 'main' into fix/replace-pnpm-in-README
anthonyshew 870f012
Fix tests.
anthonyshew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/create-turbo/src/transforms/update-commands-in-readme.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| 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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 🤔