forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
374 lines (322 loc) · 9.83 KB
/
utils.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import colors from 'kleur';
import MagicString from 'magic-string';
import { execFileSync, execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import semver from 'semver';
import ts from 'typescript';
/** @param {string} message */
export function bail(message) {
console.error(colors.bold().red(message));
process.exit(1);
}
/** @param {string} file */
export function relative(file) {
return path.relative('.', file);
}
/**
*
* @param {string} file
* @param {string} renamed
* @param {string} content
* @param {boolean} use_git
*/
export function move_file(file, renamed, content, use_git) {
if (use_git) {
execFileSync('git', ['mv', file, renamed]);
} else {
fs.unlinkSync(file);
}
fs.writeFileSync(renamed, content);
}
/**
* @param {string} contents
* @param {string} indent
*/
export function comment(contents, indent) {
return contents.replace(new RegExp(`^${indent}`, 'gm'), `${indent}// `);
}
/** @param {string} content */
export function dedent(content) {
const indent = guess_indent(content);
if (!indent) return content;
/** @type {string[]} */
const substitutions = [];
try {
const ast = ts.createSourceFile(
'filename.ts',
content,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS
);
const code = new MagicString(content);
/** @param {ts.Node} node */
function walk(node) {
if (ts.isTemplateLiteral(node)) {
let pos = node.pos;
while (/\s/.test(content[pos])) pos += 1;
code.overwrite(pos, node.end, `____SUBSTITUTION_${substitutions.length}____`);
substitutions.push(node.getText());
}
node.forEachChild(walk);
}
ast.forEachChild(walk);
return code
.toString()
.replace(new RegExp(`^${indent}`, 'gm'), '')
.replace(/____SUBSTITUTION_(\d+)____/g, (match, index) => substitutions[index]);
} catch {
// as above — ignore this edge case
return content;
}
}
/** @param {string} content */
export function guess_indent(content) {
const lines = content.split('\n');
const tabbed = lines.filter((line) => /^\t+/.test(line));
const spaced = lines.filter((line) => /^ {2,}/.test(line));
if (tabbed.length === 0 && spaced.length === 0) {
return null;
}
// More lines tabbed than spaced? Assume tabs, and
// default to tabs in the case of a tie (or nothing
// to go on)
if (tabbed.length >= spaced.length) {
return '\t';
}
// Otherwise, we need to guess the multiple
const min = spaced.reduce((previous, current) => {
const count = /^ +/.exec(current)?.[0].length ?? 0;
return Math.min(count, previous);
}, Infinity);
return ' '.repeat(min);
}
/**
* @param {string} content
* @param {number} offset
*/
export function indent_at_line(content, offset) {
const substr = content.substring(content.lastIndexOf('\n', offset) + 1, offset);
return /\s*/.exec(substr)?.[0] ?? '';
}
/**
* @param {string} content
* @param {string} except
*/
export function except_str(content, except) {
const start = content.indexOf(except);
const end = start + except.length;
return content.substring(0, start) + content.substring(end);
}
/**
* @returns {boolean} True if git is installed
*/
export function check_git() {
let use_git = false;
let dir = process.cwd();
do {
if (fs.existsSync(path.join(dir, '.git'))) {
use_git = true;
break;
}
} while (dir !== (dir = path.dirname(dir)));
if (use_git) {
try {
const status = execSync('git status --porcelain', { stdio: 'pipe' }).toString();
if (status) {
const message =
'Your git working directory is dirty — we recommend committing your changes before running this migration.\n';
console.log(colors.bold().red(message));
}
} catch {
// would be weird to have a .git folder if git is not installed,
// but always expect the unexpected
const message =
'Could not detect a git installation. If this is unexpected, please raise an issue: https://github.com/sveltejs/kit.\n';
console.log(colors.bold().red(message));
use_git = false;
}
}
return use_git;
}
/**
* Get a list of all files in a directory
* @param {string} cwd - the directory to walk
* @param {boolean} [dirs] - whether to include directories in the result
*/
export function walk(cwd, dirs = false) {
/** @type {string[]} */
const all_files = [];
/** @param {string} dir */
function walk_dir(dir) {
const files = fs.readdirSync(path.join(cwd, dir));
for (const file of files) {
const joined = path.join(dir, file);
const stats = fs.statSync(path.join(cwd, joined));
if (stats.isDirectory()) {
if (dirs) all_files.push(joined);
walk_dir(joined);
} else {
all_files.push(joined);
}
}
}
return walk_dir(''), all_files;
}
/** @param {string} str */
export function posixify(str) {
return str.replace(/\\/g, '/');
}
/**
* @param {string} content
* @param {Array<[string, string, string?, ('dependencies' | 'devDependencies')?]>} updates
*/
export function update_pkg(content, updates) {
const indent = content.split('\n')[1].match(/^\s+/)?.[0] || ' ';
const pkg = JSON.parse(content);
/**
* @param {string} name
* @param {string} version
* @param {string} [additional]
* @param {'dependencies' | 'devDependencies' | undefined} [insert]
*/
function update_pkg(name, version, additional = '', insert) {
/**
* @param {string} type
*/
const updateVersion = (type) => {
const existingRange = pkg[type]?.[name];
if (
existingRange &&
semver.validRange(existingRange) &&
!semver.subset(existingRange, version)
) {
// Check if the new version range is an upgrade
const minExistingVersion = semver.minVersion(existingRange);
const minNewVersion = semver.minVersion(version);
if (minExistingVersion && minNewVersion && semver.gt(minNewVersion, minExistingVersion)) {
log_migration(`Updated ${name} to ${version}`);
pkg[type][name] = version;
}
}
};
updateVersion('dependencies');
updateVersion('devDependencies');
if (insert && !pkg[insert]?.[name]) {
if (!pkg[insert]) pkg[insert] = {};
// Insert the property in sorted position without adjusting other positions so diffs are easier to read
const sorted_keys = Object.keys(pkg[insert]).sort();
const index = sorted_keys.findIndex((key) => name.localeCompare(key) === -1);
const insert_index = index !== -1 ? index : sorted_keys.length;
const new_properties = Object.entries(pkg[insert]);
new_properties.splice(insert_index, 0, [name, version]);
pkg[insert] = Object.fromEntries(new_properties);
log_migration(`Added ${name} version ${version} ${additional}`);
}
}
for (const update of updates) {
update_pkg(...update);
}
return JSON.stringify(pkg, null, indent);
}
const logged_migrations = new Set();
/**
* @param {import('ts-morph').SourceFile} source
* @param {string} text
*/
export function log_on_ts_modification(source, text) {
let logged = false;
const log = () => {
if (!logged) {
logged = true;
log_migration(text);
}
};
source.onModified(log);
return () => source.onModified(log, false);
}
/** @param {string} text */
export function log_migration(text) {
if (logged_migrations.has(text)) return;
console.log(text);
logged_migrations.add(text);
}
/**
* Parses the scripts contents and invoked `transform_script_code` with it, then runs the result through `transform_svelte_code`.
* The result is written back to disk.
* @param {string} file_path
* @param {(code: string, is_ts: boolean, file_path: string) => string} transform_script_code
* @param {(code: string, file_path: string) => string} transform_svelte_code
*/
export function update_svelte_file(file_path, transform_script_code, transform_svelte_code) {
try {
const content = fs.readFileSync(file_path, 'utf-8');
const updated = content.replace(
/<script([^]*?)>([^]+?)<\/script>(\n*)/g,
(_match, attrs, contents, whitespace) => {
return `<script${attrs}>${transform_script_code(
contents,
(attrs.includes('lang=') || attrs.includes('type=')) &&
(attrs.includes('ts') || attrs.includes('typescript')),
file_path
)}</script>${whitespace}`;
}
);
fs.writeFileSync(file_path, transform_svelte_code(updated, file_path), 'utf-8');
} catch (e) {
console.error(`Error updating ${file_path}:`, e);
}
}
/**
* Reads the file and invokes `transform_code` with its contents. The result is written back to disk.
* @param {string} file_path
* @param {(code: string, is_ts: boolean, file_path: string) => string} transform_code
*/
export function update_js_file(file_path, transform_code) {
try {
const content = fs.readFileSync(file_path, 'utf-8');
const updated = transform_code(content, file_path.endsWith('.ts'), file_path);
fs.writeFileSync(file_path, updated, 'utf-8');
} catch (e) {
console.error(`Error updating ${file_path}:`, e);
}
}
/**
* Updates the tsconfig/jsconfig.json file with the provided function.
* @param {(content: string) => string} update_tsconfig_content
*/
export function update_tsconfig(update_tsconfig_content) {
const file = fs.existsSync('tsconfig.json')
? 'tsconfig.json'
: fs.existsSync('jsconfig.json')
? 'jsconfig.json'
: null;
if (file) {
fs.writeFileSync(file, update_tsconfig_content(fs.readFileSync(file, 'utf8')));
}
}
/** @param {string | URL} test_file */
export function read_samples(test_file) {
const markdown = fs.readFileSync(test_file, 'utf8').replaceAll('\r\n', '\n');
const samples = markdown
.split(/^##/gm)
.slice(1)
.map((block) => {
const description = block.split('\n')[0];
const before = /```(js|ts|svelte) before\n([^]*?)\n```/.exec(block);
const after = /```(js|ts|svelte) after\n([^]*?)\n```/.exec(block);
const match = /> file: (.+)/.exec(block);
return {
description,
before: before ? before[2] : '',
after: after ? after[2] : '',
filename: match?.[1],
solo: block.includes('> solo')
};
});
if (samples.some((sample) => sample.solo)) {
return samples.filter((sample) => sample.solo);
}
return samples;
}