2022-06-17 15:13:00 -07:00
|
|
|
const fs = require("fs")
|
|
|
|
|
const espree = require("espree")
|
|
|
|
|
const format = require("string-format")
|
2022-06-17 18:58:11 -07:00
|
|
|
const core = require('@actions/core')
|
2022-06-16 14:28:43 -07:00
|
|
|
|
|
|
|
|
// Parse the AST
|
|
|
|
|
const espreeOptions = {
|
2022-06-17 15:13:00 -07:00
|
|
|
ecmaVersion: 6,
|
|
|
|
|
sourceType: "module",
|
|
|
|
|
range: true,
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
class ConfigParser {
|
|
|
|
|
constructor(staticSiteConfig) {
|
2022-06-17 15:56:00 -07:00
|
|
|
this.pathPropertyNuxt = `router: {\n base: '{0}'\n }`
|
|
|
|
|
this.pathPropertyNext = `basePath: '{0}'`
|
|
|
|
|
this.pathPropertyGatsby = `pathPrefix: '{0}'`
|
|
|
|
|
this.configskeleton = `export default {\n {0}\n}`
|
2022-06-17 15:13:00 -07:00
|
|
|
this.staticSiteConfig = staticSiteConfig
|
|
|
|
|
this.config = fs.existsSync(this.staticSiteConfig.filePath) ? fs.readFileSync(this.staticSiteConfig.filePath, "utf8") : null
|
|
|
|
|
this.validate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate() {
|
|
|
|
|
if (!this.config) {
|
2022-06-29 16:10:51 -05:00
|
|
|
core.info(`original raw configuration was empty:\n${this.config}`)
|
2022-06-29 16:07:12 -05:00
|
|
|
core.info('Generating a default configuration to start from...')
|
2022-06-17 15:13:00 -07:00
|
|
|
|
2022-06-29 15:56:54 -05:00
|
|
|
// Update the `config` property with a default configuration file
|
|
|
|
|
this.config = this.generateConfigFile()
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateConfigFile() {
|
|
|
|
|
switch (this.staticSiteConfig.type) {
|
|
|
|
|
case "nuxt":
|
|
|
|
|
return format(this.configskeleton, format(this.pathPropertyNuxt, this.staticSiteConfig.newPath))
|
|
|
|
|
break
|
|
|
|
|
case "next":
|
|
|
|
|
return format(this.configskeleton, format(this.pathPropertyNext, this.staticSiteConfig.newPath))
|
|
|
|
|
break
|
|
|
|
|
case "gatsby":
|
|
|
|
|
return format(this.configskeleton, format(this.pathPropertyGatsby, this.staticSiteConfig.newPath))
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw "Unknown config type"
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateConfigProperty() {
|
|
|
|
|
switch (this.staticSiteConfig.type) {
|
|
|
|
|
case "nuxt":
|
|
|
|
|
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
|
|
|
|
break
|
|
|
|
|
case "next":
|
|
|
|
|
return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
|
|
|
|
break
|
|
|
|
|
case "gatsby":
|
|
|
|
|
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw "Unknown config type"
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
parse() {
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info(`original configuration:\n${this.config}`)
|
2022-06-17 15:13:00 -07:00
|
|
|
const ast = espree.parse(this.config, espreeOptions);
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
// Find the default export declaration node
|
|
|
|
|
var exportNode = ast.body.find(node => node.type === 'ExpressionStatement')
|
|
|
|
|
if (exportNode) {
|
|
|
|
|
var property = this.getPropertyModuleExport(exportNode)
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
} else {
|
|
|
|
|
exportNode = ast.body.find(node => node.type === 'ExportDefaultDeclaration')
|
|
|
|
|
if (!exportNode) throw "Unable to find default export"
|
|
|
|
|
var property = this.getPropertyExportDefault(exportNode)
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
if (property) {
|
|
|
|
|
switch (this.staticSiteConfig.type) {
|
|
|
|
|
case "nuxt":
|
|
|
|
|
this.parseNuxt(property)
|
|
|
|
|
break
|
|
|
|
|
case "next":
|
|
|
|
|
case "gatsby":
|
|
|
|
|
this.parseNextGatsby(property)
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
throw "Unknown config type"
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info(`parsed configuration:\n${this.config}`)
|
2022-06-17 18:36:26 -07:00
|
|
|
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
|
|
|
|
|
return this.config
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPropertyModuleExport(exportNode) {
|
|
|
|
|
var propertyNode = exportNode.expression.right.properties.find(
|
|
|
|
|
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (!propertyNode) {
|
|
|
|
|
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("Unable to find property, insert it : " + this.staticSiteConfig.pathName)
|
2022-06-17 15:13:00 -07:00
|
|
|
if (exportNode.expression.right.properties.length > 0) {
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, exportNode.expression.right.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.expression.right.properties[0].range[0])
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("new config = \n" + this.config)
|
2022-06-17 15:13:00 -07:00
|
|
|
} else {
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, exportNode.expression.right.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.expression.right.range[1] - 1)
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("new config = \n" + this.config)
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return propertyNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPropertyExportDefault(exportNode) {
|
|
|
|
|
var propertyNode = exportNode.declaration.properties.find(
|
|
|
|
|
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (!propertyNode) {
|
|
|
|
|
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("Unable to find property, insert it " + this.staticSiteConfig.pathName)
|
2022-06-17 15:13:00 -07:00
|
|
|
if (exportNode.declaration.properties.length > 0) {
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("new config = \n" + this.config)
|
2022-06-17 15:13:00 -07:00
|
|
|
} else {
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
|
2022-06-17 18:58:11 -07:00
|
|
|
core.info("new config = \n" + this.config)
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
return propertyNode
|
|
|
|
|
}
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
parseNuxt(propertyNode) {
|
|
|
|
|
// Find the base node
|
|
|
|
|
if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
|
|
|
|
|
var baseNode = propertyNode.value.properties.find(node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.subPathName)//'base')
|
|
|
|
|
if (baseNode) {
|
|
|
|
|
// Swap the base value by a hardcoded string and print it
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
2022-06-16 14:28:43 -07:00
|
|
|
|
2022-06-17 15:13:00 -07:00
|
|
|
parseNextGatsby(pathNode) {
|
|
|
|
|
if (pathNode) {
|
2022-06-17 18:36:26 -07:00
|
|
|
this.config = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
|
2022-06-16 14:28:43 -07:00
|
|
|
}
|
2022-06-17 15:13:00 -07:00
|
|
|
}
|
2022-06-17 15:56:00 -07:00
|
|
|
}
|
2022-06-17 18:36:26 -07:00
|
|
|
|
|
|
|
|
module.exports = {ConfigParser}
|