Skip to content

Commit 983d773

Browse files
authored
Merge pull request #139 from actions/config-auto-detect
Attempt to auto-detect configuration files with varying file extensions
2 parents 7781abd + 9cf6e24 commit 983d773

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

dist/index.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -36719,13 +36719,25 @@ module.exports = function removeTrailingSlash(str) {
3671936719
/***/ 6310:
3672036720
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
3672136721

36722+
const fs = __nccwpck_require__(7147)
3672236723
const core = __nccwpck_require__(2186)
3672336724
const { ConfigParser } = __nccwpck_require__(8395)
3672436725
const removeTrailingSlash = __nccwpck_require__(9255)
3672536726
const { convertErrorToAnnotationProperties } = __nccwpck_require__(1507)
3672636727

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

36730+
function detectOrDefaultConfigFile(fileBaseName, defaultExt = '.js') {
36731+
for (const ext of SUPPORTED_FILE_EXTENSIONS) {
36732+
const potentialConfigFile = `./${fileBaseName}${ext}`
36733+
if (fs.existsSync(potentialConfigFile)) {
36734+
return potentialConfigFile
36735+
}
36736+
}
36737+
// If none of them exist yet, fall back to returning the filename with the defaultExt extension
36738+
return `./${fileBaseName}${defaultExt}`
36739+
}
36740+
3672936741
// Return the settings to be passed to a {ConfigParser} for a given static site generator,
3673036742
// optional configuration file path, and a Pages siteUrl value to inject
3673136743
function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
@@ -36734,7 +36746,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
3673436746
switch (staticSiteGenerator) {
3673536747
case 'nuxt':
3673636748
return {
36737-
configurationFile: generatorConfigFile || './nuxt.config.js',
36749+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('nuxt.config'),
3673836750
blankConfigurationFile: __nccwpck_require__.ab + "nuxt.js",
3673936751
properties: {
3674036752
// Configure a base path on the router
@@ -36750,7 +36762,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
3675036762
path = removeTrailingSlash(path)
3675136763

3675236764
return {
36753-
configurationFile: generatorConfigFile || './next.config.js',
36765+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('next.config'),
3675436766
blankConfigurationFile: __nccwpck_require__.ab + "next.js",
3675536767
properties: {
3675636768
// Static export
@@ -36768,7 +36780,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
3676836780
}
3676936781
case 'gatsby':
3677036782
return {
36771-
configurationFile: generatorConfigFile || './gatsby-config.js',
36783+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('gatsby-config'),
3677236784
blankConfigurationFile: __nccwpck_require__.ab + "gatsby.js",
3677336785
properties: {
3677436786
// Configure a path prefix
@@ -36782,7 +36794,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
3678236794
path = removeTrailingSlash(path)
3678336795

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

3679936811
// Inject Pages configuration in a given static site generator's configuration file
3680036812
function setPagesConfig({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
36813+
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
36814+
if (generatorConfigFile && !isSupportedFileExtension) {
36815+
const supportedExtensionList = SUPPORTED_FILE_EXTENSIONS.map(ext => JSON.stringify(ext)).join(', ')
36816+
core.warning(
36817+
`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.`
36818+
)
36819+
}
36820+
3680136821
try {
3680236822
// Parse the configuration file and try to inject the Pages configuration in it
3680336823
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl })
3680436824
new ConfigParser(settings).injectAll()
3680536825
} catch (error) {
36806-
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
36807-
36808-
// Logging
36809-
if (!isSupportedFileExtension) {
36810-
core.warning(
36811-
`Unsupported configuration file extension. Currently supported extensions: ${SUPPORTED_FILE_EXTENSIONS.map(
36812-
ext => JSON.stringify(ext)
36813-
).join(', ')}. Error: ${error.message}`,
36814-
convertErrorToAnnotationProperties(error)
36815-
)
36816-
} else {
36817-
core.warning(
36818-
`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}`,
36819-
convertErrorToAnnotationProperties(error)
36820-
)
36821-
}
36826+
core.warning(
36827+
`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}`,
36828+
convertErrorToAnnotationProperties(error)
36829+
)
3682236830
}
3682336831
}
3682436832

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/set-pages-config.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
const fs = require('fs')
12
const core = require('@actions/core')
23
const { ConfigParser } = require('./config-parser')
34
const removeTrailingSlash = require('./remove-trailing-slash')
45
const { convertErrorToAnnotationProperties } = require('./error-utils')
56

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

9+
function detectOrDefaultConfigFile(fileBaseName, defaultExt = '.js') {
10+
for (const ext of SUPPORTED_FILE_EXTENSIONS) {
11+
const potentialConfigFile = `./${fileBaseName}${ext}`
12+
if (fs.existsSync(potentialConfigFile)) {
13+
return potentialConfigFile
14+
}
15+
}
16+
// If none of them exist yet, fall back to returning the filename with the defaultExt extension
17+
return `./${fileBaseName}${defaultExt}`
18+
}
19+
820
// Return the settings to be passed to a {ConfigParser} for a given static site generator,
921
// optional configuration file path, and a Pages siteUrl value to inject
1022
function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
@@ -13,7 +25,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
1325
switch (staticSiteGenerator) {
1426
case 'nuxt':
1527
return {
16-
configurationFile: generatorConfigFile || './nuxt.config.js',
28+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('nuxt.config'),
1729
blankConfigurationFile: `${__dirname}/blank-configurations/nuxt.js`,
1830
properties: {
1931
// Configure a base path on the router
@@ -29,7 +41,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
2941
path = removeTrailingSlash(path)
3042

3143
return {
32-
configurationFile: generatorConfigFile || './next.config.js',
44+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('next.config'),
3345
blankConfigurationFile: `${__dirname}/blank-configurations/next.js`,
3446
properties: {
3547
// Static export
@@ -47,7 +59,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
4759
}
4860
case 'gatsby':
4961
return {
50-
configurationFile: generatorConfigFile || './gatsby-config.js',
62+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('gatsby-config'),
5163
blankConfigurationFile: `${__dirname}/blank-configurations/gatsby.js`,
5264
properties: {
5365
// Configure a path prefix
@@ -61,7 +73,7 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
6173
path = removeTrailingSlash(path)
6274

6375
return {
64-
configurationFile: generatorConfigFile || './svelte.config.js',
76+
configurationFile: generatorConfigFile || detectOrDefaultConfigFile('svelte.config'),
6577
blankConfigurationFile: `${__dirname}/blank-configurations/sveltekit.js`,
6678
properties: {
6779
// Configure a base path
@@ -77,27 +89,23 @@ function getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, sit
7789

7890
// Inject Pages configuration in a given static site generator's configuration file
7991
function setPagesConfig({ staticSiteGenerator, generatorConfigFile, siteUrl }) {
92+
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
93+
if (generatorConfigFile && !isSupportedFileExtension) {
94+
const supportedExtensionList = SUPPORTED_FILE_EXTENSIONS.map(ext => JSON.stringify(ext)).join(', ')
95+
core.warning(
96+
`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.`
97+
)
98+
}
99+
80100
try {
81101
// Parse the configuration file and try to inject the Pages configuration in it
82102
const settings = getConfigParserSettings({ staticSiteGenerator, generatorConfigFile, siteUrl })
83103
new ConfigParser(settings).injectAll()
84104
} catch (error) {
85-
const isSupportedFileExtension = SUPPORTED_FILE_EXTENSIONS.some(ext => generatorConfigFile.endsWith(ext))
86-
87-
// Logging
88-
if (!isSupportedFileExtension) {
89-
core.warning(
90-
`Unsupported configuration file extension. Currently supported extensions: ${SUPPORTED_FILE_EXTENSIONS.map(
91-
ext => JSON.stringify(ext)
92-
).join(', ')}. Error: ${error.message}`,
93-
convertErrorToAnnotationProperties(error)
94-
)
95-
} else {
96-
core.warning(
97-
`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}`,
98-
convertErrorToAnnotationProperties(error)
99-
)
100-
}
105+
core.warning(
106+
`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}`,
107+
convertErrorToAnnotationProperties(error)
108+
)
101109
}
102110
}
103111

0 commit comments

Comments
 (0)