forked from microsoft/react-component-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewcomponent.mjs
141 lines (113 loc) · 6.18 KB
/
newcomponent.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import fs from 'fs';
import path from 'path';
const componentName = process.argv[2].toLowerCase();
const componentDir = "./src/components/" + componentName;
const componentPlaceHolderName = "ComponentTemplate";
const componentUCTemplateName = "UCCompTemplate";
const componentIndexTsFile = "index.ts"
const componentPackageJson = "package.json";
const componentTsxFile = componentPlaceHolderName + ".tsx";
const componentTypesFile = componentPlaceHolderName + ".types.ts";
const componentStoriesFile = componentPlaceHolderName + ".stories.tsx";
const componentSnapShotFile = componentPlaceHolderName + ".test.tsx.snap";
const componentUnitTestFile = componentPlaceHolderName + ".test.tsx";
const rollupConfigPath = "./rollup.config.js";
const rollupConfigBackupPath = rollupConfigPath + ".backup";
const templateDir = "./templates/tempcomp/";
const indexTsPath = path.join(templateDir, componentIndexTsFile);
const packageJsonPath = path.join(templateDir, componentPackageJson);
const tsxPath = path.join(templateDir, componentTsxFile);
const typesPath = path.join(templateDir, componentTypesFile);
const storiesPath = path.join(templateDir, componentStoriesFile);
const snapShotPath = path.join(templateDir, componentSnapShotFile);
const unitTestPath = path.join(templateDir, componentUnitTestFile);
const outputIndexTs = path.join(componentDir, componentIndexTsFile);
const outputPackageJson = path.join(componentDir, componentPackageJson);
const outputTsx = path.join(componentDir, componentName + ".tsx");
const outputTypes = path.join(componentDir, componentName + ".types.ts");
const outputStoriesPath = "./src/stories/";
const outputUnitTestSnapshot = "./src/unittests/__snapshots__/";
const outputUnitTestPath = "./src/unittests/";
const outputStories = path.join(outputStoriesPath, componentName + ".stories.tsx");
const outputUnitTest = path.join(outputUnitTestPath, componentName + ".test.tsx");
const outputSnapShot = path.join(outputUnitTestSnapshot, componentName + ".test.tsx.snap");
console.log(`React Component Toolkit - Create New Component - ${componentName}`);
console.log("");
const addDirectImport = (text, importName, importPath) => {
const startSectionRegex = /^const\sdirectImports\s*=\s*{/;
const endSectionRegex = /^}/;
const lines = text.split('\n');
const newLines = [];
let addComplete = false;
lines.forEach(line => {
if (!addComplete && startSectionRegex.test(line)) {
newLines.push(line);
newLines.push(` ${importName}: '${importPath}',`);
addComplete = true
}
else {
newLines.push(line);
}
});
return newLines.join('\n');
};
if (!fs.existsSync(componentDir)) {
console.log(" [CREATE] Directory: " + componentDir);
fs.mkdirSync(componentDir);
const componentRegEx = new RegExp(componentPlaceHolderName, 'g');
const componentUCTemplateRegEx = new RegExp(componentUCTemplateName, 'g');
console.log(" [CREATE] File: " + outputIndexTs);
const indexTsSource = fs.readFileSync(indexTsPath, 'utf8');
const newIndexTsOutput = indexTsSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputIndexTs, newIndexTsOutput, 'utf8');
console.log(" [CREATE] File: " + outputPackageJson);
const packageJsonSource = fs.readFileSync(packageJsonPath, 'utf8');
const newPackageJsonOutput = packageJsonSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputPackageJson, newPackageJsonOutput, 'utf8');
console.log(" [CREATE] File: " + outputTsx);
const componentTsxSource = fs.readFileSync(tsxPath, 'utf8');
let newComponentTsxOutput = componentTsxSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputTsx, newComponentTsxOutput, 'utf8');
console.log(" [CREATE] File: " + outputTypes);
const componentTypesSource = fs.readFileSync(typesPath, 'utf8');
const newComponentTypesOutput = componentTypesSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputTypes, newComponentTypesOutput, 'utf8');
console.log(" [CREATE] File: " + outputPackageJson);
const componentStorySource = fs.readFileSync(storiesPath, 'utf8');
let newComponentStoryOutput = componentStorySource.replace(componentUCTemplateRegEx, capitalizeFirstLetter(componentName));
newComponentStoryOutput = newComponentStoryOutput.replace(componentRegEx, componentName);
fs.writeFileSync(outputStories, newComponentStoryOutput, 'utf8');
console.log(" [CREATE] File: " + outputSnapShot);
const componentSnapShotSource = fs.readFileSync(snapShotPath, 'utf8');
const newComponentSnapShotOutput = componentSnapShotSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputSnapShot, newComponentSnapShotOutput, 'utf8');
console.log(" [CREATE] File: " + outputUnitTest);
const componentUnitTestSource = fs.readFileSync(unitTestPath, 'utf8');
let newComponentUnitTestOutput = componentUnitTestSource.replace(componentRegEx, componentName);
fs.writeFileSync(outputUnitTest, newComponentUnitTestOutput, 'utf8');
console.log(" [BACKING UP] Rollup Configuration to " + rollupConfigBackupPath);
const rollupConfigFile = fs.readFileSync(rollupConfigPath, 'utf8');
fs.writeFileSync(rollupConfigBackupPath, rollupConfigFile, 'utf-8');
console.log(" [UPDATING] Rollup Configuration");
const newRollupConfig = addDirectImport(rollupConfigFile, componentName, `src/components/${componentName}/index.ts`);
fs.writeFileSync(rollupConfigPath, newRollupConfig, 'utf-8');
console.log("");
console.log(`All done, your new component ${componentName} is located in : ${componentDir}`);
console.log("");
console.log(" To run the component, run the following command:");
console.log(" npm run ladle:dev");
console.log("");
}
else {
console.log(` [ERROR] Component already exists: ${componentDir}`);
console.log("");
console.log(" To remove the existing component, run the following command:");
console.log(" npm run removecomponent " + componentName);
console.log("");
process.exit(1);
}
function capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}