v-ingredients
Version:
Reusable Components
139 lines (107 loc) • 4.77 kB
JavaScript
/* eslint-disable no-console */
const fs = require('fs')
const path = require('path')
const merge = require('merge-dirs').default
class Conductor {
constructor(pageName = 'test-name') {
const componentName = pageName.split('-').map(item => item[0].toUpperCase() + item.slice(1))
this.pageName = pageName
this.componentName = componentName.join('')
this.pagesDir = path.join(__dirname, `pages/components`)
this.componentsDir = path.join(__dirname, `components`)
this.interfaceDir = path.join(this.componentsDir, 'interfaces')
this.rootDir = path.join(__dirname, '../..')
}
createDirIfDontExist(dir) {
return new Promise((resolve, reject) => {
fs.stat(dir, (err) => {
if (err) {
fs.mkdirSync(dir, { recursive: true }, (err) => {
if (err) console.log(err)
})
resolve('Dir created')
return true
}
reject(console.error('Dir already exists'))
})
})
}
replaceFileContent(item, oldVal = /BaseComponent/g, newVal = this.componentName) {
return item.replace(oldVal, newVal)
}
saveFile(oldFile, newFile, newDir, contentToReplace = /BaseComponent/g, replaceValue = this.componentName) {
fs.readFile(oldFile, 'utf-8', async (err, item) => {
if (err) console.log(err)
await this.createDirIfDontExist(newDir)
const newFileValue = this.replaceFileContent(item, contentToReplace, replaceValue)
await new Promise((resolve) => {
fs.writeFile(newFile, newFileValue, (err) => {
if (err) console.log(err)
console.log(`New component was created in: ${newDir}`)
resolve()
})
})
})
}
createComponent() {
const oldDir = path.join(this.componentsDir, 'BaseComponent')
const newDir = path.join(__dirname, `${this.componentName}/components/${this.componentName}`)
const oldFile = path.join(oldDir, 'BaseComponent.vue')
const newFile = path.join(newDir, `${this.componentName}.vue`)
this.saveFile(oldFile, newFile, newDir)
}
createInterface() {
const oldDir = path.join(this.componentsDir, 'BaseComponent/interfaces')
const newDir = path.join(__dirname, `${this.componentName}/components/${this.componentName}/interfaces`)
const oldFile = path.join(oldDir, 'IBaseComponent.ts')
const newFile = path.join(newDir, `I${this.componentName}.ts`)
this.saveFile(oldFile, newFile, newDir, /BaseComponent/g)
}
createPage() {
const oldDir = path.join(this.pagesDir, 'base-component')
const newDir = path.join(__dirname, `${this.componentName}/pages/components`)
const oldFile = path.join(oldDir, 'index.vue')
const newFile = path.join(newDir, `${this.pageName}.vue`)
this.saveFile(oldFile, newFile, newDir, /BaseComponent/g)
this.addRoute()
}
async moveItems() {
console.log(`Moving`)
const finalDir = path.join(__dirname, `${this.componentName}/`)
await merge(finalDir, this.rootDir)
fs.rmdirSync(finalDir, { recursive: true })
}
addRoute() {
const routeFile = path.join(this.rootDir, 'pages/components/routes.js')
fs.readFile(routeFile, 'utf-8', async (err, item) => {
if (err) console.log(err)
const data = {
link: `/components/${this.pageName}`,
label: this.componentName,
};
const replaceValue = `, ${JSON.stringify(data)}]`
const newFileValue = this.replaceFileContent(item, ']', replaceValue)
await new Promise((resolve) => {
fs.writeFile(routeFile, newFileValue, (err) => {
if (err) console.log(err)
console.log(`Route updated`)
resolve()
})
})
})
}
init() {
if (!process.argv[3]) {
this.createComponent()
this.createInterface()
this.createPage()
} else {
if (process.argv[3].includes('c')) this.createComponent()
if (process.argv[3].includes('i')) this.createInterface()
if (process.argv[3].includes('p')) this.createPage()
}
setTimeout(() => this.moveItems(), 3000)
}
}
const director = new Conductor(process.argv[2])
director.init()