@steedos/service-package-loader 2.5.17-beta.8 → 2.5.17

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 CHANGED
@@ -1,2 +1,11 @@
1
+ <!--
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2021-10-21 14:19:05
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-27 13:00:22
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/README.md
7
+ * @Description:
8
+ -->
1
9
  ## 功能说明
2
- - 用于加载自定义对象
10
+ - 用于加载自定义对象
11
+ - 工作区初始化完成后默认导入软件包main/default/data文件夹下的{objectname}.data.json、{objectname}.data.csv、{flowApiname}.flow.data.json数据
package/index.js CHANGED
@@ -28,6 +28,8 @@ const getPackageYmlData = (packagePath)=>{
28
28
  return packageYmlData;
29
29
  }
30
30
 
31
+ const methods = require('./lib/methods')
32
+
31
33
  /**
32
34
  * @typedef {import('moleculer').Context} Context Moleculer's Context
33
35
  */
@@ -73,8 +75,11 @@ module.exports = {
73
75
  }
74
76
  },
75
77
  "space.initialized": {
76
- async handler() {
78
+ async handler(ctx) {
77
79
  await this.loadDataOnServiceStarted();
80
+ const spaceDoc = ctx.params
81
+ // 扫描main/default/data文件夹
82
+ await this.importData(path.join(this.settings.packageInfo.path, 'main', 'default', 'data'), true, spaceDoc._id);
78
83
  }
79
84
  }
80
85
  },
@@ -83,6 +88,8 @@ module.exports = {
83
88
  * Methods
84
89
  */
85
90
  methods: {
91
+ ...methods,
92
+
86
93
  checkPackageMetadataFiles: async function (packagePath) {
87
94
 
88
95
  if(this.core){
@@ -0,0 +1,3 @@
1
+ export interface Base {
2
+ readFile(filePath: string, options?: any): any;
3
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Base.js","sourceRoot":"","sources":["../../src/imports/Base.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import ImportJson from './ImportJson';
2
+ export default class ImportCsv extends ImportJson {
3
+ readFile(filePath: string): Promise<Array<{
4
+ objectName: string;
5
+ records: Array<any>;
6
+ }>>;
7
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const ImportJson_1 = require("./ImportJson");
5
+ const csv = require("csvtojson");
6
+ const path = require("path");
7
+ const metadata_core_1 = require("@steedos/metadata-core");
8
+ class ImportCsv extends ImportJson_1.default {
9
+ readFile(filePath) {
10
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
+ let results = [];
12
+ const filePatten = [
13
+ path.join(filePath, "**", "*.data.csv"),
14
+ "!" + path.join(filePath, "node_modules"),
15
+ ];
16
+ const matchedPaths = (0, metadata_core_1.syncMatchFiles)(filePatten);
17
+ for (const matchedPath of matchedPaths) {
18
+ let records = yield csv().fromFile(matchedPath);
19
+ let objectName = path.basename(matchedPath).split('.')[0];
20
+ results.push({ objectName: objectName, records: records });
21
+ }
22
+ return results;
23
+ });
24
+ }
25
+ }
26
+ exports.default = ImportCsv;
27
+ //# sourceMappingURL=ImportCsv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportCsv.js","sourceRoot":"","sources":["../../src/imports/ImportCsv.ts"],"names":[],"mappings":";;;AAQA,6CAAsC;AACtC,iCAAkC;AAClC,6BAA8B;AAC9B,0DAAwD;AAExD,MAAqB,SAAU,SAAQ,oBAAU;IAEvC,QAAQ,CAAC,QAAgB;;YAC3B,IAAI,OAAO,GAAQ,EAAE,CAAA;YACrB,MAAM,UAAU,GAAG;gBACf,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,YAAY,CAAC;gBACvC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC;aAC5C,CAAA;YACD,MAAM,YAAY,GAAa,IAAA,8BAAc,EAAC,UAAU,CAAC,CAAC;YAC1D,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;gBACpC,IAAI,OAAO,GAAG,MAAM,GAAG,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;gBAChD,IAAI,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC1D,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;aAC9D;YACD,OAAO,OAAO,CAAA;QAClB,CAAC;KAAA;CACJ;AAhBD,4BAgBC"}
@@ -0,0 +1,7 @@
1
+ import { Base } from './Base';
2
+ export default class ImportExcel implements Base {
3
+ readFile(filePath: string, options?: any): {
4
+ datas: Array<any>;
5
+ headers: Array<string>;
6
+ };
7
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_xlsx_1 = require("node-xlsx");
4
+ class ImportExcel {
5
+ readFile(filePath, options = {}) {
6
+ const { sheetIndex = 0, headerIndex = 0 } = options;
7
+ let workbook = node_xlsx_1.default.parse(filePath, {
8
+ cellDates: true,
9
+ });
10
+ const data = workbook[sheetIndex].data;
11
+ const headers = data[headerIndex];
12
+ const datas = data.slice(1);
13
+ return {
14
+ headers,
15
+ datas
16
+ };
17
+ }
18
+ }
19
+ exports.default = ImportExcel;
20
+ //# sourceMappingURL=ImportExcel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportExcel.js","sourceRoot":"","sources":["../../src/imports/ImportExcel.ts"],"names":[],"mappings":";;AAQA,yCAA6B;AAG7B,MAAqB,WAAW;IAE5B,QAAQ,CAAC,QAAgB,EAAE,UAAe,EAAE;QACxC,MAAM,EAAE,UAAU,GAAG,CAAC,EAAE,WAAW,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;QACpD,IAAI,QAAQ,GAAG,mBAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;YAChC,SAAS,EAAE,IAAI;SAClB,CAAC,CAAC;QACH,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC;QACvC,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC;QAClC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC5B,OAAO;YACH,OAAO;YACP,KAAK;SACR,CAAC;IACN,CAAC;CAEJ;AAhBD,8BAgBC"}
@@ -0,0 +1,4 @@
1
+ import { Base } from './Base';
2
+ export default class ImportFlow implements Base {
3
+ readFile(filePath: string): any;
4
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const metadata_core_1 = require("@steedos/metadata-core");
4
+ const path = require("path");
5
+ const _ = require("lodash");
6
+ class ImportFlow {
7
+ readFile(filePath) {
8
+ const filePatten = [
9
+ path.join(filePath, "**", "*.flow.data.json"),
10
+ "!" + path.join(filePath, "node_modules"),
11
+ ];
12
+ const matchedPaths = (0, metadata_core_1.syncMatchFiles)(filePatten);
13
+ let flows = {};
14
+ for (let k = 0; k < matchedPaths.length; k++) {
15
+ let matchedPath = matchedPaths[k];
16
+ let json = (0, metadata_core_1.loadFile)(matchedPath);
17
+ let formName = matchedPath.substring(matchedPath.lastIndexOf('/') + 1, matchedPath.indexOf('.flow'));
18
+ let form = {};
19
+ try {
20
+ if (json) {
21
+ let flowKeys = _.keys(json);
22
+ for (let m in flowKeys) {
23
+ let key = flowKeys[m];
24
+ if (typeof key === 'function') {
25
+ continue;
26
+ }
27
+ let val = json[key];
28
+ if (typeof val === 'function') {
29
+ json[key] = val.toString();
30
+ }
31
+ form[key] = json[key];
32
+ }
33
+ flows[formName] = json;
34
+ }
35
+ }
36
+ catch (error) {
37
+ console.error('loadFlows error', matchedPath, error);
38
+ throw error;
39
+ }
40
+ }
41
+ return flows;
42
+ }
43
+ }
44
+ exports.default = ImportFlow;
45
+ //# sourceMappingURL=ImportFlow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportFlow.js","sourceRoot":"","sources":["../../src/imports/ImportFlow.ts"],"names":[],"mappings":";;AAQA,0DAAkE;AAElE,6BAA8B;AAC9B,4BAA6B;AAE7B,MAAqB,UAAU;IAE3B,QAAQ,CAAC,QAAgB;QACrB,MAAM,UAAU,GAAG;YACf,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,kBAAkB,CAAC;YAC7C,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC;SAC5C,CAAA;QACD,MAAM,YAAY,GAAa,IAAA,8BAAc,EAAC,UAAU,CAAC,CAAC;QAC1D,IAAI,KAAK,GAAG,EAAE,CAAC;QACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC1C,IAAI,WAAW,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;YAClC,IAAI,IAAI,GAAG,IAAA,wBAAQ,EAAC,WAAW,CAAC,CAAC;YACjC,IAAI,QAAQ,GAAG,WAAW,CAAC,SAAS,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;YACrG,IAAI,IAAI,GAAG,EAAE,CAAC;YACd,IAAI;gBACA,IAAI,IAAI,EAAE;oBAEN,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;oBAC5B,KAAK,IAAI,CAAC,IAAI,QAAQ,EAAE;wBACpB,IAAI,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;wBACtB,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;4BAC3B,SAAS;yBACZ;wBACD,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;wBACpB,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;4BAC3B,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;yBAC9B;wBACD,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;qBACzB;oBACD,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;iBAC1B;aACJ;YAAC,OAAO,KAAK,EAAE;gBACZ,OAAO,CAAC,KAAK,CAAC,iBAAiB,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;gBACrD,MAAM,KAAK,CAAA;aACd;SACJ;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;CAEJ;AAvCD,6BAuCC"}
@@ -0,0 +1,7 @@
1
+ import { Base } from './Base';
2
+ export default class ImportJson implements Base {
3
+ readFile(filePath: string): Promise<Array<{
4
+ objectName: string;
5
+ records: Array<any>;
6
+ }>>;
7
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const lodash_1 = require("lodash");
5
+ const metadata_core_1 = require("@steedos/metadata-core");
6
+ const path = require("path");
7
+ const fs = require("fs");
8
+ class ImportJson {
9
+ readFile(filePath) {
10
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
+ let results = [];
12
+ const filePatten = [
13
+ path.join(filePath, "**", "*.data.json"),
14
+ "!" + path.join(filePath, "**", "*.flow.data.json"),
15
+ "!" + path.join(filePath, "node_modules")
16
+ ];
17
+ const matchedPaths = (0, metadata_core_1.syncMatchFiles)(filePatten);
18
+ (0, lodash_1.each)(matchedPaths, (matchedPath) => {
19
+ let records = JSON.parse(fs.readFileSync(matchedPath, 'utf8').normalize('NFC'));
20
+ let objectName = path.basename(matchedPath).split('.')[0];
21
+ results.push({ objectName: objectName, records: records });
22
+ });
23
+ return results;
24
+ });
25
+ }
26
+ }
27
+ exports.default = ImportJson;
28
+ //# sourceMappingURL=ImportJson.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportJson.js","sourceRoot":"","sources":["../../src/imports/ImportJson.ts"],"names":[],"mappings":";;;AAQA,mCAA8B;AAE9B,0DAAwD;AACxD,6BAA8B;AAC9B,yBAA0B;AAE1B,MAAqB,UAAU;IAErB,QAAQ,CAAC,QAAgB;;YAC3B,IAAI,OAAO,GAAQ,EAAE,CAAA;YACrB,MAAM,UAAU,GAAG;gBACf,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,aAAa,CAAC;gBACxC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,kBAAkB,CAAC;gBACnD,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC;aAAC,CAAC;YAE/C,MAAM,YAAY,GAAa,IAAA,8BAAc,EAAC,UAAU,CAAC,CAAC;YAC1D,IAAA,aAAI,EAAC,YAAY,EAAE,CAAC,WAAmB,EAAE,EAAE;gBACvC,IAAI,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;gBAChF,IAAI,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC1D,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;YAC/D,CAAC,CAAC,CAAA;YACF,OAAO,OAAO,CAAA;QAClB,CAAC;KAAA;CAEJ;AAlBD,6BAkBC"}
@@ -0,0 +1 @@
1
+ export declare function handler(filePath: string, onlyInsert: boolean, spaceId: string): Promise<void>;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handler = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const ImportJson_1 = require("../imports/ImportJson");
6
+ const ImportCsv_1 = require("../imports/ImportCsv");
7
+ const ImportFlow_1 = require("../imports/ImportFlow");
8
+ const fs = require("fs");
9
+ function handler(filePath, onlyInsert, spaceId) {
10
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
+ if (!filePath) {
12
+ return;
13
+ }
14
+ if (!fs.existsSync(filePath)) {
15
+ return;
16
+ }
17
+ const importer = {
18
+ csv: new ImportCsv_1.default(),
19
+ json: new ImportJson_1.default(),
20
+ flow: new ImportFlow_1.default()
21
+ };
22
+ const csvData = yield importer.csv.readFile(filePath);
23
+ const jsonData = yield importer.json.readFile(filePath);
24
+ const flowData = yield importer.flow.readFile(filePath);
25
+ yield this.broker.call("~packages-@steedos/data-import.importData", {
26
+ data: {
27
+ "csv": csvData,
28
+ "json": jsonData,
29
+ "flow": flowData,
30
+ },
31
+ spaceId,
32
+ onlyInsert: true,
33
+ });
34
+ });
35
+ }
36
+ exports.handler = handler;
37
+ //# sourceMappingURL=importData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"importData.js","sourceRoot":"","sources":["../../src/methods/importData.ts"],"names":[],"mappings":";;;;AAQA,sDAA+C;AAC/C,oDAA4C;AAC5C,sDAA8C;AAE9C,yBAA0B;AAE1B,SAAsB,OAAO,CAAC,QAAgB,EAAE,UAAmB,EAAE,OAAe;;QAChF,IAAI,CAAC,QAAQ,EAAE;YACX,OAAM;SACT;QACD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;YAC1B,OAAM;SACT;QAED,MAAM,QAAQ,GAAG;YACb,GAAG,EAAE,IAAI,mBAAS,EAAE;YACpB,IAAI,EAAE,IAAI,oBAAU,EAAE;YACtB,IAAI,EAAE,IAAI,oBAAU,EAAE;SACzB,CAAA;QAED,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAEtD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAExD,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAExD,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2CAA2C,EAAE;YAChE,IAAI,EAAE;gBACF,KAAK,EAAE,OAAO;gBACd,MAAM,EAAE,QAAQ;gBAChB,MAAM,EAAE,QAAQ;aACnB;YACD,OAAO;YACP,UAAU,EAAE,IAAI;SACnB,CAAC,CAAA;IAEN,CAAC;CAAA;AA9BD,0BA8BC"}
@@ -0,0 +1 @@
1
+ export * as importData from './importData';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importData = void 0;
4
+ exports.importData = require("./importData");
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/methods/index.ts"],"names":[],"mappings":";;;AAQA,6CAA2C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steedos/service-package-loader",
3
- "version": "2.5.17-beta.8",
3
+ "version": "2.5.17",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,12 +10,14 @@
10
10
  "author": "",
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
- "@steedos/metadata-core": "2.5.17-beta.8",
14
- "@steedos/metadata-registrar": "2.5.17-beta.8",
15
- "@steedos/router": "2.5.17-beta.8",
16
- "@steedos/service-object-mixin": "2.5.17-beta.8",
13
+ "@steedos/metadata-core": "2.5.17",
14
+ "@steedos/metadata-registrar": "2.5.17",
15
+ "@steedos/router": "2.5.17",
16
+ "@steedos/service-object-mixin": "2.5.17",
17
17
  "clone": "^2.1.2",
18
+ "csvtojson": "~2.0.10",
18
19
  "moleculer": "^0.14.25",
20
+ "node-xlsx": "^0.16.1",
19
21
  "underscore": "^1.12.0"
20
22
  },
21
23
  "devDependencies": {
@@ -25,5 +27,5 @@
25
27
  "publishConfig": {
26
28
  "access": "public"
27
29
  },
28
- "gitHead": "a4bfc7089af2c007f24f6a2c50e39248968be149"
30
+ "gitHead": "c1e5161c0f7357c2daf621a09818f675fa76861e"
29
31
  }
@@ -0,0 +1,11 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:31:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:41:08
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/imports/Base.ts
7
+ * @Description:
8
+ */
9
+ export interface Base {
10
+ readFile(filePath: string, options?: any): any
11
+ }
@@ -0,0 +1,30 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:31:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:51:00
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/imports/ImportCsv.ts
7
+ * @Description:
8
+ */
9
+ import ImportJson from './ImportJson';
10
+ import csv = require('csvtojson');
11
+ import path = require('path');
12
+ import { syncMatchFiles } from '@steedos/metadata-core';
13
+
14
+ export default class ImportCsv extends ImportJson {
15
+
16
+ async readFile(filePath: string): Promise<Array<{ objectName: string, records: Array<any> }>> {
17
+ let results: any = []
18
+ const filePatten = [
19
+ path.join(filePath, "**", "*.data.csv"),
20
+ "!" + path.join(filePath, "node_modules"),
21
+ ]
22
+ const matchedPaths: [string] = syncMatchFiles(filePatten);
23
+ for (const matchedPath of matchedPaths) {
24
+ let records = await csv().fromFile(matchedPath);
25
+ let objectName = path.basename(matchedPath).split('.')[0];
26
+ results.push({ objectName: objectName, records: records });
27
+ }
28
+ return results
29
+ }
30
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:31:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:44:27
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/imports/ImportExcel.ts
7
+ * @Description:
8
+ */
9
+ import xlsx from 'node-xlsx';
10
+ import { Base } from './Base';
11
+
12
+ export default class ImportExcel implements Base {
13
+
14
+ readFile(filePath: string, options: any = {}): { datas: Array<any>, headers: Array<string> } {
15
+ const { sheetIndex = 0, headerIndex = 0 } = options;
16
+ let workbook = xlsx.parse(filePath, {
17
+ cellDates: true,
18
+ });
19
+ const data = workbook[sheetIndex].data;
20
+ const headers = data[headerIndex];
21
+ const datas = data.slice(1);
22
+ return {
23
+ headers,
24
+ datas
25
+ };
26
+ }
27
+
28
+ }
@@ -0,0 +1,53 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:31:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:46:25
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/imports/ImportFlow.ts
7
+ * @Description:
8
+ */
9
+ import { loadFile, syncMatchFiles } from '@steedos/metadata-core';
10
+ import { Base } from './Base';
11
+ import path = require('path');
12
+ import _ = require('lodash');
13
+
14
+ export default class ImportFlow implements Base {
15
+
16
+ readFile(filePath: string): any {
17
+ const filePatten = [
18
+ path.join(filePath, "**", "*.flow.data.json"),
19
+ "!" + path.join(filePath, "node_modules"),
20
+ ]
21
+ const matchedPaths: [string] = syncMatchFiles(filePatten);
22
+ let flows = {};
23
+ for (let k = 0; k < matchedPaths.length; k++) {
24
+ let matchedPath = matchedPaths[k];
25
+ let json = loadFile(matchedPath);
26
+ let formName = matchedPath.substring(matchedPath.lastIndexOf('/') + 1, matchedPath.indexOf('.flow'));
27
+ let form = {};
28
+ try {
29
+ if (json) {
30
+
31
+ let flowKeys = _.keys(json);
32
+ for (let m in flowKeys) {
33
+ let key = flowKeys[m];
34
+ if (typeof key === 'function') {
35
+ continue;
36
+ }
37
+ let val = json[key];
38
+ if (typeof val === 'function') {
39
+ json[key] = val.toString();
40
+ }
41
+ form[key] = json[key];
42
+ }
43
+ flows[formName] = json;
44
+ }
45
+ } catch (error) {
46
+ console.error('loadFlows error', matchedPath, error);
47
+ throw error
48
+ }
49
+ }
50
+ return flows;
51
+ }
52
+
53
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:31:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:48:39
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/imports/ImportJson.ts
7
+ * @Description:
8
+ */
9
+ import { each } from 'lodash';
10
+ import { Base } from './Base';
11
+ import { syncMatchFiles } from '@steedos/metadata-core';
12
+ import path = require('path');
13
+ import fs = require("fs");
14
+
15
+ export default class ImportJson implements Base {
16
+
17
+ async readFile(filePath: string): Promise<Array<{ objectName: string, records: Array<any> }>> {
18
+ let results: any = []
19
+ const filePatten = [
20
+ path.join(filePath, "**", "*.data.json"),
21
+ "!" + path.join(filePath, "**", "*.flow.data.json"),
22
+ "!" + path.join(filePath, "node_modules")];
23
+
24
+ const matchedPaths: [string] = syncMatchFiles(filePatten);
25
+ each(matchedPaths, (matchedPath: string) => {
26
+ let records = JSON.parse(fs.readFileSync(matchedPath, 'utf8').normalize('NFC'));
27
+ let objectName = path.basename(matchedPath).split('.')[0];
28
+ results.push({ objectName: objectName, records: records });
29
+ })
30
+ return results
31
+ }
32
+
33
+ }
@@ -0,0 +1,45 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:22:14
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-27 12:12:57
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/methods/importData.ts
7
+ * @Description:
8
+ */
9
+ import ImportJson from '../imports/ImportJson';
10
+ import ImportCsv from '../imports/ImportCsv'
11
+ import ImportFlow from '../imports/ImportFlow'
12
+
13
+ import fs = require('fs');
14
+
15
+ export async function handler(filePath: string, onlyInsert: boolean, spaceId: string) {
16
+ if (!filePath) {
17
+ return
18
+ }
19
+ if (!fs.existsSync(filePath)) {
20
+ return
21
+ }
22
+
23
+ const importer = {
24
+ csv: new ImportCsv(),
25
+ json: new ImportJson(),
26
+ flow: new ImportFlow()
27
+ }
28
+
29
+ const csvData = await importer.csv.readFile(filePath);
30
+
31
+ const jsonData = await importer.json.readFile(filePath);
32
+
33
+ const flowData = await importer.flow.readFile(filePath);
34
+
35
+ await this.broker.call("~packages-@steedos/data-import.importData", {
36
+ data: {
37
+ "csv": csvData,
38
+ "json": jsonData,
39
+ "flow": flowData,
40
+ },
41
+ spaceId,
42
+ onlyInsert: true,
43
+ })
44
+
45
+ }
@@ -0,0 +1,9 @@
1
+ /*
2
+ * @Author: 孙浩林 sunhaolin@steedos.com
3
+ * @Date: 2023-10-26 10:21:50
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2023-10-26 10:40:43
6
+ * @FilePath: /steedos-platform-2.3/services/service-package-loader/src/methods/index.ts
7
+ * @Description:
8
+ */
9
+ export * as importData from './importData';