-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathplopfile.js
128 lines (116 loc) · 4.57 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
let rspProject = 'React Spectrum v3';
let otherProject = 'other';
module.exports = function (plop) {
const currentYear = new Date(Date.now()).getUTCFullYear();
plop.setHelper('currentYear', () => currentYear);
plop.setHelper('replace', function (match, replacement, options) {
let string = options.fn(this);
return string.replace(match, replacement);
});
plop.setHelper('includes', function (array, string) {
return array.includes(string);
});
// controller generator
plop.setGenerator('component', {
description: 'add new component',
prompts: [{
type: 'list',
name: 'projectType',
message: 'what type of project are you setting up',
choices: [rspProject, otherProject]
}, {
type: 'checkbox',
name: 'scopes',
message: 'scope name(s)',
choices: ['@react-aria', '@react-spectrum', '@react-stately', '@react-types'],
validate: (answer) => answer.length > 0,
when: ({projectType}) => projectType === rspProject
}, {
type: 'input',
name: 'scopeName',
message: 'scope name for new package',
validate: (answer) => answer.length > 0,
when: ({projectType}) => projectType === otherProject
}, {
type: 'input',
name: 'packageName',
message: 'package name, all lowercase (e.g. textfield)',
validate: (answer) => answer.length > 0
}, {
type: 'input',
name: 'componentName',
message: 'component name, please use appropriate uppercase (e.g. TextField)',
validate: (answer) => answer.length > 0,
when: ({projectType}) => projectType === rspProject
}, {
type: 'input',
name: 'componentCSS',
message: 'component css module name, blank if N/A. If unsure, check @adobe/spectrum-css-temp/components for a module containing the desired css (e.g. textfield)',
validate: (answer) => answer.length >= 0,
default: null,
when: ({projectType, scopes}) => projectType === rspProject && scopes.includes('@react-spectrum')
}],
actions: function (data) {
let {projectType, scopes, scopeName, packageName, componentName, componentCSS} = data;
let actions = [];
if (projectType === rspProject) {
if (scopes.includes('@react-aria')) {
actions.push({
type: 'addMany',
templateFiles: '../plop-templates/@react-aria/**',
base: '../plop-templates/@react-aria/',
destination: `../packages/@react-aria/${packageName}`,
data: {componentName, scopes}
});
}
if (scopes.includes('@react-spectrum')) {
actions.push({
type: 'addMany',
templateFiles: '../plop-templates/@react-spectrum/**',
base: '../plop-templates/@react-spectrum/',
destination: `../packages/@react-spectrum/${packageName}`,
data: {packageName, componentName, componentCSS, scopes}
});
}
if (scopes.includes('@react-stately')) {
actions.push({
type: 'addMany',
templateFiles: '../plop-templates/@react-stately/**',
base: '../plop-templates/@react-stately/',
destination: `../packages/@react-stately/${packageName}`,
data: {packageName, componentName, scopes}
});
}
if (scopes.includes('@react-types')) {
actions.push({
type: 'addMany',
templateFiles: '../plop-templates/@react-types/**',
base: '../plop-templates/@react-types/',
destination: `../packages/@react-types/${packageName}`,
data: {packageName, componentName, scopes}
});
}
} else {
actions.push({
type: 'addMany',
templateFiles: '../plop-templates/@scope/**',
base: '../plop-templates/@scope/',
destination: `../packages/@${scopeName}/${packageName}`,
data: {scopeName, packageName, componentName}
});
}
return actions;
}
});
};