-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.js
More file actions
58 lines (47 loc) · 1.72 KB
/
Copy pathpreprocess.js
File metadata and controls
58 lines (47 loc) · 1.72 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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const srcDir = path.join(__dirname, 'src');
function processFiles(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filepath = path.join(dir, file);
const stat = fs.statSync(filepath);
if (stat.isDirectory() && file !== 'node_modules') {
processFiles(filepath);
} else if (file.endsWith('.ts') && !file.endsWith('.spec.ts')) {
let content = fs.readFileSync(filepath, 'utf-8');
const original = content;
// Strip .ts extensions ONLY for relative imports (starting with . or /)
content = content.replace(/from\s+['"]([./][^'"]*).ts['"]/g, "from '$1'");
if (content !== original) {
fs.writeFileSync(filepath, content, 'utf-8');
}
}
});
}
function restoreFiles(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filepath = path.join(dir, file);
const stat = fs.statSync(filepath);
if (stat.isDirectory() && file !== 'node_modules') {
restoreFiles(filepath);
} else if (file.endsWith('.ts') && !file.endsWith('.spec.ts')) {
let content = fs.readFileSync(filepath, 'utf-8');
// Restore .ts extensions ONLY for relative imports (starting with . or /)
content = content.replace(/from\s+['"]([./][^'"]*)['"]/g, "from '$1.ts'");
fs.writeFileSync(filepath, content, 'utf-8');
}
});
}
if (process.argv[2] === 'restore') {
restoreFiles(srcDir);
console.log('✓ Restored .ts extensions');
} else {
processFiles(srcDir);
console.log('✓ Stripped .ts extensions temporarily');
}