2017-07-22 00:55:34 +00:00
|
|
|
// @flow
|
2017-08-01 16:58:08 +00:00
|
|
|
import * as electron from 'electron';
|
2017-07-22 00:55:34 +00:00
|
|
|
import childProcess from 'child_process';
|
|
|
|
import {PLUGIN_PATH} from '../common/constants';
|
2017-08-01 16:58:08 +00:00
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import path from 'path';
|
|
|
|
import * as tar from 'tar';
|
|
|
|
import * as crypto from 'crypto';
|
2017-07-22 00:55:34 +00:00
|
|
|
|
2017-11-04 20:53:40 +00:00
|
|
|
const YARN_PATH = path.resolve(__dirname, '../bin/yarn-standalone.js');
|
|
|
|
|
2017-07-22 00:55:34 +00:00
|
|
|
export default async function (moduleName: string): Promise<void> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2017-08-01 16:58:08 +00:00
|
|
|
let info: Object = {};
|
2017-07-22 00:55:34 +00:00
|
|
|
try {
|
2017-08-01 16:58:08 +00:00
|
|
|
info = await _isInsomniaPlugin(moduleName);
|
2017-07-22 00:55:34 +00:00
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-01 16:58:08 +00:00
|
|
|
const pluginDir = path.join(PLUGIN_PATH, moduleName);
|
|
|
|
|
|
|
|
// Make plugin directory
|
|
|
|
mkdirp.sync(pluginDir);
|
|
|
|
|
|
|
|
// Download the module
|
|
|
|
const request = electron.remote.net.request(info.dist.tarball);
|
|
|
|
request.on('response', response => {
|
|
|
|
const bodyBuffers = [];
|
|
|
|
|
2017-11-18 22:47:54 +00:00
|
|
|
console.debug(`[plugins] Downloading plugin tarball from ${info.dist.tarball}`);
|
2017-08-01 16:58:08 +00:00
|
|
|
response.on('end', () => {
|
2017-11-18 22:47:54 +00:00
|
|
|
console.debug(`[plugins] Extracting plugin to ${pluginDir}`);
|
2017-08-01 16:58:08 +00:00
|
|
|
const w = tar.extract({
|
|
|
|
cwd: pluginDir, // Extract to plugin's directory
|
|
|
|
strict: true, // Fail on anything
|
|
|
|
strip: 1 // Skip the "package/*" parent folder
|
|
|
|
});
|
|
|
|
|
|
|
|
w.on('error', err => {
|
|
|
|
reject(new Error(`Failed to extract ${info.dist.tarball}: ${err.message}`));
|
|
|
|
});
|
|
|
|
|
2017-11-18 22:47:54 +00:00
|
|
|
console.debug(`[plugins] Running Yarn install in "${pluginDir}"`);
|
2017-08-01 16:58:08 +00:00
|
|
|
w.on('end', () => {
|
2017-11-04 20:53:40 +00:00
|
|
|
childProcess.execFile(
|
|
|
|
process.execPath,
|
|
|
|
[YARN_PATH, 'install'],
|
|
|
|
{
|
|
|
|
timeout: 5 * 60 * 1000,
|
|
|
|
maxBuffer: 1024 * 1024,
|
|
|
|
cwd: pluginDir,
|
|
|
|
env: {
|
|
|
|
'NODE_ENV': 'production',
|
|
|
|
'ELECTRON_RUN_AS_NODE': 'true'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(new Error(stderr));
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
2017-08-01 16:58:08 +00:00
|
|
|
}
|
2017-11-04 20:53:40 +00:00
|
|
|
);
|
2017-08-01 16:58:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const body = Buffer.concat(bodyBuffers);
|
|
|
|
|
|
|
|
const shasum = crypto.createHash('sha1').update(body).digest('hex');
|
|
|
|
if (shasum !== info.dist.shasum) {
|
|
|
|
reject(new Error('Plugin shasum doesn\'t match npm'));
|
|
|
|
return;
|
2017-07-22 00:55:34 +00:00
|
|
|
}
|
2017-08-01 16:58:08 +00:00
|
|
|
|
|
|
|
w.end(body);
|
|
|
|
});
|
|
|
|
|
|
|
|
response.on('data', chunk => {
|
|
|
|
bodyBuffers.push(chunk);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.end();
|
2017-07-22 00:55:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-01 16:58:08 +00:00
|
|
|
async function _isInsomniaPlugin (moduleName: string): Promise<Object> {
|
2017-07-22 00:55:34 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-18 22:47:54 +00:00
|
|
|
console.debug(`[plugins] Fetching module info from npm`);
|
2017-11-04 20:53:40 +00:00
|
|
|
childProcess.execFile(
|
|
|
|
process.execPath,
|
|
|
|
[YARN_PATH, 'info', moduleName, '--json'],
|
|
|
|
{
|
|
|
|
timeout: 5 * 60 * 1000,
|
|
|
|
maxBuffer: 1024 * 1024,
|
|
|
|
env: {
|
|
|
|
'NODE_ENV': 'production',
|
|
|
|
'ELECTRON_RUN_AS_NODE': 'true'
|
|
|
|
}
|
|
|
|
}, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(new Error(`${moduleName} install error: ${err.message}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stderr) {
|
|
|
|
reject(new Error(`Yarn error ${stderr.toString('utf8')}`));
|
2017-07-22 00:55:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:53:40 +00:00
|
|
|
let yarnOutput;
|
|
|
|
try {
|
|
|
|
yarnOutput = JSON.parse(stdout.toString('utf8'));
|
|
|
|
} catch (err) {
|
|
|
|
reject(new Error(`Yarn response not JSON: ${err.message}`));
|
|
|
|
return;
|
|
|
|
}
|
2017-08-01 16:58:08 +00:00
|
|
|
|
2017-11-04 20:53:40 +00:00
|
|
|
const data = yarnOutput.data;
|
|
|
|
if (!data.hasOwnProperty('insomnia')) {
|
2017-08-01 16:58:08 +00:00
|
|
|
reject(new Error(`"${moduleName}" not a plugin! Package missing "insomnia" attribute`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-18 22:47:54 +00:00
|
|
|
console.debug(`[plugins] Detected Insomnia plugin ${data.name}`);
|
2017-11-04 20:53:40 +00:00
|
|
|
|
2017-08-01 16:58:08 +00:00
|
|
|
resolve({
|
2017-11-04 20:53:40 +00:00
|
|
|
insomnia: data.insomnia,
|
|
|
|
name: data.name,
|
|
|
|
version: data.version,
|
2017-08-01 16:58:08 +00:00
|
|
|
dist: {
|
2017-11-04 20:53:40 +00:00
|
|
|
shasum: data.dist.shasum,
|
|
|
|
tarball: data.dist.tarball
|
2017-08-01 16:58:08 +00:00
|
|
|
}
|
|
|
|
});
|
2017-07-22 00:55:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|