@nathapp/na-cli 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/index.js +140 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/template.js +9 -0
- package/dist/utils/template.js.map +1 -0
- package/package.json +29 -0
- package/src/index.ts +182 -0
- package/src/utils/template.ts +9 -0
- package/tsconfig.json +24 -0
package/README.md
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
var inquirer = require("inquirer");
|
|
5
|
+
var fs = require("fs");
|
|
6
|
+
var path = require("path");
|
|
7
|
+
var shell = require("shelljs");
|
|
8
|
+
var template = require("./utils/template");
|
|
9
|
+
var chalk_1 = require("chalk");
|
|
10
|
+
var yargs = require("yargs");
|
|
11
|
+
var CHOICES = fs.readdirSync(path.join(__dirname, 'templates'));
|
|
12
|
+
var QUESTIONS = [
|
|
13
|
+
{
|
|
14
|
+
name: 'template',
|
|
15
|
+
type: 'list',
|
|
16
|
+
message: 'What project template would you like to generate?',
|
|
17
|
+
choices: CHOICES,
|
|
18
|
+
when: function () { return !yargs.argv['template']; }
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'name',
|
|
22
|
+
type: 'input',
|
|
23
|
+
message: 'Project name:',
|
|
24
|
+
when: function () { return !yargs.argv['name']; },
|
|
25
|
+
validate: function (input) {
|
|
26
|
+
if (/^([A-Za-z\-\_\d])+$/.test(input))
|
|
27
|
+
return true;
|
|
28
|
+
else
|
|
29
|
+
return 'Project name may only include letters, numbers, underscores and hashes.';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
var CURR_DIR = process.cwd();
|
|
34
|
+
inquirer.prompt(QUESTIONS)
|
|
35
|
+
.then(function (answers) {
|
|
36
|
+
answers = Object.assign({}, answers, yargs.argv);
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
var projectChoice = answers['template'];
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
var projectName = answers['name'];
|
|
41
|
+
var templatePath = path.join(__dirname, 'templates', projectChoice);
|
|
42
|
+
var tartgetPath = path.join(CURR_DIR, projectName);
|
|
43
|
+
var templateConfig = getTemplateConfig(templatePath);
|
|
44
|
+
var options = {
|
|
45
|
+
projectName: projectName,
|
|
46
|
+
templateName: projectChoice,
|
|
47
|
+
templatePath: templatePath,
|
|
48
|
+
tartgetPath: tartgetPath,
|
|
49
|
+
config: templateConfig
|
|
50
|
+
};
|
|
51
|
+
if (!createProject(tartgetPath)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
createDirectoryContents(templatePath, projectName, templateConfig);
|
|
55
|
+
if (!postProcess(options)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
showMessage(options);
|
|
59
|
+
});
|
|
60
|
+
function showMessage(options) {
|
|
61
|
+
console.log('');
|
|
62
|
+
console.log(chalk_1.default.green('Done.'));
|
|
63
|
+
console.log(chalk_1.default.green("Go into the project: cd ".concat(options.projectName)));
|
|
64
|
+
var message = options.config.postMessage;
|
|
65
|
+
if (message) {
|
|
66
|
+
console.log('');
|
|
67
|
+
console.log(chalk_1.default.yellow(message));
|
|
68
|
+
console.log('');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function getTemplateConfig(templatePath) {
|
|
72
|
+
var configPath = path.join(templatePath, '.template.json');
|
|
73
|
+
if (!fs.existsSync(configPath))
|
|
74
|
+
return {};
|
|
75
|
+
var templateConfigContent = fs.readFileSync(configPath);
|
|
76
|
+
if (templateConfigContent) {
|
|
77
|
+
return JSON.parse(templateConfigContent.toString());
|
|
78
|
+
}
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
function createProject(projectPath) {
|
|
82
|
+
if (fs.existsSync(projectPath)) {
|
|
83
|
+
console.log(chalk_1.default.red("Folder ".concat(projectPath, " exists. Delete or use another name.")));
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
fs.mkdirSync(projectPath);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
function postProcess(options) {
|
|
90
|
+
if (isNode(options)) {
|
|
91
|
+
return postProcessNode(options);
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
function isNode(options) {
|
|
96
|
+
return fs.existsSync(path.join(options.templatePath, 'package.json'));
|
|
97
|
+
}
|
|
98
|
+
function postProcessNode(options) {
|
|
99
|
+
shell.cd(options.tartgetPath);
|
|
100
|
+
var cmd = '';
|
|
101
|
+
if (shell.which('yarn')) {
|
|
102
|
+
cmd = 'yarn';
|
|
103
|
+
}
|
|
104
|
+
else if (shell.which('npm')) {
|
|
105
|
+
cmd = 'npm install';
|
|
106
|
+
}
|
|
107
|
+
if (cmd) {
|
|
108
|
+
var result = shell.exec(cmd);
|
|
109
|
+
if (result.code !== 0) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
console.log(chalk_1.default.red('No yarn or npm found. Cannot run installation.'));
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
var SKIP_FILES = ['node_modules', '.template.json'];
|
|
119
|
+
function createDirectoryContents(templatePath, projectName, config) {
|
|
120
|
+
var filesToCreate = fs.readdirSync(templatePath);
|
|
121
|
+
filesToCreate.forEach(function (file) {
|
|
122
|
+
var origFilePath = path.join(templatePath, file);
|
|
123
|
+
// get stats about the current file
|
|
124
|
+
var stats = fs.statSync(origFilePath);
|
|
125
|
+
if (SKIP_FILES.indexOf(file) > -1)
|
|
126
|
+
return;
|
|
127
|
+
if (stats.isFile()) {
|
|
128
|
+
var contents = fs.readFileSync(origFilePath, 'utf8');
|
|
129
|
+
contents = template.render(contents, { projectName: projectName });
|
|
130
|
+
var writePath = path.join(CURR_DIR, projectName, file);
|
|
131
|
+
fs.writeFileSync(writePath, contents, 'utf8');
|
|
132
|
+
}
|
|
133
|
+
else if (stats.isDirectory()) {
|
|
134
|
+
fs.mkdirSync(path.join(CURR_DIR, projectName, file));
|
|
135
|
+
// recursive call
|
|
136
|
+
createDirectoryContents(path.join(templatePath, file), path.join(projectName, file), config);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,mCAAqC;AACrC,uBAAyB;AACzB,2BAA6B;AAC7B,+BAAiC;AACjC,2CAA6C;AAC7C,+BAA0B;AAC1B,6BAA+B;AAE/B,IAAM,OAAO,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC,CAAC;AAElE,IAAM,SAAS,GAAG;IAChB;QACE,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,mDAAmD;QAC5D,OAAO,EAAE,OAAO;QAChB,IAAI,EAAE,cAAM,OAAA,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,EAAvB,CAAuB;KACpC;IACD;QACE,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,OAAO;QACb,OAAO,EAAE,eAAe;QACxB,IAAI,EAAE,cAAM,OAAA,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAnB,CAAmB;QAC/B,QAAQ,EAAE,UAAC,KAAa;YACtB,IAAI,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC;gBAAE,OAAO,IAAI,CAAC;;gBAC9C,OAAO,yEAAyE,CAAC;QACxF,CAAC;KACF;CACF,CAAC;AAEF,IAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;AAe/B,QAAQ,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AQ,EAAE,EAAE,WAAW,aAAA,EAAE,CAAC,CAAC;YAEtD,IAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;YACzD,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;SAC/C;aAAM,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE;YAC9B,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC;YAErD,iBAAiB;YACjB,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAAC;SAC9F;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.render = void 0;
|
|
4
|
+
var ejs = require("ejs");
|
|
5
|
+
function render(content, data) {
|
|
6
|
+
return ejs.render(content, data);
|
|
7
|
+
}
|
|
8
|
+
exports.render = render;
|
|
9
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.js","sourceRoot":"","sources":["../../src/utils/template.ts"],"names":[],"mappings":";;;AAAA,yBAA2B;AAM3B,SAAgB,MAAM,CAAC,OAAe,EAAE,IAAkB;IACtD,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACrC,CAAC;AAFD,wBAEC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nathapp/na-cli",
|
|
3
|
+
"version": "1.0.0-rc.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "ts-node src/index.ts",
|
|
7
|
+
"build": "tsc && shx rm -rf dist/templates && shx cp -r templates dist"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"na-cli": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/ejs": "^3.1.1",
|
|
15
|
+
"@types/inquirer": "^0.0.43",
|
|
16
|
+
"@types/node": "^18.11.18",
|
|
17
|
+
"@types/shelljs": "^0.8.0",
|
|
18
|
+
"@types/yargs": "^17.0.19",
|
|
19
|
+
"shx": "^0.3.2",
|
|
20
|
+
"ts-node": "^10.9.1",
|
|
21
|
+
"typescript": "^4.3.5"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"ejs": "^3.1.8",
|
|
25
|
+
"inquirer": "^6.2.0",
|
|
26
|
+
"shelljs": "^0.8.2",
|
|
27
|
+
"yargs": "^17.6.2"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import * as inquirer from 'inquirer';
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import * as shell from 'shelljs';
|
|
7
|
+
import * as template from './utils/template';
|
|
8
|
+
import chalk from 'chalk';
|
|
9
|
+
import * as yargs from 'yargs';
|
|
10
|
+
|
|
11
|
+
const CHOICES = fs.readdirSync(path.join(__dirname, 'templates'));
|
|
12
|
+
|
|
13
|
+
const QUESTIONS = [
|
|
14
|
+
{
|
|
15
|
+
name: 'template',
|
|
16
|
+
type: 'list',
|
|
17
|
+
message: 'What project template would you like to generate?',
|
|
18
|
+
choices: CHOICES,
|
|
19
|
+
when: () => !yargs.argv['template']
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: 'name',
|
|
23
|
+
type: 'input',
|
|
24
|
+
message: 'Project name:',
|
|
25
|
+
when: () => !yargs.argv['name'],
|
|
26
|
+
validate: (input: string) => {
|
|
27
|
+
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
|
|
28
|
+
else return 'Project name may only include letters, numbers, underscores and hashes.';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const CURR_DIR = process.cwd();
|
|
34
|
+
|
|
35
|
+
export interface TemplateConfig {
|
|
36
|
+
files?: string[]
|
|
37
|
+
postMessage?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CliOptions {
|
|
41
|
+
projectName: string
|
|
42
|
+
templateName: string
|
|
43
|
+
templatePath: string
|
|
44
|
+
tartgetPath: string
|
|
45
|
+
config: TemplateConfig
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
inquirer.prompt(QUESTIONS)
|
|
49
|
+
.then(answers => {
|
|
50
|
+
|
|
51
|
+
answers = Object.assign({}, answers, yargs.argv);
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
const projectChoice = answers['template'];
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
const projectName = answers['name'];
|
|
56
|
+
const templatePath = path.join(__dirname, 'templates', projectChoice);
|
|
57
|
+
const tartgetPath = path.join(CURR_DIR, projectName);
|
|
58
|
+
const templateConfig = getTemplateConfig(templatePath);
|
|
59
|
+
|
|
60
|
+
const options: CliOptions = {
|
|
61
|
+
projectName,
|
|
62
|
+
templateName: projectChoice,
|
|
63
|
+
templatePath,
|
|
64
|
+
tartgetPath,
|
|
65
|
+
config: templateConfig
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!createProject(tartgetPath)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
createDirectoryContents(templatePath, projectName, templateConfig);
|
|
73
|
+
|
|
74
|
+
if (!postProcess(options)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
showMessage(options);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function showMessage(options: CliOptions) {
|
|
82
|
+
console.log('');
|
|
83
|
+
console.log(chalk.green('Done.'));
|
|
84
|
+
console.log(chalk.green(`Go into the project: cd ${options.projectName}`));
|
|
85
|
+
|
|
86
|
+
const message = options.config.postMessage;
|
|
87
|
+
|
|
88
|
+
if (message) {
|
|
89
|
+
console.log('');
|
|
90
|
+
console.log(chalk.yellow(message));
|
|
91
|
+
console.log('');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getTemplateConfig(templatePath: string): TemplateConfig {
|
|
97
|
+
const configPath = path.join(templatePath, '.template.json');
|
|
98
|
+
|
|
99
|
+
if (!fs.existsSync(configPath)) return {};
|
|
100
|
+
|
|
101
|
+
const templateConfigContent = fs.readFileSync(configPath);
|
|
102
|
+
|
|
103
|
+
if (templateConfigContent) {
|
|
104
|
+
return JSON.parse(templateConfigContent.toString());
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function createProject(projectPath: string) {
|
|
111
|
+
if (fs.existsSync(projectPath)) {
|
|
112
|
+
console.log(chalk.red(`Folder ${projectPath} exists. Delete or use another name.`));
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fs.mkdirSync(projectPath);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function postProcess(options: CliOptions) {
|
|
121
|
+
if (isNode(options)) {
|
|
122
|
+
return postProcessNode(options);
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function isNode(options: CliOptions) {
|
|
128
|
+
return fs.existsSync(path.join(options.templatePath, 'package.json'));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function postProcessNode(options: CliOptions) {
|
|
132
|
+
shell.cd(options.tartgetPath);
|
|
133
|
+
|
|
134
|
+
let cmd = '';
|
|
135
|
+
|
|
136
|
+
if (shell.which('yarn')) {
|
|
137
|
+
cmd = 'yarn';
|
|
138
|
+
} else if (shell.which('npm')) {
|
|
139
|
+
cmd = 'npm install';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (cmd) {
|
|
143
|
+
const result = shell.exec(cmd);
|
|
144
|
+
|
|
145
|
+
if (result.code !== 0) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
console.log(chalk.red('No yarn or npm found. Cannot run installation.'));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const SKIP_FILES = ['node_modules', '.template.json'];
|
|
156
|
+
|
|
157
|
+
function createDirectoryContents(templatePath: string, projectName: string, config: TemplateConfig) {
|
|
158
|
+
const filesToCreate = fs.readdirSync(templatePath);
|
|
159
|
+
|
|
160
|
+
filesToCreate.forEach(file => {
|
|
161
|
+
const origFilePath = path.join(templatePath, file);
|
|
162
|
+
|
|
163
|
+
// get stats about the current file
|
|
164
|
+
const stats = fs.statSync(origFilePath);
|
|
165
|
+
|
|
166
|
+
if (SKIP_FILES.indexOf(file) > -1) return;
|
|
167
|
+
|
|
168
|
+
if (stats.isFile()) {
|
|
169
|
+
let contents = fs.readFileSync(origFilePath, 'utf8');
|
|
170
|
+
|
|
171
|
+
contents = template.render(contents, { projectName });
|
|
172
|
+
|
|
173
|
+
const writePath = path.join(CURR_DIR, projectName, file);
|
|
174
|
+
fs.writeFileSync(writePath, contents, 'utf8');
|
|
175
|
+
} else if (stats.isDirectory()) {
|
|
176
|
+
fs.mkdirSync(path.join(CURR_DIR, projectName, file));
|
|
177
|
+
|
|
178
|
+
// recursive call
|
|
179
|
+
createDirectoryContents(path.join(templatePath, file), path.join(projectName, file), config);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "src",
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"target": "es5",
|
|
6
|
+
"lib": ["es6", "dom"],
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"noImplicitReturns": true,
|
|
12
|
+
"noImplicitThis": true,
|
|
13
|
+
"noImplicitAny": true,
|
|
14
|
+
"strictNullChecks": true,
|
|
15
|
+
"suppressImplicitAnyIndexErrors": true,
|
|
16
|
+
"noUnusedLocals": true
|
|
17
|
+
},
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist",
|
|
21
|
+
"templates"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
|