Skip to content

Commit a3c06aa

Browse files
committed
fix(lesy-plugin-generator): windows path escaped in handlebars
1 parent c59d917 commit a3c06aa

File tree

8 files changed

+107
-36
lines changed

8 files changed

+107
-36
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello {{uppercase filename}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{comma user}}

packages/plugins/lesy-plugin-generator/__tests__/fixtures/templates/{{filename}}.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/plugins/lesy-plugin-generator/__tests__/generator-plugin.spec.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import generator from "../src/generator";
1+
import generatorFeature from "../src/generator.feature";
22
import { LesyCoreClass } from "@lesy/core";
33
import {
44
existsSync,
@@ -38,19 +38,71 @@ describe("lesy-plugin-generator", () => {
3838

3939
it("should generate files", (done: any) => {
4040
const feature = {} as any;
41-
generator(feature) as any;
41+
generatorFeature(feature) as any;
4242
feature
4343
.generateFiles({
4444
data: { filename: "abc" },
4545
name: "test",
46-
destination: `${__dirname}/fixtures/outputfiles`,
47-
source: `${__dirname}/fixtures/templates`,
46+
destination: `${__dirname}/fixtures/outputfiles/`,
47+
source: `${__dirname}/fixtures/dynamic`,
4848
})
4949
.then(() => {
5050
const data = readFileSync(
5151
resolve(__dirname, "./fixtures/outputfiles/abc.txt"),
5252
);
53-
expect(data.toString()).toBe("hello abc");
53+
expect(data.toString()).toBe("hello ABC");
54+
done();
55+
});
56+
});
57+
58+
it("should use handlebars instance", (done: any) => {
59+
const feature = {} as any;
60+
generatorFeature(feature) as any;
61+
62+
const handlebarsIns = (hb) => {
63+
hb.registerHelper("comma", (string) => {
64+
return string.split("").join(",");
65+
});
66+
};
67+
68+
feature
69+
.generateFiles({
70+
data: { filename: "abc", user: "jim" },
71+
name: "test",
72+
destination: `${__dirname}/fixtures/outputfiles`,
73+
source: `${__dirname}/fixtures/static`,
74+
handlebarsInstance: handlebarsIns,
75+
})
76+
.then(() => {
77+
const data = readFileSync(
78+
resolve(__dirname, "./fixtures/outputfiles/ins.txt"),
79+
);
80+
expect(data.toString()).toBe("j,i,m");
81+
done();
82+
});
83+
});
84+
85+
it("should use handlebars options", (done: any) => {
86+
const feature = {} as any;
87+
generatorFeature(feature) as any;
88+
89+
feature
90+
.generateFiles({
91+
data: { filename: "abc", user: "jim" },
92+
name: "test",
93+
destination: `${__dirname}/fixtures/outputfiles`,
94+
source: `${__dirname}/fixtures/static`,
95+
handebarsOptions: {
96+
helpers: {
97+
comma: (str: string) => str.split("").join("-,-"),
98+
},
99+
},
100+
})
101+
.then(() => {
102+
const data = readFileSync(
103+
resolve(__dirname, "./fixtures/outputfiles/ins.txt"),
104+
);
105+
expect(data.toString()).toBe("j-,-i-,-m");
54106
done();
55107
});
56108
});

packages/plugins/lesy-plugin-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"case": "^1.6.3",
2323
"handlebars": "^4.7.6",
24-
"scaffold-generator": "^3.0.4"
24+
"scaffold-generator2": "^1.0.0"
2525
},
2626
"publishConfig": {
2727
"access": "public"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
interface GeneratorCtx {
2+
handlebarsInstance: (handlebars) => void;
3+
handebarsOptions: { [key: string]: any };
4+
data: { [key: string]: any };
5+
source: string;
6+
destination: string;
7+
}
8+
9+
interface Feature {
10+
generateFiles: (ctx: GeneratorCtx) => any;
11+
}
12+
13+
export default function (feature: Feature) {
14+
feature.generateFiles = function (data) {
15+
const scaffold = require("scaffold-generator2");
16+
const handlebars = require("handlebars");
17+
const text = require("case");
18+
const options = data.handebarsOptions || {};
19+
20+
const cases = [
21+
"upper",
22+
"lower",
23+
"capital",
24+
"snake",
25+
"pascal",
26+
"camel",
27+
"header",
28+
"constant",
29+
"title",
30+
];
31+
32+
cases.forEach((c: any) => {
33+
handlebars.registerHelper(`${c}case`, (word: string) => text[c](word));
34+
});
35+
36+
if (data.handlebarsInstance) {
37+
data.handlebarsInstance(handlebars);
38+
}
39+
40+
return new scaffold({
41+
data: data.data || {},
42+
render: (str: string, data: string) =>
43+
handlebars.compile(str)(data, options),
44+
}).copy(data.source, data.destination);
45+
};
46+
}

packages/plugins/lesy-plugin-generator/src/generator.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default {
2-
features: [`${__dirname}/generator.ts`],
2+
features: [`${__dirname}/generator.feature.ts`],
33
};

0 commit comments

Comments
 (0)