UNPKG

3.81 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const utils_1 = require("../utils");
12const fs_1 = require("fs");
13const path_1 = require("path");
14const globby = require("globby");
15const mkdirp = require("mkdirp");
16const rimraf = require("rimraf");
17const sh = require("shelljs");
18const chalk_1 = require("chalk");
19const writeFile = (fullpath, content) => {
20 const sections = fullpath.split("/");
21 sections.pop();
22 const path = sections.join("/");
23 mkdirp.sync(path);
24 fs_1.writeFileSync(fullpath, content);
25};
26const copyTemplate = (name, dirname, { mode, d, force }) => __awaiter(this, void 0, void 0, function* () {
27 const pkg = {
28 name,
29 version: "0.0.1",
30 private: true,
31 scripts: {
32 dev: "tp dev",
33 build: "tp build",
34 test: "tp test"
35 }
36 };
37 const paths = yield globby(path_1.join(__dirname, `../../templates/${mode}/**/*`));
38 const files = paths.map((path) => {
39 const splittedName = path.split(path_1.join(__dirname, `../../templates/${mode}/`));
40 return {
41 name: splittedName[splittedName.length - 1],
42 content: fs_1.readFileSync(path)
43 };
44 });
45 if (d) {
46 console.log(paths);
47 console.log(files);
48 }
49 if (!paths || !paths.length) {
50 console.log(chalk_1.default.red("Template not exists."));
51 process.exit(1);
52 return;
53 }
54 if (force) {
55 rimraf.sync(dirname);
56 }
57 if (fs_1.existsSync(dirname)) {
58 console.log(chalk_1.default.red("Dirname already exists."));
59 console.log(`use ${chalk_1.default.blue("--force")} to delete it and create a new one.`);
60 process.exit(1);
61 return;
62 }
63 mkdirp.sync(dirname);
64 files.forEach(file => {
65 writeFile(path_1.join(utils_1.rootPath, name, file.name), file.content);
66 });
67 writeFile(path_1.join(utils_1.rootPath, name, "package.json"), JSON.stringify(pkg, null, 2));
68});
69const installPackages = (dirname, mode) => {
70 const hasYarn = sh.which("yarn");
71 const install = hasYarn ? "yarn add" : "npm install --save";
72 const devInstall = hasYarn ? "yarn add -D" : "npm install --save-dev";
73 const dependencies = ["@babel/runtime"];
74 const devDependencies = [
75 "typepack",
76 "@types/node",
77 "@types/jest",
78 "@babel/core",
79 "@babel/preset-env",
80 "@babel/plugin-transform-runtime"
81 ];
82 if (mode === "react") {
83 const reactDeps = ["react", "react-dom"];
84 const reactDevDeps = ["@types/react", "@types/react-dom"];
85 dependencies.push(...reactDeps);
86 devDependencies.push(...reactDevDeps);
87 }
88 sh.cd(dirname);
89 if (sh.exec(`${install} ${dependencies.join(" ")}`).code !== 0 ||
90 sh.exec(`${devInstall} ${devDependencies.join(" ")}`).code !== 0) {
91 sh.echo("Error: failed to install needed modules");
92 sh.exit(1);
93 }
94};
95module.exports = (name, opts) => __awaiter(this, void 0, void 0, function* () {
96 const dirname = path_1.join(utils_1.rootPath, name);
97 yield copyTemplate(name, dirname, opts);
98 installPackages(dirname, opts.mode);
99});
100//# sourceMappingURL=init.js.map
\No newline at end of file