This commit is contained in:
Paul Betts 2015-05-04 00:01:56 -07:00
Родитель fde0b2b30c
Коммит e867fd7792
9 изменённых файлов: 251 добавлений и 0 удалений

15
.jshintrc Normal file
Просмотреть файл

@ -0,0 +1,15 @@
{
"es5": true,
"esnext": true,
"eqeqeq": true,
"eqnull": true,
"expr": true,
"latedef": true,
"onevar": true,
"noarg": true,
"node": true,
"trailing": true,
"undef": true,
"unused": true,
"browser": true
}

32
package.json Normal file
Просмотреть файл

@ -0,0 +1,32 @@
{
"name": "electron-rebuild",
"version": "0.1.0",
"description": "Electron supporting package to rebuild native node modules against the currently installed electron",
"main": "lib/main.js",
"scripts": {
"compile": "babel --stage 0 -d lib/ src/ && babel --stage 0 -d test-dist/ test/",
"prepublish": "npm run compile",
"test": "npm run compile && mocha test-dist/*"
},
"repository": {
"type": "git",
"url": "https://github.com/paulcbetts/electron-rebuild"
},
"keywords": [
"electron"
],
"author": "Paul Betts <paul@paulbetts.org>",
"license": "MIT",
"bugs": {
"url": "https://github.com/paulcbetts/electron-rebuild/issues"
},
"homepage": "https://github.com/paulcbetts/electron-rebuild",
"dependencies": {
"babel-core": "^5.2.9",
"lodash": "^3.8.0",
"npm": "^2.9.0",
"promise": "^7.0.1",
"rimraf": "^2.3.3",
"stream-cp": "^0.1.1"
}
}

39
src/main.js Normal file
Просмотреть файл

@ -0,0 +1,39 @@
require('babel-core');
import path from 'path';
import _ from 'lodash';
import childProcess from 'child_process';
import spawn from './spawn';
import promisify from './promisify';
const getHeadersRootDirForVersion = (version) => {
return path.resolve(__dirname, 'headers');
}
export function installNodeHeaders(nodeVersion, nodeDistUrl=null) {
let headersDir = getHeadersRootDirForVersion(nodeVersion);
let distUrl = nodeDistUrl || 'https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist';
let canary = path.join(headersDir, '.node-gyp', nodeVersion, 'common.gypi');
/*
await fs.exists
if (fs.existsSync(canary))
return rx.Observable.create (subj) ->
grunt.verbose.ok 'Found existing node.js installation, skipping install to save time!'
rx.Observable.return(true).subscribe(subj)
cmd = 'node'
args = [require.resolve('npm/node_modules/node-gyp/bin/node-gyp'), 'install',
"--target=#{nodeVersion}",
"--arch=#{nodeArch}",
"--dist-url=#{distUrl}"]
env = _.extend {}, process.env, HOME: nodeGypHome
env.USERPROFILE = env.HOME if process.platform is 'win32'
rx.Observable.create (subj) ->
grunt.verbose.ok 'Installing node.js'
spawnObservable({cmd, args, opts: {env}, stdout: stdout, stderr: stderr}).subscribe(subj)
*/
}

31
src/promisify.js Normal file
Просмотреть файл

@ -0,0 +1,31 @@
import _ from 'lodash';
const promisify = (funcOrObject) => {
if (typeof funcOrObject === 'function') {
return function(...args) {
return new Promise(function(resolve, reject) {
args.push((err, ...rest) => {
if (err) {
reject(err);
} else {
resolve(rest.length === 1 ? rest[0] : rest);
}
});
funcOrObject.apply(this, args);
});
};
}
if (typeof funcOrObject === 'object') {
return _.reduce(Object.keys(funcOrObject), (acc, x) => {
acc[x] = promisify(funcOrObject[x]);
return acc;
}, {});
}
// Neither a func or an object, just return itself
return funcOrObject;
};
export default promisify;

44
src/spawn.js Normal file
Просмотреть файл

@ -0,0 +1,44 @@
import _ from 'lodash';
import childProcess from 'child_process';
export default function (options={}) {
const stdout = (options.stdout && process.stdout) ? process.stdout : [];
const stderr = (options.stderr && process.stderr) ? process.stderr : [];
return new Promise((resolve, reject) => {
let error = null;
let proc = childProcess.spawn(options.cmd, options.args, options.opts);
proc.stdout.on('data', (data) => {
if(_.isArray(stdout)) {
stdout.push(data.toString());
} else {
stdout.write(data.toString());
}
});
proc.stderr.on('data', (data) => {
if(_.isArray(stderr)) {
stderr.push(data.toString());
} else {
stderr.write(data.toString());
}
});
proc.on('error', (processError) => error = error || processError);
proc.on('close', (exitCode, signal) => {
if (exitCode !== 0) {
error = error || new Error(signal);
}
let results = {
stderr: (_.isArray(stderr) ? stderr.join('') : ''),
stdout: (_.isArray(stdout) ? stdout.join('') : ''),
code: exitCode
};
if (error) { reject(error); } else { resolve(results); }
});
});
}

3
test/main.js Normal file
Просмотреть файл

@ -0,0 +1,3 @@
import _ from './support';
import path from 'path';
import fs from 'fs';

32
test/promisify.js Normal file
Просмотреть файл

@ -0,0 +1,32 @@
import _ from './support';
import fs from 'fs';
import path from 'path';
import promisify from '../lib/promisify.js';
describe('promisify', () => {
it('should handle fs.stat', async () => {
const stat = promisify(fs.stat);
let result = await stat(path.join(__dirname, 'main.js'));
expect(result).to.exist;
expect(result.size > 1).to.be.ok;
let shouldDie = true;
try {
await stat('__WEFJWOEFW_WE_FWEF_EFWJEIFJWEF');
} catch (err) {
expect(err).to.exist;
shouldDie = false;
}
expect(shouldDie).to.equal(false);
});
it('should map all of fs', async () => {
const promiseFs = promisify(fs);
let result = await promiseFs.stat(path.join(__dirname, 'main.js'));
expect(result).to.exist;
expect(result.size > 1).to.be.ok;
});
});

41
test/spawn.js Normal file
Просмотреть файл

@ -0,0 +1,41 @@
import _ from './support';
import path from 'path';
import fs from 'fs';
import spawn from '../lib/spawn.js';
describe('spawn', () => {
it('should work with ls', async () => {
if (process.platform === 'win32') {
return Promise.resolve(true).should.become(true);
}
let startInfo = {
cmd: '/bin/ls',
args: ['/']
};
let results = await spawn(startInfo);
expect(results.code).to.equal(0);
}) ;
it('should fail when the path is completely bogus', async () => {
if (process.platform === 'win32') {
return Promise.resolve(true).should.become(true);
}
let startInfo = {
cmd: '/bin/___nothere____ls',
args: ['/']
};
let shouldDie = true;
try {
await spawn(startInfo);
} catch (err) {
shouldDie = false;
}
expect(shouldDie).to.equal(false);
});
});

14
test/support.js Normal file
Просмотреть файл

@ -0,0 +1,14 @@
global.Promise = global.Promise || require('promise');
let chai = require("chai");
let chaiAsPromised = require("chai-as-promised");
require('babel/polyfill');
chai.should();
chai.use(chaiAsPromised);
global.chaiAsPromised = chaiAsPromised;
global.expect = chai.expect;
global.AssertionError = chai.AssertionError;
global.Assertion = chai.Assertion;
global.assert = chai.assert;