1 |
|
2 | import nodePlop, {
|
3 | NodePlopAPI,
|
4 | AddManyActionConfig,
|
5 | AddActionConfig,
|
6 | CustomActionConfig,
|
7 | Actions,
|
8 | ModifyActionConfig,
|
9 | AppendActionConfig,
|
10 | } from "./index";
|
11 | import inquirer from "inquirer";
|
12 | import prompt from "inquirer-autocomplete-prompt";
|
13 |
|
14 | const plop = await nodePlop("./file", {
|
15 | destBasePath: "./",
|
16 | force: false,
|
17 | });
|
18 |
|
19 | const generators = plop.getGeneratorList();
|
20 |
|
21 | const names = generators.map((v) => v.name);
|
22 |
|
23 | const generator = plop.getGenerator(names[0]);
|
24 |
|
25 | plop.getWelcomeMessage();
|
26 |
|
27 |
|
28 | plop.test();
|
29 |
|
30 | generator.runPrompts(["test"]).then((answers) => {
|
31 | const onComment = (): void => {
|
32 | console.log("Start");
|
33 | };
|
34 | const onSuccess = (): void => {
|
35 | console.log("This worked!");
|
36 | };
|
37 | const onFailure = (): void => {
|
38 | console.log("Failed");
|
39 | };
|
40 | return generator
|
41 | .runActions(answers, { onSuccess, onFailure, onComment })
|
42 | .then(() => {
|
43 | console.log("Done");
|
44 | });
|
45 | });
|
46 |
|
47 | plop.setGenerator("test", {
|
48 | description: "test generator",
|
49 | prompts: [
|
50 | {
|
51 | type: "input",
|
52 | name: "name",
|
53 | message(): string {
|
54 | return "test name";
|
55 | },
|
56 | validate(value): true | string {
|
57 | if (/.+/.test(value)) {
|
58 | return true;
|
59 | }
|
60 | return "test name is required";
|
61 | },
|
62 | },
|
63 | ],
|
64 | actions: [
|
65 | {
|
66 | type: "add",
|
67 | path: "tests/{{dashCase name}}.ava.js",
|
68 | templateFile: "plop-templates/ava-test.js",
|
69 | },
|
70 | ],
|
71 | });
|
72 |
|
73 | plop.setGenerator("test-dynamic-prompts-only", {
|
74 | description: "test dynamic prompts only",
|
75 | prompts: async (inquirer) => ({
|
76 | name: "something-dynamic",
|
77 | }),
|
78 | actions: [
|
79 | {
|
80 | type: "add",
|
81 | path: "tests/{{dashCase name}}.ava.js",
|
82 | templateFile: "plop-templates/ava-test.js",
|
83 | },
|
84 | ],
|
85 | });
|
86 |
|
87 | plop.setGenerator("test-dynamic-actions-only", {
|
88 | description: "test dynamic actions only",
|
89 | prompts: [
|
90 | {
|
91 | type: "input",
|
92 | name: "name",
|
93 | message(): string {
|
94 | return "test name";
|
95 | },
|
96 | validate(value): true | string {
|
97 | if (/.+/.test(value)) {
|
98 | return true;
|
99 | }
|
100 | return "test name is required";
|
101 | },
|
102 | },
|
103 | ],
|
104 | actions(data) {
|
105 | return [
|
106 | {
|
107 | type: "add",
|
108 | path: "tests/{{dashCase name}}.ava.js",
|
109 | templateFile: "plop-templates/ava-test.js",
|
110 | },
|
111 | ];
|
112 | },
|
113 | });
|
114 |
|
115 | plop.setGenerator("test-dynamic-prompts-and-actions", {
|
116 | description: "Uses dynamic prompts and actions",
|
117 | async prompts(inquirer) {
|
118 | return {
|
119 | name: "something-dynamic",
|
120 | };
|
121 | },
|
122 | actions(data) {
|
123 | return [
|
124 | {
|
125 | type: "add",
|
126 | path: "tests/{{dashCase name}}.ava.js",
|
127 | templateFile: "plop-templates/ava-test.js",
|
128 | },
|
129 | ];
|
130 | },
|
131 | });
|
132 |
|
133 | const useAddManyAction = (): AddManyActionConfig => ({
|
134 | type: "addMany",
|
135 | base: "",
|
136 | templateFiles: "",
|
137 | path: "",
|
138 | destination: "",
|
139 | stripExtensions: ["hbs"],
|
140 | globOptions: {
|
141 | dot: true,
|
142 | },
|
143 | });
|
144 |
|
145 | const useAddManyTransformAction = (): AddManyActionConfig => ({
|
146 | type: "addMany",
|
147 | base: "",
|
148 | templateFiles: "",
|
149 | path: "",
|
150 | destination: "",
|
151 | stripExtensions: ["hbs"],
|
152 | transform: (): string => "hello",
|
153 | globOptions: {
|
154 | dot: true,
|
155 | },
|
156 | });
|
157 |
|
158 | const useAddActionTemplateOnly = (): AddActionConfig => ({
|
159 | type: "add",
|
160 | path: "/some/path",
|
161 | template: "a template {{ someVar }}",
|
162 | });
|
163 |
|
164 | const useAddActionTemplateFileOnly = (): AddActionConfig => ({
|
165 | type: "add",
|
166 | path: "/some/path",
|
167 | templateFile: "path/to/some/template.hbs",
|
168 | });
|
169 |
|
170 |
|
171 | const useAddActionNoTemplateOrFileErrors = (): AddActionConfig => ({
|
172 | type: "add",
|
173 | path: "some/path",
|
174 | });
|
175 |
|
176 | function a(plop: NodePlopAPI) {
|
177 | plop.setGenerator("basics", {
|
178 | description: "this is a skeleton plopfile",
|
179 | prompts: [],
|
180 | actions: [],
|
181 | });
|
182 | }
|
183 |
|
184 | function b(plop: NodePlopAPI) {
|
185 | plop.setGenerator("controller", {
|
186 | description: "application controller logic",
|
187 | prompts: [
|
188 | {
|
189 | type: "input",
|
190 | name: "name",
|
191 | message: "controller name please",
|
192 | },
|
193 | ],
|
194 | actions: [
|
195 | {
|
196 | type: "add",
|
197 | path: "src/{{name}}.js",
|
198 | templateFile: "plop-templates/controller.hbs",
|
199 | },
|
200 | ],
|
201 | });
|
202 | }
|
203 |
|
204 | function c(plop: NodePlopAPI) {
|
205 | plop.setHelper("upperCase", function (text) {
|
206 | return text.toUpperCase();
|
207 | });
|
208 |
|
209 |
|
210 | plop.setHelper("upperCase", (txt) => txt.toUpperCase());
|
211 | }
|
212 |
|
213 | function d(plop: NodePlopAPI) {
|
214 | plop.setPartial("myTitlePartial", "<h1>{{titleCase name}}</h1>");
|
215 | }
|
216 |
|
217 | function e(plop: NodePlopAPI) {
|
218 | function doSomething(...args: any[]) {}
|
219 |
|
220 | plop.setActionType("doTheThing", function (answers, config, plop) {
|
221 |
|
222 | doSomething(config.configProp);
|
223 |
|
224 | if (!!doSomething) throw "error message";
|
225 |
|
226 | return "success status message";
|
227 | });
|
228 |
|
229 |
|
230 | plop.setActionType("doTheAsyncThing", function (answers, config, plop) {
|
231 |
|
232 | return new Promise((resolve, reject) => {
|
233 | if (!!answers) {
|
234 | resolve("success status message");
|
235 | } else {
|
236 | reject("error message");
|
237 | }
|
238 | });
|
239 | });
|
240 |
|
241 |
|
242 | plop.setGenerator("test", {
|
243 | prompts: [],
|
244 | actions: [
|
245 | {
|
246 | type: "doTheThing",
|
247 | configProp: "available from the config param",
|
248 | } as CustomActionConfig<"doTheThing">,
|
249 | {
|
250 | type: "doTheAsyncThing",
|
251 | speed: "slow",
|
252 | } as CustomActionConfig<"doTheAsyncThing">,
|
253 | ],
|
254 | });
|
255 | }
|
256 |
|
257 | function ee(plop: NodePlopAPI) {
|
258 | plop.setPrompt("directory", prompt);
|
259 | plop.setGenerator("test", {
|
260 | prompts: [
|
261 | {
|
262 | type: "directory",
|
263 | },
|
264 | ],
|
265 | });
|
266 | }
|
267 |
|
268 | function f(plop: NodePlopAPI) {
|
269 | plop.setGenerator("test", {
|
270 | prompts: [
|
271 | {
|
272 | type: "confirm",
|
273 | name: "wantTacos",
|
274 | message: "Do you want tacos?",
|
275 | },
|
276 | ],
|
277 | actions: function (data) {
|
278 | const actions: Actions = [];
|
279 |
|
280 | if (data && data.wantTacos) {
|
281 | actions.push({
|
282 | type: "add",
|
283 | path: "folder/{{dashCase name}}.txt",
|
284 | templateFile: "templates/tacos.txt",
|
285 | });
|
286 | } else {
|
287 | actions.push({
|
288 | type: "add",
|
289 | path: "folder/{{dashCase name}}.txt",
|
290 | templateFile: "templates/burritos.txt",
|
291 | });
|
292 | }
|
293 |
|
294 | return actions;
|
295 | },
|
296 | });
|
297 | }
|
298 |
|
299 | let _;
|
300 | _ = async () => {
|
301 |
|
302 | const plop = await nodePlop("test", {
|
303 | destBasePath: !!inquirer ? "test" : undefined,
|
304 | force: false,
|
305 | });
|
306 |
|
307 | const generators = plop.getGeneratorList();
|
308 | const generatorNames = generators.map((v) => v.name);
|
309 | const generatorNames2 = plop.getGeneratorList().map((v) => v.name);
|
310 | const j = generatorNames2.some(function (txt) {
|
311 | return txt.indexOf("test") === 0;
|
312 | });
|
313 |
|
314 | const generator = plop.getGenerator("test");
|
315 | if (typeof generator.prompts === "function") {
|
316 | return [];
|
317 | }
|
318 |
|
319 | const promptNames = generator.prompts.map((prompt) => prompt.name);
|
320 |
|
321 | generator
|
322 | .runPrompts(["a"])
|
323 | .then(async (answers) => {
|
324 | return answers;
|
325 | })
|
326 | .then((answers) => {
|
327 | return generator
|
328 | .runActions(answers, {
|
329 | onSuccess: (change) => {
|
330 | let line = "";
|
331 | if (change.type) {
|
332 | line += ` ${change.type}`;
|
333 | }
|
334 | if (change.path) {
|
335 | line += ` ${change.path}`;
|
336 | }
|
337 | },
|
338 | onFailure: (fail) => {
|
339 | let line = "";
|
340 | if (fail.type) {
|
341 | line += ` ${fail.type}`;
|
342 | }
|
343 | if (fail.path) {
|
344 | line += ` ${fail.path}`;
|
345 | }
|
346 | const errMsg = fail.error || fail.message;
|
347 | },
|
348 | onComment: (msg) => {
|
349 | console.log(msg);
|
350 | },
|
351 | })
|
352 | .then(() => {
|
353 | console.log("Test");
|
354 | });
|
355 | })
|
356 | .catch(function (err) {
|
357 | process.exit(1);
|
358 | });
|
359 | };
|
360 |
|
361 | function addActionTests(plop: NodePlopAPI) {
|
362 | let action;
|
363 |
|
364 | action = {
|
365 | type: "add",
|
366 | template: "",
|
367 | templateFile: "",
|
368 | } as AddActionConfig;
|
369 |
|
370 | action = {
|
371 | type: "add",
|
372 | templateFile: "",
|
373 | } as AddActionConfig;
|
374 |
|
375 | action = {
|
376 | type: "add",
|
377 | template: "",
|
378 | } as AddActionConfig;
|
379 | }
|
380 |
|
381 | function modifyActionTests(plop: NodePlopAPI) {
|
382 | let action;
|
383 |
|
384 | action = {
|
385 | type: "modify",
|
386 | template: "",
|
387 | templateFile: "",
|
388 | path: "",
|
389 | pattern: "",
|
390 | } as ModifyActionConfig;
|
391 |
|
392 | action = {
|
393 | type: "modify",
|
394 | templateFile: "",
|
395 | path: "",
|
396 | pattern: "",
|
397 | } as ModifyActionConfig;
|
398 |
|
399 | action = {
|
400 | type: "modify",
|
401 | template: "",
|
402 | path: "",
|
403 | pattern: "",
|
404 | } as ModifyActionConfig;
|
405 | }
|
406 |
|
407 | function appendActionTests(plop: NodePlopAPI) {
|
408 | let action;
|
409 |
|
410 | action = {
|
411 | type: "append",
|
412 | template: "",
|
413 | templateFile: "",
|
414 | path: "",
|
415 | pattern: "",
|
416 | } as AppendActionConfig;
|
417 |
|
418 | action = {
|
419 | type: "append",
|
420 | templateFile: "",
|
421 | path: "",
|
422 | pattern: "",
|
423 | } as AppendActionConfig;
|
424 |
|
425 | action = {
|
426 | type: "append",
|
427 | template: "",
|
428 | path: "",
|
429 | pattern: "",
|
430 | } as AppendActionConfig;
|
431 | }
|