forked from aztack/cc-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
38 lines (35 loc) · 869 Bytes
/
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
const pkgName = 'cc-modules';
const unzip = require('extract-zip')
const $fs = require('fs');
function read(relativePath, val) {
let str = val || '';
try {
str = $fs.readFileSync(Editor.url(`packages://${pkgName}/${relativePath}`), 'utf-8');
} catch (e) {
console.error(`read file ${relativePath} failed`);
}
return str;
}
function write(relativePath, data) {
const path = Editor.url(`packages://${pkgName}/${relativePath}`);
$fs.writeFileSync(path, data);
return path;
}
function extract(srcZip, destDir) {
// return $fs.createReadStream(srcZip).pipe(unzip.Extract({ path: destDir}));
return new Promise(function (resolve, reject) {
unzip(srcZip, {dir: destDir}, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
module.exports = {
read,
write,
extract,
pkgName
}