Merge pull request #139 from actions/config-auto-detect

Attempt to auto-detect configuration files with varying file extensions
This commit is contained in:
James M. Greene
2024-03-29 19:48:14 -05:00
committed by GitHub
3 changed files with 57 additions and 41 deletions

40
dist/index.js vendored
View File

@@ -36719,6 +36719,7 @@ 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)
@@ -36726,6 +36727,17 @@ 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 }) {
@@ -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
@@ -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
@@ -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
@@ -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
@@ -36798,29 +36810,25 @@ 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)
)
}
}
}
module.exports = { getConfigParserSettings, setPagesConfig }

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,3 +1,4 @@
const fs = require('fs')
const core = require('@actions/core')
const { ConfigParser } = require('./config-parser')
const removeTrailingSlash = require('./remove-trailing-slash')
@@ -5,6 +6,17 @@ 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 }) {
@@ -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
@@ -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
@@ -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
@@ -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
@@ -77,28 +89,24 @@ 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)
)
}
}
}
module.exports = { getConfigParserSettings, setPagesConfig }