-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
146 lines (123 loc) · 4.31 KB
/
index.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
import * as inquirer from 'inquirer';
import chalk from 'chalk';
import * as template from './utils/template';
import * as shell from 'shelljs';
import {Answers} from 'inquirer';
import {ShellString} from "shelljs";
console.log(chalk.green.bold('Welcome to the Next-Typescript generator \n'));
const CHOICES = fs.readdirSync(path.join(__dirname, 'templates'));
const PACKAGE_MANAGERS = ['Yarn', 'NPM'];
const QUESTIONS = [
{
name: 'template',
type: 'list',
message: 'What template would you like to use?',
choices: CHOICES
},
{
name: 'name',
type: 'input',
message: 'Please input a new project name:'
},
{
name: 'dependencies',
type: 'confirm',
message: 'Do you want us to install the dependencies for your project?'
},
{
name: 'package-manager',
type: 'list',
message: 'Do you want to install the dependencies with which package manager?',
when: function(answers: Answers){return answers.dependencies},
choices: PACKAGE_MANAGERS
}
];
export interface CliOptions {
projectName: string
templateName: string
templatePath: string
tartgetPath: string
}
const CURR_DIR = process.cwd();
inquirer.prompt(QUESTIONS).then((answers: Answers) => {
const projectChoice = answers['template'];
const projectName = answers['name'];
const dependencies = answers['dependencies'];
const packageManager = answers['package-manager'];
//@ts-ignore
const templatePath = path.join(__dirname, 'templates', projectChoice);
//@ts-ignore
const tartgetPath = path.join(CURR_DIR, projectName);
const options: CliOptions = {
//@ts-ignore
projectName,
//@ts-ignore
templateName: projectChoice,
templatePath,
tartgetPath
};
if (!createProject(tartgetPath)) {
return;
}
//@ts-ignore
createDirectoryContents(templatePath, projectName);
if(dependencies) {
postProcess(options, packageManager);
}
console.log(chalk.green.bold('\n Your project is ready to go'));
});
function createProject(projectPath: string) {
if (fs.existsSync(projectPath)) {
console.log(chalk.red(`Folder ${projectPath} exists. Delete or use another name.`));
return false;
}
fs.mkdirSync(projectPath);
return true;
}
const SKIP_FILES = ['node_modules', '.template.json', '.next', 'yarn.lock', 'package-lock.json'];
function createDirectoryContents(templatePath: string, projectName: string) {
// 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);
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: CliOptions, packageManager: string) {
const isNode = fs.existsSync(path.join(options.templatePath, 'package.json'));
if (isNode) {
shell.cd(options.tartgetPath);
let result: ShellString;
if(packageManager == 'NPM') {
result = shell.exec('npm install');
} else if (packageManager == 'Yarn') {
result = shell.exec('yarn');
} else {
return false
}
if (result.code !== 0) {
return false;
}
}
return true;
}