File size: 4,234 Bytes
7f21468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const path = require('path');
const async = require('async');
const fs = require('fs-extra');
const Git = require('./git.js');

/**
 * Generate a list of unique directory paths given a list of file paths.
 * @param {Array<string>} files List of file paths.
 * @return {Array<string>} List of directory paths.
 */
function uniqueDirs(files) {
  const dirs = new Set();
  files.forEach((filepath) => {
    const parts = path.dirname(filepath).split(path.sep);
    let partial = parts[0] || '/';
    dirs.add(partial);
    for (let i = 1, ii = parts.length; i < ii; ++i) {
      partial = path.join(partial, parts[i]);
      dirs.add(partial);
    }
  });
  return Array.from(dirs);
}

/**
 * Sort function for paths.  Sorter paths come first.  Paths of equal length are
 * sorted alphanumerically in path segment order.
 * @param {string} a First path.
 * @param {string} b Second path.
 * @return {number} Comparison.
 */
function byShortPath(a, b) {
  const aParts = a.split(path.sep);
  const bParts = b.split(path.sep);
  const aLength = aParts.length;
  const bLength = bParts.length;
  let cmp = 0;
  if (aLength < bLength) {
    cmp = -1;
  } else if (aLength > bLength) {
    cmp = 1;
  } else {
    let aPart, bPart;
    for (let i = 0; i < aLength; ++i) {
      aPart = aParts[i];
      bPart = bParts[i];
      if (aPart < bPart) {
        cmp = -1;
        break;
      } else if (aPart > bPart) {
        cmp = 1;
        break;
      }
    }
  }
  return cmp;
}
exports.byShortPath = byShortPath;

/**
 * Generate a list of directories to create given a list of file paths.
 * @param {Array<string>} files List of file paths.
 * @return {Array<string>} List of directory paths ordered by path length.
 */
function dirsToCreate(files) {
  return uniqueDirs(files).sort(byShortPath);
}
exports.copy = function (files, base, dest) {
  return new Promise((resolve, reject) => {
    const pairs = [];
    const destFiles = [];
    files.forEach((file) => {
      const src = path.resolve(base, file);
      const relative = path.relative(base, src);
      const target = path.join(dest, relative);
      pairs.push({
        src: src,
        dest: target,
      });
      destFiles.push(target);
    });

    async.eachSeries(dirsToCreate(destFiles), makeDir, (err) => {
      if (err) {
        return reject(err);
      }
      async.each(pairs, copyFile, (err) => {
        if (err) {
          return reject(err);
        } else {
          return resolve();
        }
      });
    });
  });
};

exports.copyFile = copyFile;

exports.dirsToCreate = dirsToCreate;

/**
 * Copy a file.
 * @param {Object} obj Object with src and dest properties.
 * @param {function(Error):void} callback Callback
 */
function copyFile(obj, callback) {
  let called = false;
  function done(err) {
    if (!called) {
      called = true;
      callback(err);
    }
  }

  const read = fs.createReadStream(obj.src);
  read.on('error', (err) => {
    done(err);
  });

  const write = fs.createWriteStream(obj.dest);
  write.on('error', (err) => {
    done(err);
  });
  write.on('close', () => {
    done();
  });

  read.pipe(write);
}

/**
 * Make directory, ignoring errors if directory already exists.
 * @param {string} path Directory path.
 * @param {function(Error):void} callback Callback.
 */
function makeDir(path, callback) {
  fs.mkdir(path, (err) => {
    if (err) {
      // check if directory exists
      fs.stat(path, (err2, stat) => {
        if (err2 || !stat.isDirectory()) {
          callback(err);
        } else {
          callback();
        }
      });
    } else {
      callback();
    }
  });
}

/**
 * Copy a list of files.
 * @param {Array<string>} files Files to copy.
 * @param {string} base Base directory.
 * @param {string} dest Destination directory.
 * @return {Promise} A promise.
 */

exports.getUser = function (cwd) {
  return Promise.all([
    new Git(cwd).exec('config', 'user.name'),
    new Git(cwd).exec('config', 'user.email'),
  ])
    .then((results) => {
      return {name: results[0].output.trim(), email: results[1].output.trim()};
    })
    .catch((err) => {
      // git config exits with 1 if name or email is not set
      return null;
    });
};

exports.uniqueDirs = uniqueDirs;