-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix getConfig
referencing config from incorrect location
#1049
Changes from all commits
3696e80
1746460
c2c44ff
57087bb
08c410d
242242d
5a0928c
5e18a6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,7 +249,7 @@ const main = async () => { | |
.addOption( | ||
new program.Option( | ||
'-b, --buildDirectory <buildDirectory>', | ||
'a custom project directory where your build is located' | ||
'a custom project build directory that you want to push' | ||
).default(p.join(process.cwd(), 'build'), './build') | ||
) | ||
.addOption( | ||
|
@@ -285,12 +285,20 @@ const main = async () => { | |
credentialsFile | ||
}) => { | ||
// Set the deployment target env var, this is required to ensure we | ||
// get the correct configuration object. | ||
process.env.DEPLOY_TARGET = target | ||
// get the correct configuration object. Do not assign the variable it if | ||
// the target value is `undefined` as it will serialied as a "undefined" | ||
// string value. | ||
if (target) { | ||
process.env.DEPLOY_TARGET = target | ||
} | ||
Comment on lines
+291
to
+293
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that when this environment variable is used in the Adding a guard fixed this. |
||
|
||
const credentials = await scriptUtils.readCredentials(credentialsFile) | ||
|
||
const mobify = getConfig() || {} | ||
if (!fse.pathExistsSync(buildDirectory)) { | ||
throw new Error(`Supplied "buildDirectory" does not exist!`) | ||
} | ||
|
||
const mobify = getConfig({buildDirectory}) || {} | ||
|
||
if (!projectSlug) { | ||
projectSlug = await getProjectName() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2023, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/* eslint-env jest */ | ||
/* eslint max-nested-callbacks:0 */ | ||
|
||
import {getConfig} from './ssr-config.client' | ||
|
||
let windowSpy | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(window, 'window', 'get') | ||
}) | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore() | ||
}) | ||
|
||
describe('Client getConfig', () => { | ||
test('returns window.__CONFIG__ value', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
__CONFIG__: {} | ||
})) | ||
|
||
expect(getConfig()).toEqual({}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
/* istanbul ignore next */ | ||
const SUPPORTED_FILE_TYPES = ['js', 'yml', 'yaml', 'json'] | ||
|
||
const IS_REMOTE = Object.prototype.hasOwnProperty.call(process.env, 'AWS_LAMBDA_FUNCTION_NAME') | ||
|
||
/** | ||
* Returns the express app configuration file in object form. The file will be resolved in the | ||
* the following order: | ||
|
@@ -28,21 +30,26 @@ const SUPPORTED_FILE_TYPES = ['js', 'yml', 'yaml', 'json'] | |
* Each file marked with `ext` can optionally be terminated with `js`, `yml|yaml` or | ||
* `json`. The file loaded is also determined based on that precidence of file extension. | ||
* | ||
* @param {Object} opts.buildDirectory - Option path to the folder containing the configution. Byt default | ||
* it is the `build` folder when running remotely and the project folder when developing locally. | ||
* @returns - the application configuration object. | ||
*/ | ||
/* istanbul ignore next */ | ||
export const getConfig = () => { | ||
const isRemote = Object.prototype.hasOwnProperty.call(process.env, 'AWS_LAMBDA_FUNCTION_NAME') | ||
export const getConfig = (opts = {}) => { | ||
const {buildDirectory} = opts | ||
const configDirBase = IS_REMOTE ? 'build' : '' | ||
let targetName = process?.env?.DEPLOY_TARGET || '' | ||
|
||
const targetSearchPlaces = SUPPORTED_FILE_TYPES.map((ext) => `config/${targetName}.${ext}`) | ||
const localeSearchPlaces = SUPPORTED_FILE_TYPES.map((ext) => `config/local.${ext}`) | ||
const defaultSearchPlaces = SUPPORTED_FILE_TYPES.map((ext) => `config/default.${ext}`) | ||
|
||
const searchFrom = buildDirectory || process.cwd() + '/' + configDirBase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 what's the value for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The the idea here was to have the The weird thing I just spotted after you brought this up was that This behaviour in the context of pushing wasn't great because it would take a while to fail since it's possible that it found a package.json file further up the file tree and used that for the push config, then only failed during the build step. So I took extra precaution and decided to throw if the buildDirectory you supply doesn't exist. So after all that, to answer you question we are taking the absolute or relative path and passing it to cosmiconfig, which from the documentation doesn't specify if it has to be relative or absolute. It's possible that it resolves it internally. |
||
|
||
// Combined search places. | ||
const searchPlaces = [ | ||
...targetSearchPlaces, | ||
...(!isRemote ? localeSearchPlaces : []), | ||
...(targetName ? targetSearchPlaces : []), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
...(!IS_REMOTE ? localeSearchPlaces : []), | ||
...defaultSearchPlaces, | ||
'package.json' | ||
] | ||
|
@@ -52,7 +59,7 @@ export const getConfig = () => { | |
// Match config files based on the specificity from most to most general. | ||
const explorerSync = cosmiconfigSync(targetName, { | ||
packageProp: 'mobify', | ||
searchPlaces: searchPlaces.map((path) => (isRemote ? `build/${path}` : path)), | ||
searchPlaces: searchPlaces, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is not required anymore as the same result is accomplished by using the |
||
loaders: { | ||
'.js': (filepath) => { | ||
// Because `require` is bootstrapped by webpack, the builtin | ||
|
@@ -67,7 +74,7 @@ export const getConfig = () => { | |
}) | ||
|
||
// Load the config synchronously using a custom "searchPlaces". | ||
const {config} = explorerSync.search() || {} | ||
const {config} = explorerSync.search(searchFrom) || {} | ||
|
||
if (!config) { | ||
throw new Error( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2023, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/* eslint-env jest */ | ||
/* eslint max-nested-callbacks:0 */ | ||
|
||
import {getConfig} from './ssr-config.server' | ||
|
||
describe('Server "getConfig"', () => { | ||
const env = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = {...env} | ||
}) | ||
|
||
afterEach(() => { | ||
process.env = env | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Should this be the same as on line 18 above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I essentially borrowed that from here. I believe the idea is to restore to the actual value and not a copy of the original values. And to also only modify a copy, not modify (add/remove) values during the rest run. |
||
}) | ||
|
||
test('throws when no config files are found running remotely', () => { | ||
expect(getConfig).toThrow() | ||
}) | ||
|
||
test('throws when no config files are found running locally', () => { | ||
process.env.AWS_LAMBDA_FUNCTION_NAME = '' | ||
expect(getConfig).toThrow() | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this description was a little misleading, and clarified that it's the build folder that we want.