From 8df7c24f2b518d58aaa0bec0f4e3d37bff61f9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E5=86=9C?= Date: Wed, 14 Jun 2017 15:38:53 +0800 Subject: [PATCH 1/2] Support for cnpm and use electron as devDependence instead of electron-prebuilt --- package.json | 3 ++- src/rebuild.ts | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ae8d671a..a90024b4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/rebuild.ts b/src/rebuild.ts index 7cdd2b6f..c1706d47 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -28,6 +28,8 @@ class Rebuilder { nodeGypPath: string; prodDeps: Set; rebuilds: (() => Promise)[]; + realModulePaths: Array; + realNodeModulesPaths: Array; 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')); } } }; From a975dc59f3ce0450fac4abf609490b72ac0091a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E5=86=9C?= Date: Wed, 14 Jun 2017 16:25:08 +0800 Subject: [PATCH 2/2] Use Set instead of Array for better performance. --- src/rebuild.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/rebuild.ts b/src/rebuild.ts index c1706d47..d9835788 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -28,8 +28,8 @@ class Rebuilder { nodeGypPath: string; prodDeps: Set; rebuilds: (() => Promise)[]; - realModulePaths: Array; - realNodeModulesPaths: Array; + realModulePaths: Set; + realNodeModulesPaths: Set; constructor( public lifecycle: EventEmitter, @@ -44,8 +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 = []; + this.realModulePaths = new Set(); + this.realNodeModulesPaths = new Set(); } async rebuild() { @@ -191,11 +191,10 @@ class Rebuilder { // 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) { + if (this.realNodeModulesPaths.has(realNodeModulesPath)) { return; - } else { - this.realNodeModulesPaths.push(realNodeModulesPath); } + this.realNodeModulesPaths.add(realNodeModulesPath); d('scanning:', realNodeModulesPath); @@ -206,11 +205,10 @@ class Rebuilder { const finalPath = path.resolve(nodeModulesPath, modulePath); const realPath = fs.realpathSync(finalPath); - if (this.realModulePaths.indexOf(realPath) > -1) { + if (this.realModulePaths.has(realPath)) { continue; - } else { - this.realModulePaths.push(realPath); } + this.realModulePaths.add(realPath); if (this.prodDeps[`${prefix}${modulePath}`]) { this.rebuilds.push(() => this.rebuildModuleAt(realPath)); @@ -305,4 +303,3 @@ export function rebuildNativeModules( return rebuild(modulePath, electronVersion, arch, whichModule.split(',')); }; -