Skip to content

Commit

Permalink
fix(generateindex file): generate index file for all the component
Browse files Browse the repository at this point in the history
  • Loading branch information
opensrc0 committed Feb 26, 2024
1 parent 8e0a9e0 commit aa032e2
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/

__build/
__build/

**/index.js
47 changes: 47 additions & 0 deletions __app/script/generateIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const { mkdirp } = require('mkdirp');

const ignoreFiles = [
'.DS_Store',
'scripts',
'utils',
'WIP-',
];

function getRandomInt(max) {
return Math.floor(Math.random() * max);
}

const color = [
{ name: 'FgRed', value: '\x1b[31m%s\x1b[0m' },
{ name: 'FgGreen', value: '\x1b[32m%s\x1b[0m' },
{ name: 'FgYellow', value: '\x1b[33m%s\x1b[0m' },
{ name: 'FgMagenta', value: '\x1b[35m%s\x1b[0m' },
{ name: 'FgCyan', value: '\x1b[36m%s\x1b[0m' },
];

// generate exports for all platforms
const srcPath = path.resolve(__dirname, '../component');
const components = fs.readdirSync(srcPath).filter((files) => !ignoreFiles.includes(files) && !files.includes('WIP-'));
let count = 0;

components.forEach((component) => {
const componentDir = path.resolve(`${__dirname}`, `../../${component}`);
mkdirp(componentDir).then(() => {
const componentFile = path.resolve(componentDir, 'index.js');
// const componentContent = `export { default } from '../__build/${component}';\nexport * from '../__build/${component}';\n`;
const componentContent = `import ${component} from '../__build/${component}/${component}';\nexport default ${component};\n`;
fs.writeFile(componentFile, componentContent, (writeFileErr) => {
if (writeFileErr) throw writeFileErr;
console.log(color[getRandomInt(color.length)].value, ` ${count + 3}. generated: ${componentFile} \n`);
count += 1;
if (count === components.length) {
console.log(color[0].value, ` ${count + 3}. Generated: Package index files for package for direct import \n`);
console.log('\x1b[44m%s\x1b[0m', ` ${count + 4}. Final: Setup Completed Successfully`);
console.log('');
}
});
});
});
150 changes: 148 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "",
"scripts": {
"test": "echo \"Success: Verified\"",
"build": "babel __app/component -d ./__build",
"build": "npm run build:component && npm run build:index",
"build:component": "babel __app/component -d ./__build",
"build:indexfile": "node ./__app/script/generateIndex.js",
"commit": "git-cz",
"lint": "eslint .",
"semantic-release": "semantic-release --branches main"
Expand Down Expand Up @@ -79,6 +81,7 @@
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"cz-conventional-changelog": "^3.3.0",
"mkdirp": "^3.0.1",
"semantic-release": "^23.0.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
Expand Down

0 comments on commit aa032e2

Please sign in to comment.