Support for cnpm and use electron as devDependence instead of electron-prebuilt

This commit is contained in:
木农 2017-06-14 15:38:53 +08:00
Родитель cac4e98631
Коммит 8df7c24f2b
2 изменённых файлов: 32 добавлений и 6 удалений

Просмотреть файл

@ -6,6 +6,7 @@
"typings": "lib/src/main.d.ts",
"scripts": {
"compile": "tsc",
"watch": "tsc -w",
"prepublish": "npm run compile",
"test": "mocha --compilers ts:ts-node/register ./test/*.ts",
"lint": "tslint \"src/**/*.ts\" \"test/**/*.ts\""
@ -54,7 +55,7 @@
"@types/yargs": "^6.6.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"electron-prebuilt": "^1.4.13",
"electron": "^1.4.13",
"mocha": "^3.2.0",
"ts-node": "^3.0.2",
"tslint": "^4.5.1",

Просмотреть файл

@ -28,6 +28,8 @@ class Rebuilder {
nodeGypPath: string;
prodDeps: Set<string>;
rebuilds: (() => Promise<void>)[];
realModulePaths: Array<string>;
realNodeModulesPaths: Array<string>;
constructor(
public lifecycle: EventEmitter,
@ -42,6 +44,8 @@ class Rebuilder {
this.ABI = nodeAbi.getAbi(electronVersion, 'electron');
this.prodDeps = extraModules.reduce((acc, x) => acc.add(x), new Set());
this.rebuilds = [];
this.realModulePaths = [];
this.realNodeModulesPaths = [];
}
async rebuild() {
@ -184,19 +188,40 @@ class Rebuilder {
}
rebuildAllModulesIn(nodeModulesPath: string, prefix = '') {
d('scanning:', nodeModulesPath);
// While we use `cnpm`, it will make a circle scanning the dep tree.
// We also need to ensure that the `node_modules` which we are scanning has never came before.
const realNodeModulesPath = fs.realpathSync(nodeModulesPath);
if (this.realNodeModulesPaths.indexOf(realNodeModulesPath) > -1) {
return;
} else {
this.realNodeModulesPaths.push(realNodeModulesPath);
}
d('scanning:', realNodeModulesPath);
for (const modulePath of fs.readdirSync(realNodeModulesPath)) {
// If we use `cnpm` to install mudules, it will be rebuilded failed,
// because of some same modules rebuilded twice or more.
// Need to ensure one dep only be rebuilded once.
const finalPath = path.resolve(nodeModulesPath, modulePath);
const realPath = fs.realpathSync(finalPath);
if (this.realModulePaths.indexOf(realPath) > -1) {
continue;
} else {
this.realModulePaths.push(realPath);
}
for (const modulePath of fs.readdirSync(nodeModulesPath)) {
if (this.prodDeps[`${prefix}${modulePath}`]) {
this.rebuilds.push(() => this.rebuildModuleAt(path.resolve(nodeModulesPath, modulePath)));
this.rebuilds.push(() => this.rebuildModuleAt(realPath));
}
if (modulePath.startsWith('@')) {
this.rebuildAllModulesIn(path.resolve(nodeModulesPath, modulePath), `${modulePath}/`);
this.rebuildAllModulesIn(realPath, `${modulePath}/`);
}
if (fs.existsSync(path.resolve(nodeModulesPath, modulePath, 'node_modules'))) {
this.rebuildAllModulesIn(path.resolve(nodeModulesPath, modulePath, 'node_modules'));
this.rebuildAllModulesIn(path.resolve(realPath, 'node_modules'));
}
}
};