atom-nuclide
Version:
A unified developer experience for web and mobile development, built as a suite of features on top of Atom to provide hackability and the support of an active community.
166 lines (138 loc) • 6.15 kB
JavaScript
Object.defineProperty(exports, '__esModule', {
value: true
});
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
exports.getPaths = getPaths;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
var _child_process2;
function _child_process() {
return _child_process2 = _interopRequireDefault(require('child_process'));
}
var _split2;
function _split() {
return _split2 = _interopRequireDefault(require('split'));
}
function getFilesFromCommand(command, args, localDirectory, transform) {
return new Promise(function (resolve, reject) {
// Use `spawn` here to process the, possibly huge, output of the file listing.
var proc = (_child_process2 || _child_process()).default.spawn(command, args, { cwd: localDirectory });
proc.on('error', reject);
var filePaths = [];
proc.stdout.pipe((0, (_split2 || _split()).default)()).on('data', function (filePath_) {
var filePath = filePath_;
if (transform) {
filePath = transform(filePath);
}
if (filePath !== '') {
filePaths.push(filePath);
}
});
var errorString = '';
proc.stderr.on('data', function (data) {
errorString += data;
});
proc.on('close', function (code) {
if (code === 0) {
resolve(filePaths);
} else {
reject(errorString);
}
});
});
}
function getTrackedHgFiles(localDirectory) {
return getFilesFromCommand('hg', ['locate', '--fullpath', '--include', '.'], localDirectory, function (filePath) {
return filePath.slice(localDirectory.length + 1);
});
}
/**
* 'Untracked' files are files that haven't been added to the repo, but haven't
* been explicitly hg-ignored.
*/
function getUntrackedHgFiles(localDirectory) {
return getFilesFromCommand('hg',
// Calling 'hg status' with a path has two side-effects:
// 1. It returns the status of only files under the given path. In this case,
// we only want the untracked files under the given localDirectory.
// 2. It returns the paths relative to the directory in which this command is
// run. This is hard-coded to 'localDirectory' in `getFilesFromCommand`,
// which is what we want.
['status', '--unknown', '--no-status' /* No status code. */, localDirectory], localDirectory);
}
/**
* @param localDirectory The full path to a directory.
* @return If localDirectory is within an Hg repo, returns an Object where the
* keys are file paths (relative to the 'localDirectory') of tracked and untracked
* files within that directory, but not including ignored files. All values
* are 'true'. If localDirectory is not within an Hg repo, the Promise rejects.
*/
function getFilesFromHg(localDirectory) {
return Promise.all([getTrackedHgFiles(localDirectory),
// It's not a dealbreaker if untracked files fail to show up.
getUntrackedHgFiles(localDirectory).catch(function () {
return [];
})]).then(function (returnedFiles) {
var _returnedFiles = _slicedToArray(returnedFiles, 2);
var trackedFiles = _returnedFiles[0];
var untrackedFiles = _returnedFiles[1];
return trackedFiles.concat(untrackedFiles);
});
}
function getTrackedGitFiles(localDirectory) {
return getFilesFromCommand('git', ['ls-files'], localDirectory);
}
/**
* 'Untracked' files are files that haven't been added to the repo, but haven't
* been explicitly git-ignored.
*/
function getUntrackedGitFiles(localDirectory) {
// '--others' means untracked files, and '--exclude-standard' excludes ignored files.
return getFilesFromCommand('git', ['ls-files', '--exclude-standard', '--others'], localDirectory);
}
/**
* @param localDirectory The full path to a directory.
* @return If localDirectory is within a Git repo, returns an Object where the
* keys are file paths (relative to the 'localDirectory') of tracked and untracked
* files within that directory, but not including ignored files. All values
* are 'true'. If localDirectory is not within a Git repo, the Promise rejects.
*/
function getFilesFromGit(localDirectory) {
return Promise.all([getTrackedGitFiles(localDirectory), getUntrackedGitFiles(localDirectory)]).then(function (returnedFiles) {
var _returnedFiles2 = _slicedToArray(returnedFiles, 2);
var trackedFiles = _returnedFiles2[0];
var untrackedFiles = _returnedFiles2[1];
return trackedFiles.concat(untrackedFiles);
});
}
function getAllFiles(localDirectory) {
return getFilesFromCommand('find', ['.', '-type', 'f'], localDirectory,
// Slice off the leading `./` that find will add on here.
function (filePath) {
return filePath.substring(2);
});
}
function getPaths(localDirectory) {
// Attempts to get a list of files relative to `localDirectory`, hopefully from
// a fast source control index.
// TODO (williamsc) once ``{HG|Git}Repository` is working in nuclide-server,
// use those instead to determine VCS.
return getFilesFromHg(localDirectory).catch(function () {
return getFilesFromGit(localDirectory);
}).catch(function () {
return getAllFiles(localDirectory);
}).catch(function () {
throw new Error('Failed to populate FileSearch for ' + localDirectory);
});
}
var __test__ = {
getFilesFromGit: getFilesFromGit,
getFilesFromHg: getFilesFromHg
};
exports.__test__ = __test__;