-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.mjs
More file actions
93 lines (78 loc) · 2.3 KB
/
github.mjs
File metadata and controls
93 lines (78 loc) · 2.3 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
import path from 'path';
import gitclone from 'git-clone/promise.js';
import { rimraf } from 'rimraf';
import ora from 'ora';
import yaml from 'js-yaml';
import fse from 'fs-extra';
export function getGithubInfo() {
try {
const content = yaml.load(
fse.readFileSync('.github/actions/clone-crates/action.yml', 'utf-8')
);
return ['repo', 'dest', 'temp', 'ref'].reduce((info, key) => {
info[key] = content.inputs[key].default;
return info;
}, {});
} catch (e) {
console.log(e);
return {};
}
}
export async function overwriteContent(content, dest) {
fse.writeFileSync(dest, content);
}
export async function copyAndCleanUp(temp, dest, spinner) {
const updateSpinner = text => {
if (spinner) {
spinner.text = text;
}
}
updateSpinner('Copying crates to the dest...');
fse.copySync(path.join(temp, 'crates'), dest);
const pkg = JSON.parse(fse.readFileSync(path.join(temp, 'package.json'), 'utf-8'));
// Update build.rs content
const buildRsPath = path.join(dest, 'rspack_loader_swc/build.rs');
const buildRsContent = fse.readFileSync(buildRsPath, 'utf-8')
.replace('"../../Cargo.toml"', '"../../../Cargo.toml"');
fse.writeFileSync(buildRsPath, buildRsContent);
// Write package.json
fse.writeFileSync(
path.join(dest, '../package.json'),
JSON.stringify({ version: pkg.version }, null, 2)
);
updateSpinner('Clean up...');
await rimraf(temp);
if (process.env.IS_GITHUB) {
await Promise.all(
['node_binding', 'bench'].map(dir =>
rimraf(path.join(dest, dir))
)
);
}
spinner?.succeed('Cloning rspack repo succeed.');
}
export function createSpinner(text, options = {}) {
const spinner = ora({
text,
stream: process.stdout,
isEnabled: process.stdout.isTTY,
interval: 200,
...options,
});
spinner.start();
return spinner;
}
export async function getRspackCrates() {
const { repo, dest, temp, ref } = getGithubInfo();
const spinner = createSpinner('Cloning rspack repo...');
try {
await rimraf(dest);
await gitclone(`git@github.com:${repo}.git`, temp, { checkout: ref });
await copyAndCleanUp(temp, dest, spinner);
} catch (err) {
spinner.fail('Cloning rspack repo failed.');
await rimraf(temp);
console.log(err);
}
}
export default getGithubInfo();