@trayio/generator 5.34.0 → 5.35.0-unstable.fd7158a
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/dist/generator/Generator.d.ts +2 -2
- package/dist/generator/Generator.d.ts.map +1 -1
- package/dist/generator/NodeFsGenerator.d.ts +2 -2
- package/dist/generator/NodeFsGenerator.d.ts.map +1 -1
- package/dist/generator/NodeFsGenerator.js +40 -32
- package/dist/generator/NodeFsGenerator.unit.test.js +31 -20
- package/dist/generator/project.test.zip +0 -0
- package/package.json +4 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
2
|
export interface Generator {
|
|
3
|
-
generate(templatePath: string, targetPath: string, values: Record<string, string>):
|
|
3
|
+
generate(templatePath: string, targetPath: string, values: Record<string, string>): Effect.Effect<undefined, Error>;
|
|
4
4
|
}
|
|
5
5
|
//# sourceMappingURL=Generator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Generator.d.ts","sourceRoot":"","sources":["../../src/generator/Generator.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Generator.d.ts","sourceRoot":"","sources":["../../src/generator/Generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,WAAW,SAAS;IACzB,QAAQ,CACP,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;CACnC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
2
|
import { Generator } from './Generator';
|
|
3
3
|
export declare class NodeFsGenerator implements Generator {
|
|
4
|
-
generate(templatePath: string, targetPath: string, values: Record<string, string>):
|
|
4
|
+
generate(templatePath: string, targetPath: string, values: Record<string, string>): Effect.Effect<undefined, Error>;
|
|
5
5
|
private replacePlaceholdersInDir;
|
|
6
6
|
private renameParentDir;
|
|
7
7
|
private replacePlaceholdersInFile;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeFsGenerator.d.ts","sourceRoot":"","sources":["../../src/generator/NodeFsGenerator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"NodeFsGenerator.d.ts","sourceRoot":"","sources":["../../src/generator/NodeFsGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAOtC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,qBAAa,eAAgB,YAAW,SAAS;IAChD,QAAQ,CACP,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC;IAyBlC,OAAO,CAAC,wBAAwB,CA0B9B;IAEF,OAAO,CAAC,eAAe,CAsCrB;IAEF,OAAO,CAAC,yBAAyB,CAe/B;IAEF,OAAO,CAAC,4BAA4B,CAUlC;IAEF,OAAO,CAAC,SAAS,CACiD;IAElE,OAAO,CAAC,OAAO,CAGZ;IAEH,OAAO,CAAC,QAAQ,CAGb;IAEH,OAAO,CAAC,SAAS,CASd;IAEH,OAAO,CAAC,MAAM,CASX;IAEH,OAAO,CAAC,IAAI,CAST;IAEH,OAAO,CAAC,KAAK,CAYV;CACH"}
|
|
@@ -15,26 +15,34 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.NodeFsGenerator = void 0;
|
|
37
|
+
const effect_1 = require("effect");
|
|
27
38
|
const fs = __importStar(require("fs/promises"));
|
|
28
39
|
const path_1 = require("path");
|
|
29
|
-
const
|
|
30
|
-
const Task_1 = require("@trayio/commons/task/Task");
|
|
31
|
-
const function_1 = require("fp-ts/function");
|
|
40
|
+
const EffectExtensions_1 = require("@trayio/commons/effect/EffectExtensions");
|
|
32
41
|
const fse = __importStar(require("fs-extra"));
|
|
33
|
-
const Array_1 = require("fp-ts/Array");
|
|
34
42
|
const extract = require("extract-zip");
|
|
35
43
|
class NodeFsGenerator {
|
|
36
44
|
generate(templatePath, targetPath, values) {
|
|
37
|
-
return (0,
|
|
45
|
+
return (0, effect_1.pipe)(this.readStats(templatePath), effect_1.Effect.flatMap((stats) => {
|
|
38
46
|
if (stats.isDirectory()) {
|
|
39
47
|
return this.copy(templatePath, targetPath);
|
|
40
48
|
}
|
|
@@ -42,12 +50,12 @@ class NodeFsGenerator {
|
|
|
42
50
|
const unzip = this.unzip(templatePath, targetPath);
|
|
43
51
|
return unzip;
|
|
44
52
|
}
|
|
45
|
-
return
|
|
46
|
-
}),
|
|
53
|
+
return effect_1.Effect.fail(new Error('Unknown file type. Template path needs to be either a directory or a zip file'));
|
|
54
|
+
}), effect_1.Effect.flatMap(() => this.renameParentDir(targetPath, values)), effect_1.Effect.flatMap((parentDirName) => this.replacePlaceholdersInDir(parentDirName, values)), effect_1.Effect.map(() => undefined));
|
|
47
55
|
}
|
|
48
56
|
replacePlaceholdersInDir = (dirPath, values) => {
|
|
49
57
|
const targetPath = this.replacePlaceholdersInContent(dirPath, values);
|
|
50
|
-
return (0,
|
|
58
|
+
return (0, effect_1.pipe)(this.rename(dirPath, targetPath), effect_1.Effect.flatMap(() => this.readDir(targetPath)), effect_1.Effect.flatMap((direntList) => effect_1.Effect.forEach((dirent) => {
|
|
51
59
|
const fileOrDirPath = (0, path_1.join)(targetPath, dirent.name);
|
|
52
60
|
if (dirent.isDirectory()) {
|
|
53
61
|
return this.replacePlaceholdersInDir(fileOrDirPath, values);
|
|
@@ -55,27 +63,27 @@ class NodeFsGenerator {
|
|
|
55
63
|
if (dirent.isFile()) {
|
|
56
64
|
return this.replacePlaceholdersInFile(fileOrDirPath, values);
|
|
57
65
|
}
|
|
58
|
-
return
|
|
59
|
-
})(direntList)),
|
|
66
|
+
return effect_1.Effect.fail(new Error('Unknown file type. Template directory should contain either folders or files'));
|
|
67
|
+
})(direntList)), effect_1.Effect.map(() => undefined));
|
|
60
68
|
};
|
|
61
69
|
renameParentDir = (targetPath, values) => {
|
|
62
70
|
const regex = /\$(.*?)\$/g;
|
|
63
|
-
return (0,
|
|
71
|
+
return (0, effect_1.pipe)(this.readDir(targetPath), effect_1.Effect.flatMap((direntList) => effect_1.Effect.succeed(direntList.filter((dirent) => regex.test(dirent.name)))), effect_1.Effect.flatMap((direntList) => {
|
|
64
72
|
if (direntList.length === 0) {
|
|
65
|
-
return
|
|
73
|
+
return effect_1.Effect.succeed(targetPath);
|
|
66
74
|
}
|
|
67
75
|
if (direntList.length > 1) {
|
|
68
|
-
return
|
|
76
|
+
return effect_1.Effect.fail(new Error('More than one parent directory found. This is not allowed'));
|
|
69
77
|
}
|
|
70
|
-
return
|
|
71
|
-
}),
|
|
78
|
+
return effect_1.Effect.succeed(`${targetPath}/${direntList[0].name}`);
|
|
79
|
+
}), effect_1.Effect.flatMap((parentDirName) => {
|
|
72
80
|
const replacedParentDirName = this.replacePlaceholdersInContent(parentDirName, values);
|
|
73
|
-
return (0,
|
|
81
|
+
return (0, effect_1.pipe)(this.rename(parentDirName, replacedParentDirName), effect_1.Effect.map(() => replacedParentDirName));
|
|
74
82
|
}));
|
|
75
83
|
};
|
|
76
84
|
replacePlaceholdersInFile = (filePath, values) => {
|
|
77
85
|
const targetPath = this.replacePlaceholdersInContent(filePath, values);
|
|
78
|
-
return (0,
|
|
86
|
+
return (0, effect_1.pipe)(this.rename(filePath, targetPath), effect_1.Effect.flatMap(() => this.readFile(targetPath)), effect_1.Effect.map((fileContent) => this.replacePlaceholdersInContent(fileContent, values)), effect_1.Effect.flatMap((replacedContent) => this.writeFile(targetPath, replacedContent)));
|
|
79
87
|
};
|
|
80
88
|
replacePlaceholdersInContent = (templateContent, replacements) => {
|
|
81
89
|
let content = templateContent;
|
|
@@ -85,17 +93,17 @@ class NodeFsGenerator {
|
|
|
85
93
|
});
|
|
86
94
|
return content;
|
|
87
95
|
};
|
|
88
|
-
readStats = (path) =>
|
|
89
|
-
readDir = (path) =>
|
|
90
|
-
readFile = (path) =>
|
|
91
|
-
writeFile = (targetPath, generatedContent) => (0,
|
|
92
|
-
rename = (oldPath, newPath) => (0,
|
|
93
|
-
copy = (source, targetPath) => (0,
|
|
94
|
-
unzip = (source, targetPath) => (0,
|
|
96
|
+
readStats = (path) => EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fs.lstat(path));
|
|
97
|
+
readDir = (path) => EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fs.readdir(path, { withFileTypes: true }));
|
|
98
|
+
readFile = (path) => EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fs.readFile(path, 'utf-8'));
|
|
99
|
+
writeFile = (targetPath, generatedContent) => (0, effect_1.pipe)(EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fs.writeFile(targetPath, generatedContent)), effect_1.Effect.map(() => undefined));
|
|
100
|
+
rename = (oldPath, newPath) => (0, effect_1.pipe)(EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fse.rename(oldPath, newPath)), effect_1.Effect.map(() => undefined));
|
|
101
|
+
copy = (source, targetPath) => (0, effect_1.pipe)(EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => fse.copy(source, targetPath)), effect_1.Effect.map(() => undefined));
|
|
102
|
+
unzip = (source, targetPath) => (0, effect_1.pipe)(EffectExtensions_1.EffectExtensions.tryPromiseWithSimpleError(() => {
|
|
95
103
|
const extracted = extract(source, {
|
|
96
104
|
dir: targetPath,
|
|
97
105
|
});
|
|
98
106
|
return extracted;
|
|
99
|
-
}),
|
|
107
|
+
}), effect_1.Effect.map(() => undefined));
|
|
100
108
|
}
|
|
101
109
|
exports.NodeFsGenerator = NodeFsGenerator;
|
|
@@ -15,14 +15,25 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const effect_1 = require("effect");
|
|
26
37
|
const fs = __importStar(require("fs"));
|
|
27
38
|
const path_1 = require("path");
|
|
28
39
|
const extract = require("extract-zip");
|
|
@@ -52,7 +63,7 @@ const contentTest = (outputPath, values) => {
|
|
|
52
63
|
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
53
64
|
const expectedContent = `import { RequestHandler } from '@trayio/base-contract/base/Facade';
|
|
54
65
|
import { TypeCodec } from '@trayio/commons/codec/TypeCodec';
|
|
55
|
-
import
|
|
66
|
+
import { Schema } from 'effect';
|
|
56
67
|
|
|
57
68
|
export interface ${values.entity}Facade {
|
|
58
69
|
get${values.entity}: RequestHandler<Get${values.entity}, ${values.entity}>;
|
|
@@ -72,22 +83,22 @@ export type ${values.entity} = {
|
|
|
72
83
|
description: string;
|
|
73
84
|
};
|
|
74
85
|
|
|
75
|
-
export const get${values.entity}Codec =
|
|
76
|
-
|
|
77
|
-
id:
|
|
86
|
+
export const get${values.entity}Codec = TypeCodec.fromSchema(
|
|
87
|
+
Schema.Struct({
|
|
88
|
+
id: Schema.String,
|
|
78
89
|
})
|
|
79
90
|
);
|
|
80
91
|
|
|
81
|
-
export const create${values.entity}Codec =
|
|
82
|
-
|
|
83
|
-
description:
|
|
92
|
+
export const create${values.entity}Codec = TypeCodec.fromSchema(
|
|
93
|
+
Schema.Struct({
|
|
94
|
+
description: Schema.String,
|
|
84
95
|
})
|
|
85
96
|
);
|
|
86
97
|
|
|
87
|
-
export const ${values.entityVar}Codec =
|
|
88
|
-
|
|
89
|
-
id:
|
|
90
|
-
description:
|
|
98
|
+
export const ${values.entityVar}Codec = TypeCodec.fromSchema(
|
|
99
|
+
Schema.Struct({
|
|
100
|
+
id: Schema.String,
|
|
101
|
+
description: Schema.String,
|
|
91
102
|
})
|
|
92
103
|
);
|
|
93
104
|
`;
|
|
@@ -126,7 +137,7 @@ describe('NodeFsGenerator', () => {
|
|
|
126
137
|
});
|
|
127
138
|
it('Should extract all content of the template', async () => {
|
|
128
139
|
const generator = new NodeFsGenerator_1.NodeFsGenerator();
|
|
129
|
-
await generator.generate(zipTemplatePath, zipOutputPath, values)
|
|
140
|
+
await effect_1.Effect.runPromise(effect_1.Effect.either(generator.generate(zipTemplatePath, zipOutputPath, values)));
|
|
130
141
|
});
|
|
131
142
|
it('should output template and rename files/folders according to user input', () => {
|
|
132
143
|
outputTest(zipOutputPath, values);
|
|
@@ -147,7 +158,7 @@ describe('NodeFsGenerator', () => {
|
|
|
147
158
|
unzip(zipTemplatePath, folderTemplatePath);
|
|
148
159
|
it('Should copy all content of the template', async () => {
|
|
149
160
|
const generator = new NodeFsGenerator_1.NodeFsGenerator();
|
|
150
|
-
const result = await generator.generate(folderTemplatePath, folderOutputPath, values)
|
|
161
|
+
const result = await effect_1.Effect.runPromise(effect_1.Effect.either(generator.generate(folderTemplatePath, folderOutputPath, values)));
|
|
151
162
|
});
|
|
152
163
|
it('should output template and rename files/folders according to user input', () => {
|
|
153
164
|
outputTest(folderOutputPath, values);
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trayio/generator",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.35.0-unstable.fd7158a",
|
|
4
4
|
"description": "Template based generator",
|
|
5
5
|
"author": "Tray.io",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@trayio/commons": "5.
|
|
14
|
+
"@trayio/commons": "5.35.0-unstable.fd7158a",
|
|
15
|
+
"effect": "3.22.2",
|
|
15
16
|
"extract-zip": "2.0.1",
|
|
16
|
-
"fp-ts": "2.12.3",
|
|
17
17
|
"fs-extra": "11.3.4"
|
|
18
18
|
},
|
|
19
19
|
"exports": {
|
|
@@ -30,9 +30,7 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"/dist"
|
|
32
32
|
],
|
|
33
|
-
"devDependencies": {
|
|
34
|
-
"io-ts": "2.2.20"
|
|
35
|
-
},
|
|
33
|
+
"devDependencies": {},
|
|
36
34
|
"scripts": {
|
|
37
35
|
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
38
36
|
"compile": "tsc --build && tscp",
|