UNPKG

cwj-cli

Version:

66 lines (55 loc) 2.01 kB
const { promisify } = require('util') const download = promisify(require('download-git-repo')) const path = require('path') const { complie, createDir, writeToFile } = require('../utils/index') const { reactPath } = require('../config/temp-git-path') const { promiseCommand } = require('../utils/terminal') const createAction = async (project) => { console.log(`new project ${project},构建项目中...`) // 远程clone项目模板 await download(reactPath, project, { clone: true }) // 安装依赖 const command = process.platform === 'win32' ? 'yarn.cmd' : 'yarn' await promiseCommand(command, ['install'], { cwd: `./${project}` }) // 启动项目 promiseCommand(command, ['start'], { cwd: `./${project}` }) } const addComponentAction = async (name, dest) => { console.log(`new componet ${name},组件生成中...`) const component = await complie('react-component.ejs', { name, wrapperName: `${name}Wrapper`, }) const style = await complie('react-style.ejs', { wrapperName: `${name}Wrapper`, }) const targetDir = path.resolve(dest, name) if (createDir(targetDir)) { const componentPath = path.resolve(targetDir, 'index.tsx') const stylePath = path.resolve(targetDir, 'style.ts') writeToFile(componentPath, component) writeToFile(stylePath, style) } } const addView = async (name, dest) => { console.log(`new view ${name},页面生成中...`) const view = await complie('react-page.ejs', { name, wrapperName: `${name}Wrapper`, }) const style = await complie('react-style.ejs', { wrapperName: `${name}Wrapper`, }) const targetDir = path.resolve(dest, name) if (createDir(targetDir)) { const viewPath = path.resolve(targetDir, 'index.tsx') const stylePath = path.resolve(targetDir, 'style.ts') writeToFile(viewPath, view) writeToFile(stylePath, style) } } module.exports = { createAction, addComponentAction, addView, }