Skip to content

Commit

Permalink
Merge pull request #183 from wenshiqi0/cnpm
Browse files Browse the repository at this point in the history
Support for cnpm and use electron as devDependence instead of electro…
  • Loading branch information
MarshallOfSound authored Jun 16, 2017
2 parents 935f4c9 + c3049ee commit e0bcce9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
Expand Down Expand Up @@ -53,7 +54,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",
Expand Down
34 changes: 28 additions & 6 deletions src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Rebuilder {
nodeGypPath: string;
prodDeps: Set<string>;
rebuilds: (() => Promise<void>)[];
realModulePaths: Set<string>;
realNodeModulesPaths: Set<string>;

constructor(
public lifecycle: EventEmitter,
Expand All @@ -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 = new Set();
this.realNodeModulesPaths = new Set();
}

async rebuild() {
Expand Down Expand Up @@ -184,19 +188,38 @@ 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.has(realNodeModulesPath)) {
return;
}
this.realNodeModulesPaths.add(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.has(realPath)) {
continue;
}
this.realModulePaths.add(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'));
}
}
};
Expand Down Expand Up @@ -280,4 +303,3 @@ export function rebuildNativeModules(

return rebuild(modulePath, electronVersion, arch, whichModule.split(','));
};

0 comments on commit e0bcce9

Please sign in to comment.