This repository was archived by the owner on Dec 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate_boilerplate.js
More file actions
97 lines (83 loc) · 3.41 KB
/
generate_boilerplate.js
File metadata and controls
97 lines (83 loc) · 3.41 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
import fs from 'fs'
import path from 'path'
import { program } from 'commander'
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function removeNonAlphanumeric(str) {
return str.replace(/[^a-zA-Z0-9]/g, '')
}
// Program definition
program
.name('boilerplate_generator')
.description('CLI to automatically create helpwave components')
.version('0.1.0')
.option('-nop, --no-props', 'Whether the props should be omitted', true)
.option('-notl, --no-translate', 'Generates the component without a translation', true)
.option('-h, --help', 'Display help for command', false)
.option('-f, --force', 'Overwrite the file if it exists', false)
.option('-d, --debug', 'Create debug output', false)
.option('--fileType <type>', 'The file type. Defaults to tsx', 'tsx')
.argument('[Name]', 'The path or name of the component')
.action((path, options) => {
if (!path || options.help) {
program.help()
process.exit(1)
}
})
.parse()
const options = program.opts()
const args = program.args
if(options.debug){
console.debug('options', options)
console.debug('args', args)
console.debug('execute location', process.env.INIT_CWD)
}
const filePathInput = args[0] + (args[0].endsWith('.tsx') || args[0].endsWith('.ts') ? '' : '.tsx')
let cwd = process.env.INIT_CWD ?? process.cwd()
let filePath = path.resolve(cwd, filePathInput)
const dir = path.dirname(filePath)
const componentName = capitalize(removeNonAlphanumeric(path.parse(filePathInput).name))
const fileName = `${componentName}.${options.fileType}`
filePath = path.resolve(dir, fileName)
const imports = {
standard: `import clsx from 'clsx'`,
translation: `import type { Translation } from '@helpwave/hightide'\n` +
`import { useTranslation, type PropsForTranslation } from '@helpwave/hightide'`
}
const usedImports = imports['standard'] + (options.translate ? `\n${imports['translation']}` : '')
const translation = options.translate ?
`type ${componentName}Translation = {\n}\n\nconst default${componentName}Translation: Record<Languages, ${componentName}Translation > = {\n`
+ ` en: {\n },\n de: {\n }\n}`
: ''
const props = options.props ? `export type ${componentName}Props = {\n}` : ''
const propsType = options.translate ?
`{\n overwriteTranslation,\n}: PropsForTranslation<${componentName}Translation${options.props ? `, ${componentName}Props`: ''}>`
: (options.props ? `{\n} : ${componentName}Props`: '')
const body =
`/**\n * Description\n */\n` +
`export const ${componentName} = (${propsType}) => {\n` +
`${options.translate ? ` const translation = useTranslation(default${componentName}Translation, overwriteTranslation)\n` : ''}` +
` return (\n <div>\n { /* Your Code */ }\n </div>\n )\n}`
const file = [usedImports, translation, props, body].filter(value => !!value).join('\n\n') + `\n`
if (fs.existsSync(filePath) && !options.force) {
console.error(`File "${path.basename(filePath)}" already exists. To overwrite it use -f.`)
} else {
if(!fs.existsSync(dir)){
fs.mkdir(dir, {}, (err) => {
if (err) {
console.error('Error creating directory:', err)
process.exit(1)
} else {
console.debug(`Directory ${dir} created successfully.`)
}
})
}
fs.writeFile(filePath, file, (err) => {
if (err) {
console.error('Error writing to file:', err)
} else {
console.info(`File ${fileName} created successfully.`)
}
})
}