DEV Community

Discussion on: What is the best scripting language for secure automation and speed?(python, ruby, rust)

Collapse
 
wolfiton profile image
wolfiton

It seems that I will have to take into consideration node for my list of scripting languages.

Thanks @bittnkr @avalander @tobiassn

Collapse
 
bittnkr profile image
bittnkr • Edited

Searching my repositories, I found an example where I used node for automation. I think it can serve as a seed for you.

It uses 7zip to create a separated file for each sub-directory.

Useful for when you have one directory with logs for each day or month and want a single zip file per day.

It launches a process for each core of you processor as needed.

You need 7zip installed in your computer. (in linux, sudo apt install p7zip-full)

Save it as zipsubs, mark it as executable chmod +x zipsubs

zipsubs . .git node_modules will zip all subdirs of current dir ignoring .git and node_modules.

It has no dependencies.

#!/usr/bin/env node
const { exec } = require('child_process')
  , fs = require('fs')
  , path = require('path')
  , rootDir = process.argv[2]
  , ignore = process.argv.slice(3)
  , cpuCount = require('os').cpus().length / 2 // no hiper threading

if (!rootDir) {
  console.log('zipsubs: 7zip all subdirs of a directory.\n example of use: zipsubs . [ignore list] ')
  process.exit()
}

let dirs = subDirs(path.resolve(rootDir)).filter(d => ignore.indexOf(d) == -1)
  , running = 0

zipNext()

function zipNext() { // launch a process for each core, as needed
  let dir = dirs.shift()
  if (dir) {
    running++
    zipDir(dir, () => { running--; zipNext() })
    if (running < cpuCount) zipNext()
  }
}

function zipDir(dir, callback) {
  let cmd = `7z a ./${dir}.7z ${path.join(rootDir, dir)}/* -r`
  console.log(cmd)
  exec(cmd, callback)
}

function subDirs(dir) {
  let res = []
  fs.readdirSync(dir).forEach(f => {
    if (fs.statSync(path.join(dir, f)).isDirectory()) res.push(f)
  })
  return res
}
Collapse
 
wolfiton profile image
wolfiton

Thanks for sharing and explaining where you use it, really appreciate it.