generated from react18-tools/turborepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhook.js
62 lines (57 loc) · 1.58 KB
/
hook.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
const fs = require("fs");
const path = require("path");
const __dir = path.resolve(__dirname, "..");
const TEMPLATE_DIR = "scripts/templates/";
/**
* Gets actions based on the provided data.
* @param {InquirerDataType} data - Input data.
* @returns {import('plop').ActionType[]} Actions.
*/
function getActions(data) {
const actions = [];
if (!fs.existsSync(path.resolve(__dir, `${data.pkgPath}/src/hooks`, "index.ts"))) {
actions.push({
type: "add",
path: `${data.pkgPath}/src/hooks/index.ts`,
template: '// hooks exports\nexport * from "./{{kebabCase name}}";',
});
} else {
actions.push({
type: "append",
pattern: /(?<insertion> hooks exports)/,
path: `${data.pkgPath}/src/hooks/index.ts`,
template: 'export * from "./{{kebabCase name}}";',
});
}
["", ".test"].forEach(suffix => {
actions.push({
type: "add",
path: `${data.pkgPath}/src/hooks/{{kebabCase name}}${suffix}.ts`,
templateFile: `${TEMPLATE_DIR}hook${suffix}.hbs`,
});
});
return actions;
}
module.exports = {
description: "Add a new React hook.",
prompts: [
{
type: "list",
name: "pkgPath",
choices: ["treeshakable", "packages/shared"],
default: "treeshakable",
message: "Select the package",
},
{
type: "input",
name: "name",
message: "What is the name of the hook?",
},
{
type: "input",
name: "description",
message: "Describe your custom hook. (This will be added as js-doc comment.)",
},
],
actions: data => (data ? getActions(data) : []),
};