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

Workaround for node-pre-gyp's lack of electron runtime detection (and a little bugfixing) #44

Merged
merged 5 commits into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/paulcbetts/electron-rebuild",
"dependencies": {
"babel-runtime": "^5.8.20",
"glob": "^6.0.3",
"lodash": "^3.10.1",
"ncp": "^2.0.0",
"npm": "^2.14.1",
Expand Down
9 changes: 7 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import {installNodeHeaders, rebuildNativeModules, shouldRebuildNativeModules} from './main.js';
import { preGypFixSetup, preGypFixRun } from './node-pre-gyp-fix.js'
import path from 'path';
import fs from 'fs';

Expand All @@ -22,6 +23,8 @@ const argv = require('yargs')
.alias('w', 'which-module')
.describe('e', 'The path to electron-prebuilt')
.alias('e', 'electron-prebuilt-dir')
.describe('p', 'Enable the ugly (and hopefully not needed soon enough) node-pre-gyp path fixer')
.alias('p', 'pre-gyp-fix')
.epilog('Copyright 2015')
.argv;

Expand Down Expand Up @@ -50,7 +53,7 @@ let nodeModuleVersion = null;
if (!argv.n) {
try {
let pathDotText = path.join(argv.e, 'path.txt');
electronPath = fs.readFileSync(pathDotText, 'utf8');
electronPath = path.join(argv.e, fs.readFileSync(pathDotText, 'utf8'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use path.resolve instead of path.join, so it can handles both cases, when path.txt contains absolute or relative path.

} catch (e) {
console.error("Couldn't find electron-prebuilt and no --node-module-version parameter set, always rebuilding");
}
Expand Down Expand Up @@ -86,9 +89,11 @@ if (!electronPath && !nodeModuleVersion) {
shouldRebuildPromise
.then(x => {
if (!x) process.exit(0);

})
.then((x, beforeRebuild) => {
return installNodeHeaders(argv.v, null, null, argv.a)
.then(() => rebuildNativeModules(argv.v, argv.m, argv.w, null, argv.a))
.then(() => preGypFixRun(argv.m, argv.p, electronPath, nodeModuleVersion))
.then(() => process.exit(0));
})
.catch((e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const spawnWithHeadersDir = async (cmd, args, headersDir, cwd) => {
}
};

const getElectronModuleVersion = async (pathToElectronExecutable) => {
export async function getElectronModuleVersion(pathToElectronExecutable) {
let args = [ '-e', 'console.log(process.versions.modules)' ]
let env = { ATOM_SHELL_INTERNAL_RUN_AS_NODE: '1' };

Expand All @@ -50,7 +50,7 @@ const getElectronModuleVersion = async (pathToElectronExecutable) => {
throw new Error(`Failed to check Electron's module version number: ${versionAsString}`);
}

return toString(versionAsString);
return versionAsString;
}

export async function installNodeHeaders(nodeVersion, nodeDistUrl=null, headersDir=null, arch=null) {
Expand Down
21 changes: 21 additions & 0 deletions src/node-pre-gyp-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path';
import spawn from './spawn.js';
import promisify from './promisify.js';
import {getElectronModuleVersion} from './main.js';
const glob = promisify(require('glob'));
const cp = promisify(require('ncp').ncp);

export async function preGypFixRun(cwd, shouldRun, electronPath, explicitNodeVersion=null) {
if (!shouldRun) return;

let paths = await glob(path.join(cwd, '**', 'electron-v*'));
let electronModuleVersion = explicitNodeVersion || (await getElectronModuleVersion(electronPath));

for(let path of paths) {
// THE MIGHTY HACK GOES HERE!
let newPath = path.replace(/electron-v[^-]+/, 'node-v'+electronModuleVersion);

await cp(path, newPath);
console.log('node-pre-gyp fixer:', path, 'copied to', newPath)
}
}