-
Notifications
You must be signed in to change notification settings - Fork 48.2k
/
Copy pathutils.js
92 lines (84 loc) · 1.91 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
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
'use strict';
const ncp = require('ncp').ncp;
const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const exec = require('child_process').exec;
const targz = require('targz');
function asyncCopyTo(from, to) {
return asyncMkDirP(path.dirname(to)).then(
() =>
new Promise((resolve, reject) => {
ncp(from, to, error => {
if (error) {
// Wrap to have a useful stack trace.
reject(new Error(error));
} else {
// Wait for copied files to exist; ncp() sometimes completes prematurely.
// For more detail, see github.com/facebook/react/issues/22323
// Also github.com/AvianFlu/ncp/issues/127
setTimeout(resolve, 10);
}
});
})
);
}
function asyncExecuteCommand(command) {
return new Promise((resolve, reject) =>
exec(command, (error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
})
);
}
function asyncExtractTar(options) {
return new Promise((resolve, reject) =>
targz.decompress(options, error => {
if (error) {
reject(error);
return;
}
resolve();
})
);
}
function asyncMkDirP(filepath) {
return new Promise((resolve, reject) =>
mkdirp(filepath, error => {
if (error) {
reject(error);
return;
}
resolve();
})
);
}
function asyncRimRaf(filepath) {
return new Promise((resolve, reject) =>
rimraf(filepath, error => {
if (error) {
reject(error);
return;
}
resolve();
})
);
}
function resolvePath(filepath) {
if (filepath[0] === '~') {
return path.join(process.env.HOME, filepath.slice(1));
} else {
return path.resolve(filepath);
}
}
module.exports = {
asyncCopyTo,
resolvePath,
asyncExecuteCommand,
asyncExtractTar,
asyncMkDirP,
asyncRimRaf,
};