Include standalone Yarn (#569)

This commit is contained in:
Gregory Schier 2017-11-04 21:53:40 +01:00 committed by GitHub
parent db1ae27fb2
commit 31e7011a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 121481 additions and 20 deletions

View File

@ -4,5 +4,6 @@ dist/**/*
coverage/**/* coverage/**/*
node_modules/**/* node_modules/**/*
webpack/**/* webpack/**/*
bin/**/*
**/__fixtures__/prettify/* **/__fixtures__/prettify/*
flow-typed/ flow-typed/

View File

@ -7,6 +7,8 @@ import path from 'path';
import * as tar from 'tar'; import * as tar from 'tar';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
const YARN_PATH = path.resolve(__dirname, '../bin/yarn-standalone.js');
export default async function (moduleName: string): Promise<void> { export default async function (moduleName: string): Promise<void> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let info: Object = {}; let info: Object = {};
@ -27,7 +29,9 @@ export default async function (moduleName: string): Promise<void> {
request.on('response', response => { request.on('response', response => {
const bodyBuffers = []; const bodyBuffers = [];
console.log(`[plugins] Downloading plugin tarball from ${info.dist.tarball}`);
response.on('end', () => { response.on('end', () => {
console.log(`[plugins] Extracting plugin to ${pluginDir}`);
const w = tar.extract({ const w = tar.extract({
cwd: pluginDir, // Extract to plugin's directory cwd: pluginDir, // Extract to plugin's directory
strict: true, // Fail on anything strict: true, // Fail on anything
@ -38,14 +42,28 @@ export default async function (moduleName: string): Promise<void> {
reject(new Error(`Failed to extract ${info.dist.tarball}: ${err.message}`)); reject(new Error(`Failed to extract ${info.dist.tarball}: ${err.message}`));
}); });
console.log(`[plugins] Running Yarn install in "${pluginDir}"`);
w.on('end', () => { w.on('end', () => {
childProcess.exec('npm install', {cwd: pluginDir}, (err, stdout, stderr) => { 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) { if (err) {
reject(new Error(stderr)); reject(new Error(stderr));
} else { } else {
resolve(); resolve();
} }
}); }
);
}); });
const body = Buffer.concat(bodyBuffers); const body = Buffer.concat(bodyBuffers);
@ -70,27 +88,51 @@ export default async function (moduleName: string): Promise<void> {
async function _isInsomniaPlugin (moduleName: string): Promise<Object> { async function _isInsomniaPlugin (moduleName: string): Promise<Object> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
childProcess.exec( console.log(`[plugins] Fetching module info from npm`);
`npm show ${moduleName} --json`, (err, stdout, stderr) => { childProcess.execFile(
if (err && stderr.includes('E404')) { process.execPath,
reject(new Error(`${moduleName} not found on npm`)); [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; return;
} }
const info = JSON.parse(stdout); if (stderr) {
reject(new Error(`Yarn error ${stderr.toString('utf8')}`));
return;
}
if (!info.hasOwnProperty('insomnia')) { let yarnOutput;
try {
yarnOutput = JSON.parse(stdout.toString('utf8'));
} catch (err) {
reject(new Error(`Yarn response not JSON: ${err.message}`));
return;
}
const data = yarnOutput.data;
if (!data.hasOwnProperty('insomnia')) {
reject(new Error(`"${moduleName}" not a plugin! Package missing "insomnia" attribute`)); reject(new Error(`"${moduleName}" not a plugin! Package missing "insomnia" attribute`));
return; return;
} }
console.log(`[plugins] Detected Insomnia plugin ${data.name}`);
resolve({ resolve({
insomnia: info.insomnia, insomnia: data.insomnia,
name: info.name, name: data.name,
version: info.version, version: data.version,
dist: { dist: {
shasum: info.dist.shasum, shasum: data.dist.shasum,
tarball: info.dist.tarball tarball: data.dist.tarball
} }
}); });
} }

121409
bin/yarn-standalone.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -25,7 +25,7 @@
"build:clean": "rm -rf ./build/* && rm -rf ./dist/* && mkdirp ./build", "build:clean": "rm -rf ./build/* && rm -rf ./dist/* && mkdirp ./build",
"build:renderer": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.config.production.babel.js", "build:renderer": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.config.production.babel.js",
"build:main": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.config.electron.babel.js", "build:main": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.config.electron.babel.js",
"build:copy": "cp -r ./app/package.json ./app/static ./app/icons/* ./build/", "build:copy": "cp -r ./app/package.json ./bin ./app/static ./app/icons/* ./build/",
"build:install": "cd build && npm install", "build:install": "cd build && npm install",
"build": "npm run build:clean && npm run build:renderer && npm run build:main && npm run build:copy && npm run build:install", "build": "npm run build:clean && npm run build:renderer && npm run build:main && npm run build:copy && npm run build:install",
"rebuild": "electron-rebuild -f -w node-libcurl", "rebuild": "electron-rebuild -f -w node-libcurl",
@ -69,6 +69,13 @@
}, },
"build": { "build": {
"appId": "com.insomnia.app", "appId": "com.insomnia.app",
"extraResources": [
{
"from": "./bin",
"to": "./bin",
"filter": "yarn-standalone.js"
}
],
"category": "public.app-category.developer-tools", "category": "public.app-category.developer-tools",
"protocols": [ "protocols": [
{ {

View File

@ -52,6 +52,9 @@ module.exports = {
extensions: ['.js', '.json'], extensions: ['.js', '.json'],
mainFields: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main'] mainFields: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main']
}, },
node: {
__dirname: false // Use Node __dirname
},
externals: [ externals: [
// Omit all dependencies in app/package.json (we want them loaded at runtime via NodeJS) // Omit all dependencies in app/package.json (we want them loaded at runtime via NodeJS)
...Object.keys(pkg.dependencies), ...Object.keys(pkg.dependencies),

View File

@ -32,8 +32,7 @@ module.exports = {
], ],
output: output, output: output,
node: { node: {
__dirname: false, __dirname: false // Use node.js __dirname
__filename: false
}, },
target: 'electron-main', target: 'electron-main',
plugins: plugins plugins: plugins