-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (130 loc) · 5.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function () { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function (o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const inquirer = __importStar(require("inquirer"));
const chalk_1 = __importDefault(require("chalk"));
const shell = __importStar(require("shelljs"));
const figlet_1 = __importDefault(require("figlet"));
// const template = __importStar(require("./utils/template"));
const withTitle = (cli) => (0, figlet_1.default)("Vite Svelte App", function (err, data) {
if (err) {
console.log("Something went wrong...");
console.dir(err);
return;
}
console.clear();
console.log(data);
console.log(chalk_1.default.dim(`\n\nVite Svelte template configured with\n\n${chalk_1.default.underline.bgYellow.black("SWC Jest")} ${chalk_1.default.underline.bgYellow.black("ESLint")} ${chalk_1.default.underline.bgYellow.black("Prettier")} ${chalk_1.default.underline.bgYellow.black("Husky")} ${chalk_1.default.underline.bgYellow.black("lint-staged")}\n\n${chalk_1.default.underline.bgYellow.black("StoryBook")} ${chalk_1.default.underline.bgYellow.black("MSW")} ${chalk_1.default.underline.bgYellow.black("Alias")}`));
console.log(chalk_1.default.dim(`\nTemplate can be found at\n\n${chalk_1.default.green("https://github.com/jeus0630/svelte-template")}\n`));
cli();
});
const QUESTIONS = [
{
name: "name",
type: "input",
message: "Please input a new project name:",
}
];
const CURR_DIR = process.cwd();
withTitle(() => inquirer.prompt(QUESTIONS).then((answers) => {
const projectChoice = "svelte-template";
const projectName = answers["name"];
const templatePath = path.join(__dirname, "templates", projectChoice);
const tartgetPath = path.join(CURR_DIR, projectName);
const options = {
projectName,
templateName: projectChoice,
templatePath,
tartgetPath,
packageManagerChoice: "yarn",
};
if (!createProject(options.tartgetPath)) {
return;
}
createDirectoryContents(options.templatePath, options.projectName);
postProcess(options);
}));
function createProject(projectPath) {
if (fs.existsSync(projectPath)) {
console.log(chalk_1.default.red(`Folder ${projectPath} exists. Delete or use another name.`));
return false;
}
fs.mkdirSync(projectPath);
return true;
}
const SKIP_FILES = ["node_modules", ".template.json"];
function createDirectoryContents(templatePath, projectName) {
// read all files/folders (1 level) from template folder
const filesToCreate = fs.readdirSync(templatePath);
// loop each file/folder
filesToCreate.forEach((file) => {
const origFilePath = path.join(templatePath, file);
// get stats about the current file
const stats = fs.statSync(origFilePath);
// skip files that should not be copied
if (SKIP_FILES.indexOf(file) > -1)
return;
if (stats.isFile()) {
// read file content and transform it using template engine
let contents = fs.readFileSync(origFilePath, "utf8");
// contents = template.render(contents, { projectName });
// write file to destination folder
const writePath = path.join(CURR_DIR, projectName, file === "_gitignore" ? ".gitignore" : file);
if (file === "package.json") {
contents = contents.replace("svelte-template", projectName);
}
fs.writeFileSync(writePath, contents, "utf8");
}
else if (stats.isDirectory()) {
// create folder in destination folder
fs.mkdirSync(path.join(CURR_DIR, projectName, file));
// copy files/folder inside current folder recursively
createDirectoryContents(path.join(templatePath, file), path.join(projectName, file));
}
});
}
function postProcess(options) {
const isNode = fs.existsSync(path.join(options.templatePath, "package.json"));
if (isNode) {
shell.cd(options.tartgetPath);
shell.exec("git init ; chmod ug+x .husky/* ; chmod ug+x .git/hooks/*", () => {
console.log(chalk_1.default.underline.bgMagenta.white("\nNew project created successfully!"));
console.log(chalk_1.default.dim("\n\nChange directory into your project folder:"));
console.log(chalk_1.default.yellow(` cd ${options.projectName}`));
console.log(chalk_1.default.dim("\nInstall dependencies:"));
console.log(chalk_1.default.yellow(` yarn`));
console.log(chalk_1.default.dim("\nRun app:"));
console.log(chalk_1.default.yellow(` yarn dev`));
console.log(chalk_1.default.green("\nEnjoy!\n"));
})
}
return true;
}