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