-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
58 lines (51 loc) · 1.49 KB
/
init.js
File metadata and controls
58 lines (51 loc) · 1.49 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
#!/usr/bin/env node
/**
* Universal deploy.yml initializer
* Works on Windows, macOS, and Linux
*
* Usage: node init-deploy.js
*/
import { mkdir } from 'fs/promises';
import { createWriteStream } from 'fs';
import { get } from 'https';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const DEPLOY_URL = 'https://cdn.jsdelivr.net/gh/ThinhPhoenix/github-pages@main/.github/workflows/deploy.yml';
const OUTPUT_PATH = '.github/workflows/deploy.yml';
async function downloadFile(url, outputPath) {
return new Promise((resolve, reject) => {
const file = createWriteStream(outputPath);
get(url, (response) => {
if (response.statusCode === 301 || response.statusCode === 302) {
// Handle redirects
get(response.headers.location, (redirectResponse) => {
redirectResponse.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
}).on('error', reject);
} else {
response.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
}
}).on('error', (err) => {
unlink(outputPath, () => {});
reject(err);
});
});
}
async function main() {
try {
await mkdir('.github/workflows', { recursive: true });
await downloadFile(DEPLOY_URL, OUTPUT_PATH);
} catch (error) {
console.error('\nError:', error.message);
process.exit(1);
}
}
main();