UNPKG

create-elmer-ui-app

Version:

create an develop envirnment for elmer-ui-core

215 lines (212 loc) 10.1 kB
require("colors"); var Base = require("./Base"); var path = require("path"); var program = require("commander"); var fs = require("fs"); var createConfig = require("../data/config"); var packageConfig = createConfig.packageJson; var tsLintConfig = createConfig.tsLint; var readlineSync = require('readline-sync'); var cmd = require("node-cmd"); var childProcess = require("child_process"); var __extendBasic = function (a, b) { if (typeof a === "function" && typeof b === "function") { for (var key in b.prototype) { a.prototype[key] = b.prototype[key]; console.log("redefine property", key); } } } var AppCreater = (function (_super) { function AppCreater() { // constructor var _this = _super.call(this) || this; return _this; } __extendBasic(AppCreater.prototype, _super.prototype); AppCreater.prototype.isDev = false AppCreater.prototype.readSyn = function(){ process.stdin.pause(); var response = fs.readSync(process.stdin.fd, 1000, 0, "utf8"); process.stdin.resume(); return response[0].trim(); }; AppCreater.prototype.checkExists = function(checkUrl) { try{ return fs.statSync(checkUrl); }catch(e) { return false; } } AppCreater.prototype.run = function () { program.option("-m, --mode [mode]", "setting run mode, dev or prod").parse(process.argv); var env = program.mode || "prod"; var projectPath = path.resolve("./"); var name = readlineSync.question('What\'s your project name? '); this.isDev = /(dev|development)/i.test(env); if(/^[a-z0-9_\-\.]*$/i.test(name) && name.length>0){ projectPath = /\\$/.test(projectPath) ? projectPath : projectPath + "\\"; var folder = projectPath + name; var _this = this; if(!this.checkExists(folder)){ fs.mkdir(folder, function(err){ if(err) { console.log(err.message); return false; } console.log("Folder do not exists ,create an new folder.".yellow); console.log(folder.yellow); _this.savePackageJson(folder); }); } else { _this.savePackageJson(folder); } } else { console.log("Input project name format error!".red); } }; AppCreater.prototype.saveFile=function(fileName, data, success, error) { var writeStream = fs.createWriteStream(fileName); writeStream.on("finish", function(){ typeof success === "function" && success(); }); writeStream.on("error", function(err){ typeof error === "function" && success(err); }); if(typeof data === "object"){ writeStream.write(JSON.stringify(data,null, 4), "utf8"); } else { writeStream.write(data, "utf8"); } writeStream.end(); } AppCreater.prototype.savePackageJson = function(savePath){ var _this = this; fs.readdir(savePath, function(err, data){ if(err) { console.error(err); } else { if(data && data.length>0) { console.log("Please make sure the save path is empty folder".red); console.log(savePath.bgWhite.black); } else { var description = readlineSync.question("The description for the new project: "); packageConfig.description = description; var hasPackageJson = false,hasTsLint = false,hasPostCss = false,hasBabelrc = false; // ------------创建package.json _this.saveFile(savePath + "/package.json", packageConfig, function(){ console.log("The package.json file created successfully".green); hasPackageJson = true; hasPackageJson && hasPostCss && hasTsLint && hasBabelrc && _this.createWebpackConfig(savePath); }, function(err){ console.error(err); }); // --------创建tsLint _this.saveFile(savePath + "/tslint.json", tsLintConfig, function(){ console.log("The tslint.json file created successfully".green); hasTsLint = true; hasPackageJson && hasPostCss && hasTsLint && hasBabelrc && _this.createWebpackConfig(savePath); }, function(err){ console.error(err); }); // --------创建postcss _this.saveFile(savePath + "/postcss.config.js", createConfig.postcss, function(){ console.log("The postcss.config.js file created successfully".green); hasPostCss = true; hasPackageJson && hasPostCss && hasTsLint && hasBabelrc && _this.createWebpackConfig(savePath); }, function(err){ console.error(err); }); // --------创建babelrc _this.saveFile(savePath + "/.babelrc", createConfig.babelrc, function(){ console.log("The .babelrc file created successfully".green); hasBabelrc = true; hasPackageJson && hasPostCss && hasTsLint && hasBabelrc && _this.createWebpackConfig(savePath); }, function(err){ console.error(err); }); _this.saveFile(savePath + "/tsconfig.json", createConfig.tsConfig, function(){ console.log("The tsconfig.json file created successfully".green); }, function(err){ console.error(err); }); } } }); }; AppCreater.prototype.createWebpackConfig = function(savePath){ var saveConfigPath = savePath + "/config"; var _this = this; fs.mkdir(saveConfigPath, function(err) { if(err) { console.error(err); } else { var commConfig = fs.readFileSync(path.resolve(__dirname, "../data/webpack.common.js"), "utf8"); var devConfig = fs.readFileSync(path.resolve(__dirname, "../data/webpack.config.dev.js"), "utf8"); var prodConfig = fs.readFileSync(path.resolve(__dirname, "../data/webpack.config.prod.js"), "utf8"); var commonCreated = false,devCreated = false,prodCreated = false; _this.saveFile(saveConfigPath + "/webpack.common.js", commConfig, function(){ console.log("The config/webpack.common.js file created successfully".green); commonCreated = true; commonCreated && devCreated && prodCreated && _this.installPackage(savePath); }, function(err){ console.error(err); }); _this.saveFile(saveConfigPath + "/webpack.config.dev.js", devConfig, function(){ console.log("The config/webpack.common.js file created successfully".green); devCreated = true; commonCreated && devCreated && prodCreated && _this.installPackage(savePath); }, function(err){ console.error(err); }); _this.saveFile(saveConfigPath + "/webpack.config.prod.js", prodConfig, function(){ console.log("The config/webpack.common.js file created successfully".green); prodCreated = true; commonCreated && devCreated && prodCreated && _this.installPackage(savePath); }, function(err){ console.error(err); }); } }); }; AppCreater.prototype.copyDemoFiles = function(savePath){ var _this = this; var srcPath = savePath + "/src"; try{ if(!this.checkExists(srcPath)){ fs.mkdir(srcPath, function(err){ if(!err) { var indexHtml = fs.readFileSync(path.resolve(__dirname, "../data/src/index.html"), "utf8"); _this.saveFile(srcPath + "/index.html", indexHtml); var indexTs = fs.readFileSync(path.resolve(__dirname, "../data/src/index.ts"), "utf8"); _this.saveFile(srcPath + "/index.ts", indexTs); var globalTypeCode = fs.readFileSync(path.resolve(__dirname, "../data/src/global.d.ts"), "utf8"); _this.saveFile(srcPath + "/global.d.ts", globalTypeCode); } else { console.log("Create demo code fail".red, err); } }); } }catch(e) { console.error(e); } } AppCreater.prototype.installPackage = function(savePath){ this.copyDemoFiles(savePath); console.log("Start install package...".green); console.log(`Enter ${savePath}`.yellow); var subprocess = childProcess.spawn("yarn",["install"], { cwd: savePath }); subprocess.on('error', function(err) { console.error('Install package fail'.red, err); }); subprocess.on('close', function (code) { if (code !== 0) { console.log(`install process exit,exit code: ${code}`); } }); }; return AppCreater; })(Base); module.exports.AppCreater = AppCreater;