forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
200 lines (169 loc) · 4.67 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
import fs from 'node:fs';
import path from 'node:path';
import colors from 'kleur';
import ts from 'typescript';
import MagicString from 'magic-string';
import { execFileSync, execSync } from 'node:child_process';
/** @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, '/');
}