-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
209 lines (184 loc) · 5.61 KB
/
library.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import fs from 'node:fs';
import https from 'node:https';
import path from 'node:path';
import url from 'node:url';
import { parse as semverParse } from 'semver';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
let originalManifestIndentation = 2;
let originalManifestEOL = '\n';
async function fileExists (file) {
let exists = true;
try {
await fs.promises.stat(file);
} catch {
exists = false;
}
return exists;
}
function getVersions () {
return new Promise((resolve, reject) => {
const request = https.get('https://nwjs.io/versions.json', (response) => {
response.setEncoding('utf8');
let responseBody = '';
response.on('data', (chunk) => {
responseBody += chunk;
});
response.on('end', () => {
try {
responseBody = JSON.parse(responseBody);
resolve(responseBody);
} catch (error) {
reject(error);
}
});
});
request.on('error', (error) => {
reject(error);
});
});
}
function getLocalNwManifestPath () {
return new Promise(async (resolve, reject) => {
const nwManifestRelativeToCwd = path.resolve(process.cwd(), 'node_modules', 'nw', 'package.json');
const nwManifestRelativeToHere = path.resolve(__dirname, '..', 'nw', 'package.json');
const cwdExists = await fileExists(nwManifestRelativeToCwd);
const hereExists = await fileExists(nwManifestRelativeToHere);
if (cwdExists) {
resolve(nwManifestRelativeToCwd);
} else if (hereExists) {
resolve(nwManifestRelativeToHere);
} else {
reject(new Error('Could not locate nw node module manifest.'));
}
});
}
function getLocalNwManifest () {
return new Promise(async (resolve, reject) => {
try {
const nwManifest = await getLocalNwManifestPath();
let data = await fs.promises.readFile(nwManifest, { encoding: 'binary' });
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
});
}
function getLocalNWVersion () {
return new Promise(async (resolve, reject) => {
const nwManifest = await getLocalNwManifest();
let localNwVersion = nwManifest?.version || '';
const parsedVersion = semverParse(localNwVersion);
if (parsedVersion) {
resolve([
parsedVersion.major,
parsedVersion.minor,
parsedVersion.patch
].join('.'));
} else {
reject(new Error('Unable to get local NW.js version'));
}
});
}
function getCorrectNodeVersion () {
return new Promise(async (resolve, reject) => {
try {
const localNwVersion = await getLocalNWVersion();
const allNwVersions = await getVersions();
const match = allNwVersions.versions.find(function (version) {
// 'v0.82.0' === 'v' + '0.82.0'
return version.version === 'v' + localNwVersion;
});
if (!match) {
throw 'No matching NW.js version found';
}
const nodeVersion = match?.components?.node;
if (nodeVersion) {
resolve(nodeVersion);
} else {
throw 'No Node version found';
}
} catch (error) {
reject(error);
}
});
}
function determineOriginalManifestIndentation (data) {
data = data.trim();
data = data.replaceAll('\r\n', '\n');
if (data[0] !== '{' || data[1] !== '\n') {
return;
}
let first = data[2];
let second = data[3];
let third = data[4];
let fourth = data[5];
if (first === '\t') {
originalManifestIndentation = '\t';
} else if (first + second + third + fourth === ' ') {
originalManifestIndentation = 4;
} else {
originalManifestIndentation = 2;
}
}
function determinOriginalEOL (data) {
if (data.includes('\r\n')) {
originalManifestEOL = '\r\n';
} else {
originalManifestEOL = '\n';
}
}
function getManifestPath () {
return new Promise(async (resolve, reject) => {
const manifestRelativeToCwd = path.resolve(process.cwd(), 'package.json');
const manifestRelativeToHere = path.resolve(__dirname, '..', '..', 'package.json');
const cwdExists = await fileExists(manifestRelativeToCwd);
const hereExists = await fileExists(manifestRelativeToHere);
if (cwdExists) {
resolve(manifestRelativeToCwd);
} else if (hereExists) {
resolve(manifestRelativeToHere);
} else {
reject(new Error('Could not locate your manifest.'));
}
});
}
function getManifest () {
return new Promise(async (resolve, reject) => {
try {
const manifest = await getManifestPath();
let data = await fs.promises.readFile(manifest, { encoding: 'binary' });
determineOriginalManifestIndentation(String(data));
determinOriginalEOL(String(data));
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
});
}
function getManifestWithUpdatedVoltaObject () {
return new Promise(async (resolve, reject) => {
try {
const manifest = await getManifest();
const nodeVersion = await getCorrectNodeVersion();
manifest.volta = manifest.volta || {};
manifest.volta.node = nodeVersion;
resolve(manifest);
} catch (error) {
reject(error);
}
});
}
export async function run () {
try {
const manifestPath = await getManifestPath();
let mutatedManifest = await getManifestWithUpdatedVoltaObject();
mutatedManifest = JSON.stringify(mutatedManifest, null, originalManifestIndentation);
mutatedManifest = mutatedManifest.replaceAll('\r\n', '\n').replaceAll('\n', originalManifestEOL);
mutatedManifest = mutatedManifest + originalManifestEOL;
await fs.promises.writeFile(manifestPath, mutatedManifest);
} catch (error) {
console.error(error);
}
}