-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateComponent.script.cjs
More file actions
69 lines (54 loc) · 1.67 KB
/
createComponent.script.cjs
File metadata and controls
69 lines (54 loc) · 1.67 KB
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
// Command:
// Creates Component.tsx, Component.scss and Component.test.tsx files with some boilerplate code within them.
// go to /cs50w-final-project/frontend
// node createComponent.script.cjs ComponentXXX
const fs = require("fs");
const path = require("path");
if (process.argv.length < 3) {
console.error("Please provide a component name.");
process.exit(1);
}
const componentName = process.argv[2];
const folderPath = path.join(__dirname, componentName);
// Create the folder
fs.mkdirSync(folderPath);
// Create Component.tsx
const componentContent = `import './${componentName}.scss';
type ${componentName}Props = {
}
export const ${componentName} = ({}: ${componentName}Props): React.ReactNode => {
return <div>${componentName}</div>;
}`;
fs.writeFileSync(
path.join(folderPath, `${componentName}.tsx`),
componentContent
);
// Create Component.scss
fs.writeFileSync(path.join(folderPath, `${componentName}.scss`), "");
// Create Component.test.tsx
const testContent = `
import { render, screen } from '@testing-library/react';
import { ${componentName} } from '.';
import { BrowserRouter } from 'react-router-dom';
import { store } from '../../redux/store';
import { Provider } from 'react-redux';
beforeEach(() => {
render(
<BrowserRouter>
<Provider store={store}>
<${componentName} />
</Provider>
</BrowserRouter>
);
});
describe("${componentName}", () => {
it("renders correctly", () => {
expect(screen.getByText(/${componentName}/i)).toBeInTheDocument()
});
});
`;
fs.writeFileSync(
path.join(folderPath, `${componentName}.test.tsx`),
testContent
);
console.log(`${componentName} folder and files created successfully.`);