| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 |
11x
11x
11x
11x
11x
11x
11x
2x
9x
9x
9x
9x
3x
9x
6x
4x
4x
4x
4x
4x
6x
6x
6x
6x
1x
5x
5x
8x
8x
40x
1x
5x
1x
4x
4x
4x
3x
1x
2x
1x
1x
2x
1x
1x
1x
3x
3x
3x
3x
3x
3x
3x
3x
3x
3x
6x
3x
| import 'colors';
import debug from 'debug';
import fetch from 'node-fetch';
import fs from 'fs-promise';
import inquirer from 'inquirer';
import nugget from 'nugget';
import os from 'os';
import path from 'path';
import pify from 'pify';
import semver from 'semver';
import asyncOra from '../util/ora-handler';
import darwinDMGInstaller from '../installers/darwin/dmg';
import darwinZipInstaller from '../installers/darwin/zip';
import linuxDebInstaller from '../installers/linux/deb';
import linuxRPMInstaller from '../installers/linux/rpm';
import win32ExeInstaller from '../installers/win32/exe';
const d = debug('electron-forge:install');
const GITHUB_API = 'https://api.github.com';
/**
* @typedef {Object} InstallOptions
* @property {boolean} [interactive=false] Whether to use sensible defaults or prompt the user visually
* @property {boolean} [prerelease=false] Whether to install prerelease versions
* @property {string} repo The GitHub repository to install from, in the format owner/name
* @property {function} chooseAsset A function that must return the asset to use/install from a provided array of compatible GitHub assets
*/
/**
* Install an Electron application from GitHub. If you leave interactive as `false`, you MUST provide a `chooseAsset` function.
*
* @param {InstallOptions} providedOptions - Options for the install method
* @return {Promise} Will resolve when the install process is complete
*/
export default async (providedOptions = {}) => {
// eslint-disable-next-line prefer-const, no-unused-vars
let { interactive, prerelease, repo, chooseAsset } = Object.assign({
interactive: false,
prerelease: false,
}, providedOptions);
asyncOra.interactive = interactive;
let latestRelease;
let possibleAssets = [];
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';
}
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)) {
// eslint-disable-next-line no-throw-literal
throw `Failed to find releases for repository "${repo}". Please check the name and try again.`;
}
releases = releases.filter(release => !release.prerelease || prerelease);
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];
searchSpinner.text = 'Searching for Releases'; // eslint-disable-line
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';
}
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}`;
}
});
console.info(`Found latest release${prerelease ? ' (including prereleases)' : ''}: ${latestRelease.tag_name.cyan}`);
let targetAsset = possibleAssets[0];
if (possibleAssets.length > 1) {
if (chooseAsset) {
targetAsset = await Promise.resolve(chooseAsset(possibleAssets));
} else if (interactive) {
const choices = [];
possibleAssets.forEach((asset) => {
choices.push({ name: asset.name, value: asset.id });
});
const { assetID } = await inquirer.createPromptModule()({
type: 'list',
name: 'assetID',
message: 'Multiple potential assets found, please choose one from the list below:'.cyan,
choices,
});
targetAsset = possibleAssets.find(asset => asset.id === assetID);
} else {
// eslint-disable-next-line no-throw-literal
throw 'expected a chooseAsset function to be provided but it was not';
}
}
const tmpdir = path.resolve(os.tmpdir(), 'forge-install');
const pathSafeRepo = repo.replace(/[/\\]/g, '-');
const filename = `${pathSafeRepo}-${latestRelease.tag_name}-${targetAsset.name}`;
const fullFilePath = path.resolve(tmpdir, filename);
Eif (!await fs.exists(fullFilePath) || (await fs.stat(fullFilePath)).size !== targetAsset.size) {
await fs.mkdirs(tmpdir);
const nuggetOpts = {
target: filename,
dir: tmpdir,
resume: true,
strictSSL: true,
};
await pify(nugget)(targetAsset.browser_download_url, nuggetOpts);
}
await asyncOra('Installing Application', async (installSpinner) => {
const installActions = {
win32: {
'.exe': win32ExeInstaller,
},
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);
});
};
|