-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
52 lines (47 loc) · 1.23 KB
/
utils.js
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
const pkgName = 'cc-custom-build';
const $fs = require('fs');
const $path = require('path');
const buildConfigFile = 'cc-custom-build.json';
function read(relativePath) {
return $fs.readFileSync(Editor.url(`packages://${pkgName}/${relativePath}`), 'utf-8');
}
function readConfig() {
return new Promise((resolve, reject) => {
const cwd = process.cwd();
let pkg;
try {
pkg = JSON.parse($fs.readFileSync($path.resolve(cwd, 'package.json')));
} catch (e) {
reject(e);
}
const configPath = $path.resolve(cwd, pkg.name, 'settings', buildConfigFile);
if ($fs.existsSync(configPath)) {
let config = {};
try {
config = JSON.parse($fs.readFileSync(configPath).toString());
resolve(config);
} catch (e) {
reject(e);
}
} else {
reject(new Error(`Did not find custom build config, please click apply button on custom build panel`));
}
});
}
function writeConfig(configPath, config) {
return new Promise((resolve, reject) => {
try {
$fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
resolve();
} catch(e) {
reject(e)
}
});
}
module.exports = {
read,
pkgName,
buildConfigFile,
readConfig,
writeConfig
}