Created
July 23, 2021 19:01
-
-
Save vNNi/9bac2e2e2e1d6d5ca0d238443708e85e to your computer and use it in GitHub Desktop.
create package json into all atomic folders into package src
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
const fs = require('fs'); | |
const path = require('path'); | |
const infoLogger = (...msg) => console.log('\x1b[33m%s\x1b[0m', 'Info: ', ...msg); | |
const errorLogger = (...msg) => console.log('\x1b[31m', 'Error: ', ...msg); | |
const warningLogger = (...msg) => console.log('\x1b[34m', 'Warning: ', ...msg); | |
const fileLoader = async (pathOfFile, throwError = false) => { | |
try { | |
return await fs.promises.readFile(pathOfFile); | |
} catch (e) { | |
warningLogger('[FileLoader]: Import failed to - ', pathOfFile, e); | |
if (throwError) throw e; | |
return null; | |
} | |
}; | |
const packageJsonString = 'package.json'; | |
/* | |
* include in the array the folder name which we need | |
* to create package.json inside children directories | |
* e.g: ['templates', 'atoms', 'newAtomicPatternFolder'] | |
*/ | |
const atomicFolders = ['atoms', 'molecules', 'organisms', 'templates']; | |
const componentsPath = path.join(__dirname, '../', 'packages', 'stereo-ui'); | |
const copyMainPackageJson = async () => { | |
return JSON.parse(await fileLoader(path.join(componentsPath, packageJsonString))); | |
}; | |
const readAtomicFolder = async name => { | |
return fs.promises.readdir(path.join(componentsPath, 'src', name)); | |
}; | |
const createNewPackage = async (mainPackage, componentFolder) => { | |
const { scripts, workspaces, exports, ...rootPackageJson } = mainPackage; | |
try { | |
infoLogger('[CreateNewPackage]: creating new package to module - ', componentFolder); | |
/* | |
* Note: Need tu use private as false to be importable from user. | |
*/ | |
return { | |
...rootPackageJson, | |
private: false, | |
sideEffect: false, | |
main: 'index.js', | |
}; | |
} catch (e) { | |
errorLogger( | |
'[CreateNewPackage]: error creating new package to module - ', | |
componentFolder, | |
e | |
); | |
return mainPackage; | |
} | |
}; | |
const writePackage = async (folder, compName, mainPackage) => { | |
try { | |
const pathToRead = path.join(componentsPath, 'src', folder, compName); | |
const pathToWrite = path.join(componentsPath, 'build', folder, compName); | |
const data = await createNewPackage(mainPackage, pathToRead); | |
infoLogger('[WritePackage]: Readding from path', pathToRead); | |
await fs.promises.writeFile( | |
path.join(pathToWrite, packageJsonString), | |
JSON.stringify(data, null, 2) | |
); | |
} catch (e) { | |
errorLogger('[WritePackage]: Error readding component folder', e); | |
} | |
}; | |
/* | |
* In this script, we turn all components, from Atomic structure into | |
* a single "package", to be tree-shakeable manually. | |
* like: "import Button from @magalu/stereo-ui/atoms/Button". | |
*/ | |
async function run() { | |
const mainPackage = await copyMainPackageJson(); | |
await Promise.all( | |
atomicFolders.map(async folderName => { | |
const components = await readAtomicFolder(folderName); | |
components.forEach(async file => { | |
if (file.match(/\.js/)) return; | |
infoLogger('[Run]: running into Component name', file); | |
await writePackage(folderName, file, mainPackage); | |
}); | |
}) | |
); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment