diff --git a/.gitignore b/.gitignore index f3cac7d..79dae07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ +/src/fixtures/tmp # Editors .vscode/ diff --git a/package-lock.json b/package-lock.json index 827005d..a8974f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 52ddded..91824aa 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/config-parser.js b/src/config-parser.js index 5ae53d8..58f89e5 100644 --- a/src/config-parser.js +++ b/src/config-parser.js @@ -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) } diff --git a/src/fixtures/expected/gatsby-config.js b/src/fixtures/expected/gatsby-config.js index f00007b..dc69c4c 100644 --- a/src/fixtures/expected/gatsby-config.js +++ b/src/fixtures/expected/gatsby-config.js @@ -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 */ -} \ No newline at end of file + pathPrefix: '/amazing-new-repo/' /* test */ +} diff --git a/src/fixtures/expected/gatsby-config.old.js b/src/fixtures/expected/gatsby-config.old.js index 00b28db..931c175 100644 --- a/src/fixtures/expected/gatsby-config.old.js +++ b/src/fixtures/expected/gatsby-config.old.js @@ -1,3 +1,3 @@ module.exports = { pathPrefix: '/amazing-new-repo/' -} \ No newline at end of file +} diff --git a/src/fixtures/expected/next.config.old.missing.js b/src/fixtures/expected/next.config.old.missing.js index 139c3ef..793b836 100644 --- a/src/fixtures/expected/next.config.old.missing.js +++ b/src/fixtures/expected/next.config.old.missing.js @@ -1,3 +1 @@ -module.exports = { - basePath: '/amazing-new-repo/' -} +module.exports = {basePath: '/amazing-new-repo/'} diff --git a/src/fixtures/expected/nuxt.config.js b/src/fixtures/expected/nuxt.config.js index 53c5851..6148319 100644 --- a/src/fixtures/expected/nuxt.config.js +++ b/src/fixtures/expected/nuxt.config.js @@ -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/' } -} \ No newline at end of file +} diff --git a/src/fixtures/expected/nuxt.config.missing.js b/src/fixtures/expected/nuxt.config.missing.js index 88b33da..4658555 100644 --- a/src/fixtures/expected/nuxt.config.missing.js +++ b/src/fixtures/expected/nuxt.config.missing.js @@ -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' -} \ No newline at end of file +} diff --git a/src/fixtures/expected/nuxt.config.old.js b/src/fixtures/expected/nuxt.config.old.js index 67e0dcb..3033c56 100644 --- a/src/fixtures/expected/nuxt.config.old.js +++ b/src/fixtures/expected/nuxt.config.old.js @@ -1,7 +1,6 @@ - -module.exports={ +module.exports = { alias: { - 'style': resolve(__dirname, './assets/style') + style: resolve(__dirname, './assets/style') }, target: 'static', router: { diff --git a/src/fixtures/gatsby-config.js b/src/fixtures/gatsby-config.js index 18a4e4b..59728fc 100644 --- a/src/fixtures/gatsby-config.js +++ b/src/fixtures/gatsby-config.js @@ -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 */ -} \ No newline at end of file + pathPrefix: '/prefix' /* test */ +} diff --git a/src/fixtures/gatsby-config.old.js b/src/fixtures/gatsby-config.old.js index 6634ad6..f9cbe16 100644 --- a/src/fixtures/gatsby-config.old.js +++ b/src/fixtures/gatsby-config.old.js @@ -1,3 +1,3 @@ module.exports = { pathPrefix: '/prefix' -} \ No newline at end of file +} diff --git a/src/fixtures/nuxt.config.js b/src/fixtures/nuxt.config.js index 6985d46..b0856ff 100644 --- a/src/fixtures/nuxt.config.js +++ b/src/fixtures/nuxt.config.js @@ -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' } -} \ No newline at end of file +} diff --git a/src/fixtures/nuxt.config.missing.js b/src/fixtures/nuxt.config.missing.js index b2f9501..7529ebd 100644 --- a/src/fixtures/nuxt.config.missing.js +++ b/src/fixtures/nuxt.config.missing.js @@ -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' -} \ No newline at end of file +} diff --git a/src/fixtures/nuxt.config.old.js b/src/fixtures/nuxt.config.old.js index 347fa92..b61f4fa 100644 --- a/src/fixtures/nuxt.config.old.js +++ b/src/fixtures/nuxt.config.old.js @@ -1,7 +1,6 @@ - -module.exports={ +module.exports = { alias: { - 'style': resolve(__dirname, './assets/style') + style: resolve(__dirname, './assets/style') }, target: 'static', router: {