emnj-tool 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/.prettierrc ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "printWidth": 80,
3
+ "tabWidth": 2,
4
+ "useTabs": false,
5
+ "semi": true,
6
+ "singleQuote": false,
7
+ "quoteProps": "as-needed",
8
+ "jsxSingleQuote": false,
9
+ "trailingComma": "es5",
10
+ "bracketSpacing": true,
11
+ "jsxBracketSameLine": false,
12
+ "arrowParens": "always",
13
+ "rangeStart": 0,
14
+ "requirePragma": false,
15
+ "insertPragma": false,
16
+ "proseWrap": "preserve",
17
+ "htmlWhitespaceSensitivity": "css",
18
+ "vueIndentScriptAndStyle": false,
19
+ "endOfLine": "auto"
20
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Launch Program",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}/src/index.ts",
15
+ "preLaunchTask": "tsc: build - tsconfig.json",
16
+ "outFiles": [
17
+ "${workspaceFolder}/dist/**/*.js"
18
+ ]
19
+ }
20
+ ]
21
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "editor.formatOnPaste": false,
3
+ "editor.formatOnSave": true,
4
+ "editor.defaultFormatter": "esbenp.prettier-vscode"
5
+ }
package/dist/h5/h5.js ADDED
@@ -0,0 +1,40 @@
1
+ import fse from "fs-extra";
2
+ import path from "path";
3
+ import unzipper from "unzipper";
4
+ export default class InitH5 {
5
+ answerOptions;
6
+ constructor(answerOptions) {
7
+ this.answerOptions = answerOptions;
8
+ }
9
+ init() {
10
+ console.log("init h5", this.answerOptions);
11
+ this.mkdir();
12
+ }
13
+ async mkdir() {
14
+ // create dir in current workspace use option name
15
+ const targetDirPath = path.join(process.cwd(), this.answerOptions.name);
16
+ fse.ensureDirSync(targetDirPath);
17
+ this.unzipH5Template(targetDirPath);
18
+ }
19
+ /**
20
+ * unzip h5 template to current workspace path
21
+ */
22
+ unzipH5Template(targetDirPath) {
23
+ const templatePath = path.join(process.cwd(), "./templates/ts-h5.zip");
24
+ fse
25
+ .createReadStream(templatePath)
26
+ .pipe(unzipper.Extract({ path: targetDirPath }))
27
+ .on("close", () => {
28
+ const json = fse.readJSONSync(path.join(targetDirPath, "package.json"), {
29
+ encoding: "utf8",
30
+ });
31
+ const packageJson = {
32
+ ...json,
33
+ name: this.answerOptions.name,
34
+ version: this.answerOptions.version,
35
+ description: this.answerOptions.description,
36
+ };
37
+ fse.writeJSONSync(path.join(targetDirPath, "package.json"), packageJson, { encoding: "utf8", flag: "w", spaces: 2 });
38
+ });
39
+ }
40
+ }
package/dist/index.js ADDED
@@ -0,0 +1,96 @@
1
+ #! /usr/bin/env node
2
+ import { Command } from "commander";
3
+ import inquirer from "inquirer";
4
+ import InitH5 from "./h5/h5.js";
5
+ import chalk from "chalk";
6
+ const program = new Command();
7
+ const log = console.log;
8
+ program
9
+ .name("project cli")
10
+ .description("CLI to Initialize some project")
11
+ .version("0.0.1");
12
+ program
13
+ .command("split")
14
+ .description("Split a string into an array of strings using a separator.")
15
+ .argument("<string>", "The string to split")
16
+ .option("--first", "display just the first substring")
17
+ .option("-s, --separator <char>", "separator character", ",")
18
+ .action((str, options) => {
19
+ console.log("---->> ", options);
20
+ const limit = options.first ? 1 : undefined;
21
+ console.log(str.split(options.separator, limit));
22
+ });
23
+ program
24
+ .command("init")
25
+ .description("初始化项目,支持H5,Web等")
26
+ .option("-h5, --h5", "Initialize an H5 project.", false)
27
+ .option("-w, --web", "Initialize a Web project.", false)
28
+ .action(async (options) => {
29
+ // 如果没有配置argument,则第一个参数就是options
30
+ if (options.h5 === false && options.web === false) {
31
+ log("请使用" +
32
+ chalk.bgRed.bold("emnj init --h5") +
33
+ "或者" +
34
+ chalk.bgRed.bold("emnj init --web") +
35
+ "来初始化项目");
36
+ process.exit(1);
37
+ }
38
+ try {
39
+ const confirmAnswers = await inquirer.prompt([
40
+ {
41
+ type: "confirm",
42
+ name: "confirmAction",
43
+ message: `确认初始化一个${options.h5 ? "H5" : "Web"}工程项目`,
44
+ default: true,
45
+ },
46
+ ]);
47
+ if (!confirmAnswers.confirmAction) {
48
+ const message = "取消初始化";
49
+ log(chalk.redBright(message));
50
+ throw new Error(message);
51
+ }
52
+ const actionAnswers = await inquirer.prompt([
53
+ {
54
+ type: "input",
55
+ name: "name",
56
+ message: "请输入名称:",
57
+ default: "h5-project",
58
+ validate: function (value) {
59
+ return true;
60
+ },
61
+ },
62
+ {
63
+ type: "input",
64
+ name: "version",
65
+ message: "请输入版本:",
66
+ default: "0.0.1",
67
+ },
68
+ {
69
+ type: "input",
70
+ name: "description",
71
+ message: "请输入项目描述:",
72
+ default: "这是一个H5项目",
73
+ },
74
+ {
75
+ type: "list",
76
+ name: "language",
77
+ message: "是否使用typescript",
78
+ default: true,
79
+ choices: ["typescript", "javascript"],
80
+ },
81
+ ]);
82
+ if (options.h5 === true) {
83
+ const initH5 = new InitH5(actionAnswers);
84
+ initH5.init();
85
+ return;
86
+ }
87
+ if (options.web === true) {
88
+ log("初始化web项目");
89
+ return;
90
+ }
91
+ }
92
+ catch (error) {
93
+ process.exit(1);
94
+ }
95
+ });
96
+ program.parse(process.argv);
package/dist/util.js ADDED
@@ -0,0 +1,3 @@
1
+ export function sum(a, b) {
2
+ return a + b;
3
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "emnj-tool",
3
+ "version": "1.0.0",
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
+ "author": "",
19
+ "license": "ISC",
20
+ "devDependencies": {
21
+ "@types/fs-extra": "^11.0.4",
22
+ "@types/inquirer": "^9.0.7",
23
+ "@types/node": "^20.11.28",
24
+ "ts-node": "^10.9.2",
25
+ "typescript": "^5.4.2"
26
+ },
27
+ "dependencies": {
28
+ "@types/unzipper": "^0.10.9",
29
+ "chalk": "^5.3.0",
30
+ "commander": "^12.0.0",
31
+ "download-git-repo": "^3.0.2",
32
+ "fs-extra": "^11.2.0",
33
+ "inquirer": "^9.2.16",
34
+ "unzipper": "^0.10.14"
35
+ }
36
+ }
package/src/h5/h5.ts ADDED
@@ -0,0 +1,50 @@
1
+ import fse from "fs-extra";
2
+ import path from "path";
3
+ import unzipper from "unzipper";
4
+
5
+ export default class InitH5 {
6
+ private answerOptions: InquirerPromptQuestions;
7
+ constructor(answerOptions: InquirerPromptQuestions) {
8
+ this.answerOptions = answerOptions;
9
+ }
10
+
11
+ init() {
12
+ console.log("init h5", this.answerOptions);
13
+ this.mkdir();
14
+ }
15
+
16
+ async mkdir() {
17
+ // create dir in current workspace use option name
18
+ const targetDirPath = path.join(process.cwd(), this.answerOptions.name);
19
+ fse.ensureDirSync(targetDirPath);
20
+ this.unzipH5Template(targetDirPath);
21
+ }
22
+ /**
23
+ * unzip h5 template to current workspace path
24
+ */
25
+ unzipH5Template(targetDirPath: string) {
26
+ const templatePath = path.join(process.cwd(), "./templates/ts-h5.zip");
27
+ fse
28
+ .createReadStream(templatePath)
29
+ .pipe(unzipper.Extract({ path: targetDirPath }))
30
+ .on("close", () => {
31
+ const json = fse.readJSONSync(
32
+ path.join(targetDirPath, "package.json"),
33
+ {
34
+ encoding: "utf8",
35
+ }
36
+ );
37
+ const packageJson = {
38
+ ...json,
39
+ name: this.answerOptions.name,
40
+ version: this.answerOptions.version,
41
+ description: this.answerOptions.description,
42
+ };
43
+ fse.writeJSONSync(
44
+ path.join(targetDirPath, "package.json"),
45
+ packageJson,
46
+ { encoding: "utf8", flag: "w", spaces: 2 }
47
+ );
48
+ });
49
+ }
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,106 @@
1
+ #! /usr/bin/env node
2
+
3
+ import { Command } from "commander";
4
+ import inquirer from "inquirer";
5
+ import InitH5 from "./h5/h5.js";
6
+ import chalk from "chalk";
7
+
8
+ const program = new Command();
9
+
10
+ const log = console.log;
11
+
12
+ program
13
+ .name("project cli")
14
+ .description("CLI to Initialize some project")
15
+ .version("0.0.1");
16
+
17
+ program
18
+ .command("split")
19
+ .description("Split a string into an array of strings using a separator.")
20
+ .argument("<string>", "The string to split")
21
+ .option("--first", "display just the first substring")
22
+ .option("-s, --separator <char>", "separator character", ",")
23
+ .action((str, options) => {
24
+ console.log("---->> ", options);
25
+ const limit = options.first ? 1 : undefined;
26
+ console.log(str.split(options.separator, limit));
27
+ });
28
+
29
+ program
30
+ .command("init")
31
+ .description("初始化项目,支持H5,Web等")
32
+ .option("-h5, --h5", "Initialize an H5 project.", false)
33
+ .option("-w, --web", "Initialize a Web project.", false)
34
+ .action(async (options: InitOptions) => {
35
+ // 如果没有配置argument,则第一个参数就是options
36
+ if (options.h5 === false && options.web === false) {
37
+ log(
38
+ "请使用" +
39
+ chalk.bgRed.bold("emnj init --h5") +
40
+ "或者" +
41
+ chalk.bgRed.bold("emnj init --web") +
42
+ "来初始化项目"
43
+ );
44
+ process.exit(1);
45
+ }
46
+
47
+ try {
48
+ const confirmAnswers = await inquirer.prompt([
49
+ {
50
+ type: "confirm",
51
+ name: "confirmAction",
52
+ message: `确认初始化一个${options.h5 ? "H5" : "Web"}工程项目`,
53
+ default: true,
54
+ },
55
+ ]);
56
+ if (!confirmAnswers.confirmAction) {
57
+ const message = "取消初始化";
58
+ log(chalk.redBright(message));
59
+ throw new Error(message);
60
+ }
61
+
62
+ const actionAnswers: InquirerPromptQuestions = await inquirer.prompt([
63
+ {
64
+ type: "input",
65
+ name: "name",
66
+ message: "请输入名称:",
67
+ default: "h5-project",
68
+ validate: function (value) {
69
+ return true;
70
+ },
71
+ },
72
+ {
73
+ type: "input",
74
+ name: "version",
75
+ message: "请输入版本:",
76
+ default: "0.0.1",
77
+ },
78
+ {
79
+ type: "input",
80
+ name: "description",
81
+ message: "请输入项目描述:",
82
+ default: "这是一个H5项目",
83
+ },
84
+ {
85
+ type: "list",
86
+ name: "language",
87
+ message: "是否使用typescript",
88
+ default: true,
89
+ choices: ["typescript", "javascript"],
90
+ },
91
+ ]);
92
+ if (options.h5 === true) {
93
+ const initH5 = new InitH5(actionAnswers);
94
+ initH5.init();
95
+ return;
96
+ }
97
+ if (options.web === true) {
98
+ log("初始化web项目");
99
+ return;
100
+ }
101
+ } catch (error) {
102
+ process.exit(1);
103
+ }
104
+ });
105
+
106
+ program.parse(process.argv);
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 初始化项目选项的定义
3
+ */
4
+ interface InitOptions {
5
+ h5: boolean;
6
+ web: boolean;
7
+ }
8
+ /**
9
+ * 初始化项目询问选项
10
+ */
11
+ interface InquirerPromptQuestions {
12
+ name: string;
13
+ version: string;
14
+ description: string;
15
+ language: "typescript" | "javascript";
16
+ }
package/src/util.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function sum(a: number, b: number): number {
2
+ return a + b;
3
+ }
Binary file
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "Node16",
5
+ "moduleResolution": "Node16",
6
+ "esModuleInterop": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "outDir": "./dist",
11
+ "typeRoots": ["./node_modules/@types", "./src/types"]
12
+ },
13
+ "include": ["src/**/*.ts"]
14
+ }