-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspiler.config.js
93 lines (78 loc) · 2.69 KB
/
transpiler.config.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
const fs = require('fs-extra');
const path = require('path');
const babel = require('@babel/core');
// Get the directory of the current script
const scriptDir = __dirname;
// Step 1: Transpile the source files using Babel programmatically
function transpileFile(inputPath, outputPath) {
try {
const result = babel.transformFileSync(inputPath, {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
});
fs.ensureFileSync(outputPath);
fs.writeFileSync(outputPath, result.code, 'utf8');
// console.log(`Transpiled: ${inputPath} -> ${outputPath}`);
} catch (error) {
console.error(`Error transpiling file ${inputPath}:`, error.message);
}
}
function transpileDirectory(srcDir, libDir) {
fs.readdirSync(srcDir).forEach(file => {
const srcFilePath = path.join(srcDir, file);
// Skip 'RootBackup.jsx' files
if (path.basename(file) === 'RootBackup.jsx') {
return;
}
const libFilePath = path.join(libDir, file.replace(/\.jsx?$/, '.js'));
if (fs.statSync(srcFilePath).isDirectory()) {
transpileDirectory(srcFilePath, libFilePath);
} else if (/\.(js|jsx|ts|tsx)$/.test(file)) {
transpileFile(srcFilePath, libFilePath);
} else {
fs.copySync(srcFilePath, libFilePath);
}
});
}
// Step 2: Delete corresponding .jsx files in the src directory
function deleteJSXFiles(libDir, srcDir) {
if (!fs.existsSync(srcDir)) return;
fs.readdirSync(libDir).forEach(file => {
const libFilePath = path.join(libDir, file);
const srcFilePathJSX = path.join(srcDir, file.replace(/\.js$/, '.jsx'));
if (fs.statSync(libFilePath).isDirectory()) {
deleteJSXFiles(libFilePath, path.join(srcDir, file));
} else if (file.endsWith('.js') && fs.existsSync(srcFilePathJSX)) {
// Skip 'RootBackup.js' files
if (path.basename(file) === 'RootBackup.jsx') {
} else {
fs.removeSync(srcFilePathJSX);
// console.log(`Deleted: ${srcFilePathJSX}`);
}
}
});
}
// Step 3: Copy transpiled files from lib to src
function copyLibToSrc() {
const srcDir = path.join(scriptDir, 'src');
const libDir = path.join(scriptDir, 'lib');
deleteJSXFiles(libDir, srcDir);
// Copy the transpiled files from lib to src, excluding 'RootBackup.js'
fs.copySync(libDir, srcDir, {
filter: (src, dest) => {
if (path.basename(src) === 'RootBackup.jsx') {
return false;
}
return true;
}
});
// console.log('Files moved from lib to src.');
}
// Main function to run the steps
function main() {
const srcDir = path.join(scriptDir, 'src');
const libDir = path.join(scriptDir, 'lib');
transpileDirectory(srcDir, libDir);
copyLibToSrc();
}
// Execute the script
main();