-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
init.ts
145 lines (134 loc) · 4.71 KB
/
init.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import path from 'path';
import { safeYarnOrNpm } from '@electron-forge/core-utils';
import { ForgeTemplate } from '@electron-forge/shared-types';
import debug from 'debug';
import { Listr } from 'listr2';
import semver from 'semver';
import installDepList, { DepType, DepVersionRestriction } from '../util/install-dependencies';
import { readRawPackageJson } from '../util/read-package-json';
import { findTemplate } from './init-scripts/find-template';
import { initDirectory } from './init-scripts/init-directory';
import { initGit } from './init-scripts/init-git';
import { initNPM } from './init-scripts/init-npm';
const d = debug('electron-forge:init');
export interface InitOptions {
/**
* The path to the app to be initialized
*/
dir?: string;
/**
* Whether to use sensible defaults or prompt the user visually
*/
interactive?: boolean;
/**
* Whether to copy template CI files
*/
copyCIFiles?: boolean;
/**
* Whether to overwrite an existing directory
*/
force?: boolean;
/**
* The custom template to use. If left empty, the default template is used
*/
template?: string;
}
async function validateTemplate(template: string, templateModule: ForgeTemplate): Promise<void> {
if (!templateModule.requiredForgeVersion) {
throw new Error(`Cannot use a template (${template}) with this version of Electron Forge, as it does not specify its required Forge version.`);
}
const forgeVersion = (await readRawPackageJson(path.join(__dirname, '..', '..'))).version;
if (!semver.satisfies(forgeVersion, templateModule.requiredForgeVersion)) {
throw new Error(
`Template (${template}) is not compatible with this version of Electron Forge (${forgeVersion}), it requires ${templateModule.requiredForgeVersion}`
);
}
}
export default async ({ dir = process.cwd(), interactive = false, copyCIFiles = false, force = false, template = 'base' }: InitOptions): Promise<void> => {
d(`Initializing in: ${dir}`);
const packageManager = safeYarnOrNpm();
const runner = new Listr<{
templateModule: ForgeTemplate;
}>(
[
{
title: `Locating custom template: "${template}"`,
task: async (ctx) => {
ctx.templateModule = await findTemplate(dir, template);
},
},
{
title: 'Initializing directory',
task: async (_, task) => {
await initDirectory(dir, task, force);
await initGit(dir);
},
options: {
persistentOutput: true,
},
},
{
title: 'Preparing template',
task: async ({ templateModule }) => {
await validateTemplate(template, templateModule);
},
},
{
title: 'Initializing template',
task: async ({ templateModule }, task) => {
if (typeof templateModule.initializeTemplate === 'function') {
const tasks = await templateModule.initializeTemplate(dir, { copyCIFiles });
if (tasks) {
return task.newListr(tasks, { concurrent: false });
}
}
},
},
{
title: 'Installing template dependencies',
task: async ({ templateModule }, task) => {
return task.newListr(
[
{
title: 'Installing production dependencies',
task: async (_, task) => {
d('installing dependencies');
if (templateModule.dependencies?.length) {
task.output = `${packageManager} install ${templateModule.dependencies.join(' ')}`;
}
return await installDepList(dir, templateModule.dependencies || [], DepType.PROD, DepVersionRestriction.RANGE);
},
},
{
title: 'Installing development dependencies',
task: async (_, task) => {
d('installing devDependencies');
if (templateModule.devDependencies?.length) {
task.output = `${packageManager} install --dev ${templateModule.devDependencies.join(' ')}`;
}
await installDepList(dir, templateModule.devDependencies || [], DepType.DEV);
},
},
{
title: 'Finalizing dependencies',
task: async (_, task) => {
await initNPM(dir, task);
},
},
],
{
concurrent: false,
exitOnError: false,
}
);
},
},
],
{
concurrent: false,
rendererSilent: !interactive,
rendererFallback: Boolean(process.env.DEBUG && process.env.DEBUG.includes('electron-forge')),
}
);
await runner.run();
};