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.).
- 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
- Clone this repo:
$ git clone https://github.com/moma-lab/passgen-cli
$ npm install
$ 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.
- fs - https://nodejs.dev/learn/the-nodejs-fs-module
a module that enables interacting with the local file system - path - https://nodejs.dev/learn/the-nodejs-path-module
a module that provides a lot of very useful functionality to access and interact with the file system - os - https://nodejs.dev/learn/the-nodejs-os-module
a module that provides many functions that you can use to retrieve information from the underlying operating system and the computer the program runs on, and interact with it
- Commander - https://github.com/tj/commander.js
a package to easily work with node.js command-line interfaces - Chalk - https://github.com/chalk/chalk
a package to style command line outputs (strings) - Clipboardy - https://github.com/sindresorhus/clipboardy
a cross-platform package to access the system clipboard (copy/paste)
-
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 ...
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 creates a symlink so we can call "passgen" everywhere in our system
This approach works great unless you want to use ECMAScript modules in your scripts.
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.
- Traversy Media Video: Build a Node.js Password Generator
- Traversy Media Code: Code at GitHub
Created with ♥ by moma.dev.lab
Feel free to submit a pull request. Help is always appreciated!
- Please make sure to follow my Contribution Guideline.
- This Code of Conduct applies to all contributions (pull requests, issues, comments, etc).
Thank you!
Published under the MIT Licence.