Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cnpm and use electron as devDependence instead of electro… #183

Merged
merged 3 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(','));
};