Skip to content

Commit

Permalink
fix(.eo): can not update package.json file in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jun 2, 2022
1 parent 1df39ea commit e2a8a73
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/platform/node/extension-manager/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import { ModuleHandlerOptions, ModuleHandlerResult } from '../types';
import { fileExists, writeJson } from 'eo/shared/node/file';
import { CoreHandler } from './core';
import * as spawn from 'cross-spawn';
import * as fs from 'fs';
// import npmCli from 'npm';
const npmCli = require('npm');
/**
Expand Down Expand Up @@ -78,6 +78,34 @@ export class ModuleHandler extends CoreHandler {
return await this.execCommand(isLocal ? 'unlink' : 'uninstall', modules);
}

/**
* 手动操作package.json
* @param result npm install安装成功回调的结果
* @param moduleList 所有的模块列表
*/
private operatePackage(result: any[], moduleList: string[], action: 'uninstall' | 'install') {
if (Array.isArray(result)) {
const moduleNames = moduleList.map((n) => n.split('@')[0]);
const packagePath = path.join(this.baseDir, 'package.json');
result.forEach(([name]) => {
const [pkgName, pkgVersion] = name.split('@');
if (moduleNames.includes(pkgName)) {
const packageJSON = fs.readFileSync(packagePath);
const packageObj = JSON.parse(packageJSON.toString());
const dependencieKeys = Object.keys(packageObj.dependencies);
if (!dependencieKeys.includes(pkgName)) {
if (action === 'install') {
packageObj.dependencies[pkgName] = pkgVersion;
} else {
delete packageObj.dependencies[pkgName];
}
}
fs.writeFileSync(packagePath, JSON.stringify(packageObj));
}
});
}
}

/**
* 运行模块管理器
* @param command
Expand All @@ -104,6 +132,7 @@ export class ModuleHandler extends CoreHandler {
if (err) {
reject(err);
}
this.operatePackage(data, moduleList, 'install');
resolve({ code: 0, data });
});
}
Expand All @@ -113,6 +142,7 @@ export class ModuleHandler extends CoreHandler {
if (err) {
reject(err);
}
this.operatePackage(data, moduleList, 'uninstall');
resolve({ code: 0, data });
});
}
Expand Down

0 comments on commit e2a8a73

Please sign in to comment.