Skip to content

Instantly share code, notes, and snippets.

@kadiks
Created July 5, 2023 11:49
Show Gist options
  • Save kadiks/07f9ce16b4ae7468fe6874e847746cbe to your computer and use it in GitHub Desktop.
Save kadiks/07f9ce16b4ae7468fe6874e847746cbe to your computer and use it in GitHub Desktop.
Basic CLI
#!/usr/bin/env node
const { mkdirSync, writeFileSync } = require('node:fs');
const params = process.argv.slice(2);
if (params.length === 0) {
console.log('No arguments provided');
process.exit(1);
}
const dir = params[0];
mkdirSync(dir, { recursive: true });
writeFileSync(`${dir}/index.html`, '');
#!/bin/bash
mkdir $1
cd $1
touch index.html

Chose one or the other file according to your preference (Mac/Unix)

  • chmod +x <filename> => To make it executable
  • ./bash.js foo => will create a repository foo and a file foo/index.html
  • ./bash.sh bar => will create a repository bar and a file bar/index.html

Now it will run only in the current directory. If you want to run it globally on your computer

Use a name without extension:

  • mv cmd.js cmd or mv cmd.sh cmd
  • pwd => to get your current directory.
  • export PATH=$PATH:<pwd result>/cmd

Now in this terminal, you can always use your command like:

  • cmd baz => will create a repository baz and a file baz/index.html

But it will work only on this terminal, so use:

  • Move your cmd file in one of your main directories. e.g: ~/scripts folder
  • Open the ~/.bash_profile (depending on your terminal you might need to open one of this other file in the section Set your PATH permanently)
  • Add to the line export PATH (if not already there): export PATH=$PATH:/Users/<username>/scripts/cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment