-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.ts
More file actions
33 lines (25 loc) · 961 Bytes
/
File.ts
File metadata and controls
33 lines (25 loc) · 961 Bytes
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
import { promises, statSync, existsSync, mkdirSync, writeFile } from 'fs';
import { join, relative, resolve } from 'path';
import { register } from 'ts-node';
export async function* traverseFiles(folder: string): AsyncGenerator<string> {
for (const name of await promises.readdir(folder)) {
const path = join(folder, name);
if (statSync(path).isDirectory()) yield* traverseFiles(path);
else yield path;
}
}
export function saveFile(path: string, data: any) {
const folder = join(path, '..');
if (!existsSync(folder)) mkdirSync(folder, { recursive: true });
return new Promise((resolve, reject) =>
writeFile(path, data, error => (error ? reject(error) : resolve()))
);
}
const module_path = join(module.filename, '../');
register({
ignore: [],
compilerOptions: { module: 'CommonJS' }
});
export function loadModule(path: string) {
return import(relative(module_path, resolve(path)));
}