/
import-options-schema.js
executable file
·93 lines (70 loc) · 2.34 KB
/
import-options-schema.js
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
#!/usr/bin/env node
const path = require("path")
const fs = require("fs-extra")
const pluginPath= process.argv[2]
const Joi = require("gatsby-plugin-utils")
async function run() {
if(!pluginPath) {
console.error("Please pass a path to the plugin directory")
return
}
const rootDir = path.resolve(pluginPath)
if(!fs.existsSync(rootDir)) {
console.error(`The plugin directory ${rootDir} does not exist`)
return
}
const stat = await fs.stat(rootDir)
if(!stat.isDirectory()) {
console.error(`The plugin path ${rootDir} is not a directory`)
return
}
let pluginName
try {
const { name } = require(path.resolve(rootDir, "package.json"))
if(!name) {
console.error("Plugin package.json does not have a name field")
return
}
pluginName = name
} catch (e) {
console.error("Could not open package.json. Are you sure the plugin directory is correct?")
return
}
const gatsbyNodePath = path.resolve(rootDir, "gatsby-node.js")
if(!fs.existsSync(gatsbyNodePath)) {
console.error(`Could not find gatsby-node.js in ${gatsbyNodePath}. Are you sure this is a plugin directory?`)
return
}
let pluginOptionsSchema
try {
const gatsbyNode = require(gatsbyNodePath)
pluginOptionsSchema = gatsbyNode.pluginOptionsSchema
} catch(e) {
console.error(`Could not load gatsby-node.js. You may need to build the plugin first.`)
console.log("Error was:", e.message)
return
}
if(!pluginOptionsSchema) {
console.error("The plugin does not include a pluginOptionsSchema")
return
}
let optionsSchema
try {
const schema = pluginOptionsSchema({ Joi })
optionsSchema = schema.describe()
} catch (e) {
console.error("Failed to generate schema")
console.error(e.message)
return
}
const schemataPath = path.resolve(__dirname, "..", "src", "plugin-schemas.json")
if(!fs.existsSync(schemataPath)) {
console.error("Could not find output file")
return
}
const json = await fs.readJSON(schemataPath)
json[pluginName] = optionsSchema
console.log(`Writing "${pluginName} to schemataPath`)
await fs.writeJSON(schemataPath, json)
}
run()