Repository navigation
Expand file tree
/
Copy pathmergeScript.js
More file actions
69 lines (57 loc) · 2.05 KB
/
Copy pathmergeScript.js
File metadata and controls
69 lines (57 loc) · 2.05 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
import fs from "fs";
import path from "path";
/**
* ディレクトリを再帰的に探索し、.ts/.js ファイルをすべて収集する
* @param {string} dir
* @returns {string[]} ファイルパス一覧
*/
function getAllScriptFiles(dir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...getAllScriptFiles(fullPath));
} else if (
entry.isFile() &&
(fullPath.endsWith(".ts")) &&
!fullPath.endsWith(".d.ts") // 型定義ファイルは除外
) {
files.push(fullPath);
}
}
return files;
}
function removeStaticImports(content) {
return content
.replace(/^\s*import\s+(?:type\s+)?[\s\S]*?\s+from\s+["'][^"']+["'];\s*/gm, "")
.replace(/^\s*import\s+["'][^"']+["'];\s*/gm, "");
}
/**
* 複数の .ts/.js ファイルを1つにまとめる(importを統一)
* @param {string} inputDir 探索フォルダ
* @param {string} outputFile 出力ファイル
*/
async function mergeTSRecursive(inputDir, outputFile) {
const files = getAllScriptFiles(inputDir);
let body = "";
for (const file of files) {
const content = fs.readFileSync(file, "utf-8");
const bodyPart = removeStaticImports(content)
.trim()
.replace(/\n{3,}/g, "\n\n"); // 空行を整理
body += `\n// ---- ${path.relative(inputDir, file)} ----\n${bodyPart}\n`;
}
const result = `import * as modlib from \"modlib";\n` +
"\n// ===============================\n" +
"// Combined TS/JS Contents\n" +
"// ===============================\n" +
body;
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, result, "utf-8");
console.log(
`✅ ${files.length} files merged successfully into:\n ${outputFile}`
);
}
// 使用例
mergeTSRecursive("./mods", "./dist/Script.ts");