custom-app
Version:
ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������
123 lines (115 loc) • 5.15 kB
JavaScript
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
let spinner = require('../util/spinner');
let util = require('../util/util.js')
const os = require('os');
let exec = require('child_process').exec;
function init (moduleName, projectName, options) {
const cwd = options.cwd || process.cwd()
const reactAppPath = util.getReactAppPath(cwd);
const inCurrent = projectName === '.'
const targetDir = inCurrent ? reactAppPath : path.join(reactAppPath, 'src', projectName)
if (fs.existsSync(targetDir)) {
options = options || []
let loadConfig = options.hasOwnProperty("config") && options.config;
let loadInstall = options.hasOwnProperty("install") && options.install;
let load2ndpage = options.hasOwnProperty("itims42ndpage") && options.itims42ndpage;
let filePath = __dirname.substring(0, __dirname.lastIndexOf("lib"));
let configPath = path.join(filePath, 'config.zip');
let examplePath = path.join(filePath, 'example.zip');
let mavenPath = path.join(filePath, 'maven.zip');
let dllPath = path.join(filePath, 'dll', 'dll.zip');
if (loadConfig && !loadInstall) {
//配置
console.log(configPath + "___" + targetDir);
require('./unzip.js')(configPath, targetDir)
//require('./unzip.js')(mavenPath, targetDir)
//require('./unzip.js')(examplePath, targetDir)
//require('./unzip.js')(dllPath, targetDir)
} else if (!loadConfig && loadInstall) {
//脚本
require('./install.js')(reactAppPath, moduleName, projectName, function () {
spinner.stopSpinner();
})
} else if (load2ndpage) {
//脚本
require('./2ndpage.js')(path.join(reactAppPath, 'src'), function () {
spinner.stopSpinner();
})
} else {
if (fs.existsSync(path.join(reactAppPath, "package.json"))) {
let packageJson = require(path.join(reactAppPath, 'package.json'))
let oldName = packageJson.name
if (moduleName) {
if (moduleName == oldName) {
spinner.logWithSpinner('', chalk.cyan('初始化配置文件'))
var install = 'npm install';
exec(install, { cwd: reactAppPath }, function (err, stdout, stderr) {
if (err) {
console.error(chalk.red('初始化配置文件:' + stderr));
console.log(stdout);
spinner.stopSpinner();
} else {
spinner.logWithSpinner('', stdout)
spinner.logWithSpinner(chalk.cyan('初始化配置文件 成功'))
spinner.logWithSpinner(' ');
spinner.stopSpinner();
}
});
} else {
console.error(chalk.red('模块名称跟初始化的不一致,如果需要更新模块名称,请手动修改package.json的name字段'));
}
} else {
spinner.logWithSpinner('', chalk.cyan('初始化配置文件'))
var install = 'npm install';
let workerProcess = exec(install, { cwd: reactAppPath }, function (err, stdout, stderr) {
if (err) {
console.error(chalk.red('初始化配置文件:' + stderr));
console.log(stdout);
spinner.stopSpinner();
} else {
spinner.logWithSpinner('', stdout)
spinner.logWithSpinner(chalk.cyan('初始化配置文件 成功'))
spinner.logWithSpinner(' ');
spinner.stopSpinner();
}
});
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
}
} else {
if (!moduleName) {
console.error(chalk.red('工作空间不存在时,不能省略 `module-name`参数'));
return;
}
//脚本+配置
require('./install.js')(reactAppPath, moduleName, projectName, function () {
require('./unzip.js')(configPath, targetDir)
//require('./unzip.js')(dllPath, targetDir)
// require('./unzip.js')(mavenPath, targetDir)
// require('./unzip.js')(examplePath, targetDir)
const packageJson = require(path.join(targetDir, 'package.json'))
packageJson.name = moduleName;
fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(packageJson, null, 2) + os.EOL);
let gitignorePath = path.join(targetDir, ".gitignore")
let writerStream = fs.createWriteStream(gitignorePath);
writerStream.write('node_modules\nbuild', 'UTF8');
writerStream.end(); //标记文件末尾 结束写入流,释放资源
writerStream.on('finish', function () {
console.log("写入完成。");
});
writerStream.on('error', function (error) {
console.log(error.stack);
});
})
}
}
} else {
console.log(chalk.red(projectName + '不存在'));
}
}
module.exports = (...args) => {
init(...args)
}