@trayio/generator 3.8.0-beta → 3.9.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.
|
@@ -33,60 +33,6 @@ const fse = __importStar(require("fs-extra"));
|
|
|
33
33
|
const Array_1 = require("fp-ts/Array");
|
|
34
34
|
const extract = require("extract-zip");
|
|
35
35
|
class NodeFsGenerator {
|
|
36
|
-
constructor() {
|
|
37
|
-
this.replacePlaceholdersInDir = (dirPath, values) => {
|
|
38
|
-
const targetPath = this.replacePlaceholdersInContent(dirPath, values);
|
|
39
|
-
return (0, function_1.pipe)(this.rename(dirPath, targetPath), TE.chain(() => this.readDir(targetPath)), TE.chain((direntList) => (0, Array_1.traverse)(TE.ApplicativeSeq)((dirent) => {
|
|
40
|
-
const fileOrDirPath = (0, path_1.join)(targetPath, dirent.name);
|
|
41
|
-
if (dirent.isDirectory()) {
|
|
42
|
-
return this.replacePlaceholdersInDir(fileOrDirPath, values);
|
|
43
|
-
}
|
|
44
|
-
if (dirent.isFile()) {
|
|
45
|
-
return this.replacePlaceholdersInFile(fileOrDirPath, values);
|
|
46
|
-
}
|
|
47
|
-
return TE.left(new Error('Unknown file type. Template directory should contain either folders or files'));
|
|
48
|
-
})(direntList)), TE.map(() => undefined));
|
|
49
|
-
};
|
|
50
|
-
this.renameParentDir = (targetPath, values) => {
|
|
51
|
-
const regex = /\$(.*?)\$/g;
|
|
52
|
-
return (0, function_1.pipe)(this.readDir(targetPath), TE.chain((direntList) => TE.right(direntList.filter((dirent) => regex.test(dirent.name)))), TE.chain((direntList) => {
|
|
53
|
-
if (direntList.length === 0) {
|
|
54
|
-
return TE.right(targetPath);
|
|
55
|
-
}
|
|
56
|
-
if (direntList.length > 1) {
|
|
57
|
-
return TE.left(new Error('More than one parent directory found. This is not allowed'));
|
|
58
|
-
}
|
|
59
|
-
return TE.right(`${targetPath}/${direntList[0].name}`);
|
|
60
|
-
}), TE.chain((parentDirName) => {
|
|
61
|
-
const replacedParentDirName = this.replacePlaceholdersInContent(parentDirName, values);
|
|
62
|
-
return (0, function_1.pipe)(this.rename(parentDirName, replacedParentDirName), TE.map(() => replacedParentDirName));
|
|
63
|
-
}));
|
|
64
|
-
};
|
|
65
|
-
this.replacePlaceholdersInFile = (filePath, values) => {
|
|
66
|
-
const targetPath = this.replacePlaceholdersInContent(filePath, values);
|
|
67
|
-
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)));
|
|
68
|
-
};
|
|
69
|
-
this.replacePlaceholdersInContent = (templateContent, replacements) => {
|
|
70
|
-
let content = templateContent;
|
|
71
|
-
Object.entries(replacements).forEach(([placeholder, value]) => {
|
|
72
|
-
const regex = new RegExp(`\\$${placeholder}\\$`, 'g');
|
|
73
|
-
content = content.replace(regex, value);
|
|
74
|
-
});
|
|
75
|
-
return content;
|
|
76
|
-
};
|
|
77
|
-
this.readStats = (path) => (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.lstat(path));
|
|
78
|
-
this.readDir = (path) => (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.readdir(path, { withFileTypes: true }));
|
|
79
|
-
this.readFile = (path) => (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.readFile(path, 'utf-8'));
|
|
80
|
-
this.writeFile = (targetPath, generatedContent) => (0, function_1.pipe)((0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(targetPath, generatedContent)), TE.map(() => undefined));
|
|
81
|
-
this.rename = (oldPath, newPath) => (0, function_1.pipe)((0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fse.rename(oldPath, newPath)), TE.map(() => undefined));
|
|
82
|
-
this.copy = (source, targetPath) => (0, function_1.pipe)((0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fse.copy(source, targetPath)), TE.map(() => undefined));
|
|
83
|
-
this.unzip = (source, targetPath) => (0, function_1.pipe)((0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => {
|
|
84
|
-
const extracted = extract(source, {
|
|
85
|
-
dir: targetPath,
|
|
86
|
-
});
|
|
87
|
-
return extracted;
|
|
88
|
-
}), TE.map(() => undefined));
|
|
89
|
-
}
|
|
90
36
|
generate(templatePath, targetPath, values) {
|
|
91
37
|
return (0, function_1.pipe)(this.readStats(templatePath), TE.chain((stats) => {
|
|
92
38
|
if (stats.isDirectory()) {
|
|
@@ -99,5 +45,57 @@ class NodeFsGenerator {
|
|
|
99
45
|
return TE.left(new Error('Unknown file type. Template path needs to be either a directory or a zip file'));
|
|
100
46
|
}), TE.chain(() => this.renameParentDir(targetPath, values)), TE.chain((parentDirName) => this.replacePlaceholdersInDir(parentDirName, values)), TE.map(() => undefined));
|
|
101
47
|
}
|
|
48
|
+
replacePlaceholdersInDir = (dirPath, values) => {
|
|
49
|
+
const targetPath = this.replacePlaceholdersInContent(dirPath, values);
|
|
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) => {
|
|
51
|
+
const fileOrDirPath = (0, path_1.join)(targetPath, dirent.name);
|
|
52
|
+
if (dirent.isDirectory()) {
|
|
53
|
+
return this.replacePlaceholdersInDir(fileOrDirPath, values);
|
|
54
|
+
}
|
|
55
|
+
if (dirent.isFile()) {
|
|
56
|
+
return this.replacePlaceholdersInFile(fileOrDirPath, values);
|
|
57
|
+
}
|
|
58
|
+
return TE.left(new Error('Unknown file type. Template directory should contain either folders or files'));
|
|
59
|
+
})(direntList)), TE.map(() => undefined));
|
|
60
|
+
};
|
|
61
|
+
renameParentDir = (targetPath, values) => {
|
|
62
|
+
const regex = /\$(.*?)\$/g;
|
|
63
|
+
return (0, function_1.pipe)(this.readDir(targetPath), TE.chain((direntList) => TE.right(direntList.filter((dirent) => regex.test(dirent.name)))), TE.chain((direntList) => {
|
|
64
|
+
if (direntList.length === 0) {
|
|
65
|
+
return TE.right(targetPath);
|
|
66
|
+
}
|
|
67
|
+
if (direntList.length > 1) {
|
|
68
|
+
return TE.left(new Error('More than one parent directory found. This is not allowed'));
|
|
69
|
+
}
|
|
70
|
+
return TE.right(`${targetPath}/${direntList[0].name}`);
|
|
71
|
+
}), TE.chain((parentDirName) => {
|
|
72
|
+
const replacedParentDirName = this.replacePlaceholdersInContent(parentDirName, values);
|
|
73
|
+
return (0, function_1.pipe)(this.rename(parentDirName, replacedParentDirName), TE.map(() => replacedParentDirName));
|
|
74
|
+
}));
|
|
75
|
+
};
|
|
76
|
+
replacePlaceholdersInFile = (filePath, values) => {
|
|
77
|
+
const targetPath = this.replacePlaceholdersInContent(filePath, values);
|
|
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)));
|
|
79
|
+
};
|
|
80
|
+
replacePlaceholdersInContent = (templateContent, replacements) => {
|
|
81
|
+
let content = templateContent;
|
|
82
|
+
Object.entries(replacements).forEach(([placeholder, value]) => {
|
|
83
|
+
const regex = new RegExp(`\\$${placeholder}\\$`, 'g');
|
|
84
|
+
content = content.replace(regex, value);
|
|
85
|
+
});
|
|
86
|
+
return content;
|
|
87
|
+
};
|
|
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)(() => {
|
|
95
|
+
const extracted = extract(source, {
|
|
96
|
+
dir: targetPath,
|
|
97
|
+
});
|
|
98
|
+
return extracted;
|
|
99
|
+
}), TE.map(() => undefined));
|
|
102
100
|
}
|
|
103
101
|
exports.NodeFsGenerator = NodeFsGenerator;
|
|
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
26
|
const fs = __importStar(require("fs"));
|
|
36
27
|
const path_1 = require("path");
|
|
@@ -133,10 +124,10 @@ describe('NodeFsGenerator', () => {
|
|
|
133
124
|
fs.mkdirSync(zipOutputPath, {
|
|
134
125
|
recursive: true,
|
|
135
126
|
});
|
|
136
|
-
it('Should extract all content of the template', () =>
|
|
127
|
+
it('Should extract all content of the template', async () => {
|
|
137
128
|
const generator = new NodeFsGenerator_1.NodeFsGenerator();
|
|
138
|
-
|
|
139
|
-
})
|
|
129
|
+
await generator.generate(zipTemplatePath, zipOutputPath, values)();
|
|
130
|
+
});
|
|
140
131
|
it('should output template and rename files/folders according to user input', () => {
|
|
141
132
|
outputTest(zipOutputPath, values);
|
|
142
133
|
});
|
|
@@ -154,10 +145,10 @@ describe('NodeFsGenerator', () => {
|
|
|
154
145
|
recursive: true,
|
|
155
146
|
});
|
|
156
147
|
unzip(zipTemplatePath, folderTemplatePath);
|
|
157
|
-
it('Should copy all content of the template', () =>
|
|
148
|
+
it('Should copy all content of the template', async () => {
|
|
158
149
|
const generator = new NodeFsGenerator_1.NodeFsGenerator();
|
|
159
|
-
const result =
|
|
160
|
-
})
|
|
150
|
+
const result = await generator.generate(folderTemplatePath, folderOutputPath, values)();
|
|
151
|
+
});
|
|
161
152
|
it('should output template and rename files/folders according to user input', () => {
|
|
162
153
|
outputTest(folderOutputPath, values);
|
|
163
154
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trayio/generator",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.9.0",
|
|
4
4
|
"description": "Template based generator",
|
|
5
5
|
"author": "Tray.io",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@trayio/commons": "3.
|
|
14
|
+
"@trayio/commons": "3.9.0",
|
|
15
15
|
"extract-zip": "2.0.1",
|
|
16
16
|
"fs-extra": "11.1.0"
|
|
17
17
|
},
|