Add prettier configuration
This commit is contained in:
+93
-43
@@ -1,13 +1,13 @@
|
||||
const fs = require("fs")
|
||||
const espree = require("espree")
|
||||
const format = require("string-format")
|
||||
const fs = require('fs')
|
||||
const espree = require('espree')
|
||||
const format = require('string-format')
|
||||
const core = require('@actions/core')
|
||||
|
||||
// Parse the AST
|
||||
const espreeOptions = {
|
||||
ecmaVersion: 6,
|
||||
sourceType: "module",
|
||||
range: true,
|
||||
sourceType: 'module',
|
||||
range: true
|
||||
}
|
||||
|
||||
class ConfigParser {
|
||||
@@ -17,7 +17,9 @@ class ConfigParser {
|
||||
this.pathPropertyGatsby = `pathPrefix: '{0}'`
|
||||
this.configskeleton = `export default {\n {0}\n}`
|
||||
this.staticSiteConfig = staticSiteConfig
|
||||
this.config = fs.existsSync(this.staticSiteConfig.filePath) ? fs.readFileSync(this.staticSiteConfig.filePath, "utf8") : null
|
||||
this.config = fs.existsSync(this.staticSiteConfig.filePath)
|
||||
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
|
||||
: null
|
||||
this.validate()
|
||||
}
|
||||
|
||||
@@ -33,62 +35,72 @@ class ConfigParser {
|
||||
|
||||
generateConfigFile() {
|
||||
switch (this.staticSiteConfig.type) {
|
||||
case "nuxt":
|
||||
return format(this.configskeleton, format(this.pathPropertyNuxt, this.staticSiteConfig.newPath))
|
||||
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))
|
||||
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))
|
||||
case 'gatsby':
|
||||
return format(
|
||||
this.configskeleton,
|
||||
format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
||||
)
|
||||
break
|
||||
default:
|
||||
throw "Unknown config type"
|
||||
throw 'Unknown config type'
|
||||
}
|
||||
}
|
||||
|
||||
generateConfigProperty() {
|
||||
switch (this.staticSiteConfig.type) {
|
||||
case "nuxt":
|
||||
case 'nuxt':
|
||||
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
|
||||
break
|
||||
case "next":
|
||||
case 'next':
|
||||
return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
|
||||
break
|
||||
case "gatsby":
|
||||
case 'gatsby':
|
||||
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
|
||||
break
|
||||
default:
|
||||
throw "Unknown config type"
|
||||
throw 'Unknown config type'
|
||||
}
|
||||
}
|
||||
|
||||
parse() {
|
||||
core.info(`original configuration:\n${this.config}`)
|
||||
const ast = espree.parse(this.config, espreeOptions);
|
||||
const ast = espree.parse(this.config, espreeOptions)
|
||||
|
||||
// Find the default export declaration node
|
||||
var exportNode = ast.body.find(node => node.type === 'ExpressionStatement')
|
||||
if (exportNode) {
|
||||
var property = this.getPropertyModuleExport(exportNode)
|
||||
|
||||
} else {
|
||||
exportNode = ast.body.find(node => node.type === 'ExportDefaultDeclaration')
|
||||
if (!exportNode) throw "Unable to find default export"
|
||||
exportNode = ast.body.find(
|
||||
node => node.type === 'ExportDefaultDeclaration'
|
||||
)
|
||||
if (!exportNode) throw 'Unable to find default export'
|
||||
var property = this.getPropertyExportDefault(exportNode)
|
||||
}
|
||||
|
||||
if (property) {
|
||||
switch (this.staticSiteConfig.type) {
|
||||
case "nuxt":
|
||||
case 'nuxt':
|
||||
this.parseNuxt(property)
|
||||
break
|
||||
case "next":
|
||||
case "gatsby":
|
||||
case 'next':
|
||||
case 'gatsby':
|
||||
this.parseNextGatsby(property)
|
||||
break
|
||||
default:
|
||||
throw "Unknown config type"
|
||||
throw 'Unknown config type'
|
||||
}
|
||||
}
|
||||
core.info(`parsed configuration:\n${this.config}`)
|
||||
@@ -98,18 +110,34 @@ class ConfigParser {
|
||||
|
||||
getPropertyModuleExport(exportNode) {
|
||||
var propertyNode = exportNode.expression.right.properties.find(
|
||||
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
|
||||
node =>
|
||||
node.key.type === 'Identifier' &&
|
||||
node.key.name === this.staticSiteConfig.pathName
|
||||
)
|
||||
|
||||
if (!propertyNode) {
|
||||
|
||||
core.info("Unable to find property, insert it : " + this.staticSiteConfig.pathName)
|
||||
core.info(
|
||||
'Unable to find property, insert it : ' +
|
||||
this.staticSiteConfig.pathName
|
||||
)
|
||||
if (exportNode.expression.right.properties.length > 0) {
|
||||
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])
|
||||
core.info("new config = \n" + this.config)
|
||||
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])
|
||||
core.info('new config = \n' + this.config)
|
||||
} else {
|
||||
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)
|
||||
core.info("new config = \n" + this.config)
|
||||
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)
|
||||
core.info('new config = \n' + this.config)
|
||||
}
|
||||
}
|
||||
return propertyNode
|
||||
@@ -117,18 +145,30 @@ class ConfigParser {
|
||||
|
||||
getPropertyExportDefault(exportNode) {
|
||||
var propertyNode = exportNode.declaration.properties.find(
|
||||
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
|
||||
node =>
|
||||
node.key.type === 'Identifier' &&
|
||||
node.key.name === this.staticSiteConfig.pathName
|
||||
)
|
||||
|
||||
if (!propertyNode) {
|
||||
|
||||
core.info("Unable to find property, insert it " + this.staticSiteConfig.pathName)
|
||||
core.info(
|
||||
'Unable to find property, insert it ' + this.staticSiteConfig.pathName
|
||||
)
|
||||
if (exportNode.declaration.properties.length > 0) {
|
||||
this.config = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
|
||||
core.info("new config = \n" + this.config)
|
||||
this.config =
|
||||
this.config.slice(0, exportNode.declaration.properties[0].range[0]) +
|
||||
this.generateConfigProperty() +
|
||||
',\n' +
|
||||
this.config.slice(exportNode.declaration.properties[0].range[0])
|
||||
core.info('new config = \n' + this.config)
|
||||
} else {
|
||||
this.config = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
|
||||
core.info("new config = \n" + this.config)
|
||||
this.config =
|
||||
this.config.slice(0, exportNode.declaration.range[0] + 1) +
|
||||
'\n ' +
|
||||
this.generateConfigProperty() +
|
||||
'\n' +
|
||||
this.config.slice(exportNode.declaration.range[1] - 1)
|
||||
core.info('new config = \n' + this.config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,19 +178,29 @@ class ConfigParser {
|
||||
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')
|
||||
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
|
||||
this.config = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
|
||||
this.config =
|
||||
this.config.slice(0, baseNode.value.range[0]) +
|
||||
`'${this.staticSiteConfig.newPath}'` +
|
||||
this.config.slice(baseNode.value.range[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parseNextGatsby(pathNode) {
|
||||
if (pathNode) {
|
||||
this.config = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
|
||||
this.config =
|
||||
this.config.slice(0, pathNode.value.range[0]) +
|
||||
`'${this.staticSiteConfig.newPath}'` +
|
||||
this.config.slice(pathNode.value.range[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {ConfigParser}
|
||||
module.exports = {ConfigParser}
|
||||
|
||||
Reference in New Issue
Block a user