-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathrun.ts
63 lines (53 loc) · 1.75 KB
/
run.ts
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
import chalk from "chalk";
import * as chokidar from "chokidar";
import _glob from 'glob';
import * as path from "path";
import * as util from "util";
import {DtsContent} from "./dts-content";
import {DtsCreator} from "./dts-creator";
const glob = util.promisify(_glob);
interface RunOptions {
pattern?: string;
outDir?: string;
watch?: boolean;
camelCase?: boolean;
dropExtension?: boolean;
silent?: boolean;
extension?: string;
}
export async function run(searchDir: string, options: RunOptions = {}): Promise<void> {
const filesPattern = path.join(searchDir, options.pattern || '**/*.css');
const creator = new DtsCreator({
rootDir: process.cwd(),
searchDir,
outDir: options.outDir,
camelCase: options.camelCase,
dropExtension: options.dropExtension,
extension: options.extension,
});
const writeFile = async (f: string): Promise<void> => {
try {
const content: DtsContent = await creator.create(f, undefined, !!options.watch);
await content.writeFile();
if (!options.silent) {
console.log('Wrote ' + chalk.green(content.outputFilePath));
}
} catch (error) {
console.error(chalk.red('[Error] ' + error));
}
};
if (!options.watch) {
const files = await glob(filesPattern);
await Promise.all(files.map(writeFile));
} else {
console.log('Watch ' + filesPattern + '...');
const watcher = chokidar.watch([filesPattern.replace(/\\/g, "/")]);
watcher.on('add', writeFile);
watcher.on('change', writeFile);
await waitForever();
}
}
async function waitForever(): Promise<void> {
return new Promise<void>(() => {
});
}