cloudcc-cli 1.3.7 → 1.3.9

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.
@@ -1,195 +1,195 @@
1
- const { exec } = require('child_process');
2
- const { post } = require('../utils/http');
3
- const fs = require('fs');
4
- const path = require("path")
5
- const chalk = require("chalk")
6
- const inquirer = require("inquirer")
7
- // 检查cli版本更新功能
8
- const { checkUpdate } = require("../utils/checkVersion")
9
- /**
10
- * 自定义组件,组件打包命令,将组件打包发布到服务器。
11
- */
12
- class Builder {
13
- constructor() {
14
- this.options = {
15
- // 开发配置信息
16
- devConsoleConfig: {
17
- },
18
- }
19
- this.plugin = process.argv.splice(2)[0]
20
- }
21
- async init() {
22
- let res = await checkUpdate();
23
- if (!res) {
24
- this.options.devConsoleConfig = this.getPackageJson();
25
- let config = this.options.devConsoleConfig
26
- // 如果不是私有化部署,那么需要获取token
27
- if ("private" != this.options.devConsoleConfig.version) { // private私有化
28
- config = { "accessToken": await this.getToken(config) };
29
- }
30
- // 获得用户输入
31
- let answers;
32
- if (this.plugin) {
33
- answers = { buildFileName: this.plugin }
34
- } else {
35
- answers = await this.ask();
36
- }
37
- // 如果输入的是*.vue,那么表示编译全部文件
38
- if ("*.vue" == answers.buildFileName) {
39
- let dirs = fs.readdirSync("plugin")
40
- for (let i = 0; i < dirs.length; i++) {
41
- let item = dirs[i]
42
- let obj = this.getVueValue(item);
43
- this.initPluginFile(item, obj.component, "plginTemp" + i);
44
- this.build(obj, config, "plginTemp" + i)
45
- }
46
- } else {
47
- let obj = this.getVueValue(answers.buildFileName);
48
- this.initPluginFile(answers.buildFileName, obj.component, "plginTemp");
49
- this.build(obj, config, "plginTemp")
50
- }
51
- }
52
- }
53
- /**
54
- * 命令行交互
55
- * @returns 结果
56
- */
57
- ask() {
58
- const prompt = [];
59
- prompt.push({
60
- type: "input",
61
- name: "buildFileName",
62
- message: "请输入需要编译的文件名,如index.vue",
63
- })
64
- return inquirer.prompt(prompt)
65
- }
66
- /**
67
- * 获得用户信息
68
- * @returns 配置信息
69
- */
70
- getPackageJson() {
71
- const packageJson = JSON.parse(fs.readFileSync("package.json", 'utf8'));
72
- return packageJson.devConsoleConfig; // cloudcc-plugin 中的 devConsoleConfig
73
- }
74
- /**
75
- * 请求用户token
76
- * @param {用户信息} devConsoleConfig
77
- * @returns token
78
- */
79
- async getToken(devConsoleConfig) {
80
- let res = await post(this.options.devConsoleConfig.baseUrl + "/sysconfig/auth/pc/1.0/post/tokenInfo", devConsoleConfig);
81
- if (res.returnCode == 200) {
82
- return res.data.accessToken;
83
- } else {
84
- console.error(chalk.red(`登录失败`, JSON.stringify(res)));
85
- return null;
86
- }
87
- }
88
-
89
- /**
90
- * 获得Vue内容
91
- */
92
- getVueValue(buildFileName) {
93
- let vueContent = fs.readFileSync(this.getVueContent(buildFileName), 'utf8');
94
- let vueData = vueContent + ""
95
- // 添加scoped限制样式污染
96
- if (!vueContent.includes("scoped")) {
97
- vueData = vueData.replace("<style", "<style scoped")
98
- }
99
- // 去除空格
100
- // vueData = vueData.replace(/\ +/g, "")
101
- // 去除换行
102
- // vueData =vueData.replace(/[\r\n]/g, "");
103
- vueData = vueData.split("data()")[1].split("isLock:")[0] + "isLock:false,};}";
104
- vueData = "function data()" + vueData;
105
- const data = eval(`(${vueData})`)()
106
- let component = data.componentInfo.component // 组件标识
107
- let compName = data.componentInfo.compName // 组件名字
108
- vueData = JSON.stringify(data);
109
- let bizType = data.componentInfo.bizType // 组件类型
110
- let compDesc = data.componentInfo.compDesc // 组件描述
111
- let category = data.componentInfo.category // 组件状态
112
- return { compName, component, vueContent, vueData, bizType, compDesc, category }
113
- };
114
- /**
115
- * 生成依赖文件
116
- * @param {编译的文件名称} buildFileName
117
- * @param {编译后的组件名称} component
118
- */
119
- initPluginFile(buildFileName, component, plginTemp) {
120
- let newContent =
121
- `
122
- import Vue from "vue"
123
- import VueCustomElement from "vue-custom-element"
124
- Vue.use(VueCustomElement);
125
-
126
- import index from "./` + buildFileName + `"
127
- Vue.customElement('`+ component + `', index,{ destroyTimeout: ${this.options.devConsoleConfig.destroyTimeout || 5 * 60 * 1000} });
128
- `
129
-
130
- fs.writeFileSync(`plugin/${plginTemp}.js`, newContent);
131
- }
132
- /**
133
- * 编译文件,将vue编译为js文件
134
- * @param {编译对象信息} obj
135
- */
136
- build(obj, config, plginTemp) {
137
- console.log(chalk.green('编译中,请稍后...'));
138
- exec('npx vue-cli-service build --target lib --name ' + obj.component + ` --dest build plugin/${plginTemp}.js`, async (error, stdout, stderr) => {
139
- if (error) {
140
- console.log('编译失败:', error);
141
- console.log(chalk.red('编译失败:' + stdout));
142
- return;
143
- } else {
144
- console.log(chalk.green('编译成功!'));
145
- console.log();
146
- await this.upload(obj, config)
147
- fs.unlinkSync(`plugin/${plginTemp}.js`);
148
- }
149
- })
150
- }
151
- /**
152
- * 将文件上传
153
- */
154
- async upload(obj, header) {
155
- console.log(chalk.green('发布中,请稍后...'));
156
- let jsContent = "";
157
- try {
158
- jsContent = fs.readFileSync(path.join("build", obj.component + ".umd.min.js"), 'utf8')
159
- } catch (err) {
160
- console.error(err)
161
- return;
162
- }
163
- let body = {
164
- "compLabel": obj.compName,
165
- "compUniName": obj.component,
166
- "compContentJs": jsContent,
167
- "compContentVue": obj.vueContent,
168
- "vueData": obj.vueData,
169
- "bizType": obj.bizType,
170
- "compDesc": obj.compDesc,
171
- "category": obj.category,
172
- "belongOrgFlag": this.options.devConsoleConfig.belongOrgFlag || "custom" //belongOrgFlag: 所属单位标识,std:官方的组件(神州云动);custom:第三方的组件
173
- }
174
- let res = await post(this.options.devConsoleConfig.baseUrl + "/devconsole/custom/pc/1.0/post/insertCustomComp",
175
- body, header);
176
- if (res.returnCode == 200) {
177
- console.error(chalk.green(`发布成功!`));
178
- console.log();
179
- } else {
180
- console.error(chalk.red(`发布失败: ${res.returnInfo}`));
181
- console.log();
182
- }
183
- return res;
184
- }
185
-
186
- /**
187
- * 过滤出min.js文件
188
- * @returns 过滤结果
189
- */
190
- getVueContent(buildFileName) {
191
- const jsPath = "plugin";
192
- return path.join(jsPath, buildFileName);
193
- }
194
- }
1
+ const { exec } = require('child_process');
2
+ const { post } = require('../utils/http');
3
+ const fs = require('fs');
4
+ const path = require("path")
5
+ const chalk = require("chalk")
6
+ const inquirer = require("inquirer")
7
+ // 检查cli版本更新功能
8
+ const { checkUpdate } = require("../utils/checkVersion")
9
+ /**
10
+ * 自定义组件,组件打包命令,将组件打包发布到服务器。
11
+ */
12
+ class Builder {
13
+ constructor() {
14
+ this.options = {
15
+ // 开发配置信息
16
+ devConsoleConfig: {
17
+ },
18
+ }
19
+ this.plugin = process.argv.splice(2)[0]
20
+ }
21
+ async init() {
22
+ let res = await checkUpdate();
23
+ if (!res) {
24
+ this.options.devConsoleConfig = this.getPackageJson();
25
+ let config = this.options.devConsoleConfig
26
+ // 如果不是私有化部署,那么需要获取token
27
+ if ("private" != this.options.devConsoleConfig.version) { // private私有化
28
+ config = { "accessToken": await this.getToken(config) };
29
+ }
30
+ // 获得用户输入
31
+ let answers;
32
+ if (this.plugin) {
33
+ answers = { buildFileName: this.plugin }
34
+ } else {
35
+ answers = await this.ask();
36
+ }
37
+ // 如果输入的是*.vue,那么表示编译全部文件
38
+ if ("*.vue" == answers.buildFileName) {
39
+ let dirs = fs.readdirSync("plugin")
40
+ for (let i = 0; i < dirs.length; i++) {
41
+ let item = dirs[i]
42
+ let obj = this.getVueValue(item);
43
+ this.initPluginFile(item, obj.component, "plginTemp" + i);
44
+ this.build(obj, config, "plginTemp" + i)
45
+ }
46
+ } else {
47
+ let obj = this.getVueValue(answers.buildFileName);
48
+ this.initPluginFile(answers.buildFileName, obj.component, "plginTemp");
49
+ this.build(obj, config, "plginTemp")
50
+ }
51
+ }
52
+ }
53
+ /**
54
+ * 命令行交互
55
+ * @returns 结果
56
+ */
57
+ ask() {
58
+ const prompt = [];
59
+ prompt.push({
60
+ type: "input",
61
+ name: "buildFileName",
62
+ message: "请输入需要编译的文件名,如index.vue",
63
+ })
64
+ return inquirer.prompt(prompt)
65
+ }
66
+ /**
67
+ * 获得用户信息
68
+ * @returns 配置信息
69
+ */
70
+ getPackageJson() {
71
+ const packageJson = JSON.parse(fs.readFileSync("package.json", 'utf8'));
72
+ return packageJson.devConsoleConfig; // cloudcc-plugin 中的 devConsoleConfig
73
+ }
74
+ /**
75
+ * 请求用户token
76
+ * @param {用户信息} devConsoleConfig
77
+ * @returns token
78
+ */
79
+ async getToken(devConsoleConfig) {
80
+ let res = await post(this.options.devConsoleConfig.baseUrl + "/sysconfig/auth/pc/1.0/post/tokenInfo", devConsoleConfig);
81
+ if (res.returnCode == 200) {
82
+ return res.data.accessToken;
83
+ } else {
84
+ console.error(chalk.red(`登录失败`, JSON.stringify(res)));
85
+ return null;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * 获得Vue内容
91
+ */
92
+ getVueValue(buildFileName) {
93
+ let vueContent = fs.readFileSync(this.getVueContent(buildFileName), 'utf8');
94
+ let vueData = vueContent + ""
95
+ // 添加scoped限制样式污染
96
+ if (!vueContent.includes("scoped")) {
97
+ vueData = vueData.replace("<style", "<style scoped")
98
+ }
99
+ // 去除空格
100
+ // vueData = vueData.replace(/\ +/g, "")
101
+ // 去除换行
102
+ // vueData =vueData.replace(/[\r\n]/g, "");
103
+ vueData = vueData.split("data()")[1].split("isLock:")[0] + "isLock:false,};}";
104
+ vueData = "function data()" + vueData;
105
+ const data = eval(`(${vueData})`)()
106
+ let component = data.componentInfo.component // 组件标识
107
+ let compName = data.componentInfo.compName // 组件名字
108
+ vueData = JSON.stringify(data);
109
+ let bizType = data.componentInfo.bizType // 组件类型
110
+ let compDesc = data.componentInfo.compDesc // 组件描述
111
+ let category = data.componentInfo.category // 组件状态
112
+ return { compName, component, vueContent, vueData, bizType, compDesc, category }
113
+ };
114
+ /**
115
+ * 生成依赖文件
116
+ * @param {编译的文件名称} buildFileName
117
+ * @param {编译后的组件名称} component
118
+ */
119
+ initPluginFile(buildFileName, component, plginTemp) {
120
+ let newContent =
121
+ `
122
+ import Vue from "vue"
123
+ import VueCustomElement from "vue-custom-element"
124
+ Vue.use(VueCustomElement);
125
+
126
+ import index from "./` + buildFileName + `"
127
+ Vue.customElement('`+ component + `', index,{ destroyTimeout: ${this.options.devConsoleConfig.destroyTimeout || 5 * 60 * 1000} });
128
+ `
129
+
130
+ fs.writeFileSync(`plugin/${plginTemp}.js`, newContent);
131
+ }
132
+ /**
133
+ * 编译文件,将vue编译为js文件
134
+ * @param {编译对象信息} obj
135
+ */
136
+ build(obj, config, plginTemp) {
137
+ console.log(chalk.green('编译中,请稍后...'));
138
+ exec('npx vue-cli-service build --target lib --name ' + obj.component + ` --dest build plugin/${plginTemp}.js`, async (error, stdout, stderr) => {
139
+ if (error) {
140
+ console.log('编译失败:', error);
141
+ console.log(chalk.red('编译失败:' + stdout));
142
+ return;
143
+ } else {
144
+ console.log(chalk.green('编译成功!'));
145
+ console.log();
146
+ await this.upload(obj, config)
147
+ fs.unlinkSync(`plugin/${plginTemp}.js`);
148
+ }
149
+ })
150
+ }
151
+ /**
152
+ * 将文件上传
153
+ */
154
+ async upload(obj, header) {
155
+ console.log(chalk.green('发布中,请稍后...'));
156
+ let jsContent = "";
157
+ try {
158
+ jsContent = fs.readFileSync(path.join("build", obj.component + ".umd.min.js"), 'utf8')
159
+ } catch (err) {
160
+ console.error(err)
161
+ return;
162
+ }
163
+ let body = {
164
+ "compLabel": obj.compName,
165
+ "compUniName": obj.component,
166
+ "compContentJs": jsContent,
167
+ "compContentVue": obj.vueContent,
168
+ "vueData": obj.vueData,
169
+ "bizType": obj.bizType,
170
+ "compDesc": obj.compDesc,
171
+ "category": obj.category,
172
+ "belongOrgFlag": this.options.devConsoleConfig.belongOrgFlag || "custom" //belongOrgFlag: 所属单位标识,std:官方的组件(神州云动);custom:第三方的组件
173
+ }
174
+ let res = await post(this.options.devConsoleConfig.baseUrl + "/devconsole/custom/pc/1.0/post/insertCustomComp",
175
+ body, header);
176
+ if (res.returnCode == 200) {
177
+ console.error(chalk.green(`发布成功!`));
178
+ console.log();
179
+ } else {
180
+ console.error(chalk.red(`发布失败: ${res.returnInfo}`));
181
+ console.log();
182
+ }
183
+ return res;
184
+ }
185
+
186
+ /**
187
+ * 过滤出min.js文件
188
+ * @returns 过滤结果
189
+ */
190
+ getVueContent(buildFileName) {
191
+ const jsPath = "plugin";
192
+ return path.join(jsPath, buildFileName);
193
+ }
194
+ }
195
195
  module.exports = Builder;