Skip to content

Commit 5e9f45b

Browse files
committed
fix: pass projectDir to generators in plopfile.js for better configuration handling
1 parent f6b12ae commit 5e9f45b

File tree

6 files changed

+2133
-117
lines changed

6 files changed

+2133
-117
lines changed

generators/atomic-design.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
function createComponentGenerator(type, config) {
2+
const basePath = config.projectDir
3+
? `${config.projectDir}/${config.basePath || "src"}`
4+
: config.basePath || "src";
5+
26
const actions = [
37
{
4-
type: 'add',
5-
path: `${config.basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.tsx`,
6-
templateFile: 'plop-templates/component.tsx.hbs'
8+
type: "add",
9+
path: `${basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.tsx`,
10+
templateFile: "plop-templates/component.tsx.hbs",
711
},
812
{
9-
type: 'add',
10-
path: `${config.basePath}/components/${type}/{{pascalCase name}}/index.ts`,
11-
templateFile: 'plop-templates/index.ts.hbs'
12-
}
13+
type: "add",
14+
path: `${basePath}/components/${type}/{{pascalCase name}}/index.ts`,
15+
templateFile: "plop-templates/index.ts.hbs",
16+
},
1317
];
1418

1519
if (config.separateCss) {
1620
actions.push({
17-
type: 'add',
18-
path: `${config.basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.module.css`,
19-
templateFile: 'plop-templates/styles.css.hbs'
21+
type: "add",
22+
path: `${basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.module.css`,
23+
templateFile: "plop-templates/styles.css.hbs",
2024
});
2125
}
2226

2327
if (config.includeTests) {
2428
actions.push({
25-
type: 'add',
26-
path: `${config.basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.test.tsx`,
27-
templateFile: 'plop-templates/component.test.tsx.hbs'
29+
type: "add",
30+
path: `${basePath}/components/${type}/{{pascalCase name}}/{{pascalCase name}}.test.tsx`,
31+
templateFile: "plop-templates/component.test.tsx.hbs",
2832
});
2933
}
3034

3135
return {
3236
description: `Create a new ${type.slice(0, -1)} component`,
3337
prompts: [
3438
{
35-
type: 'input',
36-
name: 'name',
39+
type: "input",
40+
name: "name",
3741
message: `What is the name of the ${type.slice(0, -1)}?`,
3842
validate: (value) => {
3943
if (/.+/.test(value)) {
4044
return true;
4145
}
4246
return `${type.slice(0, -1)} name is required`;
43-
}
44-
}
47+
},
48+
},
4549
],
46-
actions
50+
actions,
4751
};
4852
}
4953

50-
exports.atomicDesignGenerator = function(plop, config) {
51-
plop.setGenerator('atom', createComponentGenerator('atoms', config));
52-
plop.setGenerator('molecule', createComponentGenerator('molecules', config));
53-
plop.setGenerator('organism', createComponentGenerator('organisms', config));
54-
plop.setGenerator('template', createComponentGenerator('templates', config));
55-
plop.setGenerator('page', createComponentGenerator('pages', config));
56-
};
54+
exports.atomicDesignGenerator = function (plop, config) {
55+
plop.setGenerator("atom", createComponentGenerator("atoms", config));
56+
plop.setGenerator("molecule", createComponentGenerator("molecules", config));
57+
plop.setGenerator("organism", createComponentGenerator("organisms", config));
58+
plop.setGenerator("template", createComponentGenerator("templates", config));
59+
plop.setGenerator("page", createComponentGenerator("pages", config));
60+
};

generators/feature-first.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,78 @@
1-
exports.featureFirstGenerator = function(plop) {
2-
plop.setGenerator('feature', {
3-
description: 'Create a new feature',
1+
exports.featureFirstGenerator = function (plop, config = {}) {
2+
const basePath = config.projectDir
3+
? `${config.projectDir}/${config.basePath || "src"}`
4+
: "src";
5+
6+
plop.setGenerator("feature", {
7+
description: "Create a new feature",
48
prompts: [
59
{
6-
type: 'input',
7-
name: 'name',
8-
message: 'What is the name of the feature?',
10+
type: "input",
11+
name: "name",
12+
message: "What is the name of the feature?",
913
validate: (value) => {
1014
if (/.+/.test(value)) {
1115
return true;
1216
}
13-
return 'Feature name is required';
14-
}
15-
}
17+
return "Feature name is required";
18+
},
19+
},
1620
],
1721
actions: [
1822
{
19-
type: 'add',
20-
path: 'src/features/{{camelCase name}}/components/.gitkeep',
21-
templateFile: 'plop-templates/gitkeep.hbs'
23+
type: "add",
24+
path: `${basePath}/features/{{camelCase name}}/components/.gitkeep`,
25+
templateFile: "plop-templates/gitkeep.hbs",
2226
},
2327
{
24-
type: 'add',
25-
path: 'src/features/{{camelCase name}}/hooks/.gitkeep',
26-
templateFile: 'plop-templates/gitkeep.hbs'
28+
type: "add",
29+
path: `${basePath}/features/{{camelCase name}}/hooks/.gitkeep`,
30+
templateFile: "plop-templates/gitkeep.hbs",
2731
},
2832
{
29-
type: 'add',
30-
path: 'src/features/{{camelCase name}}/utils/.gitkeep',
31-
templateFile: 'plop-templates/gitkeep.hbs'
33+
type: "add",
34+
path: `${basePath}/features/{{camelCase name}}/utils/.gitkeep`,
35+
templateFile: "plop-templates/gitkeep.hbs",
3236
},
3337
{
34-
type: 'add',
35-
path: 'src/features/{{camelCase name}}/index.ts',
36-
templateFile: 'plop-templates/feature-index.ts.hbs'
37-
}
38-
]
38+
type: "add",
39+
path: `${basePath}/features/{{camelCase name}}/index.ts`,
40+
templateFile: "plop-templates/feature-index.ts.hbs",
41+
},
42+
],
3943
});
4044

41-
plop.setGenerator('shared', {
42-
description: 'Create a new shared component',
45+
plop.setGenerator("shared", {
46+
description: "Create a new shared component",
4347
prompts: [
4448
{
45-
type: 'input',
46-
name: 'name',
47-
message: 'What is the name of the shared component?',
49+
type: "input",
50+
name: "name",
51+
message: "What is the name of the shared component?",
4852
validate: (value) => {
4953
if (/.+/.test(value)) {
5054
return true;
5155
}
52-
return 'Component name is required';
53-
}
54-
}
56+
return "Component name is required";
57+
},
58+
},
5559
],
5660
actions: [
5761
{
58-
type: 'add',
59-
path: 'src/shared/{{pascalCase name}}/{{pascalCase name}}.tsx',
60-
templateFile: 'plop-templates/component.tsx.hbs'
62+
type: "add",
63+
path: `${basePath}/shared/{{pascalCase name}}/{{pascalCase name}}.tsx`,
64+
templateFile: "plop-templates/component.tsx.hbs",
6165
},
6266
{
63-
type: 'add',
64-
path: 'src/shared/{{pascalCase name}}/{{pascalCase name}}.module.css',
65-
templateFile: 'plop-templates/styles.css.hbs'
67+
type: "add",
68+
path: `${basePath}/shared/{{pascalCase name}}/{{pascalCase name}}.module.css`,
69+
templateFile: "plop-templates/styles.css.hbs",
6670
},
6771
{
68-
type: 'add',
69-
path: 'src/shared/{{pascalCase name}}/index.ts',
70-
templateFile: 'plop-templates/index.ts.hbs'
71-
}
72-
]
72+
type: "add",
73+
path: `${basePath}/shared/{{pascalCase name}}/index.ts`,
74+
templateFile: "plop-templates/index.ts.hbs",
75+
},
76+
],
7377
});
74-
};
78+
};

generators/pages-components.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,75 @@
1-
exports.pagesComponentsGenerator = function(plop) {
1+
exports.pagesComponentsGenerator = function (plop, config = {}) {
2+
const basePath = config.projectDir
3+
? `${config.projectDir}/${config.basePath || "src"}`
4+
: "src";
5+
26
// Component generator
3-
plop.setGenerator('component', {
4-
description: 'Create a new reusable component',
7+
plop.setGenerator("component", {
8+
description: "Create a new reusable component",
59
prompts: [
610
{
7-
type: 'input',
8-
name: 'name',
9-
message: 'What is the name of the component?',
11+
type: "input",
12+
name: "name",
13+
message: "What is the name of the component?",
1014
validate: (value) => {
1115
if (/.+/.test(value)) {
1216
return true;
1317
}
14-
return 'Component name is required';
15-
}
16-
}
18+
return "Component name is required";
19+
},
20+
},
1721
],
1822
actions: [
1923
{
20-
type: 'add',
21-
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.tsx',
22-
templateFile: 'plop-templates/component.tsx.hbs'
24+
type: "add",
25+
path: `${basePath}/components/{{pascalCase name}}/{{pascalCase name}}.tsx`,
26+
templateFile: "plop-templates/component.tsx.hbs",
2327
},
2428
{
25-
type: 'add',
26-
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.module.css',
27-
templateFile: 'plop-templates/styles.css.hbs'
29+
type: "add",
30+
path: `${basePath}/components/{{pascalCase name}}/{{pascalCase name}}.module.css`,
31+
templateFile: "plop-templates/styles.css.hbs",
2832
},
2933
{
30-
type: 'add',
31-
path: 'src/components/{{pascalCase name}}/index.ts',
32-
templateFile: 'plop-templates/index.ts.hbs'
33-
}
34-
]
34+
type: "add",
35+
path: `${basePath}/components/{{pascalCase name}}/index.ts`,
36+
templateFile: "plop-templates/index.ts.hbs",
37+
},
38+
],
3539
});
3640

3741
// Page generator
38-
plop.setGenerator('page', {
39-
description: 'Create a new page',
42+
plop.setGenerator("page", {
43+
description: "Create a new page",
4044
prompts: [
4145
{
42-
type: 'input',
43-
name: 'name',
44-
message: 'What is the name of the page?',
46+
type: "input",
47+
name: "name",
48+
message: "What is the name of the page?",
4549
validate: (value) => {
4650
if (/.+/.test(value)) {
4751
return true;
4852
}
49-
return 'Page name is required';
50-
}
51-
}
53+
return "Page name is required";
54+
},
55+
},
5256
],
5357
actions: [
5458
{
55-
type: 'add',
56-
path: 'src/pages/{{pascalCase name}}/{{pascalCase name}}.tsx',
57-
templateFile: 'plop-templates/component.tsx.hbs'
59+
type: "add",
60+
path: `${basePath}/pages/{{pascalCase name}}/{{pascalCase name}}.tsx`,
61+
templateFile: "plop-templates/component.tsx.hbs",
5862
},
5963
{
60-
type: 'add',
61-
path: 'src/pages/{{pascalCase name}}/{{pascalCase name}}.module.css',
62-
templateFile: 'plop-templates/styles.css.hbs'
64+
type: "add",
65+
path: `${basePath}/pages/{{pascalCase name}}/{{pascalCase name}}.module.css`,
66+
templateFile: "plop-templates/styles.css.hbs",
6367
},
6468
{
65-
type: 'add',
66-
path: 'src/pages/{{pascalCase name}}/index.ts',
67-
templateFile: 'plop-templates/index.ts.hbs'
68-
}
69-
]
69+
type: "add",
70+
path: `${basePath}/pages/{{pascalCase name}}/index.ts`,
71+
templateFile: "plop-templates/index.ts.hbs",
72+
},
73+
],
7074
});
71-
};
75+
};

0 commit comments

Comments
 (0)