-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplopfile.js
88 lines (80 loc) · 2.85 KB
/
plopfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { snakeCase } = require('lodash');
module.exports = (plop) => {
plop.setHelper('spacedUpperCase', (txt) =>
snakeCase(txt).split('_').join(' ').toUpperCase(),
);
// This helper allows us to place a variable inside curly braces without spaces
// between the text and the braces.
plop.setHelper('inBraces', (txt) => `{${txt}}`);
plop.setGenerator('component', {
description: 'Create a reusable component',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is your component name?',
},
],
actions: [
{
type: 'add',
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.tsx',
templateFile: 'plop-templates/Component/Component.tsx.hbs',
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.stories.ts',
templateFile: 'plop-templates/Component/Component.stories.ts.hbs',
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.module.css',
templateFile: 'plop-templates/Component/Component.module.css.hbs',
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}/{{pascalCase name}}.test.ts',
templateFile: 'plop-templates/Component/Component.test.ts.hbs',
},
{
type: 'add',
path: 'src/components/{{pascalCase name}}/index.ts',
templateFile: 'plop-templates/Component/index.ts.hbs',
},
{
type: 'append',
pattern: /;\n$/,
separator: '',
path: 'src/index.ts',
template:
"export { default as {{pascalCase name}} } from './components/{{pascalCase name}}';",
},
// From https://github.com/bradfrost/czi-vanilla-storybook
function sortIndex() {
process.chdir(plop.getPlopfilePath());
const fs = require('fs');
const indexFile = `${plop.getDestBasePath()}/src/index.ts`;
if (fs.existsSync(indexFile)) {
// Split the index file into lines.
const lines = fs.readFileSync(indexFile, 'utf8').split('\n');
// Only sort lines that begin with "export {" in order to ignore
// comments and exported types.
const sortableLines = lines.filter((line) =>
/^export {.*/g.test(line),
);
// Ignore all other lines and turn them back into a string.
const ignoreLines = lines
.filter((line) => !/^export {.*/g.test(line))
.join('\n');
// Sort the sortable lines.
const sortedLines = sortableLines
.sort((a, b) => a.localeCompare(b))
.join('\n');
// Write all of the lines back to the file.
fs.writeFileSync(indexFile, `${sortedLines}\n${ignoreLines}\n`);
return `index.ts lines sorted`;
}
},
],
});
};