fix compling error

This commit is contained in:
yimysty
2022-06-17 15:13:00 -07:00
parent a90c52f126
commit 9a28a8d8ad
4 changed files with 1210 additions and 6818 deletions

7733
dist/index.js vendored

File diff suppressed because it is too large Load Diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,158 +1,157 @@
import * as fs from "fs"; const fs = require("fs")
import * as espree from "espree"; const espree = require("espree")
import format from "string-format" const format = require("string-format")
// Parse the AST // Parse the AST
const espreeOptions = { const espreeOptions = {
ecmaVersion: 6, ecmaVersion: 6,
sourceType: "module", sourceType: "module",
range: true, range: true,
} }
export default class ConfigParser { class ConfigParser {
constructor(staticSiteConfig) {
constructor(staticSiteConfig){ this.staticSiteConfig = staticSiteConfig
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()
this.validate() }
validate() {
if (!this.config) {
// Create the config file if it doesn't exist
fs.writeFile(this.staticSiteConfig.filePath, this.generateConfigFile(), (err) => {
// In case of a error throw err.
if (err) throw err;
})
}
}
pathPropertyNuxt = `router: {\n base: '{0}'\n }`
pathPropertyNext = `basePath: '{0}'`
pathPropertyGatsby = `pathPrefix: '{0}'`
configskeleton = `export default {\n {0}\n}`
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"
}
}
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"
}
}
parse() {
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"
var property = this.getPropertyExportDefault(exportNode)
} }
validate(){ if (property) {
if(!this.config){ switch (this.staticSiteConfig.type) {
// Create the config file if it doesn't exist case "nuxt":
fs.writeFile(this.staticSiteConfig.filePath, this.generateConfigFile(), (err) => { this.parseNuxt(property)
break
// In case of a error throw err. case "next":
if (err) throw err; case "gatsby":
}) this.parseNextGatsby(property)
} break
default:
throw "Unknown config type"
}
}
}
getPropertyModuleExport(exportNode) {
var propertyNode = exportNode.expression.right.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
)
if (!propertyNode) {
console.log("Unable to find property, insert it : " + this.staticSiteConfig.pathName)
if (exportNode.expression.right.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.expression.right.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.expression.right.properties[0].range[0])
console.log("new config = \n" + newConfig)
} else {
const newConfig = this.config.slice(0, exportNode.expression.right.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.expression.right.range[1] - 1)
console.log("new config = \n" + newConfig)
}
}
return propertyNode
}
getPropertyExportDefault(exportNode) {
var propertyNode = exportNode.declaration.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
)
if (!propertyNode) {
console.log("Unable to find property, insert it " + this.staticSiteConfig.pathName)
if (exportNode.declaration.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + newConfig)
} else {
const newConfig = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
console.log("new config = \n" + newConfig)
}
} }
pathPropertyNuxt = `router: {\n base: '{0}'\n }` return propertyNode
pathPropertyNext = `basePath: '{0}'` }
pathPropertyGatsby = `pathPrefix: '{0}'`
configskeleton = `export default {\n {0}\n}`
generateConfigFile(){ parseNuxt(propertyNode) {
switch(this.staticSiteConfig.type){ // Find the base node
case "nuxt": if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
return format(this.configskeleton, format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)) var baseNode = propertyNode.value.properties.find(node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.subPathName)//'base')
break if (baseNode) {
case "next": console.log("base node = " + JSON.stringify(baseNode.value))
return format(this.configskeleton, format(this.pathPropertyNext, this.staticSiteConfig.newPath))
break // Swap the base value by a hardcoded string and print it
case "gatsby": const newConfig = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
return format(this.configskeleton, format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)) console.log("new config = \n" + newConfig)
break }
default:
throw "Unknown config type"
}
} }
}
generateConfigProperty(){ parseNextGatsby(pathNode) {
switch(this.staticSiteConfig.type){ if (pathNode) {
case "nuxt": console.log("base node = " + JSON.stringify(pathNode.value))
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
break const newConfig = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
case "next": console.log("new config = \n" + newConfig)
return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
break
case "gatsby":
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
break
default:
throw "Unknown config type"
}
}
parse(){
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"
var property = this.getPropertyExportDefault(exportNode)
}
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"
}
}
}
getPropertyModuleExport(exportNode){
var propertyNode = exportNode.expression.right.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
)
if(!propertyNode){
console.log( "Unable to find property, insert it : " + this.staticSiteConfig.pathName)
if(exportNode.expression.right.properties.length > 0){
const newConfig = this.config.slice(0, exportNode.expression.right.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.expression.right.properties[0].range[0])
console.log("new config = \n" + newConfig)
} else {
const newConfig = this.config.slice(0, exportNode.expression.right.range[0]+1) + '\n '+ this.generateConfigProperty() + '\n' + this.config.slice(exportNode.expression.right.range[1]-1)
console.log("new config = \n" + newConfig)
}
}
return propertyNode
}
getPropertyExportDefault(exportNode){
var propertyNode = exportNode.declaration.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName
)
if(!propertyNode){
console.log( "Unable to find property, insert it " + this.staticSiteConfig.pathName)
if(exportNode.declaration.properties.length > 0){
const newConfig = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + newConfig)
} else {
const newConfig = this.config.slice(0, exportNode.declaration.range[0]+1) + '\n '+ this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1]-1)
console.log("new config = \n" + newConfig)
}
}
return propertyNode
}
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) {
console.log("base node = " + JSON.stringify(baseNode.value))
// Swap the base value by a hardcoded string and print it
const newConfig = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
console.log("new config = \n" + newConfig)
}
}
}
parseNextGatsby(pathNode){
if (pathNode) {
console.log("base node = " + JSON.stringify(pathNode.value))
const newConfig = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
console.log("new config = \n" + newConfig)
}
} }
}
} }

View File

@@ -1,6 +1,6 @@
const core = require('@actions/core') const core = require('@actions/core')
const axios = require('axios') const axios = require('axios')
import ConfigParser from './config-parser.js' const ConfigParser = require('./config-parser')
async function setPagesPath({staticSiteGenerator, baseUrl}) { async function setPagesPath({staticSiteGenerator, baseUrl}) {
try { try {
@@ -19,7 +19,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
var ssConfig = { var ssConfig = {
filePath:"./next.config.js", filePath:"./next.config.js",
type: "next", type: "next",
pathName: "basePath", pathName: "basePath",
newPath: baseUrl newPath: baseUrl
} }
break; break;
@@ -36,7 +36,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
} }
let configParser = new ConfigParser(ssConfig) let configParser = new ConfigParser(ssConfig)
if (configParser.config) configParser.parse() if (configParser.config) configParser.parse()
} catch (error) { } catch (error) {
core.error('Set pages path in the static site generator config failed', error) core.error('Set pages path in the static site generator config failed', error)