/
createPlaygrounds.ts
76 lines (62 loc) · 2.24 KB
/
createPlaygrounds.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
const path = require(`path`)
const fs = require(`fs`)
const { green } = require("chalk")
import { NodePluginArgs, CreatePagesArgs } from "gatsby"
import { addPathToSite } from "../pathsOnSiteTracker"
import { isMultiLingual } from "./languageFilter"
export const createPlaygrounds = async (
graphql: CreatePagesArgs["graphql"],
createPage: NodePluginArgs["actions"]["createPage"]
) => {
console.log(`${green("success")} Creating Playground Pages`)
const playPage = path.resolve(`./src/templates/play.tsx`)
const result = await graphql(`
query GetAllPlaygroundLocalizations {
allFile(
filter: {
sourceInstanceName: { eq: "playground-examples" }
ext: { eq: ".json" }
}
) {
nodes {
name
}
}
}
`)
if (result.errors) {
throw result.errors
}
const anyData = result.data as any
const docs = anyData.allFile.nodes
docs.forEach(lang => {
if (!isMultiLingual && lang !== "en") return
const appRoot = path.join(__dirname, "..", "..", "..", "..")
const examplesForLang = path.join(appRoot, "playground-examples", "generated", lang.name + ".json")
const examplesTOC = JSON.parse(fs.readFileSync(examplesForLang, "utf8"))
const playgroundHandbookTOCPath = path.join(appRoot, "playground-handbook", "output", "play-handbook.json")
const playgroundHandbookTOC = JSON.parse(fs.readFileSync(playgroundHandbookTOCPath, "utf8"))
// prettier-ignore
const compilerOptsForLang = path.join(appRoot, "tsconfig-reference", "output", lang.name + "-summary.json")
// prettier-ignore
const compilerOptsForLangFallback = path.join(appRoot, "tsconfig-reference", "output", "en-summary.json")
const hasOptsForLang = fs.existsSync(compilerOptsForLang)
const optionsPath = hasOptsForLang
? compilerOptsForLang
: compilerOptsForLangFallback
const optionsSummary = JSON.parse(fs.readFileSync(optionsPath, "utf8"))
.options
const pathName = lang.name === "en" ? "/play" : `/${lang.name}/play`
addPathToSite(pathName)
createPage({
path: pathName,
component: playPage,
context: {
lang: lang.name,
examplesTOC,
optionsSummary,
playgroundHandbookTOC
},
})
})
}