This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
128 lines (117 loc) · 4.21 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { writeJSON } from 'fs-extra';
import { join } from 'path';
import logger from './cli/logger';
import * as ask from './cli/ask';
import {
initialize, collectArgumentsFromCli,
} from './cli/args';
import buildArtifact from './builder';
import exitProcess from './util/system';
import { formatsForOs } from './util/values';
import { getSpinner, setSpinnerState } from './util/spinner';
import downloadAndResizeIcon from './util/icon';
function matchingOsForFormat(givenFormat: string): undefined | string {
let indexOf = -1;
const osFormats = Object.values(formatsForOs);
const osNames = Object.getOwnPropertyNames(formatsForOs);
osFormats.forEach((formatForOs, index) => {
formatForOs.forEach((formatInList) => {
if (formatInList === givenFormat) { indexOf = index; }
});
});
return (indexOf === 0) ? undefined : osNames[indexOf];
}
async function build(choices: Electronify.Choices): Promise<void> {
const spinnerInstance = getSpinner({
discardStdin: false,
text: 'Generating package..',
});
let state = false;
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-empty-function
process.stdout.write = (): void => {};
try {
spinnerInstance.start();
logger.debug(choices, 'Settings:');
await buildArtifact(choices);
spinnerInstance.text = 'Successfully finished building the artifact';
state = true;
} catch (err) {
spinnerInstance.text = 'An error occurred while building the artifact!';
if (logger.level === 'debug') {
logger.error(err);
}
} finally {
process.stdout.write = originalStdoutWrite;
setSpinnerState(spinnerInstance, state);
}
}
async function collectChoices(cliArgs: Electronify.Args, osForGivenFormat: string|undefined, skipChoices: boolean): Promise<Electronify.Choices> {
let urlOfChoice: any;
let osOfChoice: any;
let includeGenericFormats: any;
let formatOfChoice: any;
let architectureOfChoice: any;
let nameOfChoice: any;
let includeCustomIcon: any;
let iconOfChoice: any;
if (skipChoices) {
urlOfChoice = cliArgs.url || await ask.forURL();
const { platform } = process;
await downloadAndResizeIcon(urlOfChoice, platform);
} else {
urlOfChoice = cliArgs.url || await ask.forURL();
osOfChoice = osForGivenFormat || cliArgs.os || await ask.forOS();
includeGenericFormats = (cliArgs.format || osOfChoice === 'generic') ? false : await ask.forGenericFormats();
formatOfChoice = cliArgs.format || await ask.forFormat(osOfChoice, includeGenericFormats);
architectureOfChoice = cliArgs.arch || await ask.forArch();
nameOfChoice = cliArgs.name || await ask.forName();
includeCustomIcon = !cliArgs.iconPath ? await ask.forCustomIcon() : false;
if (includeCustomIcon) {
iconOfChoice = osOfChoice === 'linux' ? await ask.forIconFolder() : await ask.forIconPath();
} else {
await downloadAndResizeIcon(urlOfChoice, osOfChoice);
}
}
const useAutoMode = cliArgs.auto as unknown as boolean;
const choices: Electronify.Choices = {
auto: useAutoMode,
appName: nameOfChoice,
architecture: architectureOfChoice,
format: formatOfChoice,
iconPath: iconOfChoice,
os: osOfChoice,
url: urlOfChoice,
};
return choices;
}
export default async function execute(): Promise<void> {
let osForGivenFormat;
let skipChoices = false;
try {
await initialize(process.argv);
const argsFromCli = await collectArgumentsFromCli([
'auto', 'verbose', 'url', 'os', 'format', 'arch', 'name', 'icon',
]);
if (argsFromCli.verbose) {
logger.level = 'debug';
process.env.DEBUG = 'electron-builder';
}
if (argsFromCli.format) {
osForGivenFormat = matchingOsForFormat(argsFromCli.format);
}
if (argsFromCli.auto) {
skipChoices = true;
}
const choices = await collectChoices(argsFromCli, osForGivenFormat, skipChoices);
const configPath = join(__dirname, 'app/config.json');
await writeJSON(configPath, { url: choices.url });
await build(choices);
} catch (error) {
logger.error('An unexpected error occurred!');
logger.error(error);
exitProcess(1);
}
}