Parse the static sit config, inject pages url

This commit is contained in:
Smitha Borkar
2022-06-16 14:28:43 -07:00
parent dabdcba737
commit 351ece72e7
10 changed files with 14221 additions and 9 deletions

158
src/config-parser.js Normal file
View File

@@ -0,0 +1,158 @@
import * as fs from "fs";
import * as espree from "espree";
import format from "string-format"
// Parse the AST
const espreeOptions = {
ecmaVersion: 6,
sourceType: "module",
range: true,
}
export default class ConfigParser {
constructor(staticSiteConfig){
this.staticSiteConfig = staticSiteConfig
this.config = fs.existsSync(this.staticSiteConfig.filePath) ? fs.readFileSync(this.staticSiteConfig.filePath, "utf8") : null
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)
}
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

@@ -4,7 +4,8 @@ const core = require('@actions/core')
function getRequiredVars() {
return {
repositoryNwo: process.env.GITHUB_REPOSITORY,
githubToken: core.getInput('token')
githubToken: core.getInput('token'),
staticSiteGenerator: core.getInput('static_site_generator')
}
}

View File

@@ -1,7 +1,8 @@
const core = require('@actions/core')
const axios = require('axios')
const setPagesPath = require('./set-pages-path')
async function getPagesBaseUrl({ repositoryNwo, githubToken}) {
async function getPagesBaseUrl({ repositoryNwo, githubToken, staticSiteGenerator}) {
try {
const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages`
@@ -20,6 +21,10 @@ async function getPagesBaseUrl({ repositoryNwo, githubToken}) {
core.info(JSON.stringify(pageObject))
const siteUrl = new URL(pageObject.html_url)
if ( staticSiteGenerator ) {
setPagesPath({staticSiteGenerator, baseUrl: siteUrl.origin})
}
core.setOutput('base_url', siteUrl.href)
core.setOutput('origin', siteUrl.origin)
core.setOutput('host', siteUrl.host)

47
src/set-pages-path.js Normal file
View File

@@ -0,0 +1,47 @@
const core = require('@actions/core')
const axios = require('axios')
import ConfigParser from './config-parser.js'
async function setPagesPath({staticSiteGenerator, baseUrl}) {
try {
switch(staticSiteGenerator)
{
case 'nuxt':
var ssConfig = {
filePath:"./nuxt.config.js",
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: baseUrl
}
break;
case 'next':
var ssConfig = {
filePath:"./next.config.js",
type: "next",
pathName: "basePath",
newPath: baseUrl
}
break;
case 'gatsby':
var ssConfig = {
filePath: "./gatsby-config.js",
type: "gatsby",
pathName: "pathPrefix",
newPath: baseUrl
}
break;
default:
throw "Unknown config type"
}
let configParser = new ConfigParser(ssConfig)
if (configParser.config) configParser.parse()
} catch (error) {
core.error('Set pages path in the static site generator config failed', error)
throw error
}
}
module.exports = setPagesPath