feima-shortcuts 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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # npm cli
2
+
3
+ ## 使用方式
4
+
5
+ > npm i feima-shortcuts -g
6
+
7
+ 使用方法
8
+
9
+ > feima
10
+
11
+ ### 对应类型
12
+
13
+ a. restful-api 自动生成 restful api 接口
package/bin/feima.js ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env node
2
+ const inquirer = require("inquirer");
3
+ const feima = require("../src/generate");
4
+ // const utils = require('../src/utils/checkUpdate');
5
+ // // 当前系统是Windows还是mac;
6
+ // const isWin = /^win/.test(process.platform);
7
+ // let packagePath = '/usr/local/lib/node_modules';// node_modules全局路径
8
+
9
+ const run = () => {
10
+ inquirer
11
+ .prompt([
12
+ {
13
+ type: "input",
14
+ message: "输入接口路径:",
15
+ name: "path",
16
+ default: "admin.product.product.getList", // 默认值
17
+ },
18
+ // {
19
+ // type: "list",
20
+ // name: "type",
21
+ // message: "api",
22
+ // choices: [
23
+ // "post",
24
+ // "get",
25
+ // "delete",
26
+ // "put",
27
+ // ],
28
+ // when: function (answers) {
29
+ // return answers["path"];
30
+ // },
31
+ // },
32
+ ])
33
+ .then((answers) => {
34
+ feima.run(answers);
35
+ });
36
+ };
37
+
38
+ run();
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ const hello = function (key) {
2
+ console.log('Hello World!');
3
+ };
4
+
5
+ exports.hello = hello;
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "feima-shortcuts",
3
+ "version": "0.0.1",
4
+ "description": "快捷指令",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "lib": "lib"
8
+ },
9
+ "bin": {
10
+ "feima": "./bin/feima.js"
11
+ },
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "1198810568@qq.com",
16
+ "license": "ISC",
17
+ "dependencies": {
18
+ "chalk": "^4.1.0",
19
+ "commander": "^5.1.0",
20
+ "download-git-repo": "^3.0.2",
21
+ "fs-extra": "^9.0.1",
22
+ "inquirer": "^7.3.0",
23
+ "uuid": "^9.0.0"
24
+ }
25
+ }
@@ -0,0 +1,5 @@
1
+ const restfulApi = require("./scripts/restful-api");
2
+
3
+ exports.run = function (answers) {
4
+ restfulApi.run(answers);
5
+ };
@@ -0,0 +1,58 @@
1
+ const fs = require("fs");
2
+ const { makeDir } = require("../utils/makeDir");
3
+
4
+ const fileName = "index.js";
5
+
6
+ const postTemplat = (apiPath, functionName) => {
7
+ return `export const ${functionName} = (params) => {
8
+ return request({
9
+ method: "post",
10
+ params: {
11
+ ...params,
12
+ api: "${apiPath}",
13
+ },
14
+ });
15
+ };
16
+ `
17
+ };
18
+
19
+ function splitString(input) {
20
+ const parts = input.split('.');
21
+ const path = parts.slice(0, -1).join('/'); // 取前面的部分并用 '/' 连接
22
+ const functionName = parts[parts.length - 1]; // 取最后一部分
23
+ return { path, functionName };
24
+ }
25
+
26
+
27
+ const fileData = (answers, functionName) => {
28
+ if (fs.existsSync(`./${fileName}`)) {
29
+ const data = fs.readFileSync(`./${fileName}`, "utf8");
30
+ if (data.includes(functionName)) {
31
+ return "";
32
+ }
33
+ return data;
34
+ }
35
+ return `import request from '@/api/https'`
36
+ }
37
+
38
+ const apiAllTemplate = (answers,functionName) => {
39
+ const fileTemplate = fileData(answers, functionName);
40
+
41
+ if (!fileTemplate) return console.log(`无需重复添加 ${type}`);
42
+
43
+ const template = `${fileTemplate}
44
+
45
+ ${postTemplat(answers.path, functionName)}`;
46
+
47
+ fs.writeFileSync(`./${fileName}`, template);
48
+ };
49
+
50
+ exports.run = function (answers) {
51
+ const { path, functionName } = splitString(answers.path);
52
+ const pagePath = `./src/api/${path}`;
53
+
54
+ makeDir(pagePath);
55
+ process.chdir(pagePath);
56
+
57
+ apiAllTemplate(answers, functionName);
58
+ };
@@ -0,0 +1,53 @@
1
+ const chalk = require('chalk');
2
+ /**
3
+ * 从 npmjs 检查依赖版本
4
+ * @param {*} name 要检查更新的依赖名称
5
+ * @param {object} param1 参数
6
+ * @param {string} param1.version 指定版本
7
+ * @param {array} param1.packagePath 指定路径
8
+ */
9
+ async function checkUpdate(name, { version, packagePath } = {}) {
10
+ function hasFile(filePath) { // 判断文件或目录是否存在
11
+ const fs = require(`fs`)
12
+ return fs.existsSync(filePath)
13
+ };
14
+ function getLocalVersion(name) { // 从本地获取版本号
15
+ packagePath = packagePath || require.main.paths.concat(`${require(`path`).parse(process.execPath).dir}/node_modules`) // 全局安装目录
16
+ .find(path => hasFile(`${path}/${name}/package.json`))
17
+ // console.log(`packagePath`, packagePath);
18
+ if (packagePath) {
19
+ return require(`${packagePath}/${name}/package.json`).version // 从 package 中获取版本
20
+ }
21
+ };
22
+ function getServerVersion(name) { // 从 npmjs 中获取版本号
23
+ return new Promise((resolve, reject) => {
24
+ const https = require('https');
25
+ https.get(`https://registry.npmjs.org/${name}`, res => {
26
+ let data = ''
27
+ res.on('data', chunk => {
28
+ data += chunk
29
+ })
30
+ res.on('end', () => {
31
+ const latest = (JSON.parse(data)[`dist-tags`] || {}).latest // 获取最新版本
32
+ resolve(latest)
33
+ })
34
+ }).on(`error`, (err) => {
35
+ throw new Error(err.message)
36
+ })
37
+ })
38
+ };
39
+ const getLocalVersionRes = getLocalVersion(name);
40
+ const getServerVersionRes = await getServerVersion(name);
41
+
42
+ return new Promise((resolve, reject) => {
43
+ if (getLocalVersionRes !== getServerVersionRes) {
44
+ console.log(`${name} 已发布新版本 ${getServerVersionRes}, 你当前版本为 ${getLocalVersionRes}`);
45
+ console.log(`${chalk.red('更新执行:')}npm i -g bluedot-template@${getServerVersionRes}`);
46
+ reject({});
47
+ } else {
48
+ resolve({});
49
+ };
50
+ });
51
+ };
52
+
53
+ exports.checkUpdate = checkUpdate;
@@ -0,0 +1,21 @@
1
+ var fs = require('fs');//文件模块
2
+ /**
3
+ * 删除文件夹
4
+ * @param {object} path 文件夹路径
5
+ */
6
+ function deleteFolder(path) {
7
+ let files = [];
8
+ if (fs.existsSync(path)) {
9
+ files = fs.readdirSync(path);
10
+ files.forEach(function (file, index) {
11
+ let curPath = path + "/" + file;
12
+ if (fs.statSync(curPath).isDirectory()) {
13
+ deleteFolder(curPath);
14
+ } else {
15
+ fs.unlinkSync(curPath);
16
+ }
17
+ });
18
+ fs.rmdirSync(path);
19
+ };
20
+ };
21
+ exports.deleteFolder = deleteFolder;
@@ -0,0 +1,31 @@
1
+ var fs = require("fs"); //文件模块
2
+ var path = require("path");
3
+
4
+ /**
5
+ * 创建文件夹
6
+ * @param {object} path 文件夹路径
7
+ */
8
+ function makeDir(dirpath) {
9
+ if (!fs.existsSync(dirpath)) {
10
+ var pathtmp;
11
+ dirpath.split("/").forEach(function (dirname) {
12
+ if (pathtmp) {
13
+ pathtmp = path.join(pathtmp, dirname);
14
+ } else {
15
+ //如果在linux系统中,第一个dirname的值为空,所以赋值为"/"
16
+ if (dirname) {
17
+ pathtmp = dirname;
18
+ } else {
19
+ pathtmp = "/";
20
+ }
21
+ }
22
+ if (!fs.existsSync(pathtmp)) {
23
+ if (!fs.mkdirSync(pathtmp)) {
24
+ return false;
25
+ }
26
+ }
27
+ });
28
+ }
29
+ return true;
30
+ }
31
+ exports.makeDir = makeDir;