Skip to content

Commit

Permalink
Merge pull request #139 from actions/config-auto-detect
Browse files Browse the repository at this point in the history
Attempt to auto-detect configuration files with varying file extensions
  • Loading branch information
JamesMGreene authored Mar 30, 2024
2 parents 7781abd + 9cf6e24 commit 983d773
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 41 deletions.
48 changes: 28 additions & 20 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36719,13 +36719,25 @@ module.exports = function removeTrailingSlash(str) {
/***/ 6310:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const fs = __nccwpck_require__(7147)
const core = __nccwpck_require__(2186)
const { ConfigParser } = __nccwpck_require__(8395)
const removeTrailingSlash = __nccwpck_require__(9255)
const { convertErrorToAnnotationProperties } = __nccwpck_require__(1507)

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, fall back 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 @@ -36734,7 +36746,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
switch (staticSiteGenerator) {
case 'nuxt':
return {
configurationFile: generatorConfigFile || './nuxt.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('nuxt.config'),
blankConfigurationFile: __nccwpck_require__.ab + "nuxt.js",
properties: {
// Configure a base path on the router
Expand All @@ -36750,7 +36762,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './next.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('next.config'),
blankConfigurationFile: __nccwpck_require__.ab + "next.js",
properties: {
// Static export
Expand All @@ -36768,7 +36780,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
}
case 'gatsby':
return {
configurationFile: generatorConfigFile || './gatsby-config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('gatsby-config'),
blankConfigurationFile: __nccwpck_require__.ab + "gatsby.js",
properties: {
// Configure a path prefix
Expand All @@ -36782,7 +36794,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
path = removeTrailingSlash(path)

return {
configurationFile: generatorConfigFile || './svelte.config.js',
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('svelte.config'),
blankConfigurationFile: __nccwpck_require__.ab + "sveltekit.js",
properties: {
// Configure a base path
Expand All @@ -36798,27 +36810,23 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit

// Inject Pages configuration in a given static site generator's configuration file
function setPagesConfig({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
if (generatorConfigFile && !isSupportedFileExtension) {
const supportedExtensionList = SUPPORTED_FILE_EXTENSIONS.map(ext => JSON.stringify(ext)).join(', ')
core.warning(
`Unsupported extension in configuration file: ${generatorConfigFile}. Currently supported extensions: ${supportedExtensionList}. We will still attempt to inject the site metadata into the configuration file, but it may not work as expected.`
)
}

try {
// Parse the configuration file and try to inject the Pages configuration in it
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl })
new ConfigParser(settings).injectAll()
} catch (error) {
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))

// Logging
if (!isSupportedFileExtension) {
core.warning(
`Unsupported configuration file extension. Currently supported extensions: ${SUPPORTED_FILE_EXTENSIONS.map(
ext => JSON.stringify(ext)
).join(', ')}. Error: ${error.message}`,
convertErrorToAnnotationProperties(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: ${error.message}`,
convertErrorToAnnotationProperties(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 ${siteUrl}. Please ensure your framework is configured to generate relative links appropriately. Error: ${error.message}`,
convertErrorToAnnotationProperties(error)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

48 changes: 28 additions & 20 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, fall back 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 All @@ -77,27 +89,23 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit

// Inject Pages configuration in a given static site generator's configuration file
function setPagesConfig({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
if (generatorConfigFile && !isSupportedFileExtension) {
const supportedExtensionList = SUPPORTED_FILE_EXTENSIONS.map(ext => JSON.stringify(ext)).join(', ')
core.warning(
`Unsupported extension in configuration file: ${generatorConfigFile}. Currently supported extensions: ${supportedExtensionList}. We will still attempt to inject the site metadata into the configuration file, but it may not work as expected.`
)
}

try {
// Parse the configuration file and try to inject the Pages configuration in it
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl })
new ConfigParser(settings).injectAll()
} catch (error) {
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))

// Logging
if (!isSupportedFileExtension) {
core.warning(
`Unsupported configuration file extension. Currently supported extensions: ${SUPPORTED_FILE_EXTENSIONS.map(
ext => JSON.stringify(ext)
).join(', ')}. Error: ${error.message}`,
convertErrorToAnnotationProperties(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: ${error.message}`,
convertErrorToAnnotationProperties(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 ${siteUrl}. Please ensure your framework is configured to generate relative links appropriately. Error: ${error.message}`,
convertErrorToAnnotationProperties(error)
)
}
}

Expand Down

0 comments on commit 983d773

Please sign in to comment.