1 |
|
2 | const assert = require('assert');
|
3 | const chalk = require('chalk');
|
4 | const fsExtra = require('fs-extra');
|
5 | const path = require('path');
|
6 |
|
7 | const ensureEmptyFolder = require('./utils/ensure-empty-folder');
|
8 | const readFile = require('./utils/read-file');
|
9 | const renameTransform = require('./transforms/rename-jsx');
|
10 | const writeFile = require('./utils/write-file');
|
11 |
|
12 | module.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',
|
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 | };
|