Skip to content

Commit 41949a7

Browse files
committed
Docs - Add sandbox compilation script to new website
1 parent 70aa142 commit 41949a7

File tree

6 files changed

+138
-7
lines changed

6 files changed

+138
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
export function cleanExistingSandboxDist(
5+
contentPath: string,
6+
sandboxFilesDist: string
7+
): void {
8+
fs.readdirSync(contentPath, { withFileTypes: true }).forEach((entry) => {
9+
if (!entry.isDirectory()) return
10+
emptySandboxFilesDist(path.join(contentPath, entry.name), sandboxFilesDist)
11+
})
12+
}
13+
14+
function emptySandboxFilesDist(
15+
dirPath: string,
16+
sandboxFilesDist: string
17+
): void {
18+
fs.readdirSync(dirPath, { withFileTypes: true }).forEach((entry) => {
19+
if (!entry.isDirectory()) return
20+
21+
const entryPath = path.join(dirPath, entry.name)
22+
23+
if (entry.name === sandboxFilesDist) {
24+
fs.rmSync(entryPath, { recursive: true, force: true })
25+
fs.mkdirSync(entryPath)
26+
return
27+
}
28+
29+
emptySandboxFilesDist(entryPath, sandboxFilesDist)
30+
})
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
export function collectSandboxDistPaths(
5+
contentPath: string,
6+
sandboxFilesDist: string,
7+
result: string[] = []
8+
): string[] {
9+
fs.readdirSync(contentPath, { withFileTypes: true }).forEach((entry) => {
10+
if (!entry.isDirectory()) return
11+
12+
const entryPath = path.join(contentPath, entry.name)
13+
14+
if (entry.name === sandboxFilesDist) {
15+
result.push(entryPath)
16+
return
17+
}
18+
19+
collectSandboxDistPaths(entryPath, sandboxFilesDist, result)
20+
})
21+
return result
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
export function copySandboxesFromSrcToDist(
5+
contentPath: string,
6+
sandboxFilesSrc: string,
7+
sandboxFilesDist: string
8+
): void {
9+
fs.readdirSync(contentPath, { withFileTypes: true }).forEach((entry) => {
10+
if (!entry.isDirectory()) return
11+
findAndCopySandboxFiles(
12+
path.join(contentPath, entry.name),
13+
sandboxFilesSrc,
14+
sandboxFilesDist
15+
)
16+
})
17+
}
18+
19+
function findAndCopySandboxFiles(
20+
dirPath: string,
21+
sandboxFilesSrc: string,
22+
sandboxFilesDist: string
23+
): void {
24+
fs.readdirSync(dirPath, { withFileTypes: true }).forEach((entry) => {
25+
if (!entry.isDirectory()) return
26+
27+
const entryPath = path.join(dirPath, entry.name)
28+
29+
if (entry.name === sandboxFilesSrc) {
30+
const distPath = path.join(dirPath, sandboxFilesDist)
31+
copyDirectory(entryPath, distPath)
32+
return
33+
}
34+
35+
findAndCopySandboxFiles(entryPath, sandboxFilesSrc, sandboxFilesDist)
36+
})
37+
}
38+
39+
function copyDirectory(srcPath: string, destPath: string): void {
40+
fs.mkdirSync(destPath, { recursive: true })
41+
42+
fs.readdirSync(srcPath, { withFileTypes: true }).forEach((entry) => {
43+
const srcEntry = path.join(srcPath, entry.name)
44+
const destEntry = path.join(destPath, entry.name)
45+
46+
if (entry.isDirectory()) {
47+
copyDirectory(srcEntry, destEntry)
48+
} else {
49+
fs.copyFileSync(srcEntry, destEntry)
50+
}
51+
})
52+
}

scripts/create-sandboxes/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,41 @@ import path from 'path'
33
import { tsCompile } from './ts-compile'
44
import { CONSOLE_FONT_COLORS } from '../utils/consoleFontColors'
55
import { readFiles } from '../utils/readFiles'
6+
import { copySandboxesFromSrcToDist } from './copy-from-src-to-dist'
7+
import { cleanExistingSandboxDist } from './clean-existing'
8+
import { collectSandboxDistPaths } from './collect-dist-paths'
9+
10+
const SANDBOX_FILES_CONTENT_PATH = path.join(
11+
process.cwd(),
12+
'website',
13+
'src',
14+
'content'
15+
)
16+
const SANDBOX_FILES_DIST_FOLDER_NAME = 'SandboxFilesDist'
17+
const SANDBOX_FILES_SRC_FOLDER_NAME = 'SandboxFilesSrc'
618

719
const EXTENSION_REGEX = {
820
DECLARATION: /\.d\.ts$/,
921
TSX: /\.tsx$/,
1022
TS: /\.ts$/
1123
}
1224

13-
const PATHS_TO_SANDBOX_FILES: string[] = [
14-
path.join(process.cwd(), 'src/components/Sandbox/Vanilla/SandboxFilesDist'),
15-
path.join(process.cwd(), 'src/components/Sandbox/React/SandboxFilesDist')
16-
]
17-
1825
function main(): void {
19-
PATHS_TO_SANDBOX_FILES.forEach((pathToSandboxFile) => {
26+
cleanExistingSandboxDist(
27+
SANDBOX_FILES_CONTENT_PATH,
28+
SANDBOX_FILES_DIST_FOLDER_NAME
29+
)
30+
copySandboxesFromSrcToDist(
31+
SANDBOX_FILES_CONTENT_PATH,
32+
SANDBOX_FILES_SRC_FOLDER_NAME,
33+
SANDBOX_FILES_DIST_FOLDER_NAME
34+
)
35+
const pathsToSandboxFiles = collectSandboxDistPaths(
36+
SANDBOX_FILES_CONTENT_PATH,
37+
SANDBOX_FILES_DIST_FOLDER_NAME
38+
)
39+
40+
pathsToSandboxFiles.forEach((pathToSandboxFile) => {
2041
try {
2142
readFiles(
2243
`${pathToSandboxFile}/`,

website/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
"dev": "next dev",
1818
"build": "next build",
1919
"start": "next start",
20-
"lint": "eslint"
20+
"lint": "eslint",
21+
"predeploy:sandboxfiles-build": "npx ts-node --project ../scripts/tsconfig.node.json ../scripts/create-sandboxes/index.ts",
22+
"predeploy:format": "yarn workspace embla-carousel-monorepo run format",
23+
"predeploy": "npm-run-all predeploy:sandboxfiles-build predeploy:format"
2124
},
2225
"dependencies": {
2326
"@mdx-js/loader": "^3.1.1",
@@ -57,6 +60,7 @@
5760
"@types/react-dom": "^19",
5861
"eslint": "^9",
5962
"eslint-config-next": "16.1.6",
63+
"npm-run-all": "^4.1.5",
6064
"type-fest": "^3.2.0",
6165
"typescript": "^5"
6266
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10439,6 +10439,7 @@ __metadata:
1043910439
hast: ^1.0.0
1044010440
hastscript: ^9.0.1
1044110441
next: 16.1.6
10442+
npm-run-all: ^4.1.5
1044210443
prism-react-renderer: ^1.3.5
1044310444
prismjs: 1.30.0
1044410445
raw-loader: ^4.0.2

0 commit comments

Comments
 (0)