Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yimysty committed Jun 18, 2022
1 parent 7c74d60 commit 0af6e78
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 58 deletions.
46 changes: 23 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14527,7 +14527,7 @@ exports.debug = debug; // for test
/***/ }),

/***/ 8395:
/***/ ((__unused_webpack_module, __unused_webpack_exports, __nccwpck_require__) => {
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const fs = __nccwpck_require__(7147)
const espree = __nccwpck_require__(6910)
Expand Down Expand Up @@ -14595,6 +14595,7 @@ class ConfigParser {
}

parse() {
console.log(`original configuration:\n${this.config}`)
const ast = espree.parse(this.config, espreeOptions);

// Find the default export declaration node
Expand All @@ -14621,6 +14622,9 @@ class ConfigParser {
throw "Unknown config type"
}
}
console.log(`parsed configuration:\n${this.config}`)
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
return this.config
}

getPropertyModuleExport(exportNode) {
Expand All @@ -14632,11 +14636,11 @@ class ConfigParser {

console.log("Unable to find property, insert it : " + this.staticSiteConfig.pathName)
if (exportNode.expression.right.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.expression.right.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.expression.right.properties[0].range[0])
console.log("new config = \n" + newConfig)
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])
console.log("new config = \n" + this.config)
} else {
const newConfig = this.config.slice(0, exportNode.expression.right.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.expression.right.range[1] - 1)
console.log("new config = \n" + newConfig)
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)
console.log("new config = \n" + this.config)
}
}
return propertyNode
Expand All @@ -14651,11 +14655,11 @@ class ConfigParser {

console.log("Unable to find property, insert it " + this.staticSiteConfig.pathName)
if (exportNode.declaration.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + this.config)
} else {
const newConfig = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
console.log("new config = \n" + this.config)
}
}

Expand All @@ -14667,24 +14671,20 @@ class ConfigParser {
if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
var baseNode = propertyNode.value.properties.find(node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.subPathName)//'base')
if (baseNode) {
console.log("base node = " + JSON.stringify(baseNode.value))

// Swap the base value by a hardcoded string and print it
const newConfig = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
}
}
}

parseNextGatsby(pathNode) {
if (pathNode) {
console.log("base node = " + JSON.stringify(pathNode.value))
const newConfig = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
}
}
}

module.exports = {ConfigParser}

/***/ }),

Expand Down Expand Up @@ -14781,7 +14781,7 @@ async function getPagesBaseUrl({ repositoryNwo, githubToken, staticSiteGenerator

const siteUrl = new URL(pageObject.html_url)
if ( staticSiteGenerator ) {
setPagesPath({staticSiteGenerator, baseUrl: siteUrl.origin})
setPagesPath({staticSiteGenerator, path: siteUrl.pathname})
}

core.setOutput('base_url', siteUrl.href)
Expand All @@ -14804,9 +14804,9 @@ module.exports = getPagesBaseUrl

const core = __nccwpck_require__(2186)
const axios = __nccwpck_require__(6545)
const ConfigParser = __nccwpck_require__(8395)
const { ConfigParser } = __nccwpck_require__(8395)

async function setPagesPath({staticSiteGenerator, baseUrl}) {
async function setPagesPath({staticSiteGenerator, path}) {
try {
switch(staticSiteGenerator)
{
Expand All @@ -14816,31 +14816,31 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: baseUrl
newPath: path
}
break;
case 'next':
var ssConfig = {
filePath:"./next.config.js",
type: "next",
pathName: "basePath",
newPath: baseUrl
newPath: path
}
break;
case 'gatsby':
var ssConfig = {
filePath: "./gatsby-config.js",
type: "gatsby",
pathName: "pathPrefix",
newPath: baseUrl
newPath: path
}
break;
default:
throw "Unknown config type"
}

let configParser = new ConfigParser(ssConfig)
if (configParser.config) configParser.parse()
configParser.parse()

} catch (error) {
core.error('Set pages path in the static site generator config failed', error)
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
router: {
base: '/amazing-new-repo/'
}
}
31 changes: 16 additions & 15 deletions src/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ConfigParser {
}

parse() {
console.log(`original configuration:\n${this.config}`)
const ast = espree.parse(this.config, espreeOptions);

// Find the default export declaration node
Expand All @@ -90,6 +91,9 @@ class ConfigParser {
throw "Unknown config type"
}
}
console.log(`parsed configuration:\n${this.config}`)
fs.writeFileSync(this.staticSiteConfig.filePath, this.config)
return this.config
}

getPropertyModuleExport(exportNode) {
Expand All @@ -101,11 +105,11 @@ class ConfigParser {

console.log("Unable to find property, insert it : " + this.staticSiteConfig.pathName)
if (exportNode.expression.right.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.expression.right.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.expression.right.properties[0].range[0])
console.log("new config = \n" + newConfig)
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])
console.log("new config = \n" + this.config)
} else {
const newConfig = this.config.slice(0, exportNode.expression.right.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.expression.right.range[1] - 1)
console.log("new config = \n" + newConfig)
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)
console.log("new config = \n" + this.config)
}
}
return propertyNode
Expand All @@ -120,11 +124,11 @@ class ConfigParser {

console.log("Unable to find property, insert it " + this.staticSiteConfig.pathName)
if (exportNode.declaration.properties.length > 0) {
const newConfig = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0])
console.log("new config = \n" + this.config)
} else {
const newConfig = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1)
console.log("new config = \n" + this.config)
}
}

Expand All @@ -136,20 +140,17 @@ class ConfigParser {
if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
var baseNode = propertyNode.value.properties.find(node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.subPathName)//'base')
if (baseNode) {
console.log("base node = " + JSON.stringify(baseNode.value))

// Swap the base value by a hardcoded string and print it
const newConfig = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1])
}
}
}

parseNextGatsby(pathNode) {
if (pathNode) {
console.log("base node = " + JSON.stringify(pathNode.value))
const newConfig = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
console.log("new config = \n" + newConfig)
this.config = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1])
}
}
}

module.exports = {ConfigParser}
93 changes: 81 additions & 12 deletions src/config-parser.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,83 @@
const ConfigParser = require('./config-parser')
const { ConfigParser } = require('./config-parser')
const fs = require("fs")
const assert = require('assert')

const srcFolder = `src/fixtures`
const tmpFolder = `src/fixtures/tmp`
const expectedFolder = `src/fixtures/expected`

const repoPath = "/amazing-new-repo/"

const cases = [
["next.config.js", {
filePath: `${tmpFolder}/next.config.js`,
type: "next",
pathName: "basePath",
newPath: repoPath
}],
["next.config.old.js", {
filePath: `${tmpFolder}/next.config.old.js`,
type: "next",
pathName: "basePath",
newPath: repoPath
}],
["next.config.old.missing.js", {
filePath: `${tmpFolder}/next.config.old.missing.js`,
type: "next",
pathName: "basePath",
newPath: repoPath
}],
["gatsby-config.js", {
filePath: `${tmpFolder}/gatsby-config.js`,
type: "gatsby",
pathName: "pathPrefix",
newPath: repoPath
}],
["gatsby-config.old.js", {
filePath: `${tmpFolder}/gatsby-config.old.js`,
type: "gatsby",
pathName: "pathPrefix",
newPath: repoPath
}],
["nuxt.config.js", {
filePath:`${tmpFolder}/nuxt.config.js`,
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: repoPath
}],
["nuxt.config.missing.js", {
filePath:`${tmpFolder}/nuxt.config.missing.js`,
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: repoPath
}],
["nuxt.config.old.js", {
filePath:`${tmpFolder}/nuxt.config.old.js`,
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: repoPath
}],
];

describe('configParser', () => {
it('set nextjs url correctly', async () => {
// parser = new ConfigParser({
// filePath:"./nuxt.config.js",
// type: "nuxt",
// pathName: "router",
// subPathName: "base",
// newPath: baseUrl
// })
// parser.generateConfigFile()
})
})
test.each(cases)(
"%p parsed correctly",
(fileName, configuration) => {
srcFileName = `${srcFolder}/${fileName}`
tmpFileName = `${tmpFolder}/${fileName}`
expectedFileName = `${expectedFolder}/${fileName}`

fs.copyFileSync(srcFileName, tmpFileName)
const parser = new ConfigParser(configuration)
parser.parse()

var expectedContent = fs.readFileSync(expectedFileName).toString()
var actualContent = fs.readFileSync(tmpFileName).toString()
assert.equal(actualContent, expectedContent)
fs.rmSync(tmpFileName)
}
)
})
8 changes: 8 additions & 0 deletions src/fixtures/expected/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { resolve } from 'path'

export default {
alias: {
'style': resolve(__dirname, './assets/style')
},
pathPrefix: '/amazing-new-repo/',/* test */
}
3 changes: 3 additions & 0 deletions src/fixtures/expected/gatsby-config.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
pathPrefix: '/amazing-new-repo/'
}
9 changes: 9 additions & 0 deletions src/fixtures/expected/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import { resolve } from 'path'

export default {
alias: {
'style': resolve(__dirname, './assets/style')
},
basePath: '/amazing-new-repo/'/* test */,
}
4 changes: 4 additions & 0 deletions src/fixtures/expected/next.config.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports={
basePath: '/amazing-new-repo/'/* test */,
}

4 changes: 4 additions & 0 deletions src/fixtures/expected/next.config.old.missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports={
basePath: '/amazing-new-repo/'
}

11 changes: 11 additions & 0 deletions src/fixtures/expected/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { resolve } from 'path'

export default {
alias: {
'style': resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: '/amazing-new-repo/'
}
}
11 changes: 11 additions & 0 deletions src/fixtures/expected/nuxt.config.missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { resolve } from 'path'

export default {
router: {
base: '/amazing-new-repo/'
},
alias: {
'style': resolve(__dirname, './assets/style')
},
target: 'static'
}
10 changes: 10 additions & 0 deletions src/fixtures/expected/nuxt.config.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

module.exports={
alias: {
'style': resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: '/amazing-new-repo/'
}
}
Loading

0 comments on commit 0af6e78

Please sign in to comment.