@toptal/davinci-code
Version:
Code generation package for frontend applications
43 lines (33 loc) • 996 B
JavaScript
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'fs'
import { runSync } from '@toptal/davinci-cli-shared'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const rootDir = path.join(__dirname, '..')
const templatesDir = path.join(rootDir, './_templates')
const checkDirExists = dir => fs.existsSync(dir)
const generateModule = name =>
runSync('yarn', ['hygen', 'module', 'new', name], {
env: {
HYGEN_TMPLS: templatesDir,
},
})
const generate = (template, name, moduleName) => {
const customTemplatesDir = './_templates'
const customTemplateDirExists = checkDirExists(
`${customTemplatesDir}/${template}/new`
)
const args = ['hygen', template, 'new', name]
if (moduleName !== undefined) {
args.push(moduleName)
}
return runSync('yarn', args, {
env: {
HYGEN_TMPLS: customTemplateDirExists ? customTemplatesDir : templatesDir,
},
})
}
export default {
generateModule,
generate,
}