wip: add formatter support to the output file

This commit is contained in:
Yoann Chaudet
2022-07-18 15:51:32 -07:00
parent e2bf7f26fb
commit 3efd613ed2
15 changed files with 70 additions and 48 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
node_modules/
/src/fixtures/tmp
# Editors
.vscode/

20
package-lock.json generated
View File

@@ -13,6 +13,7 @@
"axios": "^0.27.2",
"axios-retry": "^3.2.5",
"espree": "^9.3.2",
"prettier": "^2.7.1",
"string-format": "^1.0.0"
},
"devDependencies": {
@@ -3041,6 +3042,20 @@
"node": ">=8"
}
},
"node_modules/prettier": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz",
"integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==",
"bin": {
"prettier": "bin-prettier.js"
},
"engines": {
"node": ">=10.13.0"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/pretty-format": {
"version": "28.1.1",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
@@ -5878,6 +5893,11 @@
"find-up": "^4.0.0"
}
},
"prettier": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz",
"integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g=="
},
"pretty-format": {
"version": "28.1.1",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",

View File

@@ -22,6 +22,7 @@
"axios": "^0.27.2",
"axios-retry": "^3.2.5",
"espree": "^9.3.2",
"prettier": "^2.7.1",
"string-format": "^1.0.0"
},
"devDependencies": {

View File

@@ -1,6 +1,7 @@
const fs = require('fs')
const espree = require('espree')
const format = require('string-format')
const prettier = require('prettier')
const core = require('@actions/core')
// Parse the AST
@@ -12,10 +13,10 @@ const espreeOptions = {
class ConfigParser {
constructor(staticSiteConfig) {
this.pathPropertyNuxt = `router: {\n base: '{0}'\n }`
this.pathPropertyNuxt = `router: { base: '{0}' }`
this.pathPropertyNext = `basePath: '{0}'`
this.pathPropertyGatsby = `pathPrefix: '{0}'`
this.configskeleton = `export default {\n {0}\n}`
this.configskeleton = `export default { {0} }`
this.staticSiteConfig = staticSiteConfig
this.config = fs.existsSync(this.staticSiteConfig.filePath)
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
@@ -40,19 +41,16 @@ class ConfigParser {
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'
}
@@ -62,20 +60,20 @@ class ConfigParser {
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() {
// Print current configuration
core.info(`original configuration:\n${this.config}`)
// Parse the AST
const ast = espree.parse(this.config, espreeOptions)
// Find the default export declaration node
@@ -103,8 +101,18 @@ class ConfigParser {
throw 'Unknown config type'
}
}
// Write down the updated configuration
core.info(`parsed configuration:\n${this.config}`)
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
// Format the updated configuration with prettier's default settings
this.config = prettier.format(this.config, {
filePath: this.staticSiteConfig.filePath,
parser: 'babel' /* default ot javascript for when filePath is nil */
})
// Return the new configuration
return this.config
}
@@ -127,15 +135,13 @@ class ConfigParser {
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)
}
@@ -158,15 +164,13 @@ class ConfigParser {
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)
}

View File

@@ -1,8 +1,8 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
pathPrefix: '/amazing-new-repo/',/* test */
pathPrefix: '/amazing-new-repo/' /* test */
}

View File

@@ -1,3 +1 @@
module.exports = {
basePath: '/amazing-new-repo/'
}
module.exports = {basePath: '/amazing-new-repo/'}

View File

@@ -1,8 +1,8 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {

View File

@@ -1,11 +1,11 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
router: {
base: '/amazing-new-repo/'
},
alias: {
'style': resolve(__dirname, './assets/style')
alias: {
style: resolve(__dirname, './assets/style')
},
target: 'static'
}

View File

@@ -1,7 +1,6 @@
module.exports={
module.exports = {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {

View File

@@ -1,8 +1,8 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
pathPrefix: '/prefix',/* test */
pathPrefix: '/prefix' /* test */
}

View File

@@ -1,8 +1,8 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {

View File

@@ -1,8 +1,8 @@
import { resolve } from 'path'
import {resolve} from 'path'
export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static'
}

View File

@@ -1,7 +1,6 @@
module.exports={
module.exports = {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {