-
Notifications
You must be signed in to change notification settings - Fork 6
/
example2.ts
48 lines (45 loc) · 1.54 KB
/
example2.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
import * as fs from 'fs/promises'
import { equals } from 'ramda'
import { createFormat } from '../updater/formatPlugin.js'
import { createUpdateOptions } from '../updater/updateOptions.js'
import { eslintrc, tsconfig } from './defaultFiles.js'
/**
* User-defined format '.gitignore'
*/
const gitignoreFormat = createFormat({
async read({ resolvedPath }) {
return (await fs.readFile(resolvedPath, 'utf8')).split('\n')
},
update(actual, updater, options) {
return updater(actual, options)
},
equal(expected, actual) {
return equals(expected, actual)
},
async write(expected, { resolvedPath }) {
const unique = <T extends unknown[]>(array: T) => Array.from(new Set<T[number]>(array)).sort()
await fs.writeFile(resolvedPath, unique(expected).join('\n'), 'utf8')
},
})
export default async (_workspaceDir: string) => {
return createUpdateOptions({
files: {
// builtin
'tsconfig.json': (actual, _options) => actual ?? tsconfig,
// buildin .json format with explicit format specifier
'.eslintrc [.json]': (actual) => actual ?? eslintrc,
// user-defined `#ignore` format
'.prettierignore [#ignore]': (actual) => actual ?? ['node_modules'],
'.gitignore': (actual) => actual,
'by-type.txt [#ignore]': (actual, _options) => actual,
// [explicit format specifier] takes precedence over extension detection
'by-type.json [#ignore]': (actual) => actual,
},
formats: {
'.gitignore': gitignoreFormat,
'#ignore': {
...gitignoreFormat,
},
},
})
}