UNPKG

sdata-cli

Version:

smardaten二次开发插件脚手架

75 lines (71 loc) 2.43 kB
const execa = require("execa"); const {spawn} = require('child_process'); const path = require("path"); const fs = require('fs'); const fse = require("fs-extra"); const ora = require("ora"); const {exteriorPackagesUrl} = require('../config/gitUrlMap.js'); module.exports = function (batch) { const spinner = ora(); spinner.text = "前置准备中,请稍候" spinner.start(); try { fse.existsSync(path.resolve(__dirname, "temp")) && fse.removeSync(path.join(__dirname, "/temp")); fs.readdirSync(__dirname).forEach((item) => { item.indexOf("GenerateTemplate") > 0 && fse.removeSync(path.join(__dirname, item)) }) } catch (e) { } executeCommand(`mkdir temp`); executeCommand("git init", path.join(__dirname, "temp")); executeCommand( `git remote add -f origin ${exteriorPackagesUrl}`, path.join(__dirname, "temp") ); executeCommand( `git config core.sparsecheckout true`, path.join(__dirname, "temp") ); let infoPath = path.join(__dirname, "temp", ".git/info"); if (!fse.existsSync(infoPath)) { fse.mkdirSync(infoPath); } fse.writeFileSync( path.join(__dirname, "temp", ".git/info/sparse-checkout"), "/templates" ); executeCommand( `git pull origin master`, path.join(__dirname, "temp") ); fse.removeSync(path.join(__dirname, "temp", "/.git")); const generateUUID = () => { let d = new Date().getTime() return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { let r = (d + Math.random() * 16) % 16 | 0 d = Math.floor(d / 16) return (c == "x" ? r : (r & 0x3) | 0x8).toString(16) }) } let randomString = generateUUID().slice(0, 8) const folderName = `${randomString}scliGenerateTemplate` fse.copySync( path.join(__dirname, "temp/templates"), path.join(__dirname, folderName)); fse.removeSync(path.join(__dirname, "/temp")); spinner.stop() const child = spawn('node', [path.resolve(__dirname, `${folderName}/index.js`), batch ? "--batch" : ""], { stdio: 'inherit', cwd: process.cwd() }); child.on('exit', (code) => { console.log(`Child process exited with code ${code}`); fse.removeSync(path.join(__dirname, folderName)) }); } function executeCommand(command, cwd = __dirname) { try { execa.commandSync(command, {cwd}); } catch (e) { console.error(e) } }