-
Notifications
You must be signed in to change notification settings - Fork 6
/
sync-docs.ts
169 lines (148 loc) · 4.49 KB
/
sync-docs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import prompts from 'prompts';
const path = require('path');
import dotenv from 'dotenv';
import chalk from 'chalk';
import {
DocsVersion,
docsVersions,
latestVersion,
} from './packages/utils/src/docs-versions';
import * as fs from 'fs-extra';
dotenv.config();
(async () => {
const envVar = process.env.SB_DOCS_PATH;
let monorepoRelativePath = envVar || '../storybook';
if (!envVar) {
console.log(`✳︎ the env var ${chalk.cyan('SB_DOCS_PATH')} is not set.`);
const pathPrompt = await prompts({
type: 'text',
name: 'value',
message: 'Where is Storybook monorepo located?',
initial: '../storybook',
});
monorepoRelativePath = pathPrompt.value;
const saveToEnvFile = await prompts({
type: 'confirm',
name: 'value',
message: 'Would you like to save it in your .env file?',
initial: true,
});
// Save the path to the .env file
if (saveToEnvFile.value === true) {
fs.appendFileSync(
path.join(__dirname, './.env'),
`\nSB_DOCS_PATH=${monorepoRelativePath}\n`,
);
}
}
// Check if the directory exists in path
const pathToStorybookDocs = path.join(
__dirname,
`${monorepoRelativePath}/docs`,
);
const pathToStorybookSnippets = path.join(
__dirname,
`${monorepoRelativePath}/docs/_snippets`,
);
const pathToStorybookAssets = path.join(
__dirname,
`${monorepoRelativePath}/docs/_assets`,
);
const exists =
fs.existsSync(pathToStorybookDocs) ||
fs.existsSync(pathToStorybookSnippets) ||
fs.existsSync(pathToStorybookAssets);
if (!exists) {
console.log(`✳︎ ${chalk.cyan(pathToStorybookDocs)} does not exist.`);
process.exit(1);
}
function isLatest(version: DocsVersion) {
return version.id === latestVersion.id;
}
function getChoiceTitle(version: DocsVersion) {
let title = version.label;
if (version.preRelease) {
title = title.includes(')')
? title.replace(')', ', branched from next)')
: `${title} (branched from next)`;
}
if (isLatest(version)) {
title = title.includes(')')
? title.replace(')', ', branched from main)')
: `${title} (branched from main)`;
}
return title;
}
const versionPrompt = await prompts({
type: 'select',
name: 'version',
message: 'Pick a version to sync',
choices: docsVersions
.sort((a, b) => {
if (a.preRelease) return -1;
if (b.preRelease) return 1;
return 0;
})
.map((version) => ({
title: getChoiceTitle(version),
value: version,
})),
instructions: false,
});
const pathToLocalDocs = path.join(
'./apps/frontpage/content/docs',
versionPrompt.version.id,
);
const pathToLocalSnippets = path.join(
'./apps/frontpage/content/snippets',
versionPrompt.version.id,
);
const pathToLocalAssets = path.join(
'./apps/frontpage/public/docs-assets',
versionPrompt.version.id,
);
if (!fs.existsSync(pathToLocalDocs)) {
fs.mkdirSync(pathToLocalDocs);
}
if (!fs.existsSync(pathToLocalSnippets)) {
fs.mkdirSync(pathToLocalSnippets);
}
if (!fs.existsSync(pathToLocalAssets)) {
fs.mkdirSync(pathToLocalAssets);
}
console.log(
`\n✳︎ Syncing the docs from Storybook to your local ${versionPrompt.version.label} docs`,
);
console.log(`✳︎ Storybook path: ${pathToStorybookDocs}`);
console.log(`✳︎ Docs path: ./${pathToLocalDocs}`);
console.log(`\n✳︎ Now watching ...\n`);
fs.watch(pathToStorybookDocs, { recursive: true }, (_, filename) => {
console.log(`✳︎ ${filename} has been updated.`);
// Remove all files in the local directories
fs.rmSync(pathToLocalDocs, { recursive: true });
fs.rmSync(pathToLocalSnippets, { recursive: true });
fs.rmSync(pathToLocalAssets, { recursive: true });
// Copy the docs files only to the local directories
fs.cpSync(pathToStorybookDocs, pathToLocalDocs, {
recursive: true,
filter: (src) => {
if (
src.includes('.prettierignore') ||
src.includes('.prettierrc') ||
src.includes('_assets') ||
src.includes('_snippets')
)
return false;
return true;
},
});
// Copy all snippets to the local snippets directory
fs.cpSync(pathToStorybookSnippets, pathToLocalSnippets, {
recursive: true,
});
// Copy all assets to the local assets directory
fs.cpSync(pathToStorybookAssets, pathToLocalAssets, {
recursive: true,
});
});
})();