-
Notifications
You must be signed in to change notification settings - Fork 960
/
Copy pathgenerate-plugin.mjs
216 lines (180 loc) · 5.68 KB
/
generate-plugin.mjs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env zx
// @ts-check
import "zx/globals";
const libDir = path.join(__dirname, "../lib");
const templateDir = path.join(__dirname, "./template");
const [_nodePath, _zxPath, _fileName, _pluginName] = process.argv;
const pluginName = _pluginName.includes("reactotron")
? _pluginName
: `reactotron-${_pluginName}`;
const targetDir = path.join(libDir, pluginName);
console.log(`Plugin name: ${pluginName}`);
// validate that the target directory exists
if (fs.existsSync(targetDir)) {
console.error(`Plugin already exists "${targetDir}" does not exist`);
process.exit(1);
}
fs.mkdirSync(targetDir, { recursive: true });
// static files that need to string replacements
const filesToCopy = [
"tsconfig.json",
"LICENSE",
".babelrc",
".prettierignore",
".prettierrc",
".gitignore",
];
for (const file of filesToCopy) {
fs.copyFileSync(path.join(templateDir, file), path.join(targetDir, file));
}
// dynamic files where we need to update the plugin name
// or camel case version of it, etc
fs.writeFileSync(
path.join(targetDir, "package.json"),
JSON.stringify(getPackageJson({ pluginName }), null, 2)
);
fs.writeFileSync(
path.join(targetDir, "project.json"),
createProjectJson({ pluginName })
);
fs.writeFileSync(
path.join(targetDir, `README.md`),
createTemplateREADME({ pluginName })
);
fs.writeFileSync(
path.join(targetDir, `rollup.config.ts`),
createTemplateRollupConfig({ pluginName })
);
const srcFolder = path.join(targetDir, "src");
fs.mkdirSync(srcFolder, { recursive: true });
fs.writeFileSync(
path.join(srcFolder, `index.ts`),
createTemplateIndex({ pluginName })
);
fs.writeFileSync(
path.join(srcFolder, `${pluginName}.ts`),
createTemplatePlugin({ pluginName })
);
console.log(`Voila! Start building: cd ${targetDir} && yarn`);
console.log(
"FYI you may have to update .circleci/config.yml to include your new plugin when ready."
);
/**
* Converts a string to camel case.
* @param {string} str - The string to be converted.
* @returns {string} - The camel case version of the string.
*/
function camelize(str) {
const arr = str.split("-");
const capital = arr.map((item, index) =>
index
? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()
: item.toLowerCase()
);
return capital.join("");
}
/**
* Converts a string to proper case.
* @param {string} str - The string to be converted.
* @returns {string} - The proper case version of the string.
*/
function properCase(str) {
const arr = str.split("-");
const capital = arr.map(
(item) => item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()
);
return capital.join("");
}
/**
* Creates a template index for a plugin.
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string}
*/
function createTemplateIndex({ pluginName }) {
let template = fs.readFileSync(
path.join(templateDir, "src", "index.ts"),
"utf8"
);
template = template.replace(
/templatePlugin/g,
`${camelize(pluginName.replace("reactotron-", ""))}Plugin`
);
template = template.replace(/reactotron-template/g, pluginName);
return template;
}
/**
* Creates a project.json index for a plugin.
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string}
*/
function createProjectJson({ pluginName }) {
let template = fs.readFileSync(
path.join(templateDir, "project.json"),
"utf8"
);
template = template.replace(/reactotron-template/g, pluginName);
return template;
}
/**
* Creates a template plugin
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string}
*/
function createTemplatePlugin({ pluginName }) {
let template = fs.readFileSync(
path.join(templateDir, `src/reactotron-template.ts`),
"utf8"
);
template = template.replace(
/Template/g,
`${properCase(pluginName.replace("reactotron-", ""))}`
);
template = template.replace(
/templatePlugin/g,
`${camelize(pluginName.replace("reactotron-", ""))}Plugin`
);
return template;
}
/**
* Creates a template index for a readme.
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string}
*/
function createTemplateREADME({ pluginName }) {
let template = fs.readFileSync(path.join(templateDir, "README.md"), "utf8");
template = template.replace(/reactotron-template/g, pluginName);
return template;
}
/**
* Creates a rollup config for a plugin.
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string} .
*/
function createTemplateRollupConfig({ pluginName }) {
let template = fs.readFileSync(
path.join(templateDir, "rollup.config.ts"),
"utf8"
);
template = template.replace(/reactotron-template/g, pluginName);
return template;
}
/**
* Get package.json for a plugin
* @param {Object} options - The options for creating the template index.
* @param {string} options.pluginName - The name of the plugin.
* @returns {string}
*/
function getPackageJson({ pluginName }) {
const templatePackageJson = JSON.parse(
fs.readFileSync(path.join(templateDir, "package.json"), "utf8")
);
templatePackageJson.name = pluginName;
(templatePackageJson.homepage = `https://github.com/infinitered/reactotron/tree/master/lib/${pluginName}`),
(templatePackageJson.repository = `https://github.com/infinitered/reactotron/tree/master/lib/${pluginName}`);
return templatePackageJson;
}