-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.js
More file actions
44 lines (38 loc) · 1.42 KB
/
github.js
File metadata and controls
44 lines (38 loc) · 1.42 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
import ora from "ora";
import path from 'node:path';
import fs from 'fs-extra';
import https from 'https';
import { x as tarExtract } from 'tar';
const REPO_USER = 'C3Framework';
const NORMAL_REPO_NAME = 'framework';
const THEME_REPO_NAME = 'template-theme';
const BRANCH = 'master';
export async function fetchGithub(destination, isTheme = false) {
return new Promise((resolve, reject) => {
const url = `https://codeload.github.com/${REPO_USER}/${isTheme ? THEME_REPO_NAME : NORMAL_REPO_NAME}/tar.gz/${BRANCH}`;
const tempFile = path.join(destination, 'repo.tar.gz');
const spinner = ora('Downloading template...').start();
const file = fs.createWriteStream(tempFile);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', async () => {
file.close();
spinner.succeed('Download complete. Extracting files...');
try {
await tarExtract({
file: tempFile,
cwd: destination,
strip: 1, // Remove top-level folder
});
fs.removeSync(tempFile);
resolve();
} catch (error) {
reject(error);
}
});
}).on('error', (err) => {
fs.unlinkSync(tempFile);
reject(err);
});
});
}