-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
202 lines (189 loc) · 7.63 KB
/
Copy pathbuild.js
File metadata and controls
202 lines (189 loc) · 7.63 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const esbuild = require('esbuild');
const { exec, spawnSync } = 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 buildTSapi() {
esbuild.build({
entryPoints: ['./src/web/api.ts'], // エントリーポイント
tsconfig: './tsconfig.json', // tsconfig.jsonのパス
outfile: './src/web/api.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));
}
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(args) {
// try {
// const { stdout, stderr } = await execPromise('cargo test --features web');
// process.stdout.write(stdout);
// if (stderr) {
// process.stderr.write(stderr);
// }
// } catch (error) {
// // エラーオブジェクトから終了コードを取得
// process.stderr.write(error.message+"\n");
// process.stderr.write(error.statusCode+"\n");
// throw error.statusCode;
// }
try {
let flag = args.includes("--release") ? "--release" : "--debug";
// Execute the 'wasm-pack' command synchronously with the desired options
const result = spawnSync('wasm-pack', [
'build',
flag,
'--target', 'web',
'--no-default-features',
'--features', 'web'
], {
stdio: 'inherit' // Inherit parent's stdio to support colored output
});
// Check for errors in execution
if (result.error) {
console.error(`Error: ${result.error.message}`);
process.exit(1); // Exit with error code 1 if there was an error
} else if (result.status !== 0) {
console.error(`Error: wasm-pack exited with code ${result.status}`);
process.exit(result.status);
}
console.log('Wasm build complete!');
return true;
} catch (error) {
// エラーオブジェクトから終了コードを取得
process.stderr.write(error.message+"\n");
process.stderr.write(error.statusCode+"\n");
throw error.statusCode;
}
}
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 copyDirectory(source, destination) {
await fs.promises.mkdir(destination, { recursive: true });
const entries = await fs.promises.readdir(source, { withFileTypes: true });
for (let entry of entries) {
const srcPath = path.join(source, entry.name);
const destPath = path.join(destination, entry.name);
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else {
await fs.promises.copyFile(srcPath, destPath);
}
}
}
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() {
{ // Cargo.tomlのバージョンを自動で今日の日付にする
const today = new Date(new Date().toLocaleString("en-US", { timeZone: "Asia/Tokyo" })); // JST
const year = today.getFullYear() % 100;
const month = today.getMonth() + 1;
const day = today.getDate();
const version = `${year}.${month}.${day}`;
const cargoPath = path.join(__dirname, 'Cargo.toml');
let content = fs.readFileSync(cargoPath, { encoding: 'utf-8' });
const newContent = content.replace(/version\s*=\s*".*?"/, `version = "${version}"`);
fs.writeFileSync(cargoPath, newContent, { encoding: 'utf-8' });
}
const args = process.argv.slice(2);
makedir();
await buildTSapi();
if (!args.includes("-tsonly")) {
await buildRust(args);
}
await copyFiles([
[
"./pkg/typing_lib.js",
"./src/web/typing_lib.js"
],
[
"./pkg/typing_lib.d.ts",
"./src/web/typing_lib.d.ts"
],
[
"./pkg/typing_lib_bg.wasm",
"./dist/typing_lib_bg.wasm"
],
]);
await copyDirectory('./pkg/snippets', './src/web/snippets');
await getFile('./src/web/cdom.ts','https://raw.githubusercontent.com/neknaj/cDom/50a65673454c7286830f0d131f0512ddf46a3844/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"
],
[
"./src/web/index.css",
"./dist/index.css"
],
]);
// await copyDirectory('./examples', './dist/examples');
// await copyDirectory('./layouts', './dist/layouts');
}
main()