|
1 | 1 | const path = require('path');
|
2 |
| -const async = require('async'); |
3 | 2 | const fs = require('fs-extra');
|
4 | 3 | const Git = require('./git.js');
|
5 | 4 |
|
6 |
| -/** |
7 |
| - * Generate a list of unique directory paths given a list of file paths. |
8 |
| - * @param {Array<string>} files List of file paths. |
9 |
| - * @return {Array<string>} List of directory paths. |
10 |
| - */ |
11 |
| -function uniqueDirs(files) { |
12 |
| - const dirs = new Set(); |
13 |
| - files.forEach((filepath) => { |
14 |
| - const parts = path.dirname(filepath).split(path.sep); |
15 |
| - let partial = parts[0] || '/'; |
16 |
| - dirs.add(partial); |
17 |
| - for (let i = 1, ii = parts.length; i < ii; ++i) { |
18 |
| - partial = path.join(partial, parts[i]); |
19 |
| - dirs.add(partial); |
20 |
| - } |
21 |
| - }); |
22 |
| - return Array.from(dirs); |
23 |
| -} |
24 |
| - |
25 | 5 | /**
|
26 | 6 | * Sort function for paths. Sorter paths come first. Paths of equal length are
|
27 | 7 | * sorted alphanumerically in path segment order.
|
@@ -57,120 +37,31 @@ function byShortPath(a, b) {
|
57 | 37 | }
|
58 | 38 | exports.byShortPath = byShortPath;
|
59 | 39 |
|
60 |
| -/** |
61 |
| - * Generate a list of directories to create given a list of file paths. |
62 |
| - * @param {Array<string>} files List of file paths. |
63 |
| - * @return {Array<string>} List of directory paths ordered by path length. |
64 |
| - */ |
65 |
| -function dirsToCreate(files) { |
66 |
| - return uniqueDirs(files).sort(byShortPath); |
67 |
| -} |
68 |
| -exports.copy = function (files, base, dest) { |
69 |
| - return new Promise((resolve, reject) => { |
70 |
| - const pairs = []; |
71 |
| - const destFiles = []; |
72 |
| - files.forEach((file) => { |
73 |
| - const src = path.resolve(base, file); |
74 |
| - const relative = path.relative(base, src); |
75 |
| - const target = path.join(dest, relative); |
76 |
| - pairs.push({ |
77 |
| - src: src, |
78 |
| - dest: target, |
79 |
| - }); |
80 |
| - destFiles.push(target); |
81 |
| - }); |
82 |
| - |
83 |
| - async.eachSeries(dirsToCreate(destFiles), makeDir, (err) => { |
84 |
| - if (err) { |
85 |
| - return reject(err); |
86 |
| - } |
87 |
| - async.each(pairs, copyFile, (err) => { |
88 |
| - if (err) { |
89 |
| - return reject(err); |
90 |
| - } else { |
91 |
| - return resolve(); |
92 |
| - } |
93 |
| - }); |
94 |
| - }); |
95 |
| - }); |
96 |
| -}; |
97 |
| - |
98 |
| -exports.copyFile = copyFile; |
99 |
| - |
100 |
| -exports.dirsToCreate = dirsToCreate; |
101 |
| - |
102 |
| -/** |
103 |
| - * Copy a file. |
104 |
| - * @param {Object} obj Object with src and dest properties. |
105 |
| - * @param {function(Error):void} callback Callback |
106 |
| - */ |
107 |
| -function copyFile(obj, callback) { |
108 |
| - let called = false; |
109 |
| - function done(err) { |
110 |
| - if (!called) { |
111 |
| - called = true; |
112 |
| - callback(err); |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - const read = fs.createReadStream(obj.src); |
117 |
| - read.on('error', (err) => { |
118 |
| - done(err); |
119 |
| - }); |
120 |
| - |
121 |
| - const write = fs.createWriteStream(obj.dest); |
122 |
| - write.on('error', (err) => { |
123 |
| - done(err); |
124 |
| - }); |
125 |
| - write.on('close', () => { |
126 |
| - done(); |
127 |
| - }); |
128 |
| - |
129 |
| - read.pipe(write); |
130 |
| -} |
131 |
| - |
132 |
| -/** |
133 |
| - * Make directory, ignoring errors if directory already exists. |
134 |
| - * @param {string} path Directory path. |
135 |
| - * @param {function(Error):void} callback Callback. |
136 |
| - */ |
137 |
| -function makeDir(path, callback) { |
138 |
| - fs.mkdir(path, (err) => { |
139 |
| - if (err) { |
140 |
| - // check if directory exists |
141 |
| - fs.stat(path, (err2, stat) => { |
142 |
| - if (err2 || !stat.isDirectory()) { |
143 |
| - callback(err); |
144 |
| - } else { |
145 |
| - callback(); |
146 |
| - } |
147 |
| - }); |
148 |
| - } else { |
149 |
| - callback(); |
150 |
| - } |
151 |
| - }); |
152 |
| -} |
153 |
| - |
154 | 40 | /**
|
155 | 41 | * Copy a list of files.
|
156 | 42 | * @param {Array<string>} files Files to copy.
|
157 | 43 | * @param {string} base Base directory.
|
158 | 44 | * @param {string} dest Destination directory.
|
159 | 45 | * @return {Promise} A promise.
|
160 | 46 | */
|
161 |
| - |
162 |
| -exports.getUser = function (cwd) { |
163 |
| - return Promise.all([ |
164 |
| - new Git(cwd).exec('config', 'user.name'), |
165 |
| - new Git(cwd).exec('config', 'user.email'), |
166 |
| - ]) |
167 |
| - .then((results) => { |
168 |
| - return {name: results[0].output.trim(), email: results[1].output.trim()}; |
169 |
| - }) |
170 |
| - .catch((err) => { |
171 |
| - // git config exits with 1 if name or email is not set |
172 |
| - return null; |
173 |
| - }); |
| 47 | +exports.copy = async function (files, base, dest) { |
| 48 | + for (const file of files) { |
| 49 | + const src = path.resolve(base, file); |
| 50 | + const relative = path.relative(base, src); |
| 51 | + const target = path.join(dest, relative); |
| 52 | + await fs.ensureDir(path.dirname(target)); |
| 53 | + await fs.copy(src, target); |
| 54 | + } |
174 | 55 | };
|
175 | 56 |
|
176 |
| -exports.uniqueDirs = uniqueDirs; |
| 57 | +exports.getUser = async function (cwd) { |
| 58 | + try { |
| 59 | + const results = await Promise.all([ |
| 60 | + new Git(cwd).exec('config', 'user.name'), |
| 61 | + new Git(cwd).exec('config', 'user.email'), |
| 62 | + ]); |
| 63 | + return {name: results[0].output.trim(), email: results[1].output.trim()}; |
| 64 | + } catch { |
| 65 | + return null; |
| 66 | + } |
| 67 | +}; |
0 commit comments