2022-06-16 14:28:43 -07:00
const core = require ( '@actions/core' )
2022-08-05 15:26:05 -05:00
const { ConfigParser } = require ( './config-parser' )
2022-08-18 17:54:00 -05:00
const removeTrailingSlash = require ( './remove-trailing-slash' )
2022-06-16 14:28:43 -07:00
2022-11-22 23:08:59 -06:00
const SUPPORTED _FILE _EXTENSIONS = [ '.js' , '.cjs' , '.mjs' ]
2022-08-05 17:31:59 -05:00
// Return the settings to be passed to a {ConfigParser} for a given static site generator,
2022-08-19 14:33:39 -05:00
// optional configuration file path, and a Pages siteUrl value to inject
function getConfigParserSettings ( { staticSiteGenerator , generatorConfigFile , siteUrl } ) {
2022-09-11 09:56:25 -05:00
let { pathname : path , origin } = siteUrl
2022-08-19 14:33:39 -05:00
2022-07-18 16:42:45 -07:00
switch ( staticSiteGenerator ) {
case 'nuxt' :
return {
2022-08-05 17:31:59 -05:00
configurationFile : generatorConfigFile || './nuxt.config.js' ,
2022-07-19 17:03:44 -07:00
blankConfigurationFile : ` ${ _ _dirname } /blank-configurations/nuxt.js ` ,
properties : {
2022-07-20 13:14:25 -07:00
// Configure a base path on the router
2022-09-11 09:56:25 -05:00
'router.base' : path ,
2022-07-20 13:14:25 -07:00
// 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
2022-09-11 09:56:25 -05:00
path = removeTrailingSlash ( path )
2022-07-19 16:27:57 -07:00
2022-07-18 16:42:45 -07:00
return {
2022-08-05 17:31:59 -05:00
configurationFile : generatorConfigFile || './next.config.js' ,
2022-07-19 17:03:44 -07:00
blankConfigurationFile : ` ${ _ _dirname } /blank-configurations/next.js ` ,
properties : {
2022-07-20 13:14:25 -07:00
// Configure a base path
2022-09-11 09:56:25 -05: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-10-24 23:46:06 -04:00
'experimental.images.unoptimized' : true ,
2022-11-18 00:50:00 -06:00
// No longer experimental as of Next.js v12.3.0
2022-10-24 23:09:43 -04:00
'images.unoptimized' : true
2022-07-19 17:03:44 -07:00
}
2022-07-18 16:42:45 -07:00
}
case 'gatsby' :
return {
2022-08-05 17:31:59 -05:00
configurationFile : generatorConfigFile || './gatsby-config.js' ,
2022-07-19 17:03:44 -07:00
blankConfigurationFile : ` ${ _ _dirname } /blank-configurations/gatsby.js ` ,
properties : {
2022-07-20 13:14:25 -07:00
// Configure a path prefix
2022-09-11 09:56:25 -05:00
pathPrefix : path ,
2022-08-19 14:33:39 -05:00
// Configure a site url
'siteMetadata.siteUrl' : origin
2022-07-19 17:03:44 -07:00
}
2022-07-18 16:42:45 -07:00
}
2022-08-09 20:26:30 -04:00
case 'sveltekit' :
// SvelteKit does not want a trailing slash
2022-09-11 09:56:25 -05:00
path = removeTrailingSlash ( path )
2022-08-09 20:26:30 -04:00
return {
2022-08-10 10:45:56 -04:00
configurationFile : generatorConfigFile || './svelte.config.js' ,
2022-08-09 20:26:30 -04:00
blankConfigurationFile : ` ${ _ _dirname } /blank-configurations/sveltekit.js ` ,
properties : {
// Configure a base path
2022-09-11 09:56:25 -05:00
'kit.paths.base' : path ,
2022-08-19 14:33:39 -05:00
// Configure a prerender origin
'kit.prerender.origin' : origin
2022-08-09 20:26:30 -04:00
}
}
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
2022-08-19 14:33:39 -05:00
function setPagesConfig ( { staticSiteGenerator , generatorConfigFile , siteUrl } ) {
2022-06-16 14:28:43 -07:00
try {
2022-07-19 11:47:29 -07:00
// Parse the configuration file and try to inject the Pages configuration in it
2022-08-19 14:33:39 -05:00
const settings = getConfigParserSettings ( { staticSiteGenerator , generatorConfigFile , siteUrl } )
2022-07-19 17:03:44 -07:00
new ConfigParser ( settings ) . injectAll ( )
2022-06-16 14:28:43 -07:00
} catch ( error ) {
2022-11-22 23:08:59 -06:00
const isSupportedFileExtension = SUPPORTED _FILE _EXTENSIONS . some ( ext => generatorConfigFile . endsWith ( ext ) )
2022-11-22 23:10:37 -06:00
2022-07-19 11:47:29 -07:00
// Logging
2022-11-22 23:08:59 -06:00
if ( ! isSupportedFileExtension ) {
2022-11-22 23:10:37 -06:00
core . warning (
2022-11-22 23:09:15 -06:00
` Unsupported configuration file extension. Currently supported extensions: ${ SUPPORTED _FILE _EXTENSIONS . map (
ext => JSON . stringify ( ext )
) . join ( ', ' ) } ` ,
2022-11-22 23:10:37 -06:00
error
)
} else {
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 ${ siteUrl } . Please ensure your framework is configured to generate relative links appropriately. ` ,
error
)
}
2022-06-16 14:28:43 -07:00
}
}
2022-08-19 14:33:39 -05:00
module . exports = { getConfigParserSettings , setPagesConfig }