|
| 1 | +/* eslint-disable import/no-default-export */ |
| 2 | +import type { PlopTypes } from '@turbo/gen'; |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +export default function generator(plop: PlopTypes.NodePlopAPI) { |
| 8 | + plop.setActionType('postInstall', () => { |
| 9 | + try { |
| 10 | + execSync('pnpm install', { stdio: 'inherit' }); |
| 11 | + |
| 12 | + return '✅ pnpm install 완료'; |
| 13 | + } catch (error) { |
| 14 | + console.error('❌ pnpm install 실행 중 오류 발생:', error); |
| 15 | + return '❌ pnpm install 실행 실패'; |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + plop.setGenerator('package', { |
| 20 | + description: 'packages 폴더 내부에 package를 생성합니다.', |
| 21 | + prompts: [ |
| 22 | + { |
| 23 | + type: 'input', |
| 24 | + name: 'packageName', |
| 25 | + message: '패키지 이름 입력 >', |
| 26 | + validate: (input) => { |
| 27 | + const kebabCaseRegex = /^[a-z]+(-[a-z]+)*$/; |
| 28 | + if (!kebabCaseRegex.test(input)) { |
| 29 | + return '패키지명은 kebab-case로 작성해주세요.'; |
| 30 | + } |
| 31 | + |
| 32 | + const packagePath = path.join(process.cwd(), 'packages', input); |
| 33 | + if (fs.existsSync(packagePath)) { |
| 34 | + return '이미 존재하는 패키지 이름입니다. 다른 이름을 입력해주세요.'; |
| 35 | + } |
| 36 | + |
| 37 | + return true; |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + type: 'list', |
| 42 | + name: 'packageType', |
| 43 | + message: '패키지 유형을 선택해주세요 >', |
| 44 | + choices: [ |
| 45 | + { name: '일반 패키지', value: 'regular' }, |
| 46 | + { name: 'React 패키지', value: 'react' }, |
| 47 | + ], |
| 48 | + default: 'regular', |
| 49 | + }, |
| 50 | + ], |
| 51 | + actions: (data) => { |
| 52 | + const actions = []; |
| 53 | + const { packageType } = data as { packageType: 'regular' | 'react' }; |
| 54 | + |
| 55 | + const pathPrefix = |
| 56 | + packageType === 'react' ? './templates/react' : './templates'; |
| 57 | + |
| 58 | + // 1. package.json 생성 |
| 59 | + actions.push({ |
| 60 | + type: 'add', |
| 61 | + path: 'packages/{{packageName}}/package.json', |
| 62 | + templateFile: `${pathPrefix}/package.json.hbs`, |
| 63 | + }); |
| 64 | + |
| 65 | + // 2. tsconfig.json 생성 |
| 66 | + actions.push({ |
| 67 | + type: 'add', |
| 68 | + path: 'packages/{{packageName}}/tsconfig.json', |
| 69 | + templateFile: `${pathPrefix}/tsconfig.json.hbs`, |
| 70 | + }); |
| 71 | + |
| 72 | + // 3. esliont.config.js 생성 |
| 73 | + actions.push({ |
| 74 | + type: 'add', |
| 75 | + path: 'packages/{{packageName}}/eslint.config.js', |
| 76 | + templateFile: `${pathPrefix}/eslint.config.js.hbs`, |
| 77 | + }); |
| 78 | + |
| 79 | + // 4. build.js 생성 |
| 80 | + actions.push({ |
| 81 | + type: 'add', |
| 82 | + path: 'packages/{{packageName}}/build.js', |
| 83 | + templateFile: './templates/build.js.hbs', |
| 84 | + }); |
| 85 | + |
| 86 | + // 5. src 디렉토리 및 기본 파일 생성 |
| 87 | + actions.push({ |
| 88 | + type: 'add', |
| 89 | + path: 'packages/{{packageName}}/src/index.ts', |
| 90 | + templateFile: './templates/src/index.ts.hbs', |
| 91 | + }); |
| 92 | + |
| 93 | + // 6. package.json 이름 업데이트 |
| 94 | + actions.push({ |
| 95 | + type: 'modify', |
| 96 | + path: 'packages/{{packageName}}/package.json', |
| 97 | + pattern: /"name": ".*"/, |
| 98 | + template: '"name": "@workspace/{{packageName}}"', |
| 99 | + }); |
| 100 | + |
| 101 | + // 7. pnpm install 실행 |
| 102 | + actions.push({ |
| 103 | + type: 'postInstall', |
| 104 | + }); |
| 105 | + |
| 106 | + return actions; |
| 107 | + }, |
| 108 | + }); |
| 109 | +} |
0 commit comments