chinapopin-frontend-cli 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Wei xiaoru
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # 三合力通脚手架
2
+
3
+ ## npm账号
4
+ ```
5
+ 账号:chinapopin
6
+ 密码:chinapopin123456
7
+ ```
8
+ ## npm发布
9
+ ```
10
+ npm publish
11
+ ```
12
+
13
+ ## 如何使用
package/bin/index.js ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env node
2
+
3
+ const program = require('commander')
4
+
5
+ const { version } = require('../package.json');
6
+
7
+ const figlet = require('figlet');
8
+
9
+
10
+ program
11
+ // 定义命令和参数
12
+ .command('create <app-name>')
13
+ .description('创建新的项目')
14
+ // -f or --force 为强制创建,如果创建的目录存在则直接覆盖
15
+ .option('-f, --force', '该目录下存在该文件夹,覆盖目标文件夹')
16
+ .action((name, options) => {
17
+ require('../lib/create.js')(name, options)
18
+ // 打印执行结果
19
+ // console.log('name:',name,'options:',options)
20
+ })
21
+
22
+ program
23
+ // 配置版本号信息
24
+ .version(`v${version}`)
25
+ .usage('<command> [option]')
26
+
27
+
28
+ program
29
+ .on('--help', () => {
30
+ console.log('\r\n' + figlet.textSync('shlt', {
31
+ font: 'Ghost',
32
+ horizontalLayout: 'default',
33
+ verticalLayout: 'default',
34
+ width: 80,
35
+ whitespaceBreak: true
36
+ }));
37
+ })
38
+
39
+ // 解析用户执行命令传入参数
40
+ program.parse(process.argv);
41
+
package/lib/create.js ADDED
@@ -0,0 +1,51 @@
1
+ const path = require("path");
2
+ const fs = require("fs-extra");
3
+ const inquirer = require("inquirer");
4
+ const Generator = require("./generator");
5
+
6
+ module.exports = async function (name, options) {
7
+ // 当前命令行选择的目录
8
+ const cwd = process.cwd();
9
+ // 需要创建的目录地址
10
+ const targetAir = path.join(cwd, name);
11
+ // 目录是否已经存在?
12
+ if (fs.existsSync(targetAir)) {
13
+ // 是否为强制创建?
14
+ if (options.force) {
15
+ await fs.remove(targetAir);
16
+ } else {
17
+ // 询问用户是否确定要覆盖
18
+ let { action } = await inquirer.prompt([
19
+ {
20
+ name: "action",
21
+ type: "list",
22
+ message: "在该目录下已存在相同的文件夹,是否覆盖原文件夹:",
23
+ choices: [
24
+ {
25
+ name: "覆盖",
26
+ value: "overwrite",
27
+ },
28
+ {
29
+ name: "取消",
30
+ value: false,
31
+ },
32
+ ],
33
+ },
34
+ ]);
35
+
36
+ if (!action) {
37
+ return;
38
+ } else if (action === "overwrite") {
39
+ // 移除已存在的目录
40
+ console.log(`\r\nRemoving...`);
41
+ await fs.remove(targetAir);
42
+ }
43
+ }
44
+ }
45
+
46
+ // 创建项目
47
+ const generator = new Generator(name, targetAir);
48
+
49
+ // 开始创建项目
50
+ generator.create();
51
+ };
@@ -0,0 +1,98 @@
1
+ const ora = require("ora");
2
+ const inquirer = require("inquirer");
3
+ const util = require("util");
4
+ const downloadGitRepo = require("download-git-repo");
5
+ // const http = require('./http');
6
+ const Template = require("./template");
7
+
8
+ class Generator {
9
+ constructor(name, targetDir) {
10
+ // 目录名称
11
+ this.name = name;
12
+ // 创建位置
13
+ this.targetDir = targetDir;
14
+ }
15
+
16
+ // 用户选择模板
17
+ async getRepo() {
18
+ const repos = [
19
+ { name: "PC管理端(vue3+antd)", value: "pc" },
20
+ { name: "H5(vue3+vant)", value: "h5" },
21
+ ];
22
+ const { repo } = await inquirer.prompt({
23
+ name: "repo",
24
+ type: "list",
25
+ choices: repos,
26
+ message: "请选择模板:",
27
+ });
28
+ return repo;
29
+ }
30
+ // 询问用户是否初始化git
31
+ async gitInit() {
32
+ return new Promise((resolve, reject) => {
33
+ inquirer
34
+ .prompt([
35
+ {
36
+ name: "init",
37
+ type: "confirm",
38
+ message: "是否初始化git仓库?",
39
+ },
40
+ ])
41
+ .then((answers) => {
42
+ resolve(answers.init);
43
+ });
44
+ });
45
+ }
46
+
47
+ // 下载远程模板
48
+ async download(type) {
49
+ const spinner = ora("下载模板中,请稍后").start();
50
+ const GITADDRESS = {
51
+ pc: "http://100.98.100.194/frontend/frontend-engineer/chinapopin-admin-pro.git#cli",
52
+ h5: "http://100.98.100.194/frontend/frontend-engineer/chinapopin-h5-demo-frontend.git#cli",
53
+ };
54
+ return new Promise((resolve, reject) => {
55
+ downloadGitRepo(
56
+ `direct:${GITADDRESS[type]}`,
57
+ this.targetDir,
58
+ { clone: true },
59
+ (error) => {
60
+ // 报错才有返回错误信息
61
+ if (error) {
62
+ spinner.fail("模板下载失败:" + error);
63
+ reject(error);
64
+ } else {
65
+ spinner.succeed("模板下载完成");
66
+ resolve("success");
67
+ }
68
+ }
69
+ );
70
+ });
71
+ }
72
+
73
+ async create() {
74
+ // 获取选择模板
75
+ const repo = await this.getRepo();
76
+ // 获取是否初始化git仓库
77
+ // const init = await this.gitInit();
78
+ // if (init) {
79
+
80
+ // }
81
+ // 下载项目模板
82
+ const download = await this.download(repo);
83
+ if (download !== "success") {
84
+ // 如果下载模板失败,退出执行进程
85
+ process.exit(1);
86
+ }
87
+ // const template = new Template(this.targetDir, this.name, repo);
88
+ // const res = await template.initTemplate();
89
+ // if(res === 'success') {
90
+ // process.exit(0);
91
+ // } else {
92
+ // console.log(res)
93
+ // process.exit(1);
94
+ // }
95
+ }
96
+ }
97
+
98
+ module.exports = Generator;
package/lib/http.js ADDED
@@ -0,0 +1,37 @@
1
+ // 通过 axios 处理请求
2
+ const axios = require("axios");
3
+
4
+ // 添加请求拦截器
5
+ axios.interceptors.request.use(
6
+ function (config) {
7
+ config.headers["PRIVATE-TOKEN"] = "bkBGFNSpvf92a5gDszHQ";
8
+ return config;
9
+ },
10
+ function (error) {
11
+ return Promise.reject(error);
12
+ }
13
+ );
14
+
15
+ axios.interceptors.response.use((res) => {
16
+ return res.data;
17
+ });
18
+
19
+ /**
20
+ * 获取git分组信息
21
+ * @returns Promise
22
+ */
23
+ async function getGroupsList() {
24
+ return new Promise((resolve, reject) => {
25
+ axios.get("http://100.98.100.194/api/v4/groups").then(
26
+ (res) => {
27
+ resolve(res);
28
+ },
29
+ (error) => {
30
+ reject(error);
31
+ }
32
+ );
33
+ });
34
+ }
35
+ module.exports = {
36
+ getGroupsList,
37
+ };
@@ -0,0 +1,86 @@
1
+ const fs = require("fs");
2
+ const ora = require("ora");
3
+
4
+ class Template {
5
+ constructor(path, name, repo) {
6
+ this.path = path;
7
+ this.name = name;
8
+ this.repo = repo;
9
+ }
10
+ async initTemplate() {
11
+ const spinner = ora("初始化模板中,请稍后").start();
12
+ return new Promise((resolve, reject) => {
13
+ const html = this.initFile("index.html"); // 初始化index.html
14
+ const packageJ = this.initFile("package.json"); // 初始化package.json文件
15
+ const testenv = this.initFile(".env.test"); // 初始化测试环境环境变量文件
16
+ const prodenv = this.initFile(".env.production"); // 初始化正式环境环境变量文件
17
+ const testDoc = this.initFile("docker-test/Dockerfile"); // 初始化测试环境dockerfile文件
18
+ const testNginx = this.initFile("docker-test/nginx.conf"); // 初始化测试环境Nginx配置
19
+ const prodDoc = this.initFile("docker-prod/Dockerfile"); // 初始化正式环境dockerfile文件
20
+ const prodNginx = this.initFile("docker-prod/nginx.conf"); // 初始化正式环境的Nginx配置文件
21
+ Promise.all([
22
+ html,
23
+ packageJ,
24
+ testenv,
25
+ prodenv,
26
+ testDoc,
27
+ testNginx,
28
+ prodDoc,
29
+ prodNginx,
30
+ ])
31
+ .then((result) => {
32
+ spinner.succeed("初始化模板完成");
33
+ resolve("success");
34
+ })
35
+ .catch((error) => {
36
+ spinner.fail("初始化模板失败:" + error);
37
+ reject(error);
38
+ });
39
+ });
40
+ }
41
+
42
+ // 初始化文件
43
+ async initFile(path) {
44
+ if (path === "index.html" && this.repo !== "H5") {
45
+ return new Promise((resolve, reject) => {
46
+ resolve("success");
47
+ });
48
+ } else {
49
+ return this.writeFile(path, this.replaceReg(await this.readFile(path)));
50
+ }
51
+ }
52
+
53
+ // 替换
54
+ replaceReg(data) {
55
+ return data.replace(new RegExp("<%= app-name =%>", "g"), this.name);
56
+ }
57
+
58
+ // 写入文件
59
+ writeFile(path, data) {
60
+ return new Promise((resolve, reject) => {
61
+ fs.writeFile(`${this.path}/${path}`, data, "utf8", (err) => {
62
+ if (err) {
63
+ reject(err);
64
+ } else {
65
+ resolve("success");
66
+ }
67
+ });
68
+ });
69
+ }
70
+
71
+ // 读取文件
72
+ readFile(path) {
73
+ return new Promise((resolve, reject) => {
74
+ fs.readFile(`${this.path}/${path}`, "utf8", (err, data) => {
75
+ if (err) {
76
+ console.log("读取文件错误");
77
+ reject(err);
78
+ } else {
79
+ resolve(data);
80
+ }
81
+ });
82
+ });
83
+ }
84
+ }
85
+
86
+ module.exports = Template;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "chinapopin-frontend-cli",
3
+ "private": false,
4
+ "version": "0.0.1",
5
+ "description": "前端脚手架",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "bin": {
11
+ "shlt": "./bin/index.js"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "http://100.98.100.194/frontend/frontend-engineer/chinapopin-frontend-cli"
16
+ },
17
+ "keywords": [
18
+ "chinapopin-frontend-cli"
19
+ ],
20
+ "author": "weixiaoru (514753680@qq.com)",
21
+ "license": "ISC",
22
+ "devDependencies": {
23
+ "commander": "^8.3.0"
24
+ },
25
+ "dependencies": {
26
+ "axios": "^0.24.0",
27
+ "download-git-repo": "^3.0.2",
28
+ "figlet": "^1.5.2",
29
+ "fs-extra": "^10.0.0",
30
+ "inquirer": "^8.2.0",
31
+ "ora": "^5.4.1"
32
+ }
33
+ }