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

Ship yarn with hyper #381

Merged
merged 13 commits into from
Aug 14, 2017
3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"ms": "0.7.1",
"node-fetch": "1.6.3",
"node-pty": "0.6.10",
"queue": "4.0.0",
"semver": "5.3.0",
"shell-env": "0.2.0",
"uuid": "3.0.0",
"shell-env": "0.2.0",
"winreg": "1.2.2"
}
}
95 changes: 44 additions & 51 deletions app/plugins.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const {exec} = require('child_process');
const {app, dialog} = require('electron');
const {resolve, basename} = require('path');
const {writeFileSync} = require('fs');

const {app, dialog} = require('electron');
const cp = require('child_process');
const {sync: mkdirpSync} = require('mkdirp');
const Config = require('electron-config');
const ms = require('ms');
const shellEnv = require('shell-env');
const queue = require('queue');

const spawnQueue = queue({concurrency: 1});
Copy link
Contributor

Choose a reason for hiding this comment

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

spawnQueue or yarnQueue

Copy link
Collaborator

Choose a reason for hiding this comment

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

spawnQueue is better.


const config = require('./config');
const notify = require('./notify');
Expand All @@ -18,6 +19,7 @@ const cache = new Config();
// modules path
const path = resolve(config.getConfigDir(), '.hyper_plugins');
const localPath = resolve(path, 'local');
const cachePath = resolve(path, 'cache');
const availableExtensions = new Set([
'onApp', 'onWindow', 'onRendererWindow', 'onUnload', 'middleware',
'reduceUI', 'reduceSessions', 'reduceTermGroups',
Expand Down Expand Up @@ -79,17 +81,10 @@ function updatePlugins({force = false} = {}) {

if (err) {
console.error(err.stack);
if (/not a recognized/.test(err.message) || /command not found/.test(err.message)) {
notify(
'Error updating plugins.',
'We could not find the `npm` command. Make sure it\'s in $PATH'
);
} else {
notify(
'Error updating plugins.',
'Check `~/.hyper_plugins/npm-debug.log` for more information.'
);
}
notify(
'Error updating plugins.',
err.message
);
} else {
// flag successful plugin update
cache.set('hyper.plugins', id_);
Expand Down Expand Up @@ -228,46 +223,44 @@ function toDependencies(plugins) {
}

function install(fn) {
const {shell: cfgShell, npmRegistry} = exports.getDecoratedConfig();

const shell = cfgShell && cfgShell !== '' ? cfgShell : undefined;

shellEnv(shell).then(env => {
if (npmRegistry) {
env.NPM_CONFIG_REGISTRY = npmRegistry;
}
/* eslint-disable camelcase */
env.npm_config_runtime = 'electron';
env.npm_config_target = process.versions.electron;
env.npm_config_disturl = 'https://atom.io/download/atom-shell';
/* eslint-enable camelcase */
// Shell-specific installation commands
const installCommands = {
fish: 'npm prune; and npm install --production --no-shrinkwrap',
posix: 'npm prune && npm install --production --no-shrinkwrap'
};
// determine the shell we're running in
const whichShell = (typeof cfgShell === 'string' && cfgShell.match(/fish/)) ? 'fish' : 'posix';
const execOptions = {
cwd: path,
env
function yarn(args, cb) {
const yarnPath = resolve(__dirname, '..', 'bin', 'yarn-standalone.js');
const env = {
NODE_ENV: 'production',
ELECTRON_RUN_AS_NODE: 'true'
};

// https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
// node.js requires command line parsing should be compatible with cmd.exe on Windows, should able to accept `/d /s /c`
// but most custom shell doesn't. Instead, falls back to default shell
if (process.platform !== 'win32') {
execOptions.shell = shell;
}
spawnQueue.push(end => {
const cmd = [process.execPath, yarnPath].concat(args).join(' ');
console.log('Launching yarn:', cmd);

cp.exec(cmd, {
cwd: path,
env,
shell: true,
timeout: ms('5m'),
stdio: ['ignore', 'ignore', 'inherit']
}, err => {
if (err) {
cb(err);
} else {
cb(null);
}

// Use the install command that is appropriate for our shell
exec(installCommands[whichShell], execOptions, err => {
if (err) {
return fn(err);
}
fn(null);
end();
spawnQueue.start();
});
});
}).catch(fn);

spawnQueue.start();
}

yarn(['install', '--no-emoji', '--no-lockfile', '--cache-folder', cachePath], err => {
if (err) {
return fn(err);
}
fn(null);
});
}

exports.subscribe = function (fn) {
Expand Down
Loading