Skip to content

Instantly share code, notes, and snippets.

@TheBrenny
Last active August 26, 2020 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBrenny/68a873085e7df07375c00addd18ee5f0 to your computer and use it in GitHub Desktop.
Save TheBrenny/68a873085e7df07375c00addd18ee5f0 to your computer and use it in GitHub Desktop.
Create aliases using JS
@ECHO off
SETLOCAL
SET curdir=%~dp0
SET fileCalled=%~n0
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
"%_prog%" "%curdir%\js\%fileCalled%.js" %*
ENDLOCAL
EXIT /b %errorlevel%
// This file is Windows specific, as it goes along with my dev.to post
// post link here
const {
spawn,
exec
} = require('child_process');
const bossman = require('big-kahuna').dashCare(true);
const fs = require('fs');
const notepadCommand = "code"; // CHANGE THIS TO YOUR PREFERRED COMMAND! (eg, code, nano, npp)
const aliasFolder = process.env.ALIAS_FOLDER || process.env.USERPROFILE + "\\.aliases";
const cmdContents = [
'@ECHO off',
'SETLOCAL',
'',
'SET curdir=%~dp0',
'SET fileCalled=%~n0',
'',
'SET "_prog=node"',
'SET PATHEXT=%PATHEXT:;.JS;=;%',
'',
'"%_prog%" "%curdir%\js\%fileCalled%.js" %*',
'ENDLOCAL',
'EXIT /b %errorlevel%',
].join("\n");
if (bossman.weight == 0) {
console.log(`Opening [${aliasFolder}] folder in 'zen'...`);
zen([]);
} else if (bossman.has('-a', '--all')) {
exec("dir /b \"" + aliasFolder + "\"", (err, stdout, stderr) => {
if (err) {
console.log(stderr);
exit(err);
} else console.log(stdout);
});
} else if (bossman.has("-h", "--help", "-?")) {
printHelp();
} else if (bossman.weight >= 1) {
let items = [];
for (let i = 0; i < bossman.weight; i++) {
let alias = bossman.must.cabinet(i);
if (!fs.existsSync(aliasFolder + alias + ".cmd")) {
fs.writeFileSync(aliasFolder + alias + ".cmd", cmdContents);
fs.writeFileSync(aliasFolder + "js\\" + alias + ".js", `console.log("Hello, ${alias}");`);
}
items.push("js\\" + alias + ".js");
}
zen(items);
}
function zen(items) {
items = (items || []).map(i => aliasFolder + i);
items = [aliasFolder].concat(items).map(i => i.replace(" ", "\" \""));
// console.table(items); // debugging
spawn(`"${notepadCommand}"`, items, {
detached: true,
stdio: "ignore",
shell: true
}).unref();
}
function printHelp() {
let out = [
"",
" Saves aliases to the registered alias folder!",
"",
" Usage:",
" - alias",
" Opens the alias folder in zen mode.",
"",
" - alias {name}",
" Creates two files: name.cmd and js\\name.js. The cmd file contains",
" default text to execute the js file in the Node runtime. The js file",
" contains a 'Hello, name' console command used as a starting point for",
" the js program to function. Zen mode is subsequently opened to the",
" alias folder and the js file.",
"",
" - alias -a",
" - alias --all",
" Lists all alias files in the alias directory.",
"",
" - alias -h",
" - alias -?",
" - alias --help",
" Shows this help text.",
"",
" To change the location of the alias folder, set the 'aliasFolder' variable.",
" The current location of the alias folder is:",
" " + aliasFolder,
""
];
out.forEach(o => console.log(o));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment