-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend-js-extension.js
More file actions
executable file
·80 lines (67 loc) · 2.46 KB
/
Copy pathappend-js-extension.js
File metadata and controls
executable file
·80 lines (67 loc) · 2.46 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
#!/usr/bin/env node
import * as fs from 'fs/promises';
import * as path from 'path';
// Regex to find @JSImport annotations
const jsImportRegex = /@JSImport\("([^"]+)"([^)]*)\)/g;
async function processFile(filePath) {
try {
// Read file content
const content = await fs.readFile(filePath, 'utf8');
// Replace @JSImport paths without any extension
const updatedContent = content.replace(jsImportRegex, (match, importPath, rest) => {
// Extract the last part of the path (after the last slash or the whole path if no slash)
const lastSegment = importPath.split('/').pop();
// Check if the last segment has an extension (contains a period)
const hasExtension = lastSegment.includes('.');
// Only append .js if:
// 1. It doesn't already have any extension
// 2. It's not a namespace import (ending with /)
// 3. It contains "/dist/" or starts with "dist/"
if (!hasExtension &&
!importPath.endsWith('/') &&
(importPath.includes('/dist/') || importPath.startsWith('dist/'))) {
return `@JSImport("${importPath}.js"${rest})`;
}
return match;
});
// Only write if content was changed
if (content !== updatedContent) {
await fs.writeFile(filePath, updatedContent);
console.log(`Updated: ${filePath}`);
}
} catch (error) {
console.error(`Error processing file ${filePath}:`, error);
}
}
async function traverseDirectory(dirPath) {
try {
const entries = await fs.readdir(dirPath);
for (const entry of entries) {
const entryPath = path.join(dirPath, entry);
const stats = await fs.stat(entryPath);
if (stats.isDirectory()) {
// Recursively process subdirectories
await traverseDirectory(entryPath);
} else if (stats.isFile() && entryPath.endsWith('.scala')) {
// Process .scala files
await processFile(entryPath);
}
}
} catch (error) {
console.error(`Error traversing directory ${dirPath}:`, error);
}
}
async function main() {
// Get target directory from command line argument or use default
const targetDir = process.argv[2] || './ui5-webcomponents';
console.log(`Processing .scala files in ${targetDir}...`);
console.log(`Only appending .js to imports from 'dist/' paths`);
try {
await traverseDirectory(targetDir);
console.log('Done!');
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
main();