Skip to content

Commit

Permalink
Attempt to detect existing config files matching the expected basenam…
Browse files Browse the repository at this point in the history
…e plus one of the supported file extensions
  • Loading branch information
JamesMGreene committed Mar 29, 2024
1 parent e9382ac commit 215cd51
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/set-pages-config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
const fs = require('fs')
const core = require('@actions/core')
const { ConfigParser } = require('./config-parser')
const removeTrailingSlash = require('./remove-trailing-slash')
const { convertErrorToAnnotationProperties } = require('./error-utils')

const SUPPORTED_FILE_EXTENSIONS = ['.js', '.cjs', '.mjs']

function detectOrDefaultConfigFile(fileBaseName, defaultExt = '.js') {
for (const ext of SUPPORTED_FILE_EXTENSIONS) {
const potentialConfigFile = `./${fileBaseName}${ext}`
if (fs.existsSync(potentialConfigFile)) {
return potentialConfigFile
}
}
// If none of them exist yet, default to returning the filename with the defaultExt extension
return `./${fileBaseName}${defaultExt}`
}

// Return the settings to be passed to a {ConfigParser} for a given static site generator,
// optional configuration file path, and a Pages siteUrl value to inject
function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
Expand All @@ -13,7 +25,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
switch (staticSiteGenerator) {
case 'nuxt':
return {
configurationFile: generatorConfigFile || './nuxt.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('nuxt.config'),
blankConfigurationFile: `${__dirname}/blank-configurations/nuxt.js`,
properties: {
// Configure a base path on the router
Expand All @@ -29,7 +41,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './next.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('next.config'),
blankConfigurationFile: `${__dirname}/blank-configurations/next.js`,
properties: {
// Static export
Expand All @@ -47,7 +59,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
}
case 'gatsby':
return {
configurationFile: generatorConfigFile || './gatsby-config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('gatsby-config'),
blankConfigurationFile: `${__dirname}/blank-configurations/gatsby.js`,
properties: {
// Configure a path prefix
Expand All @@ -61,7 +73,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './svelte.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('svelte.config'),
blankConfigurationFile: `${__dirname}/blank-configurations/sveltekit.js`,
properties: {
// Configure a base path
Expand Down

0 comments on commit 215cd51

Please sign in to comment.