@vueless/storybook 1.2.0 → 1.2.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/commands/index.js +5 -0
- package/bin/commands/init.js +86 -0
- package/bin/constants.js +3 -0
- package/bin/index.js +14 -78
- package/package.json +1 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { cwd } from "node:process";
|
|
5
|
+
import { styleText } from "node:util";
|
|
6
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
7
|
+
import { cpSync, renameSync, existsSync } from "node:fs";
|
|
8
|
+
|
|
9
|
+
import { detectTypeScript } from "vueless/utils/node/helper.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Initializes the Storybook configuration by determining the appropriate settings for TypeScript or JavaScript projects.
|
|
13
|
+
* It copies the corresponding Storybook preset directory and adds Storybook-related commands to the project.
|
|
14
|
+
* This function handles the setup required for running Storybook in the current project.
|
|
15
|
+
*
|
|
16
|
+
* @return {Promise<void>} A promise that resolves once the initialization process is complete.
|
|
17
|
+
*/
|
|
18
|
+
export async function storybookInit() {
|
|
19
|
+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
|
20
|
+
const sourceTs = path.join(__dirname, "..", ".storybook-ts");
|
|
21
|
+
const sourceJs = path.join(__dirname, "..", ".storybook-js");
|
|
22
|
+
const target = path.join(cwd(), ".storybook");
|
|
23
|
+
|
|
24
|
+
(await detectTypeScript())
|
|
25
|
+
? copyStorybookPreset(sourceTs, target)
|
|
26
|
+
: copyStorybookPreset(sourceJs, target);
|
|
27
|
+
|
|
28
|
+
await addStorybookCommands();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Copy Storybook preset to target directory.
|
|
33
|
+
* @param {string} source - Source directory.
|
|
34
|
+
* @param {string} target - Target directory.
|
|
35
|
+
* @param {Object} options - Options object.
|
|
36
|
+
* @param {boolean} options.consoles - Show console messages.
|
|
37
|
+
*/
|
|
38
|
+
function copyStorybookPreset(source, target, { consoles = true } = {}) {
|
|
39
|
+
if (existsSync(target)) {
|
|
40
|
+
const timestamp = Date.now();
|
|
41
|
+
const renamedTarget = `${target}-backup-${timestamp}`;
|
|
42
|
+
|
|
43
|
+
renameSync(target, renamedTarget);
|
|
44
|
+
|
|
45
|
+
if (consoles) {
|
|
46
|
+
console.log(
|
|
47
|
+
styleText(
|
|
48
|
+
"yellow",
|
|
49
|
+
`Current Storybook preset backed into: '${path.basename(renamedTarget)}'. Remove it before commit.`,
|
|
50
|
+
),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
cpSync(source, target, { recursive: true, force: true });
|
|
56
|
+
|
|
57
|
+
if (consoles) {
|
|
58
|
+
console.log(
|
|
59
|
+
styleText("green", `Storybook preset successfully saved into '${path.basename(target)}'.`),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Add Storybook commands to package.json.
|
|
66
|
+
*/
|
|
67
|
+
async function addStorybookCommands() {
|
|
68
|
+
try {
|
|
69
|
+
const storybookCommands = {
|
|
70
|
+
"sb:dev": "storybook dev -p 6006 --no-open",
|
|
71
|
+
"sb:dev:docs": "storybook dev -p 6006 --docs --no-open",
|
|
72
|
+
"sb:build": "storybook build --docs",
|
|
73
|
+
"sb:preview": "vite preview --host --outDir=storybook-static",
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const packageJsonPath = path.resolve(cwd(), "package.json");
|
|
77
|
+
const data = await readFile(packageJsonPath, "utf8");
|
|
78
|
+
const packageJson = JSON.parse(data);
|
|
79
|
+
|
|
80
|
+
packageJson.scripts = { ...packageJson.scripts, ...storybookCommands };
|
|
81
|
+
|
|
82
|
+
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n", "utf8");
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error("Error:", error);
|
|
85
|
+
}
|
|
86
|
+
}
|
package/bin/constants.js
ADDED
package/bin/index.js
CHANGED
|
@@ -1,89 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
|
|
2
3
|
/* eslint-disable no-console */
|
|
3
4
|
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { cwd } from "node:process";
|
|
6
5
|
import { styleText } from "node:util";
|
|
7
|
-
import fs, { promises } from "node:fs";
|
|
8
|
-
import { detectTypeScript } from "vueless/utils/node/helper.js";
|
|
9
|
-
|
|
10
|
-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
|
11
|
-
const sourceTs = path.join(__dirname, "..", ".storybook-ts");
|
|
12
|
-
const sourceJs = path.join(__dirname, "..", ".storybook-js");
|
|
13
|
-
const target = path.join(cwd(), ".storybook");
|
|
14
|
-
|
|
15
|
-
const hasTypeScript = await detectTypeScript();
|
|
16
|
-
|
|
17
|
-
hasTypeScript ? copyStorybookPreset(sourceTs, target) : copyStorybookPreset(sourceJs, target);
|
|
18
|
-
|
|
19
|
-
await addStorybookCommands();
|
|
20
6
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
* @param {string} source - Source directory.
|
|
24
|
-
* @param {string} target - Target directory.
|
|
25
|
-
* @param {Object} options - Options object.
|
|
26
|
-
* @param {boolean} options.consoles - Show console messages.
|
|
27
|
-
*/
|
|
28
|
-
function copyStorybookPreset(source, target, { consoles = true } = {}) {
|
|
29
|
-
if (fs.existsSync(target)) {
|
|
30
|
-
const timestamp = new Date().valueOf();
|
|
31
|
-
const renamedTarget = `${target}-backup-${timestamp}`;
|
|
7
|
+
import { commands } from "./commands/index.js";
|
|
8
|
+
import { DEFAULT_EXIT_CODE, FAILURE_CODE } from "./constants.js";
|
|
32
9
|
|
|
33
|
-
|
|
10
|
+
const [command, ...options] = process.argv.slice(2);
|
|
34
11
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
console.log(warnMessage);
|
|
12
|
+
try {
|
|
13
|
+
if (!command || command === "undefiend") {
|
|
14
|
+
process.exit(DEFAULT_EXIT_CODE);
|
|
41
15
|
}
|
|
42
16
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
files.forEach((file) => {
|
|
48
|
-
const srcFile = path.join(source, file);
|
|
49
|
-
const destFile = path.join(target, file);
|
|
50
|
-
const stat = fs.lstatSync(srcFile);
|
|
51
|
-
|
|
52
|
-
stat.isDirectory()
|
|
53
|
-
? copyStorybookPreset(srcFile, destFile, { consoles: false })
|
|
54
|
-
: fs.copyFileSync(srcFile, destFile);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
if (consoles) {
|
|
58
|
-
const successMessage = styleText(
|
|
59
|
-
"green",
|
|
60
|
-
`Storybook preset successfully saved into '${path.basename(target)}' folder.`,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
console.log(successMessage);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Add Storybook commands to package.json.
|
|
69
|
-
*/
|
|
70
|
-
async function addStorybookCommands() {
|
|
71
|
-
try {
|
|
72
|
-
const storybookCommands = {
|
|
73
|
-
"sb:dev": "storybook dev -p 6006 --no-open",
|
|
74
|
-
"sb:dev:docs": "storybook dev -p 6006 --docs --no-open",
|
|
75
|
-
"sb:build": "storybook build --docs",
|
|
76
|
-
"sb:preview": "vite preview --host --outDir=storybook-static",
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const packageJsonPath = path.resolve(cwd(), "package.json");
|
|
80
|
-
const data = await promises.readFile(packageJsonPath, "utf8");
|
|
81
|
-
const packageJson = JSON.parse(data);
|
|
82
|
-
|
|
83
|
-
packageJson.scripts = { ...packageJson.scripts, ...storybookCommands };
|
|
84
|
-
|
|
85
|
-
await promises.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n", "utf8");
|
|
86
|
-
} catch (error) {
|
|
87
|
-
console.error("Error:", error);
|
|
17
|
+
if (command in commands) {
|
|
18
|
+
commands[command](options);
|
|
19
|
+
} else {
|
|
20
|
+
throw new Error(styleText("red", `There is no such command: ${command}`));
|
|
88
21
|
}
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(styleText("red", error.message));
|
|
24
|
+
process.exit(error.code || FAILURE_CODE);
|
|
89
25
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vueless/storybook",
|
|
3
|
-
"version": "1.2.0",
|
|
3
|
+
"version": "1.2.1-beta.0",
|
|
4
4
|
"description": "Simplifies Storybook configuration for Vueless UI library.",
|
|
5
5
|
"author": "Johnny Grid <hello@vueless.com> (https://vueless.com)",
|
|
6
6
|
"homepage": "https://vueless.com",
|