|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import fs from "fs"; |
3 | 3 | import path from "path"; |
4 | | -import { colorsMap } from "@shared/models"; |
| 4 | +import { CodefatherConfig, colorsMap, CrewName } from "@shared/models"; |
5 | 5 | import { safeJSONParse } from "@shared/parser"; |
| 6 | +import { generateConfigFromCodeowners } from "./generate-from-codeowner"; |
6 | 7 |
|
7 | 8 | const args = process.argv.slice(2); |
8 | 9 | const useJson = args.includes("--json"); |
| 10 | +const canOverwrite = args.includes("--overwrite"); |
9 | 11 |
|
10 | | -export function runInit() { |
11 | | - const rootDir = process.cwd(); |
12 | | - const configPath = path.join( |
13 | | - rootDir, |
14 | | - useJson ? "codefather.json" : "codefather.ts" |
15 | | - ); |
16 | | - const pkgPath = path.join(rootDir, "package.json"); |
17 | | - |
18 | | - if (!fs.existsSync(configPath)) { |
19 | | - if (useJson) { |
20 | | - fs.writeFileSync(configPath, JSON.stringify({ rules: [] }, null, 2)); |
21 | | - } else { |
22 | | - fs.writeFileSync( |
23 | | - configPath, |
24 | | - `import type { CodefatherConfig } from "@donedeal0/codefather";\n\n` + |
25 | | - `export default { rules: [] } satisfies CodefatherConfig;\n` |
26 | | - ); |
27 | | - } |
| 12 | +function informFileCreated( |
| 13 | + configPath: string, |
| 14 | + isCodeownersBased: boolean, |
| 15 | + crews: CrewName[] |
| 16 | +) { |
| 17 | + if (canOverwrite) { |
28 | 18 | console.log( |
29 | 19 | colorsMap.info, |
30 | | - `- A ${path.basename(configPath)} config file has been created.` |
| 20 | + `- A new ${configPath} config file has been created, your former config was overwritten.` |
31 | 21 | ); |
32 | 22 | } else { |
33 | 23 | console.log( |
34 | 24 | colorsMap.info, |
35 | | - `- A ${path.basename(configPath)} file already exists.` |
| 25 | + `- A ${configPath} config file has been created.` |
36 | 26 | ); |
37 | 27 | } |
38 | | - |
39 | | - if (fs.existsSync(pkgPath)) { |
40 | | - const pkg = safeJSONParse<{ |
41 | | - scripts?: Record<string, string>; |
42 | | - }>(fs.readFileSync(pkgPath, "utf-8")); |
43 | | - |
44 | | - pkg.scripts = pkg.scripts || {}; |
45 | | - if (!pkg.scripts.codefather) { |
46 | | - pkg.scripts.codefather = "codefather"; |
47 | | - fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2)); |
| 28 | + if (isCodeownersBased) { |
| 29 | + console.log( |
| 30 | + colorsMap.info, |
| 31 | + `- The rules were filled with your CODEOWNER file.` |
| 32 | + ); |
| 33 | + if (crews?.length > 0) { |
48 | 34 | console.log( |
49 | | - colorsMap.info, |
50 | | - "- A codefather script has been added to your package.json." |
| 35 | + colorsMap.warning, |
| 36 | + `- The following crews were detected:\n${crews |
| 37 | + .map((crew) => ` - ${crew}`) |
| 38 | + .join("\n")} |
| 39 | +Please specify their members in the codefather config for CLI enforcement.` |
51 | 40 | ); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export async function runInit() { |
| 46 | + try { |
| 47 | + const rootDir = process.cwd(); |
| 48 | + const codeownersPath = path.join(rootDir, "./.github/CODEOWNERS"); |
| 49 | + let baseConfig: CodefatherConfig = { rules: [] }; |
| 50 | + let isCodeownersBased = false; |
| 51 | + const crews: CrewName[] = []; |
| 52 | + if (fs.existsSync(codeownersPath)) { |
| 53 | + const data = await generateConfigFromCodeowners(codeownersPath); |
| 54 | + baseConfig = data.config; |
| 55 | + isCodeownersBased = true; |
| 56 | + crews.push(...data.crews); |
| 57 | + } |
| 58 | + const configPath = path.join( |
| 59 | + rootDir, |
| 60 | + useJson ? "codefather.json" : "codefather.ts" |
| 61 | + ); |
| 62 | + const pkgPath = path.join(rootDir, "package.json"); |
| 63 | + const config = JSON.stringify(baseConfig, null, 2); |
| 64 | + if (!fs.existsSync(configPath) || canOverwrite) { |
| 65 | + if (useJson) { |
| 66 | + fs.writeFileSync(configPath, config); |
| 67 | + } else { |
| 68 | + fs.writeFileSync( |
| 69 | + configPath, |
| 70 | + `import type { CodefatherConfig } from "@donedeal0/codefather";\n\n` + |
| 71 | + `export default ${config} satisfies CodefatherConfig;\n` |
| 72 | + ); |
| 73 | + } |
| 74 | + informFileCreated(path.basename(configPath), isCodeownersBased, crews); |
52 | 75 | } else { |
53 | 76 | console.log( |
54 | 77 | colorsMap.info, |
55 | | - "- A codefather script already exists in your package.json." |
| 78 | + `- A ${path.basename(configPath)} file already exists.` |
56 | 79 | ); |
57 | 80 | } |
58 | | - } else { |
| 81 | + |
| 82 | + if (fs.existsSync(pkgPath)) { |
| 83 | + const pkg = safeJSONParse<{ |
| 84 | + scripts?: Record<string, string>; |
| 85 | + }>(fs.readFileSync(pkgPath, "utf-8")); |
| 86 | + |
| 87 | + pkg.scripts = pkg.scripts || {}; |
| 88 | + if (!pkg.scripts.codefather) { |
| 89 | + pkg.scripts.codefather = "codefather"; |
| 90 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2)); |
| 91 | + console.log( |
| 92 | + colorsMap.info, |
| 93 | + "- A codefather script has been added to your package.json." |
| 94 | + ); |
| 95 | + } else { |
| 96 | + console.log( |
| 97 | + colorsMap.info, |
| 98 | + "- A codefather script already exists in your package.json." |
| 99 | + ); |
| 100 | + } |
| 101 | + } else { |
| 102 | + return console.log( |
| 103 | + colorsMap.error, |
| 104 | + "⚠ No package.json found in the project root. Skipping script setup." |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + return console.log( |
| 109 | + colorsMap.success, |
| 110 | + "\n✓ Setup complete. Run `npm run codefather` to enforce your rules." |
| 111 | + ); |
| 112 | + } catch (err: unknown) { |
59 | 113 | return console.log( |
60 | 114 | colorsMap.error, |
61 | | - "⚠️ No package.json found in the project root. Skipping script setup." |
| 115 | + err instanceof Error ? err.message : String(err) |
62 | 116 | ); |
63 | 117 | } |
64 | | - |
65 | | - return console.log( |
66 | | - colorsMap.success, |
67 | | - "\n✓ Setup complete. Run `npm run codefather` to enforce your rules." |
68 | | - ); |
69 | 118 | } |
70 | 119 |
|
71 | 120 | if (import.meta.url === new URL(import.meta.url).href) { |
|
0 commit comments