UNPKG

@dakingindanorf/hive

Version:

A template for creating a nuxt generated static site using Netlify CMS to power the backend. Individual site components can be managed through Bit

218 lines (188 loc) 5.31 kB
const fs = require('fs-extra') const replace = require('replace-in-file') const execSh = require('exec-sh') const { Command } = require('commander') const Actions = {} const chalk = require('chalk') const inquirer = require('inquirer') /* * Actions */ Actions.list = () => { execSh(['bit list']) } Actions.create = async(bit) => { await cloneBit(bit) .then(async() => { await execSh([`bit add bits/components/${bit} --id ${bit} --main bits/components/${bit}/index.vue`]) await execSh([`bit tag ${bit}`]) }) } // TODO: Actions.upgradeMinor = (bit) => { } // TODO: Actions.upgradeMajor = (bit) => { } Actions.share = (bit) => { execSh([`bit export the_hive.contributor ${bit}`], () => {}) } Actions.clone = (bit) => { inquirer.prompt([ { type: 'list', name: 'clone', message: chalk.green('Which bit would you like to clone?'), choices: [ ] } ]).then((answers) => { inquirer.prompt([ { type: 'input', name: 'bit', message: chalk.green('Which bit would you like to clone?'), choices: [ ] } ]).then((answers) => { cloneBit(answers.bit) renameBit(answers.clone) }) }) } Actions.remove = (bit) => { removeBit(bit) } Actions.delete = (bit) => { inquirer.prompt([ { type: 'list', name: 'delete', message: chalk.red(`Confirm deletion ${bit} from the_hive.contributor. This action is irreversable.`), choices: [ 'True', 'False' ] } ]).then((answers) => { removeBit(bit) execSh([`bit remove the_hive.contributor/${bit} --remote`], () => {}) }) } Actions.udpate = (bit) => { execSh([`bit tag ${bit}`], (stdout) => {}) execSh([`bit export the_hive.contributor ${bit}`], () => {}) } Actions.download = (bit) => { execSh([`bit import the_hive.contributor/${bit}`], (stdout) => { }) } /* * Functions */ const cloneBit = async(bit) => { if (!fs.existsSync(`${componentsPath}/${bit}`) || !fs.existsSync(`${bitsPath}/${bit}`)) { console.log('cloning....') await fs.copy(templatePath, `${componentsPath}/${bit}`) .then(() => { renameBit(bit) }) .catch(err => console.error(err)) } else { console.log(chalk.red('Component already exists - use a different name.')) } } const renameBit = async(bit) => { console.log(chalk.yellow('renaming....')) const nameC = await nameCamel(bit) const nameL = await nameLabel(nameC) const nameU = await nameUnderscore(bit) const fieldsReplace = { files: `${componentsPath}/${bit}/fields.json`, from: [/__bit__/g, /__NAME__/g], to: [`${nameU}`, `${nameL}`] } const vueReplace = { files: `${componentsPath}/${bit}/index.vue`, from: /__bit__/g, to: `${nameU}` } await replace(fieldsReplace) await replace(vueReplace) } const removeBit = (bit) => { fs.remove(`${componentsPath}/${bit}`, (err) => { if (err) { return console.error(err) } console.log('success!') }) } /* * Renaming Functions */ const nameUnderscore = (name) => { const nameTemp = nameCamel(name) return nameTemp.split(/(?=[A-Z])/).join('_').toLowerCase() } const nameLabel = (name) => { const nameTemp = name.replace(/([A-Z])/g, ' $1') return nameTemp.charAt(0).toUpperCase() + nameTemp.slice(1) } const nameCamel = (name) => { return name.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) { if (+match === 0) { return '' // or if (/\s+/.test(match)) for white spaces } return index === 0 ? match.toLowerCase() : match.toUpperCase() }) } /* * Install */ const install = () => { execSh(['npm install'], (stdout) => {}) execSh(['bit import bit.envs/compilers/vue --compiler'], (stdout) => {}) } Actions.install = () => { install() console.log('Hive has been installed!') } Actions.help = () => { console.log('Display help text.') } /* * Init */ const templatePath = './bits/components/_template' const bitsPath = '/bits/components' const componentsPath = './components' const program = new Command() const init = () => { program .version('0.0.1') .arguments('<action>', 'action to take') .arguments('[bits...]', 'name of bit') .action((action, bits, command) => { if (action) { if (bits.length === 0 && action !== 'list') { console.log(chalk.red('No component name given.')) return } if (bits.length > 1) { bits.forEach((element) => { Actions[action](element) }) } else if (bits.length === 1) { Actions[action](bits[0]) } else { Actions[action]() } } else { Actions.help() } }) program.parse(process.argv) } init()