Skip to content

Commit

Permalink
wip: add formatter support to the output file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoann Chaudet committed Jul 18, 2022
1 parent e2bf7f2 commit 3efd613
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
/src/fixtures/tmp

# Editors
.vscode/
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
32 changes: 18 additions & 14 deletions src/config-parser.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand All @@ -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'
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions src/fixtures/expected/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -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 */
}
2 changes: 1 addition & 1 deletion src/fixtures/expected/gatsby-config.old.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
pathPrefix: '/amazing-new-repo/'
}
}
4 changes: 1 addition & 3 deletions src/fixtures/expected/next.config.old.missing.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
module.exports = {
basePath: '/amazing-new-repo/'
}
module.exports = {basePath: '/amazing-new-repo/'}
6 changes: 3 additions & 3 deletions src/fixtures/expected/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { resolve } from 'path'
import {resolve} from 'path'

export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: '/amazing-new-repo/'
}
}
}
12 changes: 6 additions & 6 deletions src/fixtures/expected/nuxt.config.missing.js
Original file line number Diff line number Diff line change
@@ -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')
base: '/amazing-new-repo/'
},
alias: {
style: resolve(__dirname, './assets/style')
},
target: 'static'
}
}
5 changes: 2 additions & 3 deletions src/fixtures/expected/nuxt.config.old.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

module.exports={
module.exports = {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
Expand Down
8 changes: 4 additions & 4 deletions src/fixtures/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -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 */
}
2 changes: 1 addition & 1 deletion src/fixtures/gatsby-config.old.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
pathPrefix: '/prefix'
}
}
6 changes: 3 additions & 3 deletions src/fixtures/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { resolve } from 'path'
import {resolve} from 'path'

export default {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: 'some/path'
}
}
}
6 changes: 3 additions & 3 deletions src/fixtures/nuxt.config.missing.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
5 changes: 2 additions & 3 deletions src/fixtures/nuxt.config.old.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

module.exports={
module.exports = {
alias: {
'style': resolve(__dirname, './assets/style')
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
Expand Down

0 comments on commit 3efd613

Please sign in to comment.