zeeth-boilerplate-generator-cli
Version:
Boilerplate generator for the zeeth frontend framework
138 lines (125 loc) • 4.34 kB
JavaScript
import chalk from "chalk"
import { basicIndexTemplate, basicStoriesTemplate } from "../templates/basic"
import fs from "fs"
const capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1)
}
export async function generateBasic(options) {
options = {
...options,
targetDirectory: options.targetDirectory || process.cwd(),
}
if (options.name === null) {
console.log("%s No basic name given, exiting.", chalk.yellow.bold("FAILED"))
return true
}
if (!fs.existsSync(`./components/basics/${options.name}`)) {
const path = `./components/basics/${capitalizeFirstLetter(options.name)}`
fs.mkdir(
`./components/basics/${capitalizeFirstLetter(options.name)}`,
{ recursive: true },
function (err) {
if (err) {
console.log(err)
} else {
console.log(
`%s New directory: ${path} successfully created.`,
chalk.green.bold("DONE")
)
if (!fs.existsSync(`${path}/index.tsx`)) {
fs.writeFile(
`${path}/index.tsx`,
basicIndexTemplate(options.name, options.type),
{ recursive: true },
() => {
console.log(
`%s New file: ${path}/index.tsx successfully created.`,
chalk.green.bold("DONE")
)
}
)
}
// if (!fs.existsSync(`${path}/styles.js`)) {
// fs.writeFile(
// `${path}/styles.js`,
// basicStylesTemplate(options.name, options.type),
// { recursive: true },
// () => {
// console.log(
// `%s New directory: ${path}/styles.js successfully created.`,
// chalk.green.bold("DONE")
// )
// }
// )
// }
// if (!fs.existsSync(`${path}/test.js`)) {
// fs.writeFile(
// `${path}/test.js`,
// basicTestTemplate(options.name),
// { recursive: true },
// () => {
// console.log(
// `%s New directory: ${path}/test.js successfully created.`,
// chalk.green.bold("DONE")
// )
// }
// )
// }
if (options.storybook) {
fs.writeFile(
`${path}/${options.name}.stories.tsx`,
basicStoriesTemplate(capitalizeFirstLetter(options.name)),
{ recursive: true },
() => {
console.log(
`%s New directory: ${path}/stories.js successfully created.`,
chalk.green.bold("DONE")
)
}
)
}
}
// const index = "./components/basics/index.tsx"
// if (!fs.existsSync(index)) {
// fs.writeFile(index, "\nexport {}", { recursive: true }, (err) => {
// if (err) throw err
// console.log(
// `%s New file: ${index} successfully created.`,
// chalk.green.bold("DONE")
// )
// updateIndex(index)
// })
// } else updateIndex(index)
}
)
// const updateIndex = (index) => {
// const data = fs.readFileSync(index)
// const fd = fs.openSync(index, "w+")
// const insert = Buffer.from(
// `import ${capitalizeFirstLetter(options.name)} from './${
// options.name
// }' \n`
// )
// fs.writeSync(fd, insert, 0, insert.length, 0)
// fs.writeSync(fd, data, 0, data.length, insert.length)
// fs.close(fd, (err) => {
// if (err) throw err
// })
// fs.readFile(index, "utf8", function (err, data) {
// if (err) {
// return console.log(err)
// }
// var result = data.replace(
// /}/g,
// `, \n ${capitalizeFirstLetter(options.name)} }`
// )
// var result_cleaned = result.replace(/{,/g, `{`)
// fs.writeFile(index, result_cleaned, "utf8", function (err) {
// if (err) return console.log(err)
// })
// })
// }
}
console.log("%s Basic created", chalk.green.bold("DONE"))
return true
}