link685 link686 link687 link688 link689 link690 link691 link692 link693 link694 link695 link696 link697 link698 link699 link700 link701 link702 link703 link704 link705 link706 link707 link708 link709 link710 link711 link712 link713 link714 link715 link716 link717 link718 link719 link720 link721 link722 link723 link724 link725 link726 link727 link728 link729 link730 link731 link732 link733 link734 link735 link736 link737 link738 link739 link740 link741 link742 link743 link744 link745 link746 link747 link748 link749 link750 link751 link752 link753 link754 link755 link756 link757 link758 link759 link760 link761 link762 link763 link764 link765 link766 link767 link768 link769 link770 link771 link772 link773 link774 link775 link776 link777 link778 link779 link780 link781 link782 link783 link784 link785 link786 link787 link788 link789 link790 link791 link792 link793 link794 link795 link796 link797 link798 link799 link800 link801 link802 link803 link804 link805 link806 link807 link808 link809 link810 link811 link812 link813 link814 link815 link816 link817 link818 link819 link820 link821

[Vue.js] (Code Refactoring) shell command using child_process in javascript

when working on a nuxt.js / vue.js project and applying the atomic design methodology, it means that the components will be divided into and hundreds of components in each folder

/components
/atoms
/molecules
/organisms

and need to import in a grouped and smart way, so i do:

in webpack.config.js or nuxt.config.js

using Compiler Hooks from webpack for each build, generating an index.js exporting components

const exec = require(‘child_process’).exec;

module.exports = {

// … other config here …

plugins: [

// … other plugins here …

{
apply: compiler => {
compiler.hooks.beforeCompile.tap(‘MyPlugin’, compilation => {
exec(‘sh assets/DynamicExport.sh’, (err, stdout, stderr) => {
if (stdout) process.stdout.write(stdout)
if (stderr) process.stderr.write(stderr)
})
})
}
}
]
};

in assets/DynamicExport.sh

ls components/atoms/ | grep -v index.js | sed ‘s#^\([^.]*\).*$#export {default as \1} from “./&”#’ > components/atoms/index.js

ls components/molecules/ | grep -v index.js | sed ‘s#^\([^.]*\).*$#export {default as \1} from “./&”#’ > components/molecules/index.js

ls components/organisms/ | grep -v index.js | sed ‘s#^\([^.]*\).*$#export {default as \1} from “./&”#’ > components/organisms/index.js

then an index.js file is generated in each folder by exporting all components of the folder

export { default as ButtonStyled } from ‘./ButtonStyled.vue’
export { default as TextLead } from ‘./TextLead.vue’
export { default as InputSearch } from ‘./InputSearch.vue’
….

Finally i can import in a clear, grouped and smart way,
anywhere in my application.

import {
ButtonStyled,
TextLead,
InputSearch
} from ‘@/components/atoms’

import {
SearchForm
} from ‘@/components/molecules’

everything works fine, however I’m finding the solution a bit big, calling a file in assets, maybe it has another way that I do not know ..

Is there any way to refactor and lower the content of assets/DynamicExport.sh something less repetitive?

Any code refactoring will be welcome.

Thanks in advance.

Solution :

It’s just a shell file, so you can do something like this:

parameters=( atoms molecules organisms )
for item in ${parameters[*]}
do
ls components/$item/ | grep -v index.js | sed ‘s#^\([^.]*\).*$#export {default as \1} from “./&”#’ > components/$item/index.js
done

And if you wanted to be really sneaky, and you knew that every subdirectory in the components directory needed to be iterated, then you could even replace the first line with:

parameters=$(ls components)

EDIT parsing ls is not safe. Here is a better approach:

for item in components/*;
do
# do something with item
done