-
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathelectron-version.ts
More file actions
173 lines (151 loc) · 4.8 KB
/
electron-version.ts
File metadata and controls
173 lines (151 loc) · 4.8 KB
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
169
170
171
172
173
import path from 'node:path';
import debug from 'debug';
import findUp from 'find-up';
import fs from 'fs-extra';
import semver from 'semver';
const d = debug('electron-forge:electron-version');
const electronPackageNames = ['electron-nightly', 'electron'];
type PackageJSONWithDeps = {
devDependencies?: Record<string, string>;
dependencies?: Record<string, string>;
};
function findElectronDep(dep: string): boolean {
return electronPackageNames.includes(dep);
}
async function findAncestorNodeModulesPath(
dir: string,
packageName: string,
): Promise<string | undefined> {
d('Looking for a lock file to indicate the root of the repo');
const lockPath = await findUp(
[
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'bun.lock',
'bun.lockb',
],
{ cwd: dir, type: 'file' },
);
if (lockPath) {
d(`Found lock file: ${lockPath}`);
const nodeModulesPath = path.join(
path.dirname(lockPath),
'node_modules',
packageName,
);
if (await fs.pathExists(nodeModulesPath)) {
return nodeModulesPath;
}
}
return Promise.resolve(undefined);
}
async function determineNodeModulesPath(
dir: string,
packageName: string,
): Promise<string | undefined> {
const nodeModulesPath: string | undefined = path.join(
dir,
'node_modules',
packageName,
);
if (await fs.pathExists(nodeModulesPath)) {
return nodeModulesPath;
}
return findAncestorNodeModulesPath(dir, packageName);
}
export class PackageNotFoundError extends Error {
constructor(packageName: string, dir: string) {
super(
`Cannot find the package "${packageName}". Perhaps you need to run install it in "${dir}"?`,
);
}
}
function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
if (!packageJSON.devDependencies) {
throw new Error('package.json for app does not have any devDependencies');
}
// Why: checked above
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const packageName = electronPackageNames.find(
(pkg) => packageJSON.devDependencies![pkg],
);
if (packageName === undefined) {
throw new Error('Could not find any Electron packages in devDependencies');
}
return packageName;
}
async function getElectronPackageJSONPath(
dir: string,
packageName: string,
): Promise<string | undefined> {
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
if (!nodeModulesPath) {
throw new PackageNotFoundError(packageName, dir);
}
const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json');
if (await fs.pathExists(electronPackageJSONPath)) {
return electronPackageJSONPath;
}
return undefined;
}
export async function getElectronModulePath(
dir: string,
packageJSON: PackageJSONWithDeps,
): Promise<string | undefined> {
const moduleName = getElectronModuleName(packageJSON);
const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName);
if (packageJSONPath) {
return path.dirname(packageJSONPath);
}
return undefined;
}
export async function getElectronVersion(
dir: string,
packageJSON: PackageJSONWithDeps,
): Promise<string> {
const packageName = getElectronModuleName(packageJSON);
// Why: checked in getElectronModuleName
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let version = packageJSON.devDependencies![packageName];
if (!semver.valid(version)) {
// It's not an exact version, find it in the actual module
const electronPackageJSONPath = await getElectronPackageJSONPath(
dir,
packageName,
);
if (electronPackageJSONPath) {
const electronPackageJSON = await fs.readJson(electronPackageJSONPath);
version = electronPackageJSON.version;
} else {
throw new PackageNotFoundError(packageName, dir);
}
}
return version;
}
export function updateElectronDependency(
packageJSON: PackageJSONWithDeps,
dev: string[],
exact: string[],
): [string[], string[]] {
const alteredDev = ([] as string[]).concat(dev);
let alteredExact = ([] as string[]).concat(exact);
// Why: checked in getElectronModuleName
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (Object.keys(packageJSON.devDependencies!).find(findElectronDep)) {
alteredExact = alteredExact.filter((dep) => dep !== 'electron');
} else if (packageJSON.dependencies) {
const electronKey = Object.keys(packageJSON.dependencies).find(
findElectronDep,
);
if (electronKey) {
alteredExact = alteredExact.filter((dep) => dep !== 'electron');
d(`Moving ${electronKey} from dependencies to devDependencies`);
alteredDev.push(
`${electronKey}@${packageJSON.dependencies[electronKey]}`,
);
delete packageJSON.dependencies[electronKey];
}
}
return [alteredDev, alteredExact];
}