From 73a5b168bcb998f8019b271954001aa67c0ce310 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Wed, 30 Oct 2024 15:51:27 +0800 Subject: [PATCH] feat: allow bypass update version --- README.md | 8 +++++--- src/index.ts | 24 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c14bf2b..eadbeb8 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,15 @@ A shared package for create-rspack, create-rsbuild, create-rspress and create-rs npm add create-rstack -D ``` -## Example +## Examples -See: [create-rsbuild](https://github.com/web-infra-dev/rsbuild/tree/main/packages/create-rsbuild). +| Project | Link | +| ------- | -------------------------------------------------------------------------------------------- | +| Rsbuild | [create-rsbuild](https://github.com/web-infra-dev/rsbuild/tree/main/packages/create-rsbuild) | +| Rslib | [create-rslib](https://github.com/web-infra-dev/rslib/tree/main/packages/create-rslib) | ![image](https://github.com/user-attachments/assets/2dda3501-720c-4151-bd3e-5e038dca9e68) - ## License [MIT](./LICENSE). diff --git a/src/index.ts b/src/index.ts index 29f2b7e..4debcfe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -315,8 +315,8 @@ export function mergePackageJson(targetPackage: string, extraPackage: string) { * Copy files from one folder to another. * @param from Source folder * @param to Destination folder - * @param version Version to update in package.json - * @param packageName Name to update in package.json + * @param version - Optional. The version to update in the package.json. If not provided, version will not be updated. + * @param name - Optional. The name to update in the package.json. If not provided, name will not be updated. * @param isMergePackageJson Merge package.json files * @param skipFiles Files to skip */ @@ -330,7 +330,7 @@ export function copyFolder({ }: { from: string; to: string; - version: string; + version?: string; packageName?: string; isMergePackageJson?: boolean; skipFiles?: string[]; @@ -383,17 +383,25 @@ const isStableVersion = (version: string) => { ); }; +/** + * Updates the package.json file at the specified path with the provided version and name. + * + * @param pkgJsonPath - The file path to the package.json file. + * @param version - Optional. The version to update in the package.json. If not provided, version will not be updated. + * @param name - Optional. The name to update in the package.json. If not provided, name will not be updated. + */ const updatePackageJson = ( pkgJsonPath: string, - version: string, + version?: string, name?: string, ) => { let content = fs.readFileSync(pkgJsonPath, 'utf-8'); - // Lock the version if it is not stable - const targetVersion = isStableVersion(version) ? `^${version}` : version; - - content = content.replace(/workspace:\*/g, targetVersion); + if (typeof version === 'string') { + // Lock the version if it is not stable + const targetVersion = isStableVersion(version) ? `^${version}` : version; + content = content.replace(/workspace:\*/g, targetVersion); + } const pkg = JSON.parse(content);