add test cases

This commit is contained in:
yimysty
2022-06-17 18:36:26 -07:00
parent 7c74d60067
commit 0af6e78e1a
15 changed files with 193 additions and 58 deletions

46
dist/index.js vendored
View File

@@ -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)
@@ -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
@@ -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) {
@@ -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
@@ -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)
}
}
@@ -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}
/***/ }),
@@ -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)
@@ -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)
{
@@ -14816,7 +14816,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: baseUrl
newPath: path
}
break;
case 'next':
@@ -14824,7 +14824,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
filePath:"./next.config.js",
type: "next",
pathName: "basePath",
newPath: baseUrl
newPath: path
}
break;
case 'gatsby':
@@ -14832,7 +14832,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
filePath: "./gatsby-config.js",
type: "gatsby",
pathName: "pathPrefix",
newPath: baseUrl
newPath: path
}
break;
default:
@@ -14840,7 +14840,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
}
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)

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

5
nuxt.config.js Normal file
View File

@@ -0,0 +1,5 @@
export default {
router: {
base: '/amazing-new-repo/'
}
}

View File

@@ -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
@@ -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) {
@@ -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
@@ -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)
}
}
@@ -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}

View File

@@ -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)
}
)
})

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,4 @@
module.exports={
basePath: '/amazing-new-repo/'/* test */,
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -22,7 +22,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)

View File

@@ -1,8 +1,8 @@
const core = require('@actions/core')
const axios = require('axios')
const ConfigParser = require('./config-parser')
const { ConfigParser } = require('./config-parser')
async function setPagesPath({staticSiteGenerator, baseUrl}) {
async function setPagesPath({staticSiteGenerator, path}) {
try {
switch(staticSiteGenerator)
{
@@ -12,7 +12,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
type: "nuxt",
pathName: "router",
subPathName: "base",
newPath: baseUrl
newPath: path
}
break;
case 'next':
@@ -20,7 +20,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
filePath:"./next.config.js",
type: "next",
pathName: "basePath",
newPath: baseUrl
newPath: path
}
break;
case 'gatsby':
@@ -28,7 +28,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
filePath: "./gatsby-config.js",
type: "gatsby",
pathName: "pathPrefix",
newPath: baseUrl
newPath: path
}
break;
default:
@@ -36,7 +36,7 @@ async function setPagesPath({staticSiteGenerator, baseUrl}) {
}
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)