-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathnode.js
153 lines (137 loc) · 3.62 KB
/
node.js
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
/**
* 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 {InternalHasteMap} from 'types/HasteMap';
import type {IgnoreMatcher, CrawlerOptions} from '../types';
import fs from 'fs';
import path from 'path';
import {spawn} from 'child_process';
import H from '../constants';
type Callback = (result: Array<[/* id */ string, /* mtime */ number]>) => void;
function find(
roots: Array<string>,
extensions: Array<string>,
ignore: IgnoreMatcher,
callback: Callback,
): void {
const result = [];
let activeCalls = 0;
function search(directory: string): void {
activeCalls++;
fs.readdir(directory, (err, names) => {
activeCalls--;
names.forEach(file => {
file = path.join(directory, file);
if (ignore(file)) {
return;
}
activeCalls++;
fs.lstat(file, (err, stat) => {
activeCalls--;
if (!err && stat && !stat.isSymbolicLink()) {
if (stat.isDirectory()) {
search(file);
} else {
const ext = path.extname(file).substr(1);
if (extensions.indexOf(ext) !== -1) {
result.push([file, stat.mtime.getTime()]);
}
}
}
if (activeCalls === 0) {
callback(result);
}
});
});
if (activeCalls === 0) {
callback(result);
}
});
}
if (roots.length > 0) {
roots.forEach(search);
} else {
callback(result);
}
}
function findNative(
roots: Array<string>,
extensions: Array<string>,
ignore: IgnoreMatcher,
callback: Callback,
): void {
const args = [].concat(roots);
args.push('-type', 'f');
if (extensions.length) {
args.push('(');
}
extensions.forEach((ext, index) => {
if (index) {
args.push('-o');
}
args.push('-iname');
args.push('*.' + ext);
});
if (extensions.length) {
args.push(')');
}
const child = spawn('find', args);
let stdout = '';
child.stdout.setEncoding('utf-8');
child.stdout.on('data', data => (stdout += data));
child.stdout.on('close', () => {
const lines = stdout
.trim()
.split('\n')
.filter(x => !ignore(x));
const result = [];
let count = lines.length;
if (!count) {
callback([]);
} else {
lines.forEach(path => {
fs.stat(path, (err, stat) => {
if (!err && stat) {
result.push([path, stat.mtime.getTime()]);
}
if (--count === 0) {
callback(result);
}
});
});
}
});
}
module.exports = function nodeCrawl(
options: CrawlerOptions,
): Promise<InternalHasteMap> {
const {data, extensions, forceNodeFilesystemAPI, ignore, roots} = options;
return new Promise(resolve => {
const callback = list => {
const files = Object.create(null);
list.forEach(fileData => {
const name = fileData[0];
const mtime = fileData[1];
const existingFile = data.files[name];
if (existingFile && existingFile[H.MTIME] === mtime) {
files[name] = existingFile;
} else {
// See ../constants.js; SHA-1 will always be null and fulfilled later.
files[name] = ['', mtime, 0, [], null];
}
});
data.files = files;
resolve(data);
};
if (forceNodeFilesystemAPI || process.platform === 'win32') {
find(roots, extensions, ignore, callback);
} else {
findNative(roots, extensions, ignore, callback);
}
});
};