@tokens-studio/sdk
Version:
The official SDK for Tokens Studio
75 lines (74 loc) • 3.03 kB
JavaScript
/* eslint-disable no-console */
import readlineSync from 'node:readline';
import readline from 'node:readline/promises';
import chalk from 'chalk';
import { createHelpOverview } from './create-help-overview.js';
import { setup } from './setup/setup.js';
import { pull } from './pull/index.js';
import { error } from './utils/messages.js';
import { getKey, setKey } from './utils/api-key.js';
import { commands } from './args-spec.js';
async function askForKey() {
if (!getKey()) {
const warningPrefix = 'You did not pass an API key in the environment variables';
if (process.env.CI) {
throw new Error(`${warningPrefix}. Since we are in a CI environment: exiting.`);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
/**
* When the user fills out their API key in the STDIN through the prompt,
* we want to obfuscate that input as to prevent that other folks can see
* the user's password. For security reasons..
*/
// @ts-expect-error rl.input property definitely exists but the Node type definition is missing it
rl.input.on('keypress', function () {
// get the number of characters entered so far:
const len = rl.line.length;
// move cursor back to the beginning of the input:
// @ts-expect-error rl.output property definitely exists but the Node type definition is missing it
readlineSync.moveCursor(rl.output, -len, 0);
// clear everything to the right of the cursor:
// @ts-expect-error rl.output property definitely exists but the Node type definition is missing it
readlineSync.clearLine(rl.output, 1);
// replace the original input with asterisks:
for (let i = 0; i < len; i++) {
// @ts-expect-error rl.output property definitely exists but the Node type definition is missing it
rl.output.write('●');
}
});
const key = await rl.question(`${warningPrefix}, but you can paste one here.
You can create an API key in Studio user settings by navigating to a project dashboard
and clicking the bottom left menu -> API keys.
${chalk.magentaBright(`\tAPI key`)}: `);
rl.close();
setKey(key);
}
}
export async function runCommand(command, args) {
if (args['--help'] || command === '') {
console.log(createHelpOverview(command));
return;
}
if (!commands.includes(command)) {
const errMessage = `Command ${command} not recognized, use --help for more info.`;
error(errMessage);
return;
}
switch (command) {
case 'setup': {
await askForKey();
setup(args);
break;
}
case 'pull': {
await askForKey();
pull(args);
break;
}
default:
}
}
//# sourceMappingURL=run-command.js.map