-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathconfigureTools.ts
More file actions
132 lines (113 loc) · 3.24 KB
/
configureTools.ts
File metadata and controls
132 lines (113 loc) · 3.24 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import fs from 'fs-extra';
import path from 'node:path';
import { applyTemplate, type TemplateConfiguration } from '../template';
import sortObjectKeys from './sortObjectKeys';
type Tool = {
name: string;
description: string;
condition?: (config: TemplateConfiguration) => boolean;
};
type Options = {
tools: string[];
root: string;
packageJson: Record<string, unknown>;
config: TemplateConfiguration;
};
const ESLINT = {
name: 'ESLint with Prettier',
description: 'Lint and format code',
};
const LEFTHOOK = {
name: 'Lefthook with Commitlint',
description: 'Manage Git hooks and lint commit messages',
};
const RELEASE_IT = {
name: 'Release It',
description: 'Automate versioning and package publishing tasks',
};
const JEST = {
name: 'Jest',
description: 'Test JavaScript and TypeScript code',
};
const TURBOREPO = {
name: 'Turborepo',
description: 'Cache build outputs on CI',
};
export const AVAILABLE_TOOLS = {
eslint: ESLINT,
jest: JEST,
lefthook: LEFTHOOK,
'release-it': RELEASE_IT,
} as const satisfies Record<string, Tool>;
const REQUIRED_TOOLS = {
turborepo: TURBOREPO,
} as const satisfies Record<string, Tool>;
const ALL_TOOLS = {
...AVAILABLE_TOOLS,
...REQUIRED_TOOLS,
} as const;
export async function configureTools({
tools,
config,
root,
packageJson,
}: Options) {
for (const key of [
...tools,
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
...(Object.keys(REQUIRED_TOOLS) as (keyof typeof REQUIRED_TOOLS)[]),
]) {
if (!(key in ALL_TOOLS)) {
throw new Error(
`Invalid tool '${key}'. Available tools are: ${Object.keys(
AVAILABLE_TOOLS
).join(', ')}.`
);
}
// @ts-expect-error: We checked the key above
const tool: Tool = ALL_TOOLS[key];
if (tool.condition && !tool.condition(config)) {
continue;
}
const toolDir = path.resolve(__dirname, `../../templates/tools/${key}`);
if (fs.existsSync(toolDir)) {
await applyTemplate(config, toolDir, root);
}
const pkgPath = path.join(toolDir, '~package.json');
if (fs.existsSync(pkgPath)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const toolPkg = (await fs.readJson(pkgPath)) as Record<string, unknown>;
for (const [field, value] of Object.entries(toolPkg)) {
if (
typeof value === 'object' &&
value !== null &&
!Array.isArray(value)
) {
if (
typeof packageJson[field] === 'object' ||
packageJson[field] == null
) {
packageJson[field] = {
...packageJson[field],
...value,
};
if (
field === 'dependencies' ||
field === 'devDependencies' ||
field === 'peerDependencies'
) {
// @ts-expect-error: We know they are objects here
packageJson[field] = sortObjectKeys(packageJson[field]);
}
} else {
throw new Error(
`Cannot merge '${field}' field because it is not an object (got '${String(packageJson[field])}').`
);
}
} else {
packageJson[field] = value;
}
}
}
}
}