forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhg.js
More file actions
74 lines (68 loc) · 2.14 KB
/
hg.js
File metadata and controls
74 lines (68 loc) · 2.14 KB
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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Path} from 'types/Config';
import type {Options, SCMAdapter} from 'types/ChangedFiles';
import path from 'path';
import childProcess from 'child_process';
const env = Object.assign({}, process.env, {
HGPLAIN: 1,
});
const adapter: SCMAdapter = {
findChangedFiles: async (
cwd: string,
options: Options,
): Promise<Array<Path>> => {
return new Promise((resolve, reject) => {
let args = ['status', '-amnu'];
if (options && options.withAncestor) {
args.push('--rev', 'ancestor(.^)');
} else if (options && options.changedSince) {
args.push('--rev', `ancestor(., ${options.changedSince})`);
} else if (options && options.lastCommit === true) {
args = ['tip', '--template', '{files%"{file}\n"}'];
}
const child = childProcess.spawn('hg', args, {cwd, env});
let stdout = '';
let stderr = '';
child.stdout.on('data', data => (stdout += data));
child.stderr.on('data', data => (stderr += data));
child.on('error', (error: Error) => reject(error));
child.on('close', code => {
if (code === 0) {
stdout = stdout.trim();
if (stdout === '') {
resolve([]);
} else {
resolve(
stdout
.split('\n')
.map(changedPath => path.resolve(cwd, changedPath)),
);
}
} else {
reject(new Error(code + ': ' + stderr));
}
});
});
},
getRoot: async (cwd: Path): Promise<?Path> => {
return new Promise(resolve => {
try {
let stdout = '';
const child = childProcess.spawn('hg', ['root'], {cwd, env});
child.stdout.on('data', data => (stdout += data));
child.on('error', () => resolve(null));
child.on('close', code => resolve(code === 0 ? stdout.trim() : null));
} catch (e) {
resolve(null);
}
});
},
};
export default adapter;