UNPKG

uino-kiss-cli

Version:

uino-kiss-cli 用来初始化项目的方便工具

79 lines (69 loc) 2.28 kB
#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); const log = console.log; const isWin32 = process.platform === 'win32'; const download = require('download-git-repo'); const inquirer = require('./inquirer'); const installPacks = require('./install-packs'); const uiList = ['KissUI'] const commands = { 'KissUI':[ 'import KissUI from \'@uino/kiss-ui\'', 'import \'@uino/kiss-ui/dist/styles/kiss-ui.css\'', 'Vue.use(KissUI)' ] } const custom = { 'KissUI': '@import \'~@uino/kiss-ui/src/styles/custom.less\';' } async function start(name){ if(!name) { let options = await inquirer.askOptions('ui'); name = options.ui !== '不需要' ? options.ui : name } if(!name) return process.exit(1); if(!uiList.includes(name)) { if(isWin32) spawn('chcp 65001') log(chalk.red(`${name} 不是可用的UI组件`)) return process.exit(1); } installPacks.ui(name,register) } function register(name, dirName){ const basePath = `${path.resolve('./')}/${(dirName ? (dirName + '/') : '')}`; const package = require(basePath+'package.json'); const filePath = `${basePath}src/${package.main}` const com = commands[name] // 读取入口 js 文件 let data = fs.readFileSync(filePath, 'utf8').split(/\r\n|\n|\r/gm); const requireString = com.shift() if(!data.includes(requireString)){ data.unshift(requireString) const location = '// 其他配置文件',location2 = 'new Vue({' const n1 = data.findIndex(e=>e===location) const n2 = data.findIndex(e=>e===location2) let index = n1>-1?n1+1:n2 if(index>-1) data.splice(index,0,...com) // 写入入口 js 文件 fs.writeFileSync(filePath, data.join('\r\n')) log(chalk.green(`✔ ${name} 配置完成`)) } else { log(chalk.red(`✖ ${name} 配置已存在`)) } // 配置 custom const customPath = `${basePath}/src/styles/custom.less` if(fs.existsSync(customPath)){ const customString = custom[name] let customData = fs.readFileSync(customPath, 'utf8').split(/\r\n|\n|\r/gm); if(!customData.includes(customString)){ customData.unshift(customString) fs.writeFileSync(customPath, customData.join('\r\n')) } } } module.exports = { start, register }