main
js 92 lines 1.91 KB
Raw
1 'use strict';
2
3 const ncp = require('ncp').ncp;
4 const path = require('path');
5 const mkdirp = require('mkdirp');
6 const rimraf = require('rimraf');
7 const exec = require('child_process').exec;
8 const targz = require('targz');
9
10 function asyncCopyTo(from, to) {
11 return asyncMkDirP(path.dirname(to)).then(
12 () =>
13 new Promise((resolve, reject) => {
14 ncp(from, to, error => {
15 if (error) {
16 // Wrap to have a useful stack trace.
17 reject(new Error(error));
18 } else {
19 // Wait for copied files to exist; ncp() sometimes completes prematurely.
20 // For more detail, see github.com/facebook/react/issues/22323
21 // Also github.com/AvianFlu/ncp/issues/127
22 setTimeout(resolve, 10);
23 }
24 });
25 })
26 );
27 }
28
29 function asyncExecuteCommand(command) {
30 return new Promise((resolve, reject) =>
31 exec(command, (error, stdout) => {
32 if (error) {
33 reject(error);
34 return;
35 }
36 resolve(stdout);
37 })
38 );
39 }
40
41 function asyncExtractTar(options) {
42 return new Promise((resolve, reject) =>
43 targz.decompress(options, error => {
44 if (error) {
45 reject(error);
46 return;
47 }
48 resolve();
49 })
50 );
51 }
52
53 function asyncMkDirP(filepath) {
54 return new Promise((resolve, reject) =>
55 mkdirp(filepath, error => {
56 if (error) {
57 reject(error);
58 return;
59 }
60 resolve();
61 })
62 );
63 }
64
65 function asyncRimRaf(filepath) {
66 return new Promise((resolve, reject) =>
67 rimraf(filepath, error => {
68 if (error) {
69 reject(error);
70 return;
71 }
72 resolve();
73 })
74 );
75 }
76
77 function resolvePath(filepath) {
78 if (filepath[0] === '~') {
79 return path.join(process.env.HOME, filepath.slice(1));
80 } else {
81 return path.resolve(filepath);
82 }
83 }
84
85 module.exports = {
86 asyncCopyTo,
87 resolvePath,
88 asyncExecuteCommand,
89 asyncExtractTar,
90 asyncMkDirP,
91 asyncRimRaf,
92 };