Add prettier configuration

This commit is contained in:
Yoann Chaudet
2022-07-18 15:06:14 -07:00
parent bfe36bc062
commit 61e591d3b5
10 changed files with 283 additions and 168 deletions

10
.prettierrc.yaml Normal file
View File

@@ -0,0 +1,10 @@
# Prettier (formatter) configuration
---
printWidth: 80
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
trailingComma: none
bracketSpacing: false
arrowParens: avoid

View File

@@ -1,7 +1,7 @@
{ {
"name": "configure-pages", "name": "configure-pages",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "An action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt", "prepare": "ncc build src/index.js -o dist --source-map --license licenses.txt",

View File

@@ -1,13 +1,13 @@
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 core = require('@actions/core') const core = require('@actions/core')
// Parse the AST // Parse the AST
const espreeOptions = { const espreeOptions = {
ecmaVersion: 6, ecmaVersion: 6,
sourceType: "module", sourceType: 'module',
range: true, range: true
} }
class ConfigParser { class ConfigParser {
@@ -17,7 +17,9 @@ class ConfigParser {
this.pathPropertyGatsby = `pathPrefix: '{0}'` this.pathPropertyGatsby = `pathPrefix: '{0}'`
this.configskeleton = `export default {\n {0}\n}` this.configskeleton = `export default {\n {0}\n}`
this.staticSiteConfig = staticSiteConfig this.staticSiteConfig = staticSiteConfig
this.config = fs.existsSync(this.staticSiteConfig.filePath) ? fs.readFileSync(this.staticSiteConfig.filePath, "utf8") : null this.config = fs.existsSync(this.staticSiteConfig.filePath)
? fs.readFileSync(this.staticSiteConfig.filePath, 'utf8')
: null
this.validate() this.validate()
} }
@@ -33,62 +35,72 @@ class ConfigParser {
generateConfigFile() { generateConfigFile() {
switch (this.staticSiteConfig.type) { switch (this.staticSiteConfig.type) {
case "nuxt": case 'nuxt':
return format(this.configskeleton, format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)) return format(
this.configskeleton,
format(this.pathPropertyNuxt, this.staticSiteConfig.newPath)
)
break break
case "next": case 'next':
return format(this.configskeleton, format(this.pathPropertyNext, this.staticSiteConfig.newPath)) return format(
this.configskeleton,
format(this.pathPropertyNext, this.staticSiteConfig.newPath)
)
break break
case "gatsby": case 'gatsby':
return format(this.configskeleton, format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)) return format(
this.configskeleton,
format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
)
break break
default: default:
throw "Unknown config type" throw 'Unknown config type'
} }
} }
generateConfigProperty() { generateConfigProperty() {
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 break
case "next": case 'next':
return format(this.pathPropertyNext, this.staticSiteConfig.newPath) return format(this.pathPropertyNext, this.staticSiteConfig.newPath)
break break
case "gatsby": case 'gatsby':
return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath) return format(this.pathPropertyGatsby, this.staticSiteConfig.newPath)
break break
default: default:
throw "Unknown config type" throw 'Unknown config type'
} }
} }
parse() { parse() {
core.info(`original configuration:\n${this.config}`) core.info(`original configuration:\n${this.config}`)
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
var exportNode = ast.body.find(node => node.type === 'ExpressionStatement') var exportNode = ast.body.find(node => node.type === 'ExpressionStatement')
if (exportNode) { if (exportNode) {
var property = this.getPropertyModuleExport(exportNode) var property = this.getPropertyModuleExport(exportNode)
} else { } else {
exportNode = ast.body.find(node => node.type === 'ExportDefaultDeclaration') exportNode = ast.body.find(
if (!exportNode) throw "Unable to find default export" node => node.type === 'ExportDefaultDeclaration'
)
if (!exportNode) throw 'Unable to find default export'
var property = this.getPropertyExportDefault(exportNode) var property = this.getPropertyExportDefault(exportNode)
} }
if (property) { if (property) {
switch (this.staticSiteConfig.type) { switch (this.staticSiteConfig.type) {
case "nuxt": case 'nuxt':
this.parseNuxt(property) this.parseNuxt(property)
break break
case "next": case 'next':
case "gatsby": case 'gatsby':
this.parseNextGatsby(property) this.parseNextGatsby(property)
break break
default: default:
throw "Unknown config type" throw 'Unknown config type'
} }
} }
core.info(`parsed configuration:\n${this.config}`) core.info(`parsed configuration:\n${this.config}`)
@@ -98,18 +110,34 @@ class ConfigParser {
getPropertyModuleExport(exportNode) { getPropertyModuleExport(exportNode) {
var propertyNode = exportNode.expression.right.properties.find( var propertyNode = exportNode.expression.right.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName node =>
node.key.type === 'Identifier' &&
node.key.name === this.staticSiteConfig.pathName
) )
if (!propertyNode) { if (!propertyNode) {
core.info(
core.info("Unable to find property, insert it : " + this.staticSiteConfig.pathName) 'Unable to find property, insert it : ' +
this.staticSiteConfig.pathName
)
if (exportNode.expression.right.properties.length > 0) { if (exportNode.expression.right.properties.length > 0) {
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]) this.config =
core.info("new config = \n" + 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])
core.info('new config = \n' + this.config)
} else { } 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) this.config =
core.info("new config = \n" + 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)
} }
} }
return propertyNode return propertyNode
@@ -117,18 +145,30 @@ class ConfigParser {
getPropertyExportDefault(exportNode) { getPropertyExportDefault(exportNode) {
var propertyNode = exportNode.declaration.properties.find( var propertyNode = exportNode.declaration.properties.find(
node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.pathName node =>
node.key.type === 'Identifier' &&
node.key.name === this.staticSiteConfig.pathName
) )
if (!propertyNode) { if (!propertyNode) {
core.info(
core.info("Unable to find property, insert it " + this.staticSiteConfig.pathName) 'Unable to find property, insert it ' + this.staticSiteConfig.pathName
)
if (exportNode.declaration.properties.length > 0) { if (exportNode.declaration.properties.length > 0) {
this.config = this.config.slice(0, exportNode.declaration.properties[0].range[0]) + this.generateConfigProperty() + ',\n' + this.config.slice(exportNode.declaration.properties[0].range[0]) this.config =
core.info("new config = \n" + 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 { } else {
this.config = this.config.slice(0, exportNode.declaration.range[0] + 1) + '\n ' + this.generateConfigProperty() + '\n' + this.config.slice(exportNode.declaration.range[1] - 1) this.config =
core.info("new config = \n" + 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)
} }
} }
@@ -138,19 +178,29 @@ class ConfigParser {
parseNuxt(propertyNode) { parseNuxt(propertyNode) {
// Find the base node // Find the base node
if (propertyNode && propertyNode.value.type === 'ObjectExpression') { if (propertyNode && propertyNode.value.type === 'ObjectExpression') {
var baseNode = propertyNode.value.properties.find(node => node.key.type === 'Identifier' && node.key.name === this.staticSiteConfig.subPathName)//'base') var baseNode = propertyNode.value.properties.find(
node =>
node.key.type === 'Identifier' &&
node.key.name === this.staticSiteConfig.subPathName
) //'base')
if (baseNode) { if (baseNode) {
// Swap the base value by a hardcoded string and print it // Swap the base value by a hardcoded string and print it
this.config = this.config.slice(0, baseNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(baseNode.value.range[1]) this.config =
this.config.slice(0, baseNode.value.range[0]) +
`'${this.staticSiteConfig.newPath}'` +
this.config.slice(baseNode.value.range[1])
} }
} }
} }
parseNextGatsby(pathNode) { parseNextGatsby(pathNode) {
if (pathNode) { if (pathNode) {
this.config = this.config.slice(0, pathNode.value.range[0]) + `'${this.staticSiteConfig.newPath}'` + this.config.slice(pathNode.value.range[1]) this.config =
this.config.slice(0, pathNode.value.range[0]) +
`'${this.staticSiteConfig.newPath}'` +
this.config.slice(pathNode.value.range[1])
} }
} }
} }
module.exports = {ConfigParser} module.exports = {ConfigParser}

View File

@@ -1,83 +1,104 @@
const { ConfigParser } = require('./config-parser') const {ConfigParser} = require('./config-parser')
const fs = require("fs") const fs = require('fs')
const assert = require('assert') const assert = require('assert')
const srcFolder = `${process.cwd()}/src/fixtures` const srcFolder = `${process.cwd()}/src/fixtures`
const tmpFolder = `${process.cwd()}/src/fixtures/tmp` const tmpFolder = `${process.cwd()}/src/fixtures/tmp`
const expectedFolder = `${process.cwd()}/src/fixtures/expected` const expectedFolder = `${process.cwd()}/src/fixtures/expected`
const repoPath = "/amazing-new-repo/" const repoPath = '/amazing-new-repo/'
const cases = [ const cases = [
["next.config.js", { [
filePath: `${tmpFolder}/next.config.js`, 'next.config.js',
type: "next", {
pathName: "basePath", filePath: `${tmpFolder}/next.config.js`,
newPath: repoPath type: 'next',
}], pathName: 'basePath',
["next.config.old.js", { newPath: repoPath
filePath: `${tmpFolder}/next.config.old.js`, }
type: "next", ],
pathName: "basePath", [
newPath: repoPath 'next.config.old.js',
}], {
["next.config.old.missing.js", { filePath: `${tmpFolder}/next.config.old.js`,
filePath: `${tmpFolder}/next.config.old.missing.js`, type: 'next',
type: "next", pathName: 'basePath',
pathName: "basePath", newPath: repoPath
newPath: repoPath }
}], ],
["gatsby-config.js", { [
filePath: `${tmpFolder}/gatsby-config.js`, 'next.config.old.missing.js',
type: "gatsby", {
pathName: "pathPrefix", filePath: `${tmpFolder}/next.config.old.missing.js`,
newPath: repoPath type: 'next',
}], pathName: 'basePath',
["gatsby-config.old.js", { newPath: repoPath
filePath: `${tmpFolder}/gatsby-config.old.js`, }
type: "gatsby", ],
pathName: "pathPrefix", [
newPath: repoPath 'gatsby-config.js',
}], {
["nuxt.config.js", { filePath: `${tmpFolder}/gatsby-config.js`,
filePath:`${tmpFolder}/nuxt.config.js`, type: 'gatsby',
type: "nuxt", pathName: 'pathPrefix',
pathName: "router", newPath: repoPath
subPathName: "base", }
newPath: repoPath ],
}], [
["nuxt.config.missing.js", { 'gatsby-config.old.js',
filePath:`${tmpFolder}/nuxt.config.missing.js`, {
type: "nuxt", filePath: `${tmpFolder}/gatsby-config.old.js`,
pathName: "router", type: 'gatsby',
subPathName: "base", pathName: 'pathPrefix',
newPath: repoPath newPath: repoPath
}], }
["nuxt.config.old.js", { ],
filePath:`${tmpFolder}/nuxt.config.old.js`, [
type: "nuxt", 'nuxt.config.js',
pathName: "router", {
subPathName: "base", filePath: `${tmpFolder}/nuxt.config.js`,
newPath: repoPath 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', () => { describe('configParser', () => {
test.each(cases)( test.each(cases)('%p parsed correctly', (fileName, configuration) => {
"%p parsed correctly", srcFileName = `${srcFolder}/${fileName}`
(fileName, configuration) => { tmpFileName = `${tmpFolder}/${fileName}`
srcFileName = `${srcFolder}/${fileName}` expectedFileName = `${expectedFolder}/${fileName}`
tmpFileName = `${tmpFolder}/${fileName}` fs.mkdirSync(tmpFolder, {recursive: true})
expectedFileName = `${expectedFolder}/${fileName}` fs.copyFileSync(srcFileName, tmpFileName)
fs.mkdirSync(tmpFolder, {recursive: true}) const parser = new ConfigParser(configuration)
fs.copyFileSync(srcFileName, tmpFileName) parser.parse()
const parser = new ConfigParser(configuration)
parser.parse()
var expectedContent = fs.readFileSync(expectedFileName).toString() var expectedContent = fs.readFileSync(expectedFileName).toString()
var actualContent = fs.readFileSync(tmpFileName).toString() var actualContent = fs.readFileSync(tmpFileName).toString()
assert.equal(actualContent, expectedContent) assert.equal(actualContent, expectedContent)
fs.rmSync(tmpFileName) fs.rmSync(tmpFileName)
} })
)
}) })

View File

@@ -1,19 +1,19 @@
const core = require('@actions/core') const core = require('@actions/core')
const axios = require('axios') const axios = require('axios')
async function enablePages({ repositoryNwo, githubToken }) { async function enablePages({repositoryNwo, githubToken}) {
const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages` const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages`
try { try {
const response = await axios.post( const response = await axios.post(
pagesEndpoint, pagesEndpoint,
{ build_type: 'workflow' }, {build_type: 'workflow'},
{ {
headers: { headers: {
Accept: 'application/vnd.github.v3+json', Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${githubToken}`, Authorization: `Bearer ${githubToken}`,
'Content-type': 'application/json', 'Content-type': 'application/json'
}, }
} }
) )
core.info('Created pages site') core.info('Created pages site')
@@ -23,7 +23,7 @@ async function enablePages({ repositoryNwo, githubToken }) {
return return
} }
core.error('Couldn\'t create pages site', error) core.error("Couldn't create pages site", error)
throw error throw error
} }
} }

View File

@@ -18,29 +18,36 @@ describe('enablePages', () => {
}) })
it('makes a request to create a page', async () => { it('makes a request to create a page', async () => {
jest jest.spyOn(axios, 'post').mockImplementationOnce(() => Promise.resolve({}))
.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.resolve({ }))
await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await enablePages({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
}) })
it('handles a 409 response when the page already exists', async () => { it('handles a 409 response when the page already exists', async () => {
jest jest
.spyOn(axios, 'post') .spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } })) .mockImplementationOnce(() => Promise.reject({response: {status: 409}}))
// Simply assert that no error is raised // Simply assert that no error is raised
await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await enablePages({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
}) })
it('re-raises errors on failure status codes', async () => { it('re-raises errors on failure status codes', async () => {
jest jest
.spyOn(axios, 'post') .spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) .mockImplementationOnce(() => Promise.reject({response: {status: 404}}))
try { try {
await enablePages({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await enablePages({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
} catch (error) { } catch (error) {
expect(error.response.status).toEqual(404) expect(error.response.status).toEqual(404)
} }

View File

@@ -2,26 +2,27 @@ const core = require('@actions/core')
const axios = require('axios') const axios = require('axios')
const setPagesPath = require('./set-pages-path') const setPagesPath = require('./set-pages-path')
async function getPagesBaseUrl({ repositoryNwo, githubToken, staticSiteGenerator}) { async function getPagesBaseUrl({
repositoryNwo,
githubToken,
staticSiteGenerator
}) {
try { try {
const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages` const pagesEndpoint = `https://api.github.com/repos/${repositoryNwo}/pages`
core.info(`Get the Base URL to the page with endpoint ${pagesEndpoint}`) core.info(`Get the Base URL to the page with endpoint ${pagesEndpoint}`)
const response = await axios.get( const response = await axios.get(pagesEndpoint, {
pagesEndpoint, headers: {
{ Accept: 'application/vnd.github.v3+json',
headers: { Authorization: `Bearer ${githubToken}`
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${githubToken}`
}
} }
) })
pageObject = response.data pageObject = response.data
core.info(JSON.stringify(pageObject)) core.info(JSON.stringify(pageObject))
const siteUrl = new URL(pageObject.html_url) const siteUrl = new URL(pageObject.html_url)
if ( staticSiteGenerator ) { if (staticSiteGenerator) {
setPagesPath({staticSiteGenerator, path: siteUrl.pathname}) setPagesPath({staticSiteGenerator, path: siteUrl.pathname})
} }
core.setOutput('base_url', siteUrl.href) core.setOutput('base_url', siteUrl.href)

View File

@@ -11,7 +11,9 @@ describe('getPagesBaseUrl', () => {
beforeEach(() => { beforeEach(() => {
jest.restoreAllMocks() jest.restoreAllMocks()
jest.spyOn(core, 'setOutput').mockImplementation((key, value) => { key, value }) jest.spyOn(core, 'setOutput').mockImplementation((key, value) => {
key, value
})
jest.spyOn(core, 'setFailed').mockImplementation(param => param) jest.spyOn(core, 'setFailed').mockImplementation(param => param)
// Mock error/warning/info/debug // Mock error/warning/info/debug
@@ -26,12 +28,20 @@ describe('getPagesBaseUrl', () => {
jest jest
.spyOn(axios, 'get') .spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.resolve({ data: { html_url: baseUrl } })) .mockImplementationOnce(() =>
Promise.resolve({data: {html_url: baseUrl}})
)
await getPagesBaseUrl({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await getPagesBaseUrl({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl)
expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith(
'origin',
'https://octocat.github.io'
)
expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io')
expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') expect(core.setOutput).toHaveBeenCalledWith('base_path', '/')
}) })
@@ -41,12 +51,20 @@ describe('getPagesBaseUrl', () => {
jest jest
.spyOn(axios, 'get') .spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.resolve({ data: { html_url: baseUrl } })) .mockImplementationOnce(() =>
Promise.resolve({data: {html_url: baseUrl}})
)
await getPagesBaseUrl({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await getPagesBaseUrl({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl)
expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith(
'origin',
'https://octocat.github.io'
)
expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io') expect(core.setOutput).toHaveBeenCalledWith('host', 'octocat.github.io')
expect(core.setOutput).toHaveBeenCalledWith('base_path', '/my-repo/') expect(core.setOutput).toHaveBeenCalledWith('base_path', '/my-repo/')
}) })
@@ -56,12 +74,20 @@ describe('getPagesBaseUrl', () => {
jest jest
.spyOn(axios, 'get') .spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.resolve({ data: { html_url: baseUrl } })) .mockImplementationOnce(() =>
Promise.resolve({data: {html_url: baseUrl}})
)
await getPagesBaseUrl({ repositoryNwo: GITHUB_REPOSITORY, githubToken: GITHUB_TOKEN }) await getPagesBaseUrl({
repositoryNwo: GITHUB_REPOSITORY,
githubToken: GITHUB_TOKEN
})
expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl) expect(core.setOutput).toHaveBeenCalledWith('base_url', baseUrl)
expect(core.setOutput).toHaveBeenCalledWith('origin', 'https://www.example.com') expect(core.setOutput).toHaveBeenCalledWith(
'origin',
'https://www.example.com'
)
expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com') expect(core.setOutput).toHaveBeenCalledWith('host', 'www.example.com')
expect(core.setOutput).toHaveBeenCalledWith('base_path', '/') expect(core.setOutput).toHaveBeenCalledWith('base_path', '/')
}) })

View File

@@ -17,6 +17,5 @@ async function main() {
} }
} }
// Main // Main
main() main()

View File

@@ -1,45 +1,46 @@
const core = require('@actions/core') const core = require('@actions/core')
const axios = require('axios') const axios = require('axios')
const { ConfigParser } = require('./config-parser') const {ConfigParser} = require('./config-parser')
async function setPagesPath({staticSiteGenerator, path}) { async function setPagesPath({staticSiteGenerator, path}) {
try { try {
switch(staticSiteGenerator) switch (staticSiteGenerator) {
{
case 'nuxt': case 'nuxt':
var ssConfig = { var ssConfig = {
filePath:"./nuxt.config.js", filePath: './nuxt.config.js',
type: "nuxt", type: 'nuxt',
pathName: "router", pathName: 'router',
subPathName: "base", subPathName: 'base',
newPath: path newPath: path
} }
break; break
case 'next': case 'next':
var ssConfig = { var ssConfig = {
filePath:"./next.config.js", filePath: './next.config.js',
type: "next", type: 'next',
pathName: "basePath", pathName: 'basePath',
newPath: path newPath: path
} }
break; break
case 'gatsby': case 'gatsby':
var ssConfig = { var ssConfig = {
filePath: "./gatsby-config.js", filePath: './gatsby-config.js',
type: "gatsby", type: 'gatsby',
pathName: "pathPrefix", pathName: 'pathPrefix',
newPath: path newPath: path
} }
break; break
default: default:
throw "Unknown config type" throw 'Unknown config type'
} }
let configParser = new ConfigParser(ssConfig) let configParser = new ConfigParser(ssConfig)
configParser.parse() configParser.parse()
} catch (error) { } catch (error) {
core.warning(`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`, error) core.warning(
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
error
)
} }
} }