Skip to content

Commit

Permalink
chore(installer): use the ora helper in the install command
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Dec 31, 2016
1 parent 943cc6e commit 9358eb4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 74 deletions.
143 changes: 70 additions & 73 deletions src/electron-forge-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import debug from 'debug';
import fetch from 'node-fetch';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import nugget from 'nugget';
import opn from 'opn';
import os from 'os';
import path from 'path';
import pify from 'pify';
import program from 'commander';
import nugget from 'nugget';
import ora from 'ora';
import semver from 'semver';

import './util/terminate';
import asyncOra from './util/ora-handler';

import darwinDMGInstaller from './installers/darwin/dmg';
import darwinZipInstaller from './installers/darwin/zip';
Expand All @@ -24,8 +24,6 @@ const d = debug('electron-forge:lint');
const GITHUB_API = 'https://api.github.com';

const main = async () => {
const searchSpinner = ora.ora('Searching for Application').start();

let repo;

program
Expand All @@ -36,64 +34,65 @@ const main = async () => {
})
.parse(process.argv);

if (!repo || repo.indexOf('/') === -1) {
searchSpinner.fail();
console.error('Invalid repository name, must be in the format owner/name'.red);
process.exit(1);
}
let latestRelease;
let possibleAssets = [];

d('searching for repo:', repo);
let releases;
try {
releases = await (await fetch(`${GITHUB_API}/repos/${repo}/releases`)).json();
} catch (err) {
// Ignore error
}
if (!releases || releases.message === 'Not Found' || !Array.isArray(releases)) {
searchSpinner.fail();
console.error(`Failed to find releases for repository "${repo}". Please check the name and try again.`.red);
process.exit(1);
}
await asyncOra('Searching for Application', async (searchSpinner) => {
if (!repo || repo.indexOf('/') === -1) {
// eslint-disable-next-line no-throw-literal
throw 'Invalid repository name, must be in the format owner/name';
}

const sortedReleases = releases.sort((releaseA, releaseB) => {
let tagA = releaseA.tag_name;
if (tagA.substr(0, 1) === 'v') tagA = tagA.substr(1);
let tagB = releaseB.tag_name;
if (tagB.substr(0, 1) === 'v') tagB = tagB.substr(1);
return (semver.gt(tagB, tagA) ? 1 : -1);
});
const latestRelease = sortedReleases[0];
d('searching for repo:', repo);
let releases;
try {
releases = await (await fetch(`${GITHUB_API}/repos/${repo}/releases`)).json();
} catch (err) {
// Ignore error
}

searchSpinner.text = 'Searching for Releases';
if (!releases || releases.message === 'Not Found' || !Array.isArray(releases)) {
// eslint-disable-next-line no-throw-literal
throw `Failed to find releases for repository "${repo}". Please check the name and try again.`;
}

const assets = latestRelease.assets;
if (!assets || !Array.isArray(assets)) {
searchSpinner.fail();
console.error('Could not find any assets for the latest release'.red);
process.exit(1);
}
const sortedReleases = releases.sort((releaseA, releaseB) => {
let tagA = releaseA.tag_name;
if (tagA.substr(0, 1) === 'v') tagA = tagA.substr(1);
let tagB = releaseB.tag_name;
if (tagB.substr(0, 1) === 'v') tagB = tagB.substr(1);
return (semver.gt(tagB, tagA) ? 1 : -1);
});
latestRelease = sortedReleases[0];

const installTargets = {
win32: [/\.exe$/],
darwin: [/OSX.*\.zip$/, /darwin.*\.zip$/, /macOS.*\.zip$/, /mac.*\.zip$/, /\.dmg$/],
linux: [/\.rpm$/, /\.deb$/],
};
searchSpinner.text = 'Searching for Releases'; // eslint-disable-line

const possibleAssets = assets.filter((asset) => {
const targetSuffixes = installTargets[process.platform];
for (const suffix of targetSuffixes) {
if (suffix.test(asset.name)) return true;
const assets = latestRelease.assets;
if (!assets || !Array.isArray(assets)) {
// eslint-disable-next-line no-throw-literal
throw 'Could not find any assets for the latest release';
}
return false;
});

if (possibleAssets.length === 0) {
searchSpinner.fail();
console.error('Failed to find any installable assets for target platform:'.red, process.platform.cyan);
process.exit(1);
}
const installTargets = {
win32: [/\.exe$/],
darwin: [/OSX.*\.zip$/, /darwin.*\.zip$/, /macOS.*\.zip$/, /mac.*\.zip$/, /\.dmg$/],
linux: [/\.rpm$/, /\.deb$/],
};

possibleAssets = assets.filter((asset) => {
const targetSuffixes = installTargets[process.platform];
for (const suffix of targetSuffixes) {
if (suffix.test(asset.name)) return true;
}
return false;
});

if (possibleAssets.length === 0) {
// eslint-disable-next-line no-throw-literal
throw `Failed to find any installable assets for target platform: ${`${process.platform}`.cyan}`;
}
});

searchSpinner.succeed();
console.info('Found latest release:', `${latestRelease.tag_name}`.cyan);

let targetAsset = possibleAssets[0];
Expand Down Expand Up @@ -125,26 +124,24 @@ const main = async () => {
await pify(nugget)(targetAsset.browser_download_url, nuggetOpts);
}

const installSpinner = ora.ora('Installing Application').start();

const installActions = {
win32: {
'.exe': async filePath => await opn(filePath, { wait: false }),
},
darwin: {
'.zip': darwinZipInstaller,
'.dmg': darwinDMGInstaller,
},
linux: {
'.deb': linuxDebInstaller,
'.rpm': linuxRPMInstaller,
},
};

const suffixFnIdent = Object.keys(installActions[process.platform]).find(suffix => targetAsset.name.endsWith(suffix));
await installActions[process.platform][suffixFnIdent](fullFilePath, installSpinner);

installSpinner.succeed();
await asyncOra('Installing Application', async (installSpinner) => {
const installActions = {
win32: {
'.exe': async filePath => await opn(filePath, { wait: false }),
},
darwin: {
'.zip': darwinZipInstaller,
'.dmg': darwinDMGInstaller,
},
linux: {
'.deb': linuxDebInstaller,
'.rpm': linuxRPMInstaller,
},
};

const suffixFnIdent = Object.keys(installActions[process.platform]).find(suffix => targetAsset.name.endsWith(suffix));
await installActions[process.platform][suffixFnIdent](fullFilePath, installSpinner);
});
};

main();
3 changes: 2 additions & 1 deletion src/installers/darwin/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default async (filePath, installSpinner) => {
message: `The application "${path.basename(targetApplicationPath)}" appears to already exist in /Applications. Do you want to replace it?`,
});
if (!confirm) {
throw new Error('Installation stopped by user');
// eslint-disable-next-line no-throw-literal
throw 'Installation stopped by user';
} else {
installSpinner.start();
await fs.remove(targetApplicationPath);
Expand Down

0 comments on commit 9358eb4

Please sign in to comment.