Skip to content

moma-lab/passgen-cli

Repository files navigation

Node.js Password Generator CLI

A command line used generator for random passwords written in JavaScript (Node.js). The generated passwords are automatically copied to the clipboard to be able to instantly paste them into an input field where it's needed (like a registration form, a login, etc.).

Supported Features

  • determine custom password length (default length: 8 characters)
  • passwords are made up from numeric and alphanumeric characters (lowercase and uppercase) as well as symbols (utf-8 conform)
  • determine if numeric characters shall be used for the password (default: true)
  • determine if symbols shall be used for the password (default: true)
  • save password to a password file (default: false)
  • generated password is copied to clipboard for instant paste availability

Installation

  1. Clone this repo: $ git clone https://github.com/moma-lab/passgen-cli
  2. $ npm install

Usage

$ passgen <options>

Options:

Option Type Description
-V, --version output the version number
-l, --length <number> specify the password length (default: "8")
-s, --save save passwords to passwords.txt
-nn, --no-numbers exclude numbers from password
-ns, --no-symbols exclude symbols from password
-h, --help display help for command

Type in $ passgen -h on your command line to see a help with this options list.

Node.js Development Steps

Internal Node.js Modules Used

NPM Packages Used

Dev Outline

  • Create package.json file: $ npm init (given name: "passgen")

  • Install npm dependencies: $ npm i commander chalk clipboardy

  • Create the following files/folders in your project root directory:

    • index.js (main file)
    • utils/createPassword.js
    • utils/savePassword.js
    • utils/shuffle.js
  • ... code/implement everything needed ...

Run Node.js scripts as shell commands

AFTER finishing coding edit package.json:

  • Enter the following key-value pairs right under "main": "index.js"

    "preferGlobal": true,   // Designate a package as preferring global installation
    "bin": "./index.js",    // Map a command name to a local file name (=install an executable into the PATH and create a symlink using "npm link")

    or

    { "bin" : { "passgen" : "./index.js" } }
  • open index.js and add a shebang #!/usr/bin/env node at first line. That is required to be interpretable by the shell when $ passgen <options> is called from CLI.

  • type $ npm link on the command line

    • this creates a symlink so we can call "passgen" everywhere in our system
      (hint: npm unlink removes the created symlink)

This approach works great unless you want to use ECMAScript modules in your scripts.

Side Note: Execute ES Modules on the CLI

If you want to make ES modules executable on the command line you need to use some shell magic. Instead of using the well known shebang #!/usr/bin/env node or #!/usr/bin/node add the following code at the beginning of your ES module:

":" //#;exec /usr/bin/env node --input-type=module - "$@" < "$0"

import process from 'process';
const { argv } = process
console.log(argv)

Save your file as command.js and you can run zsh command.js on the shell!

Thanks to Bramus for his enlightening article on that topic.

If you want to dig deeper to find out how and why this is working, I suggest to have a look at this article from 2014.

Acknowlegements

Contributions Welcome

Created with ♥ by moma.dev.lab

Feel free to submit a pull request. Help is always appreciated!

Thank you!

License

Published under the MIT Licence.

About

A simple random password generator for command line use, written in Node.js.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published