-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.js
More file actions
121 lines (108 loc) · 4.02 KB
/
Copy pathinstall.js
File metadata and controls
121 lines (108 loc) · 4.02 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
const os = require('os');
const path = require('path');
const fs = require('fs');
const https = require('https');
const { execSync } = require('child_process');
const unzipper = require('unzipper');
// 判定用関数
function getPlatformArch() {
const platform = os.platform();
const arch = os.arch();
// 必要に応じてマッピング
return { platform, arch };
}
// Github ReleaseのURLを組み立てる
function getDownloadUrl({ platform, arch }) {
const version = 'v1.11-2025.11.05';
let filename = '';
if (platform === 'win32' && arch === 'x64') {
filename = 'openjtalk-windows-x64.zip';
} else if (platform === 'linux' && arch === 'x64') {
filename = 'openjtalk-linux-x64.zip';
} else if (platform === 'darwin' && arch === 'x64') {
filename = 'openjtalk-macos-x64.zip';
} else {
throw new Error('Unsupported platform or architecture');
}
return `https://github.com/n-soukun/openjtalk.exe/releases/download/${version}/${filename}`;
}
// ファイルをダウンロード
function downloadFile(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
function doRequest(currentUrl, redirectCount = 0) {
if (redirectCount > 5) {
return reject(new Error('Too many redirects'));
}
https
.get(currentUrl, (response) => {
if (
response.statusCode === 302 ||
response.statusCode === 301
) {
const redirectUrl = response.headers.location;
if (!redirectUrl) {
return reject(
new Error('Redirect with no location header')
);
}
// Follow redirect
doRequest(redirectUrl, redirectCount + 1);
} else if (response.statusCode !== 200) {
return reject(
new Error(
`Failed to get '${currentUrl}' (${response.statusCode})`
)
);
} else {
response.pipe(file);
file.on('finish', () => file.close(resolve));
}
})
.on('error', (err) => {
fs.unlink(dest, () => reject(err));
});
}
doRequest(url);
});
}
// Zipを展開
function extractZip(zipPath, extractTo) {
return fs
.createReadStream(zipPath)
.pipe(unzipper.Extract({ path: extractTo }))
.promise();
}
// binフォルダーにコピー
function copyToBin(srcDir, binDir) {
if (!fs.existsSync(binDir)) fs.mkdirSync(binDir);
fs.readdirSync(srcDir).forEach((file) => {
const srcFile = path.join(srcDir, file);
const destFile = path.join(binDir, file);
fs.copyFileSync(srcFile, destFile);
});
}
// メイン処理
(async () => {
try {
const { platform, arch } = getPlatformArch();
const url = getDownloadUrl({ platform, arch });
const zipPath = path.join(__dirname, 'download.zip');
const extractDir = path.join(__dirname, 'extracted');
const binDir = path.join(__dirname, 'bin');
console.log(`Downloading from ${url}...`);
await downloadFile(url, zipPath);
console.log('Extracting zip...');
await extractZip(zipPath, extractDir);
console.log('Copying to bin...');
copyToBin(extractDir, binDir);
// 後始末
fs.unlinkSync(zipPath);
fs.rmSync(extractDir, { recursive: true, force: true });
console.log('Done!');
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
})();