UNPKG

custom-app

Version:

ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������

59 lines (56 loc) 2.39 kB
const exec = require('child_process').exec; const fs = require('fs-extra') const path = require('path') const chalk = require('chalk') const os = require('os') let spinner = require('../util/spinner'); let util = require('../util/util.js') let unzipper = require("unzipper"); function init (projectName, options) { const cwd = options.cwd || process.cwd() const reactAppPath = util.getReactAppPath(cwd); execPath = path.join(reactAppPath, 'src') const targetDir = path.join(reactAppPath, 'src', projectName) if (!fs.existsSync(targetDir)) { fs.mkdirSync(targetDir) let moduleName = '' let packageJson = require(path.join(reactAppPath, 'package.json')) if (packageJson) { moduleName = packageJson.name } let filePath = __dirname.substring(0, __dirname.lastIndexOf("lib")); let configPath = path.join(filePath, 'plugin.zip'); fs.createReadStream(configPath) .pipe(unzipper.Parse()) .on('entry', function (entry) { var fileName = entry.path; var type = entry.type; // 'Directory' or 'File' var size = entry.size; if (type === "File") { entry.pipe(fs.createWriteStream(targetDir + "/" + fileName)); } else { if (!fs.existsSync(path.join(targetDir, fileName))) { fs.mkdirSync(path.join(targetDir, fileName)) } } }).on('end', function (entry) { console.log("package.json 写入名字") let packageJson = require(path.join(targetDir, 'package.json')) packageJson.name = 'itims-' + (moduleName != '' ? (moduleName + '-') : '') + projectName; packageJson.moduleType = 'plugin'; fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(packageJson, null, 2) + os.EOL); }).on('close', function (entry) { console.log("package.json 写入名字2") let packageJson = require(path.join(targetDir, 'package.json')) packageJson.name = 'itims-' + (moduleName != '' ? (moduleName + '-') : '') + projectName; packageJson.moduleType = 'plugin'; fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(packageJson, null, 2) + os.EOL); }) } else { console.log(chalk.red(projectName + '已经存在')); } } var execPath; module.exports = (...args) => { init(...args) }