|
1 |
| -import path, { dirname } from 'path' |
2 |
| -import fs from 'fs' |
3 |
| -import inquirer from 'inquirer' |
| 1 | +import { CreateServiceScript } from '@packages/scripts' |
| 2 | +import { dirname } from 'path' |
4 | 3 | import { fileURLToPath } from 'url'
|
5 | 4 |
|
6 | 5 | const __filename = fileURLToPath(import.meta.url)
|
7 | 6 | const __dirname = dirname(__filename)
|
8 | 7 |
|
9 |
| -const filesData = new Map<string, string>() |
10 |
| - |
11 |
| -function toPascalCase(str: string) { |
12 |
| - return str |
13 |
| - .split('') |
14 |
| - .map((s, i) => (i === 0 ? s.toUpperCase() : s)) |
15 |
| - .join('') |
16 |
| -} |
17 |
| - |
18 |
| -function replace(text: string, name: string) { |
19 |
| - return text.replace(/My/g, `${toPascalCase(name)}`) |
20 |
| -} |
21 |
| - |
22 |
| -async function main() { |
23 |
| - const { type } = await inquirer.prompt([ |
24 |
| - { |
25 |
| - type: 'list', |
26 |
| - name: 'type', |
27 |
| - message: 'Do you want to save in lib directory or services directory?', |
28 |
| - choices: ['services', 'lib'], |
29 |
| - default: 'services', |
30 |
| - }, |
31 |
| - ]) |
32 |
| - |
33 |
| - const templateDir = path.resolve(__dirname, `./templates/${type}`) |
34 |
| - const files = fs.readdirSync(templateDir) |
35 |
| - |
36 |
| - files.forEach((file) => { |
37 |
| - const filePath = path.resolve(templateDir, file) |
38 |
| - const fileData = fs.readFileSync(filePath, 'utf8') |
39 |
| - filesData.set(file, fileData) |
40 |
| - }) |
41 |
| - |
42 |
| - const answer = await inquirer.prompt([ |
43 |
| - { |
44 |
| - name: 'feature', |
45 |
| - message: 'Enter service name', |
46 |
| - }, |
47 |
| - ]) |
48 |
| - |
49 |
| - const dirPath = |
50 |
| - type === 'lib' |
51 |
| - ? path.resolve(__dirname, '../src/lib') |
52 |
| - : path.resolve(__dirname, `../src/services`) |
53 |
| - |
54 |
| - const filename = answer.feature.trim() |
55 |
| - |
56 |
| - createService({ |
57 |
| - type, |
58 |
| - dirPath, |
59 |
| - filename, |
60 |
| - files, |
61 |
| - }) |
62 |
| -} |
63 |
| - |
64 |
| -function createService({ type, dirPath, filename, files }: CreateServiceParams) { |
65 |
| - const newFilename = type === 'services' ? `${toPascalCase(filename)}Service` : filename |
66 |
| - |
67 |
| - const serviceDir = path.resolve(dirPath, newFilename) |
68 |
| - |
69 |
| - // create directory if not exist |
70 |
| - if (!fs.existsSync(serviceDir)) { |
71 |
| - fs.mkdirSync(serviceDir, { recursive: true }) |
72 |
| - } |
73 |
| - |
74 |
| - files.forEach((file) => { |
75 |
| - const fileData = filesData.get(file) |
76 |
| - if (fileData === undefined) return |
77 |
| - const code = replace(fileData, filename) |
78 |
| - |
79 |
| - const newFilePath = path.resolve(serviceDir, replace(file, newFilename)) |
80 |
| - fs.writeFileSync(newFilePath, code) |
81 |
| - }) |
82 |
| - |
83 |
| - console.log(`Service files are created at ${serviceDir}`) |
84 |
| -} |
85 |
| - |
86 |
| -main() |
87 |
| - |
88 |
| -type CreateServiceParams = { |
89 |
| - type: string |
90 |
| - dirPath: string |
91 |
| - filename: string |
92 |
| - files: string[] |
93 |
| -} |
| 8 | +const createServiceScript = new CreateServiceScript({ __dirname }) |
| 9 | +createServiceScript.excute() |
0 commit comments