@cyber-tools/create-cyber-library 6.0.0 → 7.0.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/.bin/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  #!/usr/bin/env node
2
- require("../index");
2
+ require("esbuild-register");
3
+ require("../index.ts");
@@ -0,0 +1,33 @@
1
+ import { inject, injectable } from "inversify";
2
+ import { IOCContainer } from "@/commons/IOCContainer";
3
+
4
+ import { CommandLineUtils } from "@/utils/CommandLineUtils";
5
+ import { ApplicationConfigManager } from "@/commons/ApplicationConfigManager";
6
+
7
+ @injectable()
8
+ export class CreateLibraryAction {
9
+
10
+ constructor(
11
+ @inject(CommandLineUtils) private readonly $CommandLineUtils: CommandLineUtils,
12
+ @inject(ApplicationConfigManager) private readonly $ApplicationConfigManager: ApplicationConfigManager
13
+ ) { };
14
+
15
+ public async execute({ remote, devDependencies }) {
16
+ try {
17
+ const scopeName = await this.$CommandLineUtils.inputScopeName();
18
+ const packageName = await this.$CommandLineUtils.inputPackageName();
19
+ const assignName = scopeName ? [scopeName, packageName].join("/") : packageName;
20
+ if (await this.$CommandLineUtils.caniUseName(assignName)) {
21
+ await this.$CommandLineUtils.downloadTemplate({ folderName: packageName, remote });
22
+ await this.$CommandLineUtils.changeJsonFile({ folderName: packageName, projectName: assignName, devDependencies });
23
+ } else {
24
+ await this.execute({ remote, devDependencies });
25
+ };
26
+ } catch (error) {
27
+ throw error;
28
+ };
29
+ };
30
+
31
+ };
32
+
33
+ IOCContainer.bind(CreateLibraryAction).toSelf().inRequestScope();
@@ -0,0 +1,57 @@
1
+ import os from "os";
2
+ import path from "path";
3
+ import { merge } from "lodash";
4
+ import pathExists from "path-exists";
5
+ import { readFile } from "jsonfile";
6
+ import { injectable } from "inversify";
7
+
8
+ import { IOCContainer } from "@/commons/IOCContainer";
9
+
10
+ @injectable()
11
+ export class ApplicationConfigManager {
12
+
13
+ /** 应用层内置的默认配置 **/
14
+ private defaultConfig: any = {
15
+ librarys: [{
16
+ name: "基于typescript编译的库脚手架",
17
+ value: {
18
+ remote: "https://github.com/cyber-scaffold/pure-typescript-library"
19
+ }
20
+ }]
21
+ };
22
+
23
+ /** 操作系统层的配置 **/
24
+ private systemConfig: any = {};
25
+
26
+ /** $HOME目录下的配置 **/
27
+ private custmerConfig: any = {};
28
+
29
+ /** 声明在操作系统层的配置文件路径 **/
30
+ get systemConfigPath() {
31
+ return path.join("/etc/", "/redis-broadcast-discover/", "./config.json");
32
+ };
33
+
34
+ /** 声明在$HOME目录下的配置文件路径 **/
35
+ get custmerConfigPath() {
36
+ return path.join(os.homedir(), "/.redis-broadcast-discover/", "./config.json");
37
+ };
38
+
39
+ /** 初始化并加载配置到运行时 **/
40
+ public async initialize() {
41
+ if (await pathExists(this.systemConfigPath)) {
42
+ this.systemConfig = await readFile(this.systemConfigPath);
43
+ };
44
+ if (await pathExists(this.custmerConfigPath)) {
45
+ this.custmerConfig = await readFile(this.custmerConfigPath);
46
+ };
47
+ };
48
+
49
+ /** 获取最终组合之后的运行时配置 **/
50
+ public getRuntimeConfig() {
51
+ const composeConfig = merge(this.defaultConfig, this.systemConfig, this.custmerConfig);
52
+ return composeConfig;
53
+ };
54
+
55
+ };
56
+
57
+ IOCContainer.bind(ApplicationConfigManager).toSelf().inSingletonScope();
@@ -0,0 +1,4 @@
1
+ import "reflect-metadata";
2
+ import { Container } from "inversify";
3
+
4
+ export const IOCContainer = new Container();
package/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { IOCContainer } from "@/commons/IOCContainer";
2
+ import { CommandLineUtils } from "@/utils/CommandLineUtils";
3
+ import { CreateLibraryAction } from "@/actions/CreateLibraryAction";
4
+ import { ApplicationConfigManager } from "@/commons/ApplicationConfigManager";
5
+
6
+ setImmediate(async () => {
7
+ /** 初始化配置 **/
8
+ const $ApplicationConfigManager = IOCContainer.get(ApplicationConfigManager);
9
+ await $ApplicationConfigManager.initialize();
10
+ /** 选择远程摸板 **/
11
+ const $CommandLineUtils = IOCContainer.get(CommandLineUtils);
12
+ const { remote, devDependencies } = await $CommandLineUtils.selectTemplate();
13
+ /** 创建项目 **/
14
+ const $CreateLibraryAction = IOCContainer.get(CreateLibraryAction);
15
+ await $CreateLibraryAction.execute({ remote, devDependencies });
16
+ });
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@cyber-tools/create-cyber-library",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "用于创建cyber库文件的命令行工具",
5
- "main": "index.js",
5
+ "main": "./.bin/index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "echo test"
8
8
  },
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
+ "bin": "./.bin/index.js",
12
13
  "keywords": [
13
14
  "cyber",
14
15
  "create",
@@ -18,18 +19,23 @@
18
19
  "author": "",
19
20
  "license": "ISC",
20
21
  "dependencies": {
21
- "@cyber-tools/cli-utils": "^2.4.1",
22
+ "cli-spinners": "^2.4.0",
22
23
  "colors": "1.4.0",
23
24
  "download-git-repo": "^3.0.2",
24
- "es6-promisify": "^6.1.1",
25
- "inquirer": "^7.3.2",
25
+ "inquirer": "^7.3.3",
26
+ "inversify": "^6.0.2",
26
27
  "jsonfile": "^6.0.1",
27
- "module-alias": "^2.2.2",
28
+ "lodash": "^4.17.23",
28
29
  "npm-name": "^6.0.1",
30
+ "ora": "^5.1.0",
31
+ "path-exists": "^4.0.0",
29
32
  "prompt": "1.2.2",
33
+ "reflect-metadata": "^0.2.2",
30
34
  "validate-npm-package-name": "^3.0.0",
35
+ "esbuild-register": "^3.6.0",
31
36
  "winston": "3.6.0"
32
37
  },
33
- "bin": "./.bin/index.js",
34
- "gitHead": "4b779ead2720faee5ec067535249e0cfada6d363"
35
- }
38
+ "devDependencies": {
39
+ "esbuild": "^0.27.4"
40
+ }
41
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "emitDecoratorMetadata": true,
4
+ "experimentalDecorators": true,
5
+ "target": "es2016",
6
+ "module": "commonjs",
7
+ "baseUrl": "./",
8
+ "moduleResolution": "node",
9
+ "resolveJsonModule": true,
10
+ "esModuleInterop": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "strict": false,
13
+ "skipLibCheck": true,
14
+ "sourceMap": true,
15
+ "paths": {
16
+ "@/*": [
17
+ "./*"
18
+ ]
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import ora from "ora";
2
+ import { dots } from "cli-spinners";
3
+ import { injectable, inject } from "inversify";
4
+ import { IOCContainer } from "@/commons/IOCContainer";
5
+
6
+ @injectable()
7
+ export class CommandLineToast {
8
+
9
+ public toast = ora(dots);
10
+
11
+ public start(...params: any) {
12
+ this.toast.start(...arguments);
13
+ };
14
+
15
+ public succeed(params: any) {
16
+ this.toast.succeed(...arguments);
17
+ };
18
+
19
+ public fail(params: any) {
20
+ this.toast.fail(...arguments);
21
+ };
22
+
23
+ };
24
+
25
+ IOCContainer.bind(CommandLineToast).toSelf().inRequestScope();
26
+
@@ -0,0 +1,141 @@
1
+ import path from "path";
2
+ import prompt from "prompt";
3
+ import colors from "colors";
4
+ import npmName from "npm-name";
5
+ import inquirer from "inquirer";
6
+ import { promisify } from "util";
7
+ import download from "download-git-repo";
8
+ import { readFile, writeFile } from "jsonfile";
9
+ import { injectable, inject } from "inversify";
10
+ import validate from "validate-npm-package-name";
11
+
12
+ import { IOCContainer } from "@/commons/IOCContainer";
13
+ import { CommandLineToast } from "@/utils/CommandLineToast";
14
+ import { ApplicationConfigManager } from "@/commons/ApplicationConfigManager";
15
+
16
+ @injectable()
17
+ export class CommandLineUtils {
18
+
19
+ constructor(
20
+ @inject(CommandLineToast) private readonly $CommandLineToast: CommandLineToast,
21
+ @inject(ApplicationConfigManager) private readonly $ApplicationConfigManager: ApplicationConfigManager
22
+ ) { };
23
+
24
+ /**
25
+ * 选择远程摸板
26
+ * **/
27
+ public async selectTemplate() {
28
+ const { librarys } = await this.$ApplicationConfigManager.getRuntimeConfig();
29
+ const { template } = await inquirer.prompt({
30
+ type: "list",
31
+ name: "template",
32
+ defaultValue: librarys[0]["name"],
33
+ message: "选择拉取的库文件类型:",
34
+ choices: librarys
35
+ });
36
+ return template;
37
+ };
38
+
39
+ /**
40
+ * 输入作用域@scope
41
+ * **/
42
+ public async inputScopeName() {
43
+ try {
44
+ prompt.message = undefined;
45
+ prompt.delimiter = ":";
46
+ prompt.start();
47
+ const { scopeName } = await promisify(prompt.get)([{
48
+ name: "scopeName",
49
+ description: colors.white("请输入作用域@scope")
50
+ }]);
51
+ return scopeName;
52
+ } catch (error) {
53
+ if (error.message === "canceled") {
54
+ process.exit(0);
55
+ };
56
+ throw error;
57
+ };
58
+ };
59
+
60
+ /**
61
+ * 输入包的名称@packageName
62
+ * **/
63
+ public async inputPackageName() {
64
+ try {
65
+ prompt.message = undefined;
66
+ prompt.delimiter = ":";
67
+ prompt.start();
68
+ const { packageName } = await promisify(prompt.get)([{
69
+ name: "packageName",
70
+ required: true,
71
+ message: colors.red("项目名称必须填写!"),
72
+ description: colors.white("请输入项目名称")
73
+ }]);
74
+ return packageName;
75
+ } catch (error) {
76
+ if (error.message === "canceled") {
77
+ process.exit(0);
78
+ };
79
+ throw error;
80
+ };
81
+ };
82
+
83
+ /**
84
+ * 检查包明是否已经在npm注册
85
+ * **/
86
+ public async caniUseName(name: string) {
87
+ try {
88
+ this.$CommandLineToast.start(["正在查询包名(", name, ")是否可用", "... ..."].join(""));
89
+ const { validForNewPackages, validForOldPackages } = await validate(name);
90
+ const isValidate = (validForNewPackages && validForOldPackages);
91
+ const isAvailable = await npmName(name);
92
+ if (isValidate && isAvailable) {
93
+ this.$CommandLineToast.toast.succeed(["包名(", name, ")可用!"].join(""));
94
+ return true;
95
+ };
96
+ this.$CommandLineToast.toast.fail(["包名(", name, ")不可用!"].join(""));
97
+ return false;
98
+ } catch (error) {
99
+ throw error;
100
+ };
101
+ };
102
+
103
+ /**
104
+ * 下载远程仓库中的代码摸板
105
+ * **/
106
+ public async downloadTemplate({ remote, folderName }) {
107
+ try {
108
+ const downloadTemplate = `direct:${remote.replace(/\.git$/ig, "")}.git`;
109
+ this.$CommandLineToast.start("正在拉取项目文件...");
110
+ const projectPath = path.join(process.cwd(), folderName);
111
+ await promisify(download)(downloadTemplate, projectPath, { clone: true });
112
+ this.$CommandLineToast.succeed("拉取成功!");
113
+ } catch (error) {
114
+ this.$CommandLineToast.fail("拉取失败!");
115
+ throw error;
116
+ };
117
+ };
118
+
119
+ /**
120
+ * 修改package.json中的相关内容
121
+ * **/
122
+ public async changeJsonFile({ folderName, projectName, devDependencies }) {
123
+ try {
124
+ this.$CommandLineToast.toast.start("修改package.json");
125
+ const jsonFilePath = path.resolve(process.cwd(), folderName, "./package.json");
126
+ const jsonObject = await promisify(readFile)(jsonFilePath);
127
+ const rewriteJsonObject = Object.assign({}, jsonObject, {
128
+ name: projectName,
129
+ devDependencies: Object.assign({}, jsonObject.devDependencies, devDependencies)
130
+ });
131
+ await promisify(writeFile)(jsonFilePath, rewriteJsonObject, { spaces: 2, EOL: '\r\n' });
132
+ this.$CommandLineToast.succeed("package.json修改成功!");
133
+ } catch (error) {
134
+ this.$CommandLineToast.fail("package.json修改失败!");
135
+ throw error;
136
+ };
137
+ };
138
+
139
+ };
140
+
141
+ IOCContainer.bind(CommandLineUtils).toSelf().inRequestScope();
@@ -1,12 +0,0 @@
1
-
2
- module.exports = [{
3
- name: "es6-babel-library",
4
- value: {
5
- remote: "https://github.com/cyber-scaffold/es6-babel-library"
6
- }
7
- }, {
8
- name: "es6-react-library",
9
- value: {
10
- remote: "https://github.com/cyber-scaffold/es6-react-library"
11
- }
12
- }];
package/index.js DELETED
@@ -1,14 +0,0 @@
1
- require("@cyber-tools/cli-utils/initial");
2
- require("module-alias").addAlias("@", __dirname);
3
- const createLibrary = require("@/scripts/create-library");
4
- const selectTemplate = require("@/utils/selectTemplate");
5
-
6
- (async () => {
7
- try {
8
- const { remote, devDependencies } = await selectTemplate();
9
- await createLibrary({ remote, devDependencies });
10
- } catch (error) {
11
- throw error;
12
- };
13
- })();
14
-
@@ -1,21 +0,0 @@
1
- const downloadTemplate = require("@/utils/downloadTemplate");
2
- const inputScopeName = require("@/utils/inputScopeName");
3
- const inputPackageName = require("@/utils/inputPackageName");
4
- const changeJsonFile = require("@/utils/changeJsonFile");
5
- const caniUseName = require("@/utils/caniUseName");
6
-
7
- module.exports = async function createLibrary({ remote, devDependencies }) {
8
- try {
9
- const scopeName = await inputScopeName();
10
- const packageName = await inputPackageName();
11
- const assignName = scopeName ? [scopeName, packageName].join("/") : packageName;
12
- if (await caniUseName(assignName)) {
13
- await downloadTemplate({ folderName: packageName, remote });
14
- await changeJsonFile({ folderName: packageName, projectName: assignName, devDependencies });
15
- } else {
16
- await createLibrary({ remote, devDependencies });
17
- };
18
- } catch (error) {
19
- throw error;
20
- };
21
- };
@@ -1,20 +0,0 @@
1
- const toast = require("@cyber-tools/cli-utils/toast");
2
- const npmName = require("npm-name");
3
- const validate = require("validate-npm-package-name");
4
-
5
- module.exports = async (name) => {
6
- try {
7
- toast.start(["正在查询包名(", name, ")是否可用", "... ..."].join(""));
8
- const { validForNewPackages, validForOldPackages } = await validate(name);
9
- const isValidate = (validForNewPackages && validForOldPackages);
10
- const isAvailable = await npmName(name);
11
- if (isValidate && isAvailable) {
12
- toast.succeed(["包名(", name, ")可用!"].join(""));
13
- return true;
14
- };
15
- toast.fail(["包名(", name, ")不可用!"].join(""));
16
- return false;
17
- } catch (error) {
18
- throw error;
19
- };
20
- };
@@ -1,23 +0,0 @@
1
- const ora = require("ora");
2
- const path = require("path");
3
- const { promisify } = require("es6-promisify");
4
- const { readFile, writeFile } = require("jsonfile");
5
-
6
-
7
- module.exports = async ({ folderName, projectName, devDependencies }) => {
8
- const toast = ora();
9
- try {
10
- toast.start("修改package.json");
11
- const jsonFilePath = path.resolve(process.cwd(), folderName, "./package.json");
12
- const jsonObject = await promisify(readFile)(jsonFilePath);
13
- const rewriteJsonObject = Object.assign({}, jsonObject, {
14
- name: projectName,
15
- devDependencies: Object.assign({}, jsonObject.devDependencies, devDependencies)
16
- });
17
- await promisify(writeFile)(jsonFilePath, rewriteJsonObject, { spaces: 2, EOL: '\r\n' });
18
- toast.succeed("package.json修改成功!");
19
- } catch (error) {
20
- toast.fail("package.json修改失败!");
21
- throw error;
22
- };
23
- };
@@ -1,18 +0,0 @@
1
- const path = require("path");
2
- const download = require("download-git-repo");
3
- const { promisify } = require("es6-promisify");
4
-
5
- const toast = require("@cyber-tools/cli-utils/toast");
6
-
7
- module.exports = async ({ folderName, remote }) => {
8
- try {
9
- const downloadTemplate = `direct:${remote.replace(/\.git$/ig, "")}.git`;
10
- toast.start("正在拉取项目文件...");
11
- const projectPath = path.join(process.cwd(), folderName);
12
- await promisify(download)(downloadTemplate, projectPath, { clone: true });
13
- toast.succeed("拉取成功!");
14
- } catch (error) {
15
- toast.fail("拉取失败!");
16
- throw error;
17
- };
18
- };
@@ -1,24 +0,0 @@
1
- const colors = require("colors");
2
- const prompt = require("prompt");
3
- const { promisify } = require("es6-promisify");
4
-
5
-
6
- module.exports = async () => {
7
- try {
8
- prompt.message = undefined;
9
- prompt.delimiter = ":";
10
- prompt.start();
11
- const { packageName } = await promisify(prompt.get)([{
12
- name: "packageName",
13
- required: true,
14
- message: colors.red("项目名称必须填写!"),
15
- description: colors.white("请输入项目名称")
16
- }]);
17
- return packageName;
18
- } catch (error) {
19
- if (error.message === "canceled") {
20
- process.exit(0);
21
- };
22
- throw error;
23
- };
24
- };
@@ -1,23 +0,0 @@
1
- const colors = require("colors");
2
- const prompt = require("prompt");
3
- const { promisify } = require("es6-promisify");
4
-
5
-
6
- module.exports = async () => {
7
- try {
8
- prompt.message = undefined;
9
- prompt.delimiter = ":";
10
- prompt.start();
11
- const { scopeName } = await promisify(prompt.get)([{
12
- name: "scopeName",
13
- description: colors.white("请输入作用域@scope")
14
- }]);
15
- return scopeName;
16
- } catch (error) {
17
- if (error.message === "canceled") {
18
- process.exit(0);
19
- };
20
- throw error;
21
- };
22
-
23
- };
@@ -1,13 +0,0 @@
1
- const inquirer = require("inquirer");
2
- const LIBRARY_LIST = require("@/configs/runtime.config");
3
-
4
- module.exports = async () => {
5
- const { template } = await inquirer.prompt({
6
- type: "list",
7
- name: "template",
8
- defaultValue: "es6-babel-library",
9
- message: "选择拉取的库文件类型:",
10
- choices: LIBRARY_LIST
11
- });
12
- return template;
13
- };