93 lines
2.2 KiB
JavaScript
93 lines
2.2 KiB
JavaScript
export const SITES = {
|
|
TWITTER: 'twitter',
|
|
BLUESKY: 'bluesky',
|
|
};
|
|
|
|
let argRegex = new RegExp('(\-\-[a-zA-Z0-9]+)(=)(.*)');
|
|
|
|
/**
|
|
* Parses args from node process
|
|
* @returns { {[ key: string ]: string | string[]} } parsed args array;
|
|
* if the arg followed format `--arg=val`, it will be indexed as `args[arg]=val`;
|
|
* otherwise it will be pushed to an array under `args['_']`
|
|
*/
|
|
const parseArgs = () => {
|
|
let _args = { '_': [] };
|
|
[...process.argv.slice(2)].forEach(arg => {
|
|
let result = argRegex.exec(arg);
|
|
if (!!result) {
|
|
_args[result[1].replace('--','')] = result[3];
|
|
} else {
|
|
_args['_'].push(arg);
|
|
}
|
|
});
|
|
|
|
return _args;
|
|
}
|
|
|
|
export const args = parseArgs();
|
|
|
|
/**
|
|
* Gets value of specified arg
|
|
* @param {string} argName the name of the arg to retrieve
|
|
* @returns {string} value of specified arg
|
|
* @throws will throw error if arg was not specified
|
|
*/
|
|
export const getArg = (argName) => {
|
|
let arg = args[argName];
|
|
if (!!arg) {
|
|
return arg;
|
|
} else {
|
|
throw(`${argName} was not supplied!`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determines if the provided arg is a supported site option and maps it to the name used throughout the repo
|
|
* @param {string} arg the name of the arg to validate as a site option
|
|
* @returns {string} the name of the site used throughout the repo
|
|
* @throws general error if site isn't supported
|
|
*/
|
|
export const validateSiteArg = (arg) => {
|
|
if (!arg) {
|
|
throw('site arg was not supplied!');
|
|
}
|
|
|
|
switch (arg.toLowerCase().trim()) {
|
|
case 'bsky':
|
|
case 'bluesky': {
|
|
return SITES.BLUESKY;
|
|
}
|
|
case 'twitter':
|
|
case 'twr':
|
|
case 'x':
|
|
case 'xitter':
|
|
case 'X, The Everything App (TM)':
|
|
case 'shitter': {
|
|
return SITES.TWITTER;
|
|
}
|
|
default: {
|
|
throw(`${arg} is not a supported site`);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Grabs the url of the site specified
|
|
* @param {string} site the site to get the url from
|
|
* @param {string} user the user to retrieve
|
|
* @returns {string} the url of the site
|
|
*/
|
|
export const getSiteUrl = (site, user) => {
|
|
switch (site) {
|
|
case SITES.BLUESKY: {
|
|
return `https://bsky.app/profile/${user}`;
|
|
}
|
|
case SITES.TWITTER: {
|
|
return `https://twitter.com/${user}/media`;
|
|
}
|
|
default: {
|
|
throw(`${site} was not valid`);
|
|
}
|
|
}
|
|
};
|