generated from neknaj/tsrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
136 lines (125 loc) · 4.57 KB
/
build.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
const esbuild = require('esbuild');
const { exec } = require('child_process');
const fs = require('fs');
const util = require('util');
const path = require('path');
const https = require('https');
const execPromise = util.promisify(exec);
function makedir() {
const destPath = './dist';
const destDir = path.resolve(destPath);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
console.log('Directory created:', destDir);
}
function buildTS() {
esbuild.build({
entryPoints: ['./src/web/index.ts'], // エントリーポイント
tsconfig: './tsconfig.json', // tsconfig.jsonのパス
outfile: './dist/index.js', // 出力先
bundle: true, // 依存関係をバンドル
// minify: true, // 圧縮
minify: false, // 圧縮
sourcemap: false, // ソースマップ生成
target: ['esnext'], // トランスパイルのターゲット
loader: { '.ts': 'ts' }, // TypeScriptを処理
format: 'esm', // 出力形式をESモジュールにする
}).then(() => {
console.log('TS Build succeeded!');
}).catch(() => process.exit(1));
}
async function buildRust() {
try {
// wasm-packコマンドを実行
const { stdout, stderr } = await execPromise('wasm-pack build --target web --no-default-features --features web');
process.stdout.write(stdout);
if (stderr) {
process.stderr.write(stderr);
}
console.log('Wasm build complete!');
return true;
} catch (error) {
// エラーオブジェクトから終了コードを取得
process.stderr.write(error.message);
console.error('Exit Code:', error.code); // 終了コードを表示
return false;
}
}
function copyFiles(files) {
// 各ファイルをコピーするプロミスの配列を作成
const copyPromises = files.map((file) => {
const sourcePath = path.resolve(file[0]);
const destinationPath = path.resolve(file[1]);
return fs.promises.copyFile(sourcePath, destinationPath);
});
return Promise.all(copyPromises);
}
async function getFile(savePath,url) {
if (fs.existsSync(savePath)) return false; // 既に存在するならダウンロードしない
await new Promise((resolve, reject) => {
https.get(url, (res) => {
if (res.statusCode === 200) {
const file = fs.createWriteStream(savePath);
res.pipe(file);
file.on('finish', () => {
resolve('File downloaded and saved');
});
file.on('error', (err) => {
reject(`Error writing to file: ${err.message}`);
});
} else {
reject(`Failed to download file. Status code: ${res.statusCode}`);
}
}).on('error', (err) => {
reject(`Error: ${err.message}`);
});
});
return true;
}
async function main() {
makedir();
if (! await buildRust()) return;
await copyFiles([
[
"./pkg/circuitgame_lib.js",
"./src/web/circuitgame_lib.js"
],
[
"./pkg/circuitgame_lib.d.ts",
"./src/web/circuitgame_lib.d.ts"
],
[
"./pkg/circuitgame_lib_bg.wasm",
"./dist/circuitgame_lib_bg.wasm"
],
]);
await getFile('./src/web/cdom.ts','https://raw.githubusercontent.com/neknaj/cDom/2622d7887d1fae564e1e5b989ea38e2243e918bf/cdom_module.ts');
if (await getFile('./src/web/layout.js','https://raw.githubusercontent.com/neknaj/webSplitLayout/c7e1c52cb37a8bfbf9968b825c05a2e9924ca88e/type1/layout.js')) {
fs.readFile('./src/web/layout.js', 'utf8', (err, data) => {
const updatedData = "import { elm } from './cdom.js';\n\n" + data + "\n\nexport { initlayout };";
fs.writeFile('./src/web/layout.js', updatedData, (err) => {});
});
};
await getFile('./dist/layout.css','https://raw.githubusercontent.com/neknaj/webSplitLayout/c7e1c52cb37a8bfbf9968b825c05a2e9924ca88e/type1/layout.css');
await buildTS();
await copyFiles([
[
"./src/web/index.html",
"./dist/index.html"
],
[
"./spec/sample.ncg",
"./dist/sample.ncg"
],
[
"./spec/icon.ico",
"./dist/favicon.ico"
],
[
"./spec/icon.png",
"./dist/icon.png"
],
]);
}
main()