Generate-passwords variants:

image

Create a ~/.generatepasswordrc file to change defaults:

length=12
numbers=true
symbols=true

See source: https://gist.github.com/abernier/3dcf17422b23e151c2f60db874494233

// Generate password
import "@johnlindquist/kit";
import { generate, generateMultiple, GenerateOptions } from "generate-password";
import rc from "rc";
import omit from "lodash.omit";
import { passwordStrength } from "check-password-strength";
const config = rc("generatepassword", { length: 10 }); // create a ~/.generate-passwordrc file (see: https://www.npmjs.com/package/rc#standards)
const flags = {
copy: {
name: "copy",
shortcut: "cmd+c",
},
};
function formatVariant(o) {
if (Object.keys(o).length === 0) o = config;
const arr = [];
Object.keys(o).forEach((k) => {
if (o[k] === true) {
arr.push(`--${k}`);
} else if (o[k] === false) {
arr.push(`--no-${k}`);
}
});
return arr.join(" ");
}
function sortedObj(o) {
return Object.fromEntries(Object.entries(o).sort());
}
const chosenPass = await arg(
{
placeholder: (config.length && String(config.length)) || undefined,
description: `Length`,
flags,
},
(input) => {
const baseOpts = { ...config };
const length = (input && Number(input)) || undefined;
if (length) baseOpts.length = length;
const variants = [
{},
{ numbers: true, symbols: true },
{
numbers: true,
symbols: true,
excludeSimilarCharacters: true,
},
{ numbers: true, symbols: true, lowercase: false },
{ numbers: true, symbols: true, uppercase: false },
{ numbers: true },
{ symbols: true },
{ lowercase: false },
{ uppercase: false },
];
return variants.map((variant) => {
const opts = { ...baseOpts, ...variant };
const newPass = generate(opts);
const description =
Object.keys(variant).length === 0
? `from config: ${config.config}`
: formatVariant(variant);
return {
name: `${newPass}`,
description,
preview() {
return md(`${passwordStrength(newPass).value}
\`\`\`json
${JSON.stringify(omit(sortedObj(opts), "_", "configs", "config"), null, 4)}
\`\`\`
see [available options](https://www.npmjs.com/package/generate-password#available-options)
`);
},
value: newPass,
};
});
}
);
if (flag?.copy) {
copy(chosenPass);
} else {
setSelectedText(chosenPass);
}