Skip to content

Commit

Permalink
fix(cli): keepPackageJsonClean开启后lock不要修改
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 9, 2024
1 parent 5474d33 commit 14d94ba
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
51 changes: 51 additions & 0 deletions packages/cli/src/utils/backupPackageFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fs from 'fs';
import path from 'path';

export const backupFile = (runtimeSource: string, file: string) => {
const filePath = path.join(runtimeSource, file);

if (fs.existsSync(filePath)) {
fs.copyFileSync(filePath, `${filePath}.bak`);
}
};

export const backupPnpmLock = (runtimeSource: string) => backupFile(runtimeSource, 'pnpm-lock.yaml');
export const backupYarnLock = (runtimeSource: string) => backupFile(runtimeSource, 'yarn-lock.json');
export const backupNpmLock = (runtimeSource: string) => backupFile(runtimeSource, 'package-lock.json');
export const backupPackageJson = (runtimeSource: string) => backupFile(runtimeSource, 'package.json');

export const backupLock = (runtimeSource: string, npmType: string) => {
if (npmType === 'pnpm') {
backupPnpmLock(runtimeSource);
} else if (npmType === 'yarn') {
backupYarnLock(runtimeSource);
}
if (npmType === 'npm') {
backupNpmLock(runtimeSource);
}
};

export const restoreFile = (runtimeSource: string, file: string) => {
const filePath = path.join(runtimeSource, file);

if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
fs.renameSync(`${filePath}.bak`, filePath);
}
};

export const restorePnpmLock = (runtimeSource: string) => restoreFile(runtimeSource, 'pnpm-lock.yaml');
export const restoreYarnLock = (runtimeSource: string) => restoreFile(runtimeSource, 'yarn-lock.json');
export const restoreNpmLock = (runtimeSource: string) => restoreFile(runtimeSource, 'package-lock.json');
export const restorePackageJson = (runtimeSource: string) => restoreFile(runtimeSource, 'package.json');

export const restoreLock = (runtimeSource: string, npmType: string) => {
if (npmType === 'pnpm') {
restorePnpmLock(runtimeSource);
} else if (npmType === 'yarn') {
restoreYarnLock(runtimeSource);
}
if (npmType === 'npm') {
restoreNpmLock(runtimeSource);
}
};
1 change: 1 addition & 0 deletions packages/cli/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './defineUserConfig';
export * from './loadUserConfig';
export * from './prepareEntryFile';
export * from './resolveAppPackages';
export * from './backupPackageFile';
14 changes: 5 additions & 9 deletions packages/cli/src/utils/resolveAppPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as recast from 'recast';
import type App from '../Core';
import { EntryType, ModuleMainFilePath, NpmConfig, PackageType } from '../types';

import { backupLock, backupPackageJson, restoreLock, restorePackageJson } from './backupPackageFile';
import { error, execInfo, info } from './logger';

type Ast = any;
Expand Down Expand Up @@ -549,18 +550,13 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => {
if (!npmConfig.keepPackageJsonClean) {
npmInstall(dependencies, source, npmConfig);
} else {
const packageFile = path.join(source, 'package.json');
const packageBakFile = path.join(source, 'package.json.bak');
if (fs.existsSync(packageFile)) {
fs.copyFileSync(packageFile, packageBakFile);
}
backupLock(source, npmConfig.client || 'npm');
backupPackageJson(source);

npmInstall(dependencies, source, npmConfig);

if (fs.existsSync(packageBakFile)) {
fs.unlinkSync(packageFile);
fs.renameSync(packageBakFile, packageFile);
}
restoreLock(source, npmConfig.client || 'npm');
restorePackageJson(source);
}
}

Expand Down

0 comments on commit 14d94ba

Please sign in to comment.