create-nextjs-storybook
Version:
Install embedded Storybook route into a NextJS project
54 lines (53 loc) • 1.73 kB
JavaScript
/* eslint-disable no-param-reassign */
import chalk from 'chalk';
const logger = console;
export const commandLog = (message) => {
process.stdout.write(chalk.cyan(' • ') + message);
// Need `void` to be able to use this function in a then of a Promise<void>
return (errorMessage, errorInfo) => {
if (errorMessage) {
process.stdout.write(`. ${chalk.red('✖')}\n`);
logger.error(`\n ${chalk.red(errorMessage)}`);
if (!errorInfo) {
return;
}
const newErrorInfo = errorInfo
.split('\n')
.map((line) => ` ${chalk.dim(line)}`)
.join('\n');
logger.error(`${newErrorInfo}\n`);
return;
}
process.stdout.write(`. ${chalk.green('✓')}\n`);
};
};
export function paddedLog(message) {
const newMessage = message
.split('\n')
.map((line) => ` ${line}`)
.join('\n');
logger.log(newMessage);
}
export function getChars(char, amount) {
let line = '';
for (let lc = 0; lc < amount; lc += 1) {
line += char;
}
return line;
}
export function codeLog(codeLines, leftPadAmount) {
let maxLength = 0;
const newLines = codeLines.map((line) => {
maxLength = line.length > maxLength ? line.length : maxLength;
return line;
});
const finalResult = newLines
.map((line) => {
const rightPadAmount = maxLength - line.length;
let newLine = line + getChars(' ', rightPadAmount);
newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `);
return newLine;
})
.join('\n');
logger.log(finalResult);
}