UNPKG

1.61 kBJavaScriptView Raw
1/* eslint-env node */
2const assert = require('assert');
3const chalk = require('chalk');
4const fsExtra = require('fs-extra');
5const path = require('path');
6
7const ensureEmptyFolder = require('./utils/ensure-empty-folder');
8const readFile = require('./utils/read-file');
9const renameTransform = require('./transforms/rename-jsx');
10const writeFile = require('./utils/write-file');
11
12module.exports = function({ componentName, eslintConfig, folderPath }) {
13 return new Promise(async (resolve, reject) => {
14 try {
15 assert(folderPath, 'No path provided');
16 assert(componentName, 'No component name provided.');
17
18 const componentPath = path.join(folderPath, componentName);
19 const jsxFilePath = path.join(componentPath, `${componentName}.jsx`);
20 const scssFilePath = path.join(componentPath, `${componentName}.scss`);
21
22 ensureEmptyFolder(componentPath);
23 await fsExtra.ensureDir(componentPath);
24
25 const jsxFileContent = readFile(
26 path.join(__dirname, './templates/component.jsx')
27 );
28
29 const newJsxFileContent = renameTransform(
30 jsxFileContent,
31 'component', // NOTE: This is the name of the template component
32 componentName,
33 eslintConfig
34 );
35
36 const messages = [
37 writeFile(jsxFilePath, newJsxFileContent),
38 writeFile(scssFilePath, `.${componentName} {}`)
39 ];
40
41 resolve({
42 messages: messages.concat({
43 emoji: '🎉',
44 text: `Created component ${chalk.greenBright(componentName)}`
45 })
46 });
47 } catch (error) {
48 reject(error.message);
49 }
50 });
51};