Skip to content

Instantly share code, notes, and snippets.

@VonHeikemen
Last active February 10, 2021 13:22
Show Gist options
  • Save VonHeikemen/eac0af578e8583c762db912b7c2a08f3 to your computer and use it in GitHub Desktop.
Save VonHeikemen/eac0af578e8583c762db912b7c2a08f3 to your computer and use it in GitHub Desktop.
Create a productive environment for small personal scripts. Only try this at home.
{
"cache": false,
"await": true
}
#! /usr/bin/env sh
export ESM_OPTIONS=/path/to/jsenv/esm.json
NODE_PATH=/path/to/jsenv/node_modules/ \
node -r esm -r /path/to/jsenv/main.js "$@"
const jsonfile = require('jsonfile');
const execa = require('execa');
const color_support = require('color-support').hasBasic;
const c = require('ansi-colors');
c.enabled = color_support;
function add_global(key, value) {
global[key] = value;
}
function stdout(str) {
process.stdout.write(str);
}
async function stdin() {
process.stdin.setEncoding('utf8');
let input = '';
for await (const chunk of process.stdin) {
input += chunk;
}
return input;
}
function format_number(number) {
return new Intl.NumberFormat('de-DE').format(number);
}
function curry(arity, fn, ...rest) {
if (arity <= rest.length) {
return fn(...rest);
}
return curry.bind(null, arity, fn, ...rest);
}
const shell = (options) => (cmd) => execa.command(cmd, options);
const sh = shell({ stdio: 'inherit' });
sh.quiet = shell();
sh.run = (cmd) => sh.quiet(cmd).then(res => res.stdout);
sh.build = shell;
sh.safe = (cmd) =>
sh(cmd)
.then((arg) => arg)
.catch((arg) => arg);
add_global('color', c);
add_global('sh', sh);
add_global('argv', process.argv.slice(2));
add_global('cli', require('arg'));
add_global('stdin', stdin);
add_global('stdout', stdout);
add_global('fetch', require('node-fetch'));
add_global('FormData', require('form-data'));
add_global('cheerio', require('cheerio'));
add_global('jsonfile', {
read: jsonfile.readFile,
write: curry(2, (path, data) => jsonfile.writeFile(path, data, {spaces: 2}))
});
add_global('curry', curry);
add_global('format_number', format_number);
{
"name": "jsenv",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"ansi-colors": "^4.1.1",
"arg": "^5.0.0",
"cheerio": "^1.0.0-rc.5",
"color-support": "^1.1.3",
"dialog": "^0.3.1",
"enquirer": "^2.3.6",
"execa": "^5.0.0",
"form-data": "^3.0.0",
"jsonfile": "^6.1.0",
"node-fetch": "^2.6.1",
"pptr-testing-library": "^0.6.4",
"puppeteer-core": "^7.0.3",
"ramda": "^0.27.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment