Guest User

Untitled

a guest
Jul 17th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. const chokidar = require("chokidar");
  2. const fs = require("fs");
  3.  
  4. const templates = {
  5. index: name =>
  6. `// @flow
  7. import React from 'react';
  8.  
  9. import './${name}.css';
  10.  
  11. const ${name} = () => (
  12. <div className="${name.toLowerCase()}">
  13. // TODO: write rest of ${name} component
  14. </div>
  15. );
  16.  
  17. export default ${name};`,
  18. test: name => `// TODO: TDD
  19. import { shallow, render } from 'enzyme';
  20. import renderer from 'react-test-renderer';
  21.  
  22. import React from 'react';
  23. import ${name} from '.';
  24.  
  25. const component = <${name} />;
  26.  
  27. describe('The ${name} component', () => {
  28. it('renders correctly', () => {
  29. const wrapper = render(component);
  30. expect(wrapper.hasClass('${name.toLowerCase()}')).toBeTruthy();
  31.  
  32. const tree = renderer.create(component).toJSON();
  33. expect(tree).toMatchSnapshot();
  34. });
  35. });`,
  36. sass: name => `.${name.toLowerCase()}
  37. color: initial
  38. background: initial`
  39. };
  40.  
  41. const fileExists = path => file => fs.existsSync(`${path}/${file}`);
  42.  
  43. const writeToPath = path => (file, content) => {
  44. const filePath = `${path}/${file}`;
  45.  
  46. fs.writeFile(filePath, content, err => {
  47. if (err) throw err;
  48. console.log("Created file: ", filePath);
  49. return true;
  50. });
  51. };
  52.  
  53. function createFiles(path, name) {
  54. const files = {
  55. index: "index.jsx",
  56. test: `${name}.test.js`,
  57. sass: `${name}.sass`
  58. };
  59.  
  60. if (name !== "components") {
  61. const writeFile = writeToPath(path);
  62. const toFileMissingBool = file => !fileExists(path)(file);
  63. const checkAllMissing = (acc, cur) => acc && cur;
  64.  
  65. const noneExist = Object.values(files)
  66. .map(toFileMissingBool)
  67. .reduce(checkAllMissing);
  68.  
  69. if (noneExist) {
  70. console.log(`Detected new component: ${name}, ${path}`);
  71. Object.entries(files).forEach(([type, fileName]) => {
  72. writeFile(files[type], templates[type](name));
  73. });
  74. }
  75. }
  76. }
  77.  
  78. const watcher = chokidar
  79. .watch("src/components/**", { ignored: /node_modules/ })
  80. .on("addDir", (path, event) => {
  81. const name = path.replace(/.*\/components\//, "");
  82. if (!name.includes("/")) createFiles(path, name);
  83. });
Add Comment
Please, Sign In to add comment