Skip to content

Commit

Permalink
refactor(boilerplate): added boiler plate to crate component
Browse files Browse the repository at this point in the history
  • Loading branch information
opensrc0 committed Apr 7, 2024
1 parent bf32592 commit 453ceea
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
6 changes: 4 additions & 2 deletions __app/component/CopyToClipboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<div>
`}
>
Pass Copy Icon here
Pass clickable(button, anchor, icon etc) element here to bind onClick event
</CopyToClipboard>
```

Expand All @@ -50,7 +50,9 @@ const successCb = ({ msgType, msg, data }) => {
successCb={successCb}
successMsg="Copied Successfully"
elementToBeCopy={`Fe-pilot library offers component like scanner, voice search, autofill otp, phonebook, share`}
/>
>
Click here to copy (Element, String, etc)
</CopyToClipboard>
```
> [!Note]
> **successCb** will get an object contains the property ```msgType```, ```msg```, ```data```
Expand Down
117 changes: 117 additions & 0 deletions __app/script/boilerPlate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// console.log('Boiler Plate');
// console.log(process.env.COMPONENT);
const fs = require('fs');
const path = require('path');
const { mkdirp } = require('mkdirp');

const { COMPONENT } = process.env;
// const srcPath = path.resolve(__dirname, '__app/component');
const componentDir = path.resolve(`${__dirname}`, `../component/${COMPONENT}`);

mkdirp(componentDir).then(() => {
const componentContent = `import React from 'react';
import PropTypes from 'prop-types';
import { handleSuccess, handleError, handleLoading } from '../services/handlerService';
import Wrapper from '../Wrapper/Wrapper';
const failureMsgDefault = {
unSupported: '${COMPONENT} is not supporting in your device',
error: 'Unable to fetch details from ${COMPONENT}',
};
function ${COMPONENT}({
successCb,
failureCb,
loadingCb,
successMsg,
failureMsg: failureMsgProps,
children,
}) {
const failureMsg = { ...failureMsgDefault, ...failureMsgProps };
const get${COMPONENT} = () => {
};
return (
React.Children.map(children || 'PhoneBook', (child) => React.cloneElement(typeof child === 'string' ? <span>{child}</span> : child, {
onClick: get${COMPONENT},
}))
);
}
${COMPONENT}.isBrowserSupport = () => globalThis && true;
${COMPONENT}.propTypes = {
successCb: PropTypes.func,
failureCb: PropTypes.func,
loadingCb: PropTypes.func,
successMsg: PropTypes.string,
failureMsg: PropTypes.object,
};
${COMPONENT}.defaultProps = {
successCb: () => {},
failureCb: () => {},
loadingCb: () => {},
successMsg: 'Phonebook details fetch Successfully',
failureMsg: { ...failureMsgDefault },
};
export default Wrapper(${COMPONENT});
`;
const IndexContent = `import ${COMPONENT} from './${COMPONENT}';
export { ${COMPONENT} };
export default ${COMPONENT};
`;

const READMEContent = `## 1. Happy Flow
#### a) Passing child
## 2. Success: successCb callBack Fn along with success msg
> [!Note]
> **successCb** will get an object contains the property **msgType**, **msg**, **data**
## 3. Failure: failureCb callBack Fn along with failure msg
> [!Note]
> **failureCb** will get an object contains the property **msgType**, **msg**
> [!Important]
Failure can happend due to multiple reasons, due to that reason **failureMsg** is an object having different kind of error property according to the error can occur in component
## 4. Failure: Device don't support the feature and you want to hide the feauture from User
> [!Note]
> if **showForever** props value is false, feature will be hidden in case of unSupported by the device
## 5. Combine with all props
`;

fs.writeFile((`${componentDir}/${COMPONENT}.js`), componentContent, () => {});
fs.writeFile((`${componentDir}/index.js`), IndexContent, () => {});
fs.writeFile((`${componentDir}/README.md`), READMEContent, () => {});
});
6 changes: 6 additions & 0 deletions __app/script/generateIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const srcPath = path.resolve(__dirname, '../component');
const components = fs.readdirSync(srcPath).filter((files) => !ignoreFiles.includes(files) && !files.includes('WIP-'));
let count = 0;

let indexImport = '';
let indexExport = '\nexport {';
components.forEach((component) => {
indexImport += `import ${component} from './${component}';\n`;
indexExport += `\n ${component},`;
const componentDir = path.resolve(`${__dirname}`, `../../${component}`);
mkdirp(componentDir).then(() => {
const componentFile = path.resolve(componentDir, 'index.js');
Expand All @@ -50,3 +54,5 @@ components.forEach((component) => {
});
});
});
indexExport += '\n};\n';
fs.writeFile(('index.js'), indexImport + indexExport, () => {});
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "echo \"Success: Verified\"",
"create": "node ./__app/script/boilerPlate.js",
"start": "npm run local",
"local": "npm run local:indexfile && npm run local:component",
"local:indexfile": "node ./__app/script/generateIndex.js",
Expand Down Expand Up @@ -81,6 +82,7 @@
"files": [
"__build/**",
"AutoFillOtp/",
"ABCD/",
"Bluetooth/",
"FaceDetector/",
"CopyToClipboard/",
Expand All @@ -93,7 +95,8 @@
"TextToSpeech/",
"VoiceRecognition/",
"Vibrate/",
"ColorPicker/"
"ColorPicker/",
"./index.js"
],
"devDependencies": {
"@babel/cli": "^7.23.9",
Expand Down

0 comments on commit 453ceea

Please sign in to comment.