From 390429de871acf0834ee6dec0affcb085b1c8930 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Wed, 20 Nov 2019 13:02:20 -0500 Subject: [PATCH] raw implementation --- .gitignore | 189 ++++++++++++++++++++++++++++++++++++++++++++++ bin/iexec | 38 ++++++++++ lib/index.js | 36 +++++++++ package-lock.json | 14 ++++ package.json | 15 ++++ yarn.lock | 8 ++ 6 files changed, 300 insertions(+) create mode 100644 .gitignore create mode 100755 bin/iexec create mode 100644 lib/index.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3b5580 --- /dev/null +++ b/.gitignore @@ -0,0 +1,189 @@ + +# Created by https://www.gitignore.io/api/osx,node,linux,macos,windows,visualstudiocode +# Edit at https://www.gitignore.io/?templates=osx,node,linux,macos,windows,visualstudiocode + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# react / gatsby +public/ + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +### OSX ### +# General + +# Icon must end with two \r + +# Thumbnails + +# Files that might appear in the root of a volume + +# Directories potentially created on remote AFP share + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/osx,node,linux,macos,windows,visualstudiocode \ No newline at end of file diff --git a/bin/iexec b/bin/iexec new file mode 100755 index 0000000..2c838de --- /dev/null +++ b/bin/iexec @@ -0,0 +1,38 @@ +#!/usr/bin/env node +const program = require('commander'); +const Exec = require('../lib/index'); + +function myParseInt(value, dummyPrevious) { + // parseInt takes a string and an optional radix + if (!value) return 1000; + return parseInt(value); +} +function args(value, previous) { + if (!previous) previous === []; + return [...previous, value]; +} +program.version('iexec v0.0.1, Made with ♥️ by Sagi Dayan'); + +program.option('-c, --command ', 'Command to execute wrapped in', null) + .option('-a, --arg [arg]', 'Arguments for the command', args, []) + .option( + '-i, --interval [milliseconds]', 'Interval milliseconds', myParseInt, + 1000) + .option( + '-w, --wait', 'Wait until execution ends before running a new process', + false); + +program.parse(process.argv); +console.log(program.args); +if (!program.command) { + console.error('Must provide a command to run. Use -c to pass a command'); + process.exit(1); +} + +console.log(program.wait); + +const _needToWait = program.wait; +const _command = program.command; +const _args = program.arg; +const _interval = program.interval; +Exec.start(_command, _args, _needToWait, _interval); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..e1760a8 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,36 @@ +const {spawn} = require('child_process'); + + +class Exec { + static start(command, args, needToWait, interval) { + let doneWithOld = true; + let numOfExecutions = 0; + console.log(`Running command '${command}' | Args: ${ + JSON.stringify( + args)} | interval ${interval}ms | needToWait ${!!needToWait}`); + setInterval(() => { + if (needToWait && !doneWithOld) return; + doneWithOld = false; + numOfExecutions++; + const execNum = numOfExecutions; + console.log(`/******* Staring command #${execNum} ********/`); + const exec = spawn(command, args); + + exec.stdout.on('data', (data) => { + console.log(`[${execNum}] ${data}`); + }); + + exec.stderr.on('data', (data) => { + console.error(`[${execNum}] ${data}`); + }); + + exec.on('close', (code) => { + console.log(`/******* Ended command #${execNum} ********/`); + doneWithOld = true; + }); + }, interval); + } +} + + +module.exports = Exec; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..39878c5 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,14 @@ +{ + "name": "iexec", + "version": "0.0.1", + "lockfileVersion": 1, + "author": "Sagi Dayan", + "requires": true, + "dependencies": { + "commander": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.0.1.tgz", + "integrity": "sha512-IPF4ouhCP+qdlcmCedhxX4xiGBPyigb8v5NeUp+0LyhwLgxMqyp3S0vl7TAPfS/hiP7FC3caI/PB9lTmP8r1NA==" + } + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..c66a8ad --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "iexec", + "version": "0.0.1", + "description": "Executes a command in intervals until interrupts", + "bin": "bin/iexec", + "main": "lib/index", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Sagi Dayan", + "license": "ISC", + "dependencies": { + "commander": "^4.0.1" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..c453e9e --- /dev/null +++ b/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +commander@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-4.0.1.tgz#b67622721785993182e807f4883633e6401ba53c" + integrity sha512-IPF4ouhCP+qdlcmCedhxX4xiGBPyigb8v5NeUp+0LyhwLgxMqyp3S0vl7TAPfS/hiP7FC3caI/PB9lTmP8r1NA==