Skip to content

Instantly share code, notes, and snippets.

@broskisworld
Created June 20, 2022 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broskisworld/b9d8d05147dc1eb20203413bb488c59f to your computer and use it in GitHub Desktop.
Save broskisworld/b9d8d05147dc1eb20203413bb488c59f to your computer and use it in GitHub Desktop.
A quick way to bulk import a bunch of files (Javascript, Typescript, Images, etc.) into a Node.js app
# USAGE
# get-js-imports [relativeDirectory = '.'] [js var suffix = '']
# EXAMPLES
# get-js-imports
# get-js-imports ../components
# get-js-imports ../media Img
# DESCRIPTION
# A quick way to bulk import a bunch of files (Javascript, Typescript, Images, etc.) into a Node.js app
# Scans relativeDirectory and creates a list import statements like the following that you can copy-paste into your editor of choice
#
# Current folder contains files this-picture.png, that-vector.svg, and these-files.js
# > get-js-imports
# ~ import thisPicture from './thisPicture.png';
# ~ import thatVector from './thatVector.svg';
# ~ import theseFiles from './theseFiles.js';
scanDir="$1"
# is scan dir blank?
if [ -z "$scanDir" ]
then
scanDir="."
fi
ls -1 $scanDir | while read -r line; do
filename=$(basename -- "$line")
extension="${filename##*.}"
filename="${filename%.*}"
if [ "$filename" != "$extension" ]
then
importName=`echo "$filename" | perl -pe 's/-(.)/\u$1/g'`
# add suffix if exists
echo "import $importName$2 from '$scanDir/$filename';"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment