Files
configure-pages/src/set-pages-path.js
T

83 lines
2.9 KiB
JavaScript
Raw Normal View History

const core = require('@actions/core')
const { ConfigParser } = require('./config-parser')
// Return the settings to be passed to a {ConfigParser} for a given static site generator,
// optional configuration file path, and a Pages path value to inject
function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, path }) {
2022-07-18 16:42:45 -07:00
switch (staticSiteGenerator) {
case 'nuxt':
return {
configurationFile: generatorConfigFile || './nuxt.config.js',
2022-07-19 17:03:44 -07:00
blankConfigurationFile: `${__dirname}/blank-configurations/nuxt.js`,
properties: {
// Configure a base path on the router
'router.base': path,
// Set the target to static too
// https://nuxtjs.org/docs/configuration-glossary/configuration-target/
2022-07-20 15:28:26 -07:00
target: 'static'
2022-07-19 17:03:44 -07:00
}
2022-07-18 16:42:45 -07:00
}
case 'next':
2022-07-19 16:27:57 -07:00
// Next does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
2022-07-18 16:42:45 -07:00
return {
configurationFile: generatorConfigFile || './next.config.js',
2022-07-19 17:03:44 -07:00
blankConfigurationFile: `${__dirname}/blank-configurations/next.js`,
properties: {
// Configure a base path
2022-07-19 17:03:44 -07:00
basePath: path,
2022-07-19 17:16:40 -07:00
// Disable server side image optimization too
// https://nextjs.org/docs/api-reference/next/image#unoptimized
2022-07-19 17:15:09 -07:00
'experimental.images.unoptimized': true
2022-07-19 17:03:44 -07:00
}
2022-07-18 16:42:45 -07:00
}
case 'gatsby':
return {
configurationFile: generatorConfigFile || './gatsby-config.js',
2022-07-19 17:03:44 -07:00
blankConfigurationFile: `${__dirname}/blank-configurations/gatsby.js`,
properties: {
// Configure a path prefix
2022-07-19 17:03:44 -07:00
pathPrefix: path
}
2022-07-18 16:42:45 -07:00
}
case 'sveltekit':
// SvelteKit does not want a trailing slash
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
return {
2022-08-10 10:45:56 -04:00
configurationFile: generatorConfigFile || './svelte.config.js',
blankConfigurationFile: `${__dirname}/blank-configurations/sveltekit.js`,
properties: {
// Configure a base path
'kit.paths.base': path
}
}
2022-07-18 16:42:45 -07:00
default:
throw `Unsupported static site generator: ${staticSiteGenerator}`
}
}
2022-07-19 11:47:29 -07:00
// Inject Pages configuration in a given static site generator's configuration file
function setPagesPath({ staticSiteGenerator, generatorConfigFile, path }) {
try {
2022-07-19 11:47:29 -07:00
// Parse the configuration file and try to inject the Pages configuration in it
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, path })
2022-07-19 17:03:44 -07:00
new ConfigParser(settings).injectAll()
} catch (error) {
2022-07-19 11:47:29 -07:00
// Logging
2022-07-18 15:06:14 -07:00
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
)
}
}
module.exports = { getConfigParserSettings, setPagesPath }