Skip to content

Commit

Permalink
Refactored CLI commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsJohnnyGrid committed Feb 19, 2025
1 parent a661fa2 commit 1e8555e
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
/* eslint-disable no-console */

import fs, { promises } from "fs";
import fs, { promises } from "node:fs";
import { styleText } from "node:util";
import { cwd } from "node:process";
import path from "path";
import path from "node:path";

// Get the command-line arguments
const args = process.argv.slice(2);
Expand All @@ -28,10 +29,12 @@ function copyStorybookPreset(source, target, { consoles = true } = {}) {

fs.renameSync(target, renamedTarget);

coloredConsole(
const warnMessage = styleText(
"yellow",
`Current Storybook preset backed into : '${path.basename(renamedTarget)}' folder. Don't forget to remove it before commit.`,
);

console.log(warnMessage);
}

fs.mkdirSync(target, { recursive: true });
Expand All @@ -49,10 +52,12 @@ function copyStorybookPreset(source, target, { consoles = true } = {}) {
});

if (consoles) {
coloredConsole(
const successMessage = styleText(
"green",
`Storybook preset successfully saved into '${path.basename(target)}' folder.`,
);

console.log(successMessage);
}
}

Expand Down Expand Up @@ -107,28 +112,3 @@ function parseArgs(args) {

return result;
}

function coloredConsole(color, message) {
let coloredMessage = message;

if (color === "green") {
coloredMessage = `\x1b[32m${message}\x1b[0m`;
}

if (color === "yellow") {
coloredMessage = `\x1b[33m${message}\x1b[0m`;
}

if (color === "red") {
coloredMessage = `\x1b[31m${message}\x1b[0m`;
}

/* Remove spaces and tabs around each line. */
coloredMessage
.trim()
.split("\n")
.map((line) => line.trim())
.join("\n");

return console.log(coloredMessage);
}

0 comments on commit 1e8555e

Please sign in to comment.