emnj-tool 1.0.3 → 1.0.4
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/dist/package.json +38 -0
- package/dist/src/h5.js +54 -0
- package/dist/src/index.js +97 -0
- package/dist/src/util.js +3 -0
- package/package.json +1 -1
- package/tsconfig.json +1 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
{
|
2
|
+
"name": "emnj-tool",
|
3
|
+
"version": "1.0.4",
|
4
|
+
"description": "一个工具,用来初始化项目",
|
5
|
+
"type": "module",
|
6
|
+
"exports": "./index.js",
|
7
|
+
"engines": {
|
8
|
+
"node": ">=16.0.0"
|
9
|
+
},
|
10
|
+
"scripts": {
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
12
|
+
"build": "npx tsc"
|
13
|
+
},
|
14
|
+
"bin": {
|
15
|
+
"emnj-tool": "./dist/index.js"
|
16
|
+
},
|
17
|
+
"keywords": [
|
18
|
+
"emnj-tool"
|
19
|
+
],
|
20
|
+
"author": "",
|
21
|
+
"license": "ISC",
|
22
|
+
"devDependencies": {
|
23
|
+
"@types/fs-extra": "^11.0.4",
|
24
|
+
"@types/inquirer": "^9.0.7",
|
25
|
+
"@types/node": "^20.11.28",
|
26
|
+
"ts-node": "^10.9.2",
|
27
|
+
"typescript": "^5.4.2"
|
28
|
+
},
|
29
|
+
"dependencies": {
|
30
|
+
"@types/unzipper": "^0.10.9",
|
31
|
+
"chalk": "^5.3.0",
|
32
|
+
"commander": "^12.0.0",
|
33
|
+
"download-git-repo": "^3.0.2",
|
34
|
+
"fs-extra": "^11.2.0",
|
35
|
+
"inquirer": "^9.2.16",
|
36
|
+
"unzipper": "^0.10.14"
|
37
|
+
}
|
38
|
+
}
|
package/dist/src/h5.js
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
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) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import fse from "fs-extra";
|
11
|
+
import path from "path";
|
12
|
+
import * as unzipper from "unzipper";
|
13
|
+
import { fileURLToPath } from "url";
|
14
|
+
const log = console.log;
|
15
|
+
export default class CreateH5 {
|
16
|
+
constructor(answerOptions) {
|
17
|
+
this.answerOptions = answerOptions;
|
18
|
+
}
|
19
|
+
init() {
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
21
|
+
console.log("init h5", this.answerOptions);
|
22
|
+
const dirPath = yield this.mkdir();
|
23
|
+
this.unzipH5Template(dirPath);
|
24
|
+
});
|
25
|
+
}
|
26
|
+
mkdir() {
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
28
|
+
// create dir in current workspace use option name
|
29
|
+
const targetDirPath = path.join(process.cwd(), this.answerOptions.name);
|
30
|
+
fse.ensureDirSync(targetDirPath);
|
31
|
+
return targetDirPath;
|
32
|
+
});
|
33
|
+
}
|
34
|
+
/**
|
35
|
+
* unzip h5 template to current workspace path
|
36
|
+
*/
|
37
|
+
unzipH5Template(targetDirPath) {
|
38
|
+
// process.cwd() 获取当前执行node命令时候的文件夹(工作区)目录名
|
39
|
+
// 获取当前执行文件的目录,即CLI工具的目录
|
40
|
+
const __filename = fileURLToPath(import.meta.url);
|
41
|
+
const dirname = path.dirname(__filename);
|
42
|
+
const templatePath = path.join(dirname, "../templates/ts-h5.zip");
|
43
|
+
fse
|
44
|
+
.createReadStream(templatePath)
|
45
|
+
.pipe(unzipper.Extract({ path: targetDirPath }))
|
46
|
+
.on("close", () => {
|
47
|
+
const json = fse.readJSONSync(path.join(targetDirPath, "package.json"), {
|
48
|
+
encoding: "utf8",
|
49
|
+
});
|
50
|
+
const packageJson = Object.assign(Object.assign({}, json), { name: this.answerOptions.name, version: this.answerOptions.version, description: this.answerOptions.description });
|
51
|
+
fse.writeJSONSync(path.join(targetDirPath, "package.json"), packageJson, { encoding: "utf8", flag: "w", spaces: 2 });
|
52
|
+
});
|
53
|
+
}
|
54
|
+
}
|
@@ -0,0 +1,97 @@
|
|
1
|
+
#! /usr/bin/env node
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
import { Command } from "commander";
|
12
|
+
import inquirer from "inquirer";
|
13
|
+
import CreateH5 from "./h5.js";
|
14
|
+
import chalk from "chalk";
|
15
|
+
import pkg from "../package.json";
|
16
|
+
const program = new Command();
|
17
|
+
const log = console.log;
|
18
|
+
program.name("项目初始化").description("CLI to Initialize some project").version(pkg.version);
|
19
|
+
// program
|
20
|
+
// .command("split")
|
21
|
+
// .description("Split a string into an array of strings using a separator.")
|
22
|
+
// .argument("<string>", "The string to split")
|
23
|
+
// .option("--first", "display just the first substring")
|
24
|
+
// .option("-s, --separator <char>", "separator character", ",")
|
25
|
+
// .action((str, options) => {
|
26
|
+
// console.log("---->> ", options);
|
27
|
+
// const limit = options.first ? 1 : undefined;
|
28
|
+
// console.log(str.split(options.separator, limit));
|
29
|
+
// });
|
30
|
+
program
|
31
|
+
.command("init")
|
32
|
+
.description("初始化项目,支持H5,Web等")
|
33
|
+
.option("-h5, --h5", "Initialize an H5 project.", false)
|
34
|
+
.option("-w, --web", "Initialize a Web project.", false)
|
35
|
+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
|
36
|
+
// 如果没有配置argument,则第一个参数就是options
|
37
|
+
if (options.h5 === false && options.web === false) {
|
38
|
+
log("请使用" + chalk.bgBlackBright.bold("emnj init --h5") + "或者" + chalk.bgBlackBright.bold("emnj init --web") + "来初始化项目");
|
39
|
+
process.exit(1);
|
40
|
+
}
|
41
|
+
try {
|
42
|
+
const confirmAnswers = yield inquirer.prompt([
|
43
|
+
{
|
44
|
+
type: "confirm",
|
45
|
+
name: "confirmAction",
|
46
|
+
message: `确认初始化一个${options.h5 ? "H5" : "Web"}工程项目`,
|
47
|
+
default: true,
|
48
|
+
},
|
49
|
+
]);
|
50
|
+
if (!confirmAnswers.confirmAction) {
|
51
|
+
const message = "取消初始化";
|
52
|
+
log(chalk.redBright(message));
|
53
|
+
throw new Error(message);
|
54
|
+
}
|
55
|
+
const actionAnswers = yield inquirer.prompt([
|
56
|
+
{
|
57
|
+
type: "input",
|
58
|
+
name: "name",
|
59
|
+
message: "请输入名称:",
|
60
|
+
default: "h5-project",
|
61
|
+
validate: function (value) {
|
62
|
+
return true;
|
63
|
+
},
|
64
|
+
},
|
65
|
+
{
|
66
|
+
type: "input",
|
67
|
+
name: "version",
|
68
|
+
message: "请输入版本:",
|
69
|
+
default: "0.0.1",
|
70
|
+
},
|
71
|
+
{
|
72
|
+
type: "input",
|
73
|
+
name: "description",
|
74
|
+
message: "请输入项目描述:",
|
75
|
+
default: "这是一个H5项目",
|
76
|
+
},
|
77
|
+
{
|
78
|
+
type: "list",
|
79
|
+
name: "language",
|
80
|
+
message: "是否使用typescript",
|
81
|
+
default: true,
|
82
|
+
choices: ["typescript", "javascript"],
|
83
|
+
},
|
84
|
+
]);
|
85
|
+
if (options.h5 === true) {
|
86
|
+
const h5 = new CreateH5(actionAnswers);
|
87
|
+
h5.init();
|
88
|
+
}
|
89
|
+
else if (options.web === true) {
|
90
|
+
log("初始化web项目");
|
91
|
+
}
|
92
|
+
}
|
93
|
+
catch (error) {
|
94
|
+
process.exit(1);
|
95
|
+
}
|
96
|
+
}));
|
97
|
+
program.parse(process.argv);
|
package/dist/src/util.js
ADDED
package/package.json
CHANGED