Skip to content

Commit

Permalink
Add prettier configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoann Chaudet committed Jul 18, 2022
1 parent bfe36bc commit 61e591d
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 168 deletions.
10 changes: 10 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Prettier (formatter) configuration
---
printWidth: 80
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
trailingComma: none
bracketSpacing: false
arrowParens: avoid
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "configure-pages",
"version": "1.0.0",
"description": "",
"description": "An action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.",
"main": "src/index.js",
"scripts": {
"prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt",
Expand Down
136 changes: 93 additions & 43 deletions src/config-parser.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
}

Expand All @@ -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}`)
Expand All @@ -98,37 +110,65 @@ 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
}

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)
}
}

Expand All @@ -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}
Loading

0 comments on commit 61e591d

Please sign in to comment.