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/ node_modules/
/src/fixtures/tmp
# Editors # Editors
.vscode/ .vscode/

20
package-lock.json generated
View File

@@ -13,6 +13,7 @@
"axios": "^0.27.2", "axios": "^0.27.2",
"axios-retry": "^3.2.5", "axios-retry": "^3.2.5",
"espree": "^9.3.2", "espree": "^9.3.2",
"prettier": "^2.7.1",
"string-format": "^1.0.0" "string-format": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
@@ -3041,6 +3042,20 @@
"node": ">=8" "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": { "node_modules/pretty-format": {
"version": "28.1.1", "version": "28.1.1",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",
@@ -5878,6 +5893,11 @@
"find-up": "^4.0.0" "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": { "pretty-format": {
"version": "28.1.1", "version": "28.1.1",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.1.tgz",

View File

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

View File

@@ -1,6 +1,7 @@
const fs = require('fs') const fs = require('fs')
const espree = require('espree') const espree = require('espree')
const format = require('string-format') const format = require('string-format')
const prettier = require('prettier')
const core = require('@actions/core') const core = require('@actions/core')
// Parse the AST // Parse the AST
@@ -12,10 +13,10 @@ const espreeOptions = {
class ConfigParser { class ConfigParser {
constructor(staticSiteConfig) { constructor(staticSiteConfig) {
this.pathPropertyNuxt = `router: {\n base: '{0}'\n }` this.pathPropertyNuxt = `router: { base: '{0}' }`
this.pathPropertyNext = `basePath: '{0}'` this.pathPropertyNext = `basePath: '{0}'`
this.pathPropertyGatsby = `pathPrefix: '{0}'` this.pathPropertyGatsby = `pathPrefix: '{0}'`
this.configskeleton = `export default {\n {0}\n}` this.configskeleton = `export default { {0} }`
this.staticSiteConfig = staticSiteConfig this.staticSiteConfig = staticSiteConfig
this.config = fs.existsSync(this.staticSiteConfig.filePath) this.config = fs.existsSync(this.staticSiteConfig.filePath)
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8') ? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
@@ -40,19 +41,16 @@ class ConfigParser {
this.configskeleton, this.configskeleton,
format(this.pathPropertyNuxt, this.staticSiteConfig.newPath) format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
) )
break
case 'next': case 'next':
return format( return format(
this.configskeleton, this.configskeleton,
format(this.pathPropertyNext, this.staticSiteConfig.newPath) format(this.pathPropertyNext, this.staticSiteConfig.newPath)
) )
break
case 'gatsby': case 'gatsby':
return format( return format(
this.configskeleton, this.configskeleton,
format(this.pathPropertyGatsby, this.staticSiteConfig.newPath) format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
) )
break
default: default:
throw 'Unknown config type' throw 'Unknown config type'
} }
@@ -62,20 +60,20 @@ class ConfigParser {
switch (this.staticSiteConfig.type) { switch (this.staticSiteConfig.type) {
case 'nuxt': case 'nuxt':
return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath) return format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
break
case 'next': case 'next':
return format(this.pathPropertyNext, this.staticSiteConfig.newPath) return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
break
case 'gatsby': case 'gatsby':
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath) return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
break
default: default:
throw 'Unknown config type' throw 'Unknown config type'
} }
} }
parse() { parse() {
// Print current configuration
core.info(`original configuration:\n${this.config}`) core.info(`original configuration:\n${this.config}`)
// Parse the AST
const ast = espree.parse(this.config, espreeOptions) const ast = espree.parse(this.config, espreeOptions)
// Find the default export declaration node // Find the default export declaration node
@@ -103,8 +101,18 @@ class ConfigParser {
throw 'Unknown config type' throw 'Unknown config type'
} }
} }
// Write down the updated configuration
core.info(`parsed configuration:\n${this.config}`) core.info(`parsed configuration:\n${this.config}`)
fs.writeFileSync(this.staticSiteConfig.filePath, 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 return this.config
} }
@@ -127,15 +135,13 @@ class ConfigParser {
exportNode.expression.right.properties[0].range[0] exportNode.expression.right.properties[0].range[0]
) + ) +
this.generateConfigProperty() + this.generateConfigProperty() +
',\n' + ',' +
this.config.slice(exportNode.expression.right.properties[0].range[0]) this.config.slice(exportNode.expression.right.properties[0].range[0])
core.info('new config = \n' + this.config) core.info('new config = \n' + this.config)
} else { } else {
this.config = this.config =
this.config.slice(0, exportNode.expression.right.range[0] + 1) + this.config.slice(0, exportNode.expression.right.range[0] + 1) +
'\n ' +
this.generateConfigProperty() + this.generateConfigProperty() +
'\n' +
this.config.slice(exportNode.expression.right.range[1] - 1) this.config.slice(exportNode.expression.right.range[1] - 1)
core.info('new config = \n' + this.config) core.info('new config = \n' + this.config)
} }
@@ -158,15 +164,13 @@ class ConfigParser {
this.config = this.config =
this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.config.slice(0, exportNode.declaration.properties[0].range[0]) +
this.generateConfigProperty() + this.generateConfigProperty() +
',\n' + ',' +
this.config.slice(exportNode.declaration.properties[0].range[0]) this.config.slice(exportNode.declaration.properties[0].range[0])
core.info('new config = \n' + this.config) core.info('new config = \n' + this.config)
} else { } else {
this.config = this.config =
this.config.slice(0, exportNode.declaration.range[0] + 1) + this.config.slice(0, exportNode.declaration.range[0] + 1) +
'\n ' +
this.generateConfigProperty() + this.generateConfigProperty() +
'\n' +
this.config.slice(exportNode.declaration.range[1] - 1) this.config.slice(exportNode.declaration.range[1] - 1)
core.info('new config = \n' + this.config) core.info('new config = \n' + this.config)
} }

View File

@@ -2,7 +2,7 @@ import { resolve } from 'path'
export default { export default {
alias: { 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 = { module.exports = {basePath: '/amazing-new-repo/'}
basePath: '/amazing-new-repo/'
}

View File

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

View File

@@ -5,7 +5,7 @@ export default {
base: '/amazing-new-repo/' base: '/amazing-new-repo/'
}, },
alias: { alias: {
'style': resolve(__dirname, './assets/style') style: resolve(__dirname, './assets/style')
}, },
target: 'static' target: 'static'
} }

View File

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

View File

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

View File

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

View File

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

View File

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