jjb-cmd 1.0.11 → 1.0.12

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.
@@ -0,0 +1,227 @@
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const {
4
+ GIT_HOST,
5
+ GIT_TEMP_DIR,
6
+ CLOUD_PROJECT
7
+ } = require('./config');
8
+
9
+ /**
10
+ * @description 删除全部
11
+ * @param path {string} 路径
12
+ */
13
+ exports.f_rm_rf = function (path) {
14
+ if (fs.existsSync(path)) {
15
+ const list = fs.readdirSync(path);
16
+ for (let i = 0; i < list.length; i++) {
17
+ const item = list[ i ];
18
+ const vPath = `${path}/${item}`;
19
+ if (fs.statSync(vPath).isDirectory()) {
20
+ exports.f_rm_rf(vPath);
21
+ fs.rmdirSync(vPath);
22
+ } else {
23
+ fs.unlinkSync(vPath);
24
+ }
25
+ }
26
+ }
27
+ };
28
+
29
+ /**
30
+ * @typedef {object} GitResource
31
+ * @property {string} path
32
+ * @property {string} compress
33
+ * @property {string} repository
34
+ */
35
+
36
+ /**
37
+ * @description 拉取git资源
38
+ * @param installResources {Resource[]} 资源名称
39
+ * @return {GitResource[]}
40
+ */
41
+ exports.f_pull_git_repository = function (installResources = []) {
42
+ return installResources.map(item => {
43
+ const resource = CLOUD_PROJECT[ item.name ] || undefined;
44
+ const template = os.tmpdir();
45
+ return {
46
+ path: `${template}\\${GIT_TEMP_DIR}\\${item.name}.zip`,
47
+ compress: `${template}\\${GIT_TEMP_DIR}\\${item.name}_zip`,
48
+ repository: `${GIT_HOST}/api/v4/projects/${resource.projectId}/repository/archive.zip?private_token=${resource.token}&ref=master`
49
+ };
50
+ });
51
+ };
52
+
53
+ /**
54
+ * @description 扫描是否存在jjb.config.json
55
+ * @param root 路径
56
+ * @returns {boolean}
57
+ */
58
+ exports.f_scan_jjb_config_json = function (root) {
59
+ return fs.existsSync(`${root}\\jjb.config.json`);
60
+ };
61
+
62
+ /**
63
+ * @typedef {object} JJB_CONFIG_JSON
64
+ * @property {string} projectType
65
+ * @property {string} installTarget
66
+ * @property {Resource[]} installResources
67
+ */
68
+
69
+ /**
70
+ * @description 验证规则
71
+ * @param root {string} 路径
72
+ * @return {JJB_CONFIG_JSON}
73
+ */
74
+ exports.f_scan_jjb_config_json_rules = function (root) {
75
+ let jjb_config_json = {};
76
+ try {
77
+ jjb_config_json = JSON.parse(fs.readFileSync(`${root}\\jjb.config.json`).toString());
78
+ } catch (e) {
79
+ console.log('【Error】:[jjb.config.json]文件解析失败,请确认是否配置正确。');
80
+ process.exit(0);
81
+ }
82
+ if (!('projectType' in jjb_config_json)) {
83
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要projectType属性。');
84
+ process.exit(0);
85
+ }
86
+ if (!('installTarget' in jjb_config_json)) {
87
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要installTarget属性。');
88
+ process.exit(0);
89
+ }
90
+ if (!('installResources' in jjb_config_json)) {
91
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要installResources属性。');
92
+ process.exit(0);
93
+ }
94
+ if (typeof jjb_config_json.projectType !== 'string') {
95
+ console.log('【Error】:[jjb.config.json.projectType]类型是一个string。');
96
+ process.exit(0);
97
+ }
98
+ if (![
99
+ 'multi',
100
+ 'spa',
101
+ 'uniapp',
102
+ 'micro-spa'
103
+ ].includes(jjb_config_json.projectType)) {
104
+ console.log('【Error】:[jjb.config.json.projectType]配置无效,有效值<multi | spa | micro-spa | uniapp>。');
105
+ process.exit(0);
106
+ }
107
+ if (typeof jjb_config_json.installTarget !== 'string') {
108
+ console.log('【Error】:[jjb.config.json.installTarget]类型是一个string。');
109
+ process.exit(0);
110
+ }
111
+ if (![
112
+ 'node_modules',
113
+ 'src'
114
+ ].includes(jjb_config_json.installTarget)) {
115
+ console.log('【Error】:[jjb.config.json.node_modules]配置无效,有效值<node_modules | src>。');
116
+ process.exit(0);
117
+ }
118
+ if (!Array.isArray(jjb_config_json.installResources)) {
119
+ console.log('【Error】:[jjb.config.json.installResources]类型是一个Array<string>。');
120
+ process.exit(0);
121
+ }
122
+ if (jjb_config_json.installResources.length === 0) {
123
+ console.log('【Error】:[jjb.config.json.installResources]无资源。');
124
+ process.exit(0);
125
+ }
126
+ const resources = exports.f_resolve_install_resources(jjb_config_json.installResources);
127
+ if (resources.map(item => item.name).filter(v => ![
128
+ 'common',
129
+ 'react-admin-component',
130
+ 'vue-unisass-component'
131
+ ].includes(v)).length !== 0) {
132
+ console.log('【Error】:[jjb.config.json.installResources]配置无效,有效值<common | react-admin-component | vue-unisass-component>。');
133
+ process.exit(0);
134
+ }
135
+ jjb_config_json.installResources = resources;
136
+ return jjb_config_json;
137
+ };
138
+
139
+ /**
140
+ * @description 创建package.json
141
+ * @param path {string} 路径
142
+ * @param name {string} 包名
143
+ * @param version {string} 版本
144
+ */
145
+ exports.f_create_package_json = function (path, name, version) {
146
+ fs.writeFileSync(`${path}\\package.json`, `{"name":"${name}","version":"${version}","main": "index.js"}`);
147
+ };
148
+
149
+ /**
150
+ * @typedef {object} Resource
151
+ * @property {string} name
152
+ * @property {string[]} importList
153
+ */
154
+
155
+ /**
156
+ * @description 分析resources
157
+ * @param installResources
158
+ * @return {Resource[]}
159
+ */
160
+ exports.f_resolve_install_resources = function (installResources = []) {
161
+ const resources = [];
162
+ if (Array.isArray(installResources)) {
163
+ installResources.forEach(resource => {
164
+ if (Array.isArray(resource)) {
165
+ const [ name, importList = [] ] = resource;
166
+ resources.push({
167
+ name,
168
+ importList
169
+ });
170
+ } else {
171
+ resources.push({
172
+ name: resource,
173
+ importList: []
174
+ });
175
+ }
176
+ });
177
+ }
178
+ return resources;
179
+ };
180
+
181
+ /**
182
+ * @description 更新项目package.json文件
183
+ * @param path {string} 路径
184
+ * @param name {string} 包名
185
+ * @param version {string} 版本
186
+ */
187
+ exports.f_update_project_package_json = function (path, name, version) {
188
+ const packageJSONFile = JSON.parse(fs.readFileSync(path).toString());
189
+ packageJSONFile.dependencies[ name ] = version;
190
+ fs.writeFileSync(path, JSON.stringify(packageJSONFile, null, 2));
191
+ };
192
+
193
+ /**
194
+ * @description 复制文件
195
+ * @param originSrc
196
+ * @param targetSrc
197
+ */
198
+ exports.f_file_copy = function (originSrc, targetSrc) {
199
+ fs.readdirSync(originSrc).forEach(dir => {
200
+ const oPath = `${originSrc}\\${dir}`;
201
+ const tPath = `${targetSrc}\\${dir}`;
202
+ if (fs.statSync(oPath).isDirectory()) {
203
+ fs.mkdirSync(tPath);
204
+ exports.f_file_copy(oPath, tPath);
205
+ } else {
206
+ fs.writeFileSync(tPath, fs.readFileSync(oPath).toString());
207
+ }
208
+ });
209
+ };
210
+
211
+ /**
212
+ * @description 替换文件操作
213
+ * @param source {[]} 替换源
214
+ * @param root {string} 路径
215
+ */
216
+ exports.f_content_replace = function (source = [], root) {
217
+ source.forEach(item => {
218
+ const path = root + item.path;
219
+ if (fs.existsSync(path)) {
220
+ let content = fs.readFileSync(path).toString();
221
+ item.replace.forEach(rep => {
222
+ content = content.replace(rep[ 0 ], rep[ 1 ]);
223
+ });
224
+ fs.writeFileSync(path, content);
225
+ }
226
+ });
227
+ };
@@ -0,0 +1,225 @@
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const {
4
+ GIT_HOST,
5
+ GIT_TEMP_DIR,
6
+ CLOUD_PROJECT
7
+ } = require('./config');
8
+
9
+ /**
10
+ * @description 删除全部
11
+ * @param path {string} 路径
12
+ */
13
+ exports.f_rm_rf = function (path) {
14
+ if (fs.existsSync(path)) {
15
+ const list = fs.readdirSync(path);
16
+ for (let i = 0; i < list.length; i++) {
17
+ const item = list[ i ];
18
+ const vPath = `${path}/${item}`;
19
+ if (fs.statSync(vPath).isDirectory()) {
20
+ exports.f_rm_rf(vPath);
21
+ fs.rmdirSync(vPath);
22
+ } else {
23
+ fs.unlinkSync(vPath);
24
+ }
25
+ }
26
+ }
27
+ };
28
+
29
+ /**
30
+ * @typedef {object} GitResource
31
+ * @property {string} path
32
+ * @property {string} compress
33
+ * @property {string} repository
34
+ */
35
+
36
+ /**
37
+ * @description 拉取git资源
38
+ * @param installResources {Resource[]} 资源名称
39
+ * @return {GitResource[]}
40
+ */
41
+ exports.f_pull_git_repository = function (installResources = []) {
42
+ return installResources.map(item => {
43
+ const resource = CLOUD_PROJECT[ item.name ] || undefined;
44
+ const template = os.tmpdir();
45
+ return {
46
+ path: `${template}\\${GIT_TEMP_DIR}\\${item.name}.zip`,
47
+ compress: `${template}\\${GIT_TEMP_DIR}\\${item.name}_zip`,
48
+ repository: `${GIT_HOST}/api/v4/projects/${resource.projectId}/repository/archive.zip?private_token=${resource.token}&ref=master`
49
+ };
50
+ });
51
+ };
52
+
53
+ /**
54
+ * @description 扫描是否存在jjb.config.json
55
+ * @param root 路径
56
+ * @returns {boolean}
57
+ */
58
+ exports.f_scan_jjb_config_json = function (root) {
59
+ return fs.existsSync(`${root}\\jjb.config.json`);
60
+ };
61
+
62
+ /**
63
+ * @typedef {object} JJB_CONFIG_JSON
64
+ * @property {string} projectType
65
+ * @property {string} installTarget
66
+ * @property {Resource[]} installResources
67
+ */
68
+
69
+ /**
70
+ * @description 验证规则
71
+ * @param root {string} 路径
72
+ * @return {JJB_CONFIG_JSON}
73
+ */
74
+ exports.f_scan_jjb_config_json_rules = function (root) {
75
+ let jjb_config_json = {};
76
+ try {
77
+ jjb_config_json = JSON.parse(fs.readFileSync(`${root}\\jjb.config.json`).toString());
78
+ } catch (e) {
79
+ console.log('【Error】:[jjb.config.json]文件解析失败,请确认是否配置正确。');
80
+ process.exit(0);
81
+ }
82
+ if (!('projectType' in jjb_config_json)) {
83
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要projectType属性。');
84
+ process.exit(0);
85
+ }
86
+ if (!('installTarget' in jjb_config_json)) {
87
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要installTarget属性。');
88
+ process.exit(0);
89
+ }
90
+ if (!('installResources' in jjb_config_json)) {
91
+ console.log('【Error】:[jjb.config.json]文件配置无效,需要installResources属性。');
92
+ process.exit(0);
93
+ }
94
+ if (typeof jjb_config_json.projectType !== 'string') {
95
+ console.log('【Error】:[jjb.config.json.projectType]类型是一个string。');
96
+ process.exit(0);
97
+ }
98
+ if (![
99
+ 'multi',
100
+ 'spa',
101
+ 'micro-spa'
102
+ ].includes(jjb_config_json.projectType)) {
103
+ console.log('【Error】:[jjb.config.json.projectType]配置无效,有效值<multi | spa | micro-spa>。');
104
+ process.exit(0);
105
+ }
106
+ if (typeof jjb_config_json.installTarget !== 'string') {
107
+ console.log('【Error】:[jjb.config.json.installTarget]类型是一个string。');
108
+ process.exit(0);
109
+ }
110
+ if (![
111
+ 'node_modules',
112
+ 'src'
113
+ ].includes(jjb_config_json.installTarget)) {
114
+ console.log('【Error】:[jjb.config.json.node_modules]配置无效,有效值<node_modules | src>。');
115
+ process.exit(0);
116
+ }
117
+ if (!Array.isArray(jjb_config_json.installResources)) {
118
+ console.log('【Error】:[jjb.config.json.installResources]类型是一个Array<string>。');
119
+ process.exit(0);
120
+ }
121
+ if (jjb_config_json.installResources.length === 0) {
122
+ console.log('【Error】:[jjb.config.json.installResources]无资源。');
123
+ process.exit(0);
124
+ }
125
+ const resources = exports.f_resolve_install_resources(jjb_config_json.installResources);
126
+ if (resources.map(item => item.name).filter(v => ![
127
+ 'common',
128
+ 'react-admin-component'
129
+ ].includes(v)).length !== 0) {
130
+ console.log('【Error】:[jjb.config.json.installResources]配置无效,有效值<common | react-admin-component>。');
131
+ process.exit(0);
132
+ }
133
+ jjb_config_json.installResources = resources;
134
+ return jjb_config_json;
135
+ };
136
+
137
+ /**
138
+ * @description 创建package.json
139
+ * @param path {string} 路径
140
+ * @param name {string} 包名
141
+ * @param version {string} 版本
142
+ */
143
+ exports.f_create_package_json = function (path, name, version) {
144
+ fs.writeFileSync(`${path}\\package.json`, `{"name":"${name}","version":"${version}","main": "index.js"}`);
145
+ };
146
+
147
+ /**
148
+ * @typedef {object} Resource
149
+ * @property {string} name
150
+ * @property {string[]} importList
151
+ */
152
+
153
+ /**
154
+ * @description 分析resources
155
+ * @param installResources
156
+ * @return {Resource[]}
157
+ */
158
+ exports.f_resolve_install_resources = function (installResources = []) {
159
+ const resources = [];
160
+ if (Array.isArray(installResources)) {
161
+ installResources.forEach(resource => {
162
+ if (Array.isArray(resource)) {
163
+ const [ name, importList = [] ] = resource;
164
+ resources.push({
165
+ name,
166
+ importList
167
+ });
168
+ } else {
169
+ resources.push({
170
+ name: resource,
171
+ importList: []
172
+ });
173
+ }
174
+ });
175
+ }
176
+ return resources;
177
+ };
178
+
179
+ /**
180
+ * @description 更新项目package.json文件
181
+ * @param path {string} 路径
182
+ * @param name {string} 包名
183
+ * @param version {string} 版本
184
+ */
185
+ exports.f_update_project_package_json = function (path, name, version) {
186
+ const packageJSONFile = JSON.parse(fs.readFileSync(path).toString());
187
+ packageJSONFile.dependencies[ name ] = version;
188
+ fs.writeFileSync(path, JSON.stringify(packageJSONFile, null, 2));
189
+ };
190
+
191
+ /**
192
+ * @description 复制文件
193
+ * @param originSrc
194
+ * @param targetSrc
195
+ */
196
+ exports.f_file_copy = function (originSrc, targetSrc) {
197
+ fs.readdirSync(originSrc).forEach(dir => {
198
+ const oPath = `${originSrc}\\${dir}`;
199
+ const tPath = `${targetSrc}\\${dir}`;
200
+ if (fs.statSync(oPath).isDirectory()) {
201
+ fs.mkdirSync(tPath);
202
+ exports.f_file_copy(oPath, tPath);
203
+ } else {
204
+ fs.writeFileSync(tPath, fs.readFileSync(oPath).toString());
205
+ }
206
+ });
207
+ };
208
+
209
+ /**
210
+ * @description 替换文件操作
211
+ * @param source {[]} 替换源
212
+ * @param root {string} 路径
213
+ */
214
+ exports.f_content_replace = function (source = [], root) {
215
+ source.forEach(item => {
216
+ const path = root + item.path;
217
+ if (fs.existsSync(path)) {
218
+ let content = fs.readFileSync(path).toString();
219
+ item.replace.forEach(rep => {
220
+ content = content.replace(rep[ 0 ], rep[ 1 ]);
221
+ });
222
+ fs.writeFileSync(path, content);
223
+ }
224
+ });
225
+ };
package/src/cli.merge.js CHANGED
@@ -28,34 +28,27 @@ module.exports = (input, source) => {
28
28
  const settingJson = JSON.parse(fs.readFileSync(`${projectPath}/${inputRegion}/setting.json`).toString());
29
29
  TileItems(settingJson.items, () => {
30
30
 
31
- Object.keys(fromFileData).forEach(fileKey => {
32
- fromFileData[fileKey].forEach(fromItem => {
33
- const itemPathArr = fromItem.pathAdr.split(fromItem.region);
34
- const filePathArr = itemPathArr[1].split('/');
35
- const application = filePathArr[1];
36
- if (!applicationJson[application]) {
37
- applicationJson[application] = {
38
- routerConfig: [],
39
- routerPath: []
40
- };
41
- }
42
- applicationJson[application].routerConfig.push(`{ path: '${fromItem.path}', component: () => import('${fromItem.file}') }`);
43
- applicationJson[application].routerPath.push(fromItem.path);
44
-
45
- let applicationStr = filePathArr.join('/');
31
+ fromFileData.router.forEach(fromItem => {
32
+ const itemPathArr = fromItem.pathAdr.split(fromItem.region);
33
+ const filePathArr = itemPathArr[1].split('/');
34
+ const application = filePathArr[1];
35
+ if (!applicationJson[application]) {
36
+ applicationJson[application] = {
37
+ routerConfig: [],
38
+ routerPath: []
39
+ };
40
+ }
41
+ applicationJson[application].routerConfig.push(`{ path: '${fromItem.path}', component: () => import('${fromItem.file}') }`);
42
+ applicationJson[application].routerPath.push(fromItem.path);
46
43
 
47
- const strArr = applicationStr.split('/');
48
- utils.CreatePaths(applicationPath, strArr.slice(0, strArr.length - 1)).then(() => {
49
- if (fromItem.pathAdr.match(/(.png|.svg)/) != null) {
50
- const imageFile = fs.readFileSync(fromItem.pathAdr, 'binary');
51
- fs.writeFileSync(`${applicationPath}/${applicationStr}`, imageFile, 'binary');
52
- } else {
53
- const fileContent = fs.readFileSync(fromItem.pathAdr).toString();
54
- fs.writeFileSync(`${applicationPath}/${applicationStr}`, fileContent);
55
- }
56
- });
44
+ let applicationStr = filePathArr.join('/');
57
45
 
46
+ const strArr = applicationStr.split('/');
47
+ utils.CreatePaths(applicationPath, strArr.slice(0, strArr.length - 1)).then(() => {
48
+ const fileContent = fs.readFileSync(fromItem.pathAdr).toString();
49
+ fs.writeFileSync(`${vesselPath}/${applicationStr}`, fileContent);
58
50
  });
51
+
59
52
  });
60
53
 
61
54
  });
package/src/cli.pull.js CHANGED
@@ -63,6 +63,8 @@ module.exports = input => {
63
63
  commonWebsiteFile = commonWebsiteFile.replace(/const\srelation\s=\srequire\(`~\/application\/\${module.code}\/enumerate\/menu`\)\.default;/, "const relation = require('~/enumerate/menu').default;");
64
64
  fs.writeFileSync(commonToolsPath, commonToolsFile);
65
65
  fs.writeFileSync(commonWebsitePath, commonWebsiteFile);
66
+ fs.writeFileSync(`${srcDir}\\common\\dva\\automatic\\router.js`, fs.readFileSync(`${__dirname}\\cli.dva.router.spa.txt`).toString());
67
+ fs.writeFileSync(`${srcDir}\\common\\dva\\automatic\\register.js`, fs.readFileSync(`${__dirname}\\cli.dva.register.spa.txt`).toString());
66
68
  } else {
67
69
  fs.exists(srcDir + '\\' + 'common\\dva\\automatic\\index.js', has => {
68
70
  if (has) {