Last active
October 5, 2018 16:50
-
-
Save renzocastro/c778b1ce30ae16b3866b2f9a7c34444c to your computer and use it in GitHub Desktop.
Comparing NodeJS script and Bash Script
This file contains hidden or 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
| const fs = require('fs'); | |
| const path = require('path'); | |
| function existsSync(pathFileOrDirectory) { | |
| try { | |
| fs.accessSync(pathFileOrDirectory); | |
| return true; | |
| } catch (err) { | |
| return false; | |
| } | |
| } | |
| function copyFileSync(source, target) { | |
| let targetFile = target; | |
| if (existsSync(target) && fs.lstatSync(target).isDirectory()) { | |
| targetFile = path.join(target, path.basename(source)); | |
| } | |
| fs.writeFileSync(targetFile, fs.readFileSync(source)); | |
| } | |
| function copyDirectoryRecursiveSync(source, target) { | |
| let files = []; | |
| // check if directory needs to be created or integrated | |
| let targetDirectory = path.join(target, path.basename(source)); | |
| if (!existsSync(targetDirectory)) { | |
| fs.mkdirSync(targetDirectory); | |
| } | |
| // copy | |
| if (fs.lstatSync(source).isDirectory()) { | |
| files = fs.readdirSync(source); | |
| files.forEach(function (file) { | |
| const curSource = path.join(source, file); | |
| if (fs.lstatSync(curSource).isDirectory()) { | |
| copyDirectoryRecursiveSync(curSource, targetDirectory); | |
| } else { | |
| copyFileSync(curSource, targetDirectory); | |
| } | |
| }); | |
| } | |
| } | |
| module.exports = { | |
| existsSync, | |
| copyDirectoryRecursiveSync | |
| }; |
This file contains hidden or 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
| #!/usr/bin/env node | |
| const fsUtil = require('./fsUtils.inc'); | |
| const fs = require('fs'); | |
| const util = require('util'); | |
| function logInfo(message) { | |
| const colorReset = '\x1b[0m'; | |
| const colorBright = '\x1b[1m'; | |
| const colorYellow = '\x1b[33m'; | |
| const colorWhite = '\x1b[37m'; | |
| console.log(`${colorBright}${colorYellow}» ${colorWhite}%s${colorReset}`, message); | |
| } | |
| function createDirectory() { | |
| logInfo('Creating ./dist directory...'); | |
| const path = './dist'; | |
| if (!fsUtil.existsSync(path)) { | |
| fs.mkdirSync(path); | |
| } | |
| } | |
| function copyAssets() { | |
| // $ cp -r ./src/assets ./dist/ | |
| logInfo('Copying ./src/assets directory into ./dist directory...'); | |
| const source = './src/assets'; | |
| const target = './dist/'; | |
| fsUtil.copyDirectoryRecursiveSync(source, target); | |
| } | |
| function main() { | |
| createDirectory(); | |
| copyAssets(); | |
| console.log(); | |
| // process.stdout.write(JSON.stringify(resp)); | |
| } | |
| try { | |
| // main(process.argv[2] || ''); | |
| main(); | |
| } catch (exception) { | |
| process.stderr.write(util.format('tasks/postinstall › %s\n', exception.message)); | |
| } |
This file contains hidden or 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
| #!/usr/bin/env bash | |
| logInfo () { | |
| COLOR_RESET='\x1b[0m' | |
| COLOR_GREY='\x1b[38;5;8m' # British word | |
| COLOR_GREEN='\x1b[38;5;10m' | |
| echo -e "» ${COLOR_GREEN}$1 ${COLOR_RESET}$2" | |
| } | |
| PATH_TARGET='./dist'; | |
| PATH_SOURCE_ASSETS='./src/assets'; | |
| PATH_TARGET_ASSETS='./dist/assets'; | |
| createTargetDirectory() { | |
| logInfo 'Creating' './dist' | |
| if [ ! -d "$PATH_TARGET" ]; then | |
| mkdir -p $PATH_TARGET | |
| fi | |
| } | |
| copyAssets() { | |
| logInfo 'Copying' './src/assets into ./dist' | |
| rm -rf $PATH_TARGET_ASSETS | |
| cp -r $PATH_SOURCE_ASSETS $PATH_TARGET | |
| } | |
| main() { | |
| createTargetDirectory | |
| copyAssets | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment