|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | + |
| 5 | +function hasPackage(name: string) { |
| 6 | + try { |
| 7 | + // Try to resolve from current working directory context |
| 8 | + const requireFn = typeof require !== 'undefined' ? require : createRequire(import.meta.url); |
| 9 | + const resolvedPath = requireFn.resolve(name, { |
| 10 | + paths: [path.resolve(process.cwd())], |
| 11 | + }); |
| 12 | + return !!resolvedPath; |
| 13 | + } catch { |
| 14 | + return false; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +const __dirname = path.dirname(new URL(import.meta.url).pathname); |
| 19 | + |
| 20 | +const YAML_SENTINEL = '# Managed by cpconfig'; |
| 21 | +const JSON_SENTINEL = '"Managed by cpconfig"'; |
| 22 | +const JS_SENTINEL = '// Managed by cpconfig'; |
| 23 | + |
| 24 | +export function config() { |
| 25 | + return { |
| 26 | + '.commitlintrc.yaml': { |
| 27 | + contents: fs.readFileSync(path.resolve(__dirname, '../templates/.commitlintrc.yaml'), 'utf8'), |
| 28 | + sentinel: YAML_SENTINEL, |
| 29 | + }, |
| 30 | + 'eslint.config.mts': { |
| 31 | + contents: fs.readFileSync(path.resolve(__dirname, '../templates/eslint.config.mts'), 'utf8'), |
| 32 | + sentinel: JS_SENTINEL, |
| 33 | + }, |
| 34 | + 'tsconfig.json': { |
| 35 | + contents: fs.readFileSync(path.resolve(__dirname, '../templates/tsconfig.json'), 'utf8'), |
| 36 | + sentinel: JSON_SENTINEL, |
| 37 | + }, |
| 38 | + 'tsconfig.build.json': { |
| 39 | + contents: fs.readFileSync( |
| 40 | + path.resolve(__dirname, '../templates/tsconfig.build.json'), |
| 41 | + 'utf8', |
| 42 | + ), |
| 43 | + sentinel: JSON_SENTINEL, |
| 44 | + }, |
| 45 | + '.prettierrc.yaml': { |
| 46 | + contents: fs.readFileSync(path.resolve(__dirname, '../templates/.prettierrc.yaml'), 'utf8'), |
| 47 | + sentinel: YAML_SENTINEL, |
| 48 | + }, |
| 49 | + 'vitest.config.ts': { |
| 50 | + contents: fs.readFileSync( |
| 51 | + path.resolve(__dirname, '../templates/vitest.config.ts.template'), |
| 52 | + 'utf8', |
| 53 | + ), |
| 54 | + sentinel: JS_SENTINEL, |
| 55 | + }, |
| 56 | + ...(hasPackage('tsup') |
| 57 | + ? { |
| 58 | + 'tsconfig.tsup.json': { |
| 59 | + contents: fs.readFileSync( |
| 60 | + path.resolve(__dirname, '../templates/tsconfig.tsup.json'), |
| 61 | + 'utf8', |
| 62 | + ), |
| 63 | + sentinel: JSON_SENTINEL, |
| 64 | + }, |
| 65 | + } |
| 66 | + : undefined), |
| 67 | + }; |
| 68 | +} |
0 commit comments