mushroom-cli 1.1.2 → 1.2.0-alpha.10

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,119 +1,101 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const targz = require("targz");
4
- const javascriptDriverGenerator = require("./gen-js-driver");
5
- const typescriptDriverGenerator = require("./gen-ts-driver");
6
- const package_json = require("./../package.json");
7
-
8
- function compressAsync(src, dest, folderName) {
9
- return new Promise((resolve, reject) => {
10
- const options = {
11
- src: src,
12
- dest: dest,
13
- };
14
- if (folderName) {
15
- options.tar = {
16
- map: (header) => {
17
- if (header.name === ".") return header;
18
- header.name = folderName + "/" + header.name;
19
- return header;
20
- },
21
- };
22
- }
23
-
24
- targz.compress(options, function (err) {
25
- if (err) {
26
- reject(err);
27
- return;
28
- }
29
- resolve();
30
- });
31
- });
32
- }
33
-
34
- async function genJavascriptDriverTarball(
35
- args,
36
- projectName,
37
- project,
38
- alias,
39
- source,
40
- ) {
41
- const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
42
-
43
- let tarballName = aliasProject.tarballName
44
- ? aliasProject.tarballName
45
- : projectName;
46
- tarballName = tarballName.toLowerCase().replace(/[^a-z-]/g, "-");
47
-
48
- const dirPath = path.join(path.dirname(aliasProject.output), tarballName);
49
-
50
- const versionGroup = package_json.version.match(/([^-]+)(-(\w+).*)?/);
51
- const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
52
-
53
- const dependencies = [`"mushroomjs": "${currentDistTag}"`];
54
- if (
55
- Array.isArray(aliasProject.features) &&
56
- aliasProject.features.includes("auth")
57
- ) {
58
- dependencies.push(`"mushroomjs-auth": "${currentDistTag}"`);
59
- }
60
- if (
61
- Array.isArray(aliasProject.features) &&
62
- aliasProject.features.includes("file")
63
- ) {
64
- dependencies.push(`"mushroomjs-file": "${currentDistTag}"`);
65
- }
66
-
67
- const packageContent = `{
68
- "version": "1.0.${new Date().getTime()}",
69
- "name": "${tarballName}",
70
- "private": true,
71
- "main": "${tarballName}.js",
72
- "types": "${tarballName}.d.ts",
73
- "dependencies": {
74
- ${dependencies.join(",\n ")}
75
- }
76
- }`;
77
-
78
- if (fs.existsSync(dirPath)) {
79
- console.log("Clean up tarball folder");
80
- fs.rmSync(dirPath, { recursive: true });
81
- }
82
-
83
- const tarballFileName = path.join(
84
- path.dirname(aliasProject.output),
85
- tarballName + ".tar.gz",
86
- );
87
-
88
- if (fs.existsSync(tarballFileName)) {
89
- console.log("Clean up old tarball file");
90
- fs.unlinkSync(tarballFileName);
91
- }
92
-
93
- console.log("Creating tarball folder");
94
-
95
- fs.mkdirSync(dirPath, { recursive: true, mode: 493 }); // 493 = 755 in octa
96
-
97
- console.log("Generating main file: " + tarballName + ".js");
98
- let code = javascriptDriverGenerator.genJavascriptDriverString(
99
- aliasProject,
100
- source,
101
- );
102
- fs.writeFileSync(path.join(dirPath, tarballName + ".js"), code);
103
-
104
- console.log("Generating types file: " + tarballName + ".d.ts");
105
- code = typescriptDriverGenerator.genDtsString(aliasProject, source);
106
- fs.writeFileSync(path.join(dirPath, tarballName + ".d.ts"), code);
107
-
108
- console.log("Generating package file: package.json");
109
- fs.writeFileSync(path.join(dirPath, "package.json"), packageContent);
110
-
111
- console.log("Generating tarball file: " + tarballFileName);
112
- await compressAsync(dirPath, tarballFileName, tarballName);
113
-
114
- console.log("Generated tarball file");
115
- fs.rmSync(dirPath, { recursive: true });
116
- console.log("Cleaned up tarball folder");
117
- }
118
-
119
- module.exports = genJavascriptDriverTarball;
1
+ const fs = require("fs");
2
+ const path = require('path');
3
+ var targz = require('targz');
4
+ const javascriptDriverGenerator = require("./gen-js-driver");
5
+ const typescriptDriverGenerator = require("./gen-ts-driver");
6
+ const package = require("./../package.json");
7
+
8
+ function compressAsync(src, dest, folderName) {
9
+ return new Promise((resolve, reject) => {
10
+ const options = {
11
+ src: src,
12
+ dest: dest
13
+ };
14
+ if (folderName) {
15
+ options.tar = {
16
+ map: (header) => {
17
+ if (header.name === ".") return header;
18
+ header.name = folderName + '/' + header.name;
19
+ return header;
20
+ }
21
+ };
22
+ }
23
+
24
+ targz.compress(options, function(err){
25
+ if(err) {
26
+ reject(err);
27
+ return;
28
+ }
29
+ resolve();
30
+ });
31
+ });
32
+ }
33
+
34
+ async function genJavascriptDriverTarball(args, projectName, project, alias, source) {
35
+ const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
36
+
37
+ let tarballName = aliasProject.tarballName ? aliasProject.tarballName : projectName;
38
+ tarballName = tarballName.toLowerCase().replace(/[^a-z-]/g, "-");
39
+
40
+ const dirPath = path.join(path.dirname(aliasProject.output), tarballName);
41
+
42
+ const versionGroup = package.version.match(/([^-]+)(-(\w+).*)?/);
43
+ const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
44
+
45
+ const dependencies = [`"mushroomjs": "${currentDistTag}"`];
46
+ if (Array.isArray(aliasProject.features) && aliasProject.features.includes("auth")) {
47
+ dependencies.push(`"mushroomjs-auth": "${currentDistTag}"`);
48
+ }
49
+ if (Array.isArray(aliasProject.features) && aliasProject.features.includes("file")) {
50
+ dependencies.push(`"mushroomjs-file": "${currentDistTag}"`);
51
+ }
52
+
53
+ const packageContent = `{
54
+ "version": "1.0.${new Date().getTime()}",
55
+ "name": "${tarballName}",
56
+ "private": true,
57
+ "main": "${tarballName}.js",
58
+ "types": "${tarballName}.d.ts",
59
+ "dependencies": {
60
+ ${dependencies.join(',\n ')}
61
+ }
62
+ }`;
63
+
64
+ if (fs.existsSync(dirPath)) {
65
+ console.log("Clean up tarball folder");
66
+ // fs.rmdirSync(dirPath, { recursive: true });
67
+ fs.rmSync(dirPath, { recursive: true });
68
+ }
69
+
70
+ const tarballFileName = path.join(path.dirname(aliasProject.output), tarballName + ".tar.gz");
71
+
72
+ if (fs.existsSync(tarballFileName)) {
73
+ console.log("Clean up old tarball file");
74
+ fs.unlinkSync(tarballFileName);
75
+ }
76
+
77
+ console.log("Creating tarball folder");
78
+
79
+ fs.mkdirSync(dirPath, { recursive: true, mode: 0755 });
80
+
81
+ console.log("Generating main file: " + tarballName + ".js");
82
+ let code = javascriptDriverGenerator.genJavascriptDriverString(aliasProject, source);
83
+ fs.writeFileSync(path.join(dirPath, tarballName + ".js"), code);
84
+
85
+ console.log("Generating types file: " + tarballName + ".d.ts");
86
+ code = typescriptDriverGenerator.genDtsString(aliasProject, source);
87
+ fs.writeFileSync(path.join(dirPath, tarballName + ".d.ts"), code);
88
+
89
+ console.log("Generating package file: package.json");
90
+ fs.writeFileSync(path.join(dirPath, "package.json"), packageContent);
91
+
92
+ console.log("Generating tarball file: " + tarballFileName);
93
+ await compressAsync(dirPath, tarballFileName, tarballName);
94
+
95
+ console.log("Generated tarball file");
96
+ // fs.rmdirSync(dirPath, { recursive: true });
97
+ fs.rmSync(dirPath, { recursive: true });
98
+ console.log("Cleaned up tarball folder");
99
+ }
100
+
101
+ module.exports = genJavascriptDriverTarball;
@@ -1,154 +1,133 @@
1
- const fs = require("fs");
2
- const common = require("../common.js");
3
- const typescriptDriverGenerator = require("./gen-ts-driver");
4
-
5
- function isAllowCache(actions, defaultValue) {
6
- let result = defaultValue;
7
- for (let i = 0; i < actions.length; i++) {
8
- const action = actions[i];
9
-
10
- if (
11
- !action ||
12
- !action.cache ||
13
- !action.cache.client ||
14
- !action.cache.client.policy ||
15
- action.cache.client.policy === "unset"
16
- )
17
- continue;
18
-
19
- if (action.cache.client.policy === "disabled") return false;
20
-
21
- if (
22
- action.cache.client.policy === "allow" ||
23
- action.cache.client.policy === "required"
24
- )
25
- result = true;
26
- }
27
- return result;
28
- }
29
-
30
- function genJavascriptDriverString(aliasProject, source) {
31
- if (!aliasProject) {
32
- return "";
33
- }
34
-
35
- const entities = common.getEntities(source, aliasProject.allowRoles);
36
-
37
- let extensionsImportLines = [];
38
- let extensionsExportLines = [];
39
- let extensionsCodeLines = [];
40
-
41
- if (Array.isArray(aliasProject.features)) {
42
- if (aliasProject.features.includes("auth")) {
43
- extensionsImportLines.push(
44
- '//import * as AuthExtension from "mushroomjs-auth";',
45
- );
46
- extensionsImportLines.push(
47
- 'var AuthExtension = require("mushroomjs-auth");',
48
- );
49
- extensionsExportLines.push("Auth: AuthExtension");
50
- extensionsCodeLines.push(
51
- "AuthExtension.useAuth(AuthExtension.AuthLocalStorage, mushroom_1.default);",
52
- );
53
- }
54
- if (aliasProject.features.includes("file")) {
55
- extensionsImportLines.push(
56
- '//import * as FileExtension from "mushroomjs-file";',
57
- );
58
- extensionsImportLines.push(
59
- 'var FileExtension = require("mushroomjs-file");',
60
- );
61
- extensionsExportLines.push("File: FileExtension");
62
- extensionsCodeLines.push("FileExtension.useFile(mushroom_1.default);");
63
- }
64
- }
65
-
66
- let code = `//import mushroom, {defineAsyncResource} from "mushroomjs";
67
- Object.defineProperty(exports, "__esModule", { value: true });
68
- var mushroom_1 = require("mushroomjs");
69
- var mushroom = mushroom_1.default;
70
- var defineAsyncResource = mushroom_1.defineAsyncResource;
71
-
72
- //export * from "mushroomjs";
73
- for (var m in mushroom_1) exports[m] = mushroom_1[m];
74
-
75
- ${extensionsImportLines.join("\n")}
76
- //export const extensions = {
77
- var extensions = {
78
- ${extensionsExportLines.join(",\n ")}
79
- };
80
- exports.extensions = extensions;
81
-
82
- "use strict";
83
-
84
- `;
85
- for (let entityName in entities) {
86
- if (!entities.hasOwnProperty(entityName)) continue;
87
-
88
- const entity = entities[entityName];
89
-
90
- const actions = {};
91
- for (const actionName in entity.actions) {
92
- if (!entity.actions.hasOwnProperty(actionName)) continue;
93
-
94
- actions[actionName] = {
95
- clientCache:
96
- (actionName === "findById" || actionName === "findMany") &&
97
- isAllowCache(entity.actions[actionName], true)
98
- ? true
99
- : undefined,
100
- includeTotal:
101
- actionName === "findMany" &&
102
- entity.actions[actionName].some((a) => a.includeTotal)
103
- ? true
104
- : undefined,
105
- };
106
- }
107
-
108
- const views = {};
109
- for (const viewName in entity.views) {
110
- if (!entity.views.hasOwnProperty(viewName)) continue;
111
-
112
- views[viewName] = {};
113
- }
114
- code += `defineAsyncResource({name:${JSON.stringify(entityName)},actions:${JSON.stringify(actions)},views:${JSON.stringify(views)}});
115
- `;
116
- }
117
- code += `
118
- mushroom.$using(${JSON.stringify(common.getRootUrl(source))});
119
-
120
- ${extensionsCodeLines.join("\n")}
121
-
122
- //export default mushroom;
123
- exports.default = mushroom;`;
124
-
125
- return code;
126
- }
127
-
128
- function genJavascriptDriver(args, projectName, project, alias, source) {
129
- const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
130
-
131
- if (!aliasProject) {
132
- throw new Error(
133
- `Not found gen js-driver configuration. Please run 'npx mushroom-cli config gen ${projectName} js-driver OUTPUT_FILE.js${alias === "default" ? "" : " alias " + alias}' first`,
134
- );
135
- }
136
-
137
- let code = genJavascriptDriverString(aliasProject, source);
138
-
139
- common.ensureFolder(aliasProject.output);
140
-
141
- fs.writeFileSync(aliasProject.output, code);
142
- console.log(`Generated ${aliasProject.output}`);
143
-
144
- code = typescriptDriverGenerator.genDtsString(aliasProject, source);
145
- const dtsFilePath = aliasProject.output.replace(/\.js$/i, ".d.ts");
146
- fs.writeFileSync(dtsFilePath, code);
147
- console.log(`Generated ${dtsFilePath}`);
148
- console.log(`Generated Javascript driver for ${projectName}@${alias}`);
149
- }
150
-
151
- module.exports = {
152
- genJavascriptDriver,
153
- genJavascriptDriverString,
154
- };
1
+ const fs = require("fs");
2
+ const common = require("../common.js");
3
+ const typescriptDriverGenerator = require("./gen-ts-driver");
4
+
5
+ function isAllowCache(actions, defaultValue) {
6
+ let result = defaultValue;
7
+ for (let i = 0; i < actions.length; i++) {
8
+ const action = actions[i];
9
+
10
+ if (!action ||
11
+ !action.cache ||
12
+ !action.cache.client ||
13
+ !action.cache.client.policy ||
14
+ action.cache.client.policy === "unset")
15
+ continue;
16
+
17
+ if (action.cache.client.policy === "disabled")
18
+ return false;
19
+
20
+ if (action.cache.client.policy === "allow" || action.cache.client.policy === "required")
21
+ result = true;
22
+ }
23
+ return result;
24
+ }
25
+
26
+ function genJavascriptDriverString(aliasProject, source) {
27
+ if (!aliasProject) {
28
+ return "";
29
+ }
30
+
31
+ const entities = common.getEntities(source, aliasProject.allowRoles);
32
+
33
+ let extensionsImportLines = [];
34
+ let extensionsExportLines = [];
35
+ let extensionsCodeLines = [];
36
+
37
+ if (Array.isArray(aliasProject.features)) {
38
+ if (aliasProject.features.includes("auth")) {
39
+ extensionsImportLines.push('//import * as AuthExtension from "mushroomjs-auth";');
40
+ extensionsImportLines.push('var AuthExtension = require("mushroomjs-auth");');
41
+ extensionsExportLines.push('Auth: AuthExtension');
42
+ extensionsCodeLines.push("AuthExtension.useAuth(AuthExtension.AuthLocalStorage, mushroom_1.default);");
43
+ }
44
+ if (aliasProject.features.includes("file")) {
45
+ extensionsImportLines.push('//import * as FileExtension from "mushroomjs-file";');
46
+ extensionsImportLines.push('var FileExtension = require("mushroomjs-file");');
47
+ extensionsExportLines.push('File: FileExtension');
48
+ extensionsCodeLines.push("FileExtension.useFile(mushroom_1.default);");
49
+ }
50
+ }
51
+
52
+ let code = `//import mushroom, {defineAsyncResource} from "mushroomjs";
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ var mushroom_1 = require("mushroomjs");
55
+ var mushroom = mushroom_1.default;
56
+ var defineAsyncResource = mushroom_1.defineAsyncResource;
57
+
58
+ //export * from "mushroomjs";
59
+ for (var m in mushroom_1) exports[m] = mushroom_1[m];
60
+
61
+ ${extensionsImportLines.join("\n")}
62
+ //export const extensions = {
63
+ var extensions = {
64
+ ${extensionsExportLines.join(",\n ")}
65
+ };
66
+ exports.extensions = extensions;
67
+
68
+ "use strict";
69
+
70
+ `;
71
+ for (let entityName in entities) {
72
+ if (!entities.hasOwnProperty(entityName))
73
+ continue;
74
+
75
+ const entity = entities[entityName];
76
+
77
+ const actions = {};
78
+ for (const actionName in entity.actions) {
79
+ if (!entity.actions.hasOwnProperty(actionName))
80
+ continue;
81
+
82
+ actions[actionName] = {
83
+ clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined,
84
+ includeTotal: actionName === "findMany" && entity.actions[actionName].some(a => a.includeTotal) ? true: undefined
85
+ };
86
+ }
87
+
88
+ const views = {};
89
+ for (const viewName in entity.views) {
90
+ if (!entity.views.hasOwnProperty(viewName))
91
+ continue;
92
+
93
+ views[viewName] = {};
94
+ }
95
+ code += `defineAsyncResource({name:${JSON.stringify(entityName)},actions:${JSON.stringify(actions)},views:${JSON.stringify(views)}});
96
+ `;
97
+ }
98
+ code += `
99
+ mushroom.$using(${JSON.stringify(common.getRootUrl(source))});
100
+
101
+ ${extensionsCodeLines.join('\n')}
102
+
103
+ //export default mushroom;
104
+ exports.default = mushroom;`;
105
+
106
+ return code;
107
+ }
108
+
109
+ function genJavascriptDriver(args, projectName, project, alias, source) {
110
+ const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
111
+
112
+ if (!aliasProject) {
113
+ throw new Error(`Not found gen js-driver configuration. Please run 'npx mushroom-cli config gen ${projectName} js-driver OUTPUT_FILE.js${alias === "default" ? "" : " alias " + alias}' first`);
114
+ }
115
+
116
+ let code = genJavascriptDriverString(aliasProject, source);
117
+
118
+ common.ensureFolder(aliasProject.output);
119
+
120
+ fs.writeFileSync(aliasProject.output, code);
121
+ console.log(`Generated ${aliasProject.output}`);
122
+
123
+ code = typescriptDriverGenerator.genDtsString(aliasProject, source);
124
+ const dtsFilePath = aliasProject.output.replace(/\.js$/i, ".d.ts");
125
+ fs.writeFileSync(dtsFilePath, code);
126
+ console.log(`Generated ${dtsFilePath}`);
127
+ console.log(`Generated Javascript driver for ${projectName}@${alias}`);
128
+ }
129
+
130
+ module.exports = {
131
+ genJavascriptDriver,
132
+ genJavascriptDriverString
133
+ };