Nerd For Tech
Published in

Nerd For Tech

Convert Files and Folders Structures to Bash Scripts, with NodeJS

This is a simple NodeJS app that takes a source folder as input and generates a Bash script. The Bash script has all the files’ content and the folders’ structure in the source folder and it can recreate them when executed.

Source code available here: https://github.com/alexadam/folders-to-script

First step, iterate through all files in the source folder:

const fs = require("fs")
const path = require("path")
const listFiles = (dirPath, result) => {
files = fs.readdirSync(dirPath)
result = result || ['#!/bin/sh'] for (const file of files) {
///...
}
return result
}
const allFiles = listFiles(rootPath)fs.writeFileSync('./result.sh', allFiles.join('\n'))

If file is a directory, add an mkdir -p command:

for (const file of files) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
result.push(`mkdir -p ${path.join(dirPath, "/", file).replace(rootPath, '.')}`)
result = listFiles(dirPath + "/" + file, result)
}
}

Else, test if file is binary or text, based on its extension:

const textExt = ['txt', 'md', 'html', 'json', 'js', 'jsx', 'ts', 'tsx'];
const binaryExt = ['jpg', 'png', 'gif', 'pdf', 'mp3', 'mp4'];
const getFileExt = (filePath) => filePath.split('.').pop()...
else {
const filePath = path.join(dirPath, "/", file);
const fileExt = getFileExt(filePath);
const fileContent = fs.readFileSync(filePath);
if (textExt.includes(fileExt)) {
result.push(`
cat > ${path.join(dirPath, "/", file).replace(rootPath, '.')} << "EOF"
${fileContent}
EOF
`)
} else if (binaryExt.includes(fileExt)) {
const bindata = fileContent.toString('binary');
const hexdata = new Buffer(bindata, 'ascii').toString('hex');
result.push(`echo '${hexdata}' | xxd -r -p > ${path.join(dirPath, "/", file).replace(rootPath, '.')}`)
}
}
...

Binary files are stored as hex strings.

You can add/remove file extensions in the textExt or binaryExt arrays.

This is the full NodeJS script:

const fs = require("fs")
const path = require("path")
const rootPath = process.argv[2]const textExt = ['txt', 'md', 'html', 'json', 'js', 'jsx', 'ts', 'tsx'];
const binaryExt = ['jpg', 'png', 'gif', 'pdf', 'mp3', 'mp4'];
const getFileExt = (filePath) => filePath.split('.').pop()const listFiles = (dirPath, result) => {
files = fs.readdirSync(dirPath)
result = result || ['#!/bin/sh'] for (const file of files) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
result.push(`mkdir -p ${path.join(dirPath, "/", file).replace(rootPath, '.')}`)
result = listFiles(dirPath + "/" + file, result)
} else {
const filePath = path.join(dirPath, "/", file);
const fileExt = getFileExt(filePath);
const fileContent = fs.readFileSync(filePath);
if (textExt.includes(fileExt)) {
result.push(`
cat > ${path.join(dirPath, "/", file).replace(rootPath, '.')} << "EOF"
${fileContent}
EOF
`)
} else if (binaryExt.includes(fileExt)) {
const bindata = fileContent.toString('binary');
const hexdata = new Buffer(bindata, 'ascii').toString('hex');
result.push(`echo '${hexdata}' | xxd -r -p > ${path.join(dirPath, "/", file).replace(rootPath, '.')}`)
}
}
}
return result
}
const allFiles = listFiles(rootPath)fs.writeFileSync('./result.sh', allFiles.join('\n'))

Test it with node index.js test -> it will generate a Bash script file named result.sh

Then, in an empty directory, execute the script with sh result.sh.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Computed Properties in React vs. Vue

It’s all about RxJS Subject

[JS “1st timers”] Recursion

The Ultimate Guide to Learning Sass/SCSS

BFS vs DFS

NodeJS proxy api for iTunes

How to update Next.js' old version to the latest version?

From vue-class-component to Composition API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
alex adam

alex adam

https://alexadam.dev/

More from Medium

Forbidden (403)- CSRF Verification failed. Request aborted — The Solution

Node js Modules

Send BLE sensor data over MQTT using Javascript MQTT.js and BleuIO

Creating an API using express and MongoDB.