-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathsources.ts
More file actions
136 lines (122 loc) · 3.67 KB
/
sources.ts
File metadata and controls
136 lines (122 loc) · 3.67 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
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
import { glob } from "tinyglobby";
import slash from "slash";
import { stat } from "fs";
import { join, basename, extname } from "path";
import { minimatch } from "minimatch";
import type { FSWatcher } from "chokidar";
/**
* Find all input files based on source globs
*/
export async function globSources(
sources: string[],
only: string[],
ignore: string[],
includeDotfiles = false
): Promise<string[]> {
const globConfig = {
dot: includeDotfiles,
ignore,
expandDirectories: false,
};
const files = await Promise.all(
sources
.filter(
source =>
includeDotfiles ||
source === "." ||
!basename(source).startsWith(".")
)
.map(source => {
return new Promise<string[]>(resolve => {
stat(source, (err, stat) => {
if (err) {
resolve([]);
return;
}
if (!stat.isDirectory()) {
resolve([source]);
} else {
glob(slash(join(source, "**")), globConfig)
.then(matches => resolve(matches))
.catch(() => resolve([]));
}
});
});
})
);
const f = files.flat().filter(filename => {
return (
!only ||
only.length === 0 ||
only.some(only => minimatch(slash(filename), only))
);
});
return Array.from(new Set<string>(f));
}
type Split = [compilable: string[], copyable: string[]];
/**
* Test if a filename ends with a compilable extension.
*/
export function isCompilableExtension(
filename: string,
allowedExtension: string[]
): boolean {
const ext = extname(filename);
return allowedExtension.includes(ext);
}
/**
* Split file list to files that can be compiled and copied
*/
export function splitCompilableAndCopyable(
files: string[],
allowedExtension: string[],
copyFiles: boolean
): Split {
const compilable: string[] = [];
const copyable: string[] = [];
for (const file of files) {
const isCompilable = isCompilableExtension(file, allowedExtension);
if (isCompilable) {
compilable.push(file);
} else if (copyFiles) {
copyable.push(file);
}
}
return [compilable, copyable];
}
export async function requireChokidar() {
try {
const { default: chokidar } = await import("chokidar");
return chokidar;
} catch (err) {
console.error(
"The optional dependency chokidar is not installed and is required for " +
"--watch. Chokidar is likely not supported on your platform."
);
throw err;
}
}
export async function watchSources(
sources: string[],
includeDotfiles = false,
only: string[] = [],
ignore: string[] = []
): Promise<FSWatcher> {
const chokidar = await requireChokidar();
return chokidar.watch(sources, {
ignored: (filename: string) => {
if (!includeDotfiles && basename(filename).startsWith(".")) {
return true;
}
if (ignore.some(ignore => minimatch(slash(filename), ignore))) {
return true;
}
return only.length > 0 && !only.some(only => minimatch(slash(filename), only));
},
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
});
}