-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathsync.mjs
91 lines (78 loc) · 3.31 KB
/
sync.mjs
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
import { program } from 'commander';
import path from 'path';
import fs from 'fs';
import glob from 'glob';
import download from 'download'
import { mkdir, rm, mv, cp } from "@puerts/shell-util"
const pwd = process.cwd();
if (!fs.existsSync(`${pwd}/package.json`)) {
console.error("[Puer] invalid directory, please run in the root of PerformanceTesting");
process.exit(1);``
}
if (!fs.existsSync(`${pwd}/node_modules`)) {
console.log("[Puer] installing node_modules");
require('child_process').execSync('npm i')
}
program.requiredOption("--version <version>", "the JS backend will be used");
program.option("--org <org>", "where to download the puerts artifact", "Tencent");
program.option("--path <path>", "put the puerts package to", "package")
program.parse(process.argv);
const options = program.opts();
const TEMP_PATH = `${process.cwd()}/.temp`.replace(/\\/g, '/')
const TARGET_PATH = `${process.cwd()}/${options.path}`.replace(/\\/g, '/')
async function run() {
console.log('[Puer] cleaning');
rm("-rf", TEMP_PATH);
console.log('[Puer] downloading');
const downNodeJS = download(`https://github.com/${options.org}/puerts/releases/download/Unity_v${options.version}/PuerTS_NodeJS_${options.version}.tgz`, path.join(TEMP_PATH, 'remote'), { extract: true });
// await Promise.all([downV8, downNodeJS]);
await downNodeJS;
console.log('[Puer] merging');
mkdir("-p", path.join(TEMP_PATH, 'local'));
if (options.version.startsWith("1")) {
cp("-r", path.join(TEMP_PATH, 'remote/Puerts/*'), path.join(TEMP_PATH, 'local'));
} else {
cp("-r", path.join(TEMP_PATH, 'remote/upm/*'), path.join(TEMP_PATH, 'local'));
}
// inherit files like changelog/LICENSE
glob.sync(path.join(TARGET_PATH, '/*').replace(/\\/g, '/')).forEach(filepath => {
cp(filepath, path.join(TEMP_PATH, 'local', path.basename(filepath)))
})
glob.sync(path.join(TARGET_PATH, '/**/*.meta').replace(/\\/g, '/')).forEach(filepath => {
const relative = path.relative(path.join(TARGET_PATH, '/'), filepath);
if (fs.existsSync(path.dirname(path.join(TEMP_PATH, 'local', relative)))) {
cp(filepath, path.dirname(path.join(TEMP_PATH, 'local', relative)))
}
});
console.log('[Puer] update package.json');
const packageJSON = fs.existsSync(path.join(TEMP_PATH, 'local', 'package.json')) ?
JSON.parse(fs.readFileSync(path.join(TEMP_PATH, 'local', 'package.json'))) : defaultPuertsPackageJSON;
packageJSON.version = options.version;
fs.writeFileSync(
path.join(TEMP_PATH, 'local', 'package.json'),
JSON.stringify(packageJSON),
'utf-8'
);
console.log('[Puer] moving');
try {
rm('-rf', TARGET_PATH);
} catch (e) { }
mv(path.join(TEMP_PATH, 'local'), TARGET_PATH)
console.log('[Puer] cleaning');
rm("-rf", TEMP_PATH);
}
const defaultPuertsPackageJSON = {
"name": "com.tencent.puerts.core",
"displayName": "Puerts",
"description": "Write your game with TypeScript in UE4 or Unity. Puerts can be pronounced as pu-erh TS(普洱TS)",
"keywords": [
"Script"
],
"author": {
"name": "Tencent Puerts Group",
"email": "zombieyang@tencent.com",
"url": "https://github.com/puerts"
},
"category": "Libraries"
};
run().catch(err => console.error(err));