-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
disk.ts
74 lines (71 loc) · 2.23 KB
/
disk.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
64
65
66
67
68
69
70
71
72
73
74
import { cosmiconfigSync } from 'cosmiconfig';
import { isNonEmptyObject } from 'expect-more';
import {
readFileSync,
readJsonSync,
removeSync,
writeFileSync,
} from 'fs-extra';
import { sync as globSync } from 'glob';
import { join } from 'path';
import { sync as readYamlSync } from 'read-yaml-file';
import { CWD } from '../constants';
import type { SyncpackConfig } from '../types';
import { verbose } from './log';
export type Disk = typeof disk;
const client = cosmiconfigSync('syncpack');
export const disk = {
process: {
exit(code: number): void {
verbose('exit(', code, ')');
process.exit(code);
},
},
globSync(pattern: string): string[] {
verbose('globSync(', pattern, ')');
return globSync(pattern, {
ignore: '**/node_modules/**',
absolute: true,
cwd: CWD,
});
},
readConfigFileSync(configPath?: string): Partial<SyncpackConfig> {
verbose('readConfigFileSync(', configPath, ')');
try {
const result = configPath ? client.load(configPath) : client.search();
if (result === null) {
const rcPath = join(CWD, 'package.json');
const pjson = readJsonSync(rcPath, { throws: false });
const rcConfig = pjson?.config?.syncpack;
console.log(rcConfig);
if (isNonEmptyObject(rcConfig)) return rcConfig;
verbose('no config file found');
return {};
}
const rcPath = result.filepath;
const rcConfig = result.config;
verbose('.syncpackrc path:', rcPath);
verbose('.syncpackrc contents:', rcConfig);
return rcConfig;
} catch (err) {
verbose('no config file found at:', configPath);
return {};
}
},
readFileSync(filePath: string): string {
verbose('readFileSync(', filePath, ')');
return readFileSync(filePath, { encoding: 'utf8' });
},
readYamlFileSync<T = unknown>(filePath: string): T {
verbose('readYamlFileSync(', filePath, ')');
return readYamlSync<T>(filePath);
},
removeSync(filePath: string): void {
verbose('removeSync(', filePath, ')');
removeSync(filePath);
},
writeFileSync(filePath: string, contents: string): void {
verbose('writeFileSync(', filePath, contents, ')');
writeFileSync(filePath, contents);
},
} as const;