raw implementation

This commit is contained in:
Sagi Dayan 2019-11-20 13:02:20 -05:00
parent a0c87eafff
commit 390429de87
6 changed files with 300 additions and 0 deletions

189
.gitignore vendored Normal file
View file

@ -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

38
bin/iexec Executable file
View file

@ -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>', '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);

36
lib/index.js Normal file
View file

@ -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;

14
package-lock.json generated Normal file
View file

@ -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=="
}
}
}

15
package.json Normal file
View file

@ -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"
}
}

8
yarn.lock Normal file
View file

@ -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==