This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstandalone.ts
98 lines (79 loc) · 2.64 KB
/
standalone.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eslint-disable import/dynamic-import-chunkname */
import { readFileSync, writeFileSync } from 'fs';
import merge from 'lodash.merge';
import { resolve } from 'path';
import { type format } from 'prettier';
import { createConfig } from '../createConfig';
const log = (message: string) => {
// eslint-disable-next-line no-console
console.log(`[eslint-config-galex] ${message}`);
};
export const standaloneSettingsName = 'eslint-galex-settings.json';
export const maybeFindSettings = (): Record<string, unknown> | null => {
try {
const path = resolve(`./${standaloneSettingsName}`);
const result = readFileSync(path, 'utf-8');
return JSON.parse(result);
} catch {
return null;
}
};
export const loadPrettier = async (): Promise<typeof format> => {
try {
const prettier = await import('prettier');
return prettier.format;
} catch {
/* istanbul ignore next cant mock import */
log(`unable to load prettier, generated json won't be formatted`);
/* istanbul ignore next cant mock import */
return (source: string) => {
return source;
};
}
};
export const defaultEslintRcJsonPath = './.eslintrc.json';
export const prettierOptions = {
parser: 'json-stringify',
};
export const maybeLoadExistingEslintrc = (
path: string
): Record<string, unknown> | null => {
try {
log(`attempting to load existing ".eslintrc.json"`);
return JSON.parse(readFileSync(path, 'utf-8'));
} catch {
return null;
}
};
export const backupExistingEslintrc = (
config: Record<string, unknown>,
format: (str: string, options?: Record<string, unknown>) => string
): void => {
const name = `./.eslintrc-${Date.now()}-bak.json`;
const targetPath = resolve(name);
log(`backing existing ".eslintrc.json" up at "${name}"`);
writeFileSync(targetPath, format(JSON.stringify(config), prettierOptions));
};
export async function generateStandalone(): Promise<void> {
try {
const settings = maybeFindSettings();
const targetPath = resolve(defaultEslintRcJsonPath);
const existingConfig = maybeLoadExistingEslintrc(targetPath);
const format = await loadPrettier();
if (existingConfig) {
backupExistingEslintrc(existingConfig, format);
}
const config = createConfig(settings ?? undefined);
const finalConfig = existingConfig ? merge(config, existingConfig) : config;
writeFileSync(
targetPath,
format(JSON.stringify(finalConfig), prettierOptions)
);
log(`wrote ".eslintrc.json" to "${targetPath}"`);
} catch (error) {
/* istanbul ignore next only throwing errors so... */
if (error instanceof Error) {
log(`error -- ${error.message}`);
}
}
}