ipcopy
Version:
Copy local/public IP-address into clipboard
57 lines (43 loc) • 1.49 kB
JavaScript
const clipboardy = require('clipboardy');
const chalk = require('chalk');
const inquirer = require('inquirer');
const getIP = require('./getip.js');
const ipcopy = async (copyMode, showChoices, ipServer) => {
const getPubIP = () => getIP.public(ipServer)
let copyIP = "";
if (!copyMode || copyMode !== "public") {
let ipArray = getIP.local();
let targetIP
if (copyMode === "all") ipArray.push(await getPubIP())
if (ipArray.length > 1 || !!showChoices) {
if (copyMode !== "local" && copyMode !== "all") ipArray.push("Public IP-address")
const choices = await inquirer.prompt([{
type: 'list',
message: 'Choose a IP-address to copy: ',
name: 'ip',
choices: ipArray
}])
if (choices && choices.ip) {
if (/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/.test(choices.ip)) {
targetIP = choices.ip
} else {
targetIP = await getPubIP();
}
}
} else if (ipArray.length == 1 && copyMode !== "local") {
console.log(`-- if you want to copy public IP directly, try ${chalk.green("pubip")}`)
}
copyIP = targetIP || ipArray[ipArray.length - 1]
} else {
copyIP = await getPubIP();
}
try {
await clipboardy.write(copyIP);
console.log(chalk.bold.cyan(`\n=== Copied to Pasteboard ===\n ${copyIP}\n`))
} catch (err) {
console.log(chalk.bold.red(`\n=== Copy Fail ===\n`))
}
process.exit();
}
module.exports = ipcopy;