Skip to content
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

Environment resolution debug statements #607

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12,004 changes: 6,670 additions & 5,334 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/gasket-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/cli`

- Add diagnostic logging for environment/config resolution when a `DEBUG=gasket:*` environment variable is set

### 6.38.0

- Fix generation of files on Windows during gasket create when plugins use globs containing Windows separators ([#547])
Expand Down
8 changes: 7 additions & 1 deletion packages/gasket-cli/src/config/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { pluginIdentifier } = require('@gasket/resolve');
const defaultPlugins = require('./default-plugins');

const debug = require('diagnostics')('gasket:cli:config:utils');

/**
* Returns specified env flag if set or appropriate fallback
*
Expand All @@ -10,10 +12,14 @@ const defaultPlugins = require('./default-plugins');
* @returns {string} environment
*/
function getEnvironment(flags, commandId, warn) {
if (flags.env) return flags.env;
if (flags.env) {
debug('Environment was passed through command line flags', flags.env);
return flags.env;
}

// special snowflake case to match up `local` env with command unless set
if (commandId === 'local') {
debug('Environment defaulting to `local` due to `local` command');
return 'local';
}

Expand Down
1 change: 1 addition & 0 deletions packages/gasket-cli/src/hooks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function initHook({ id, config: oclifConfig, argv }) {

try {
const env = getEnvironment(flags, id, warn);
debug('Detected gasket environment', env);

// expose Gasket settings on process
process.env.GASKET_ENV = env;
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-resolve/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/resolve`

- Add diagnostic logging for environment/config resolution when a `DEBUG=gasket:*` environment variable is set

### 6.35.6

- Hard fail bad requires in gasket.config while preserving missing behavior ([#590])
Expand Down
4 changes: 4 additions & 0 deletions packages/gasket-resolve/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { applyConfigOverrides, tryResolve } = require('@gasket/utils');
const { flattenPresets } = require('./preset-utils');
const jsExtension = /\.(js|cjs)$/i;

const debug = require('diagnostics')('gasket:resolve:config');

async function loadGasketConfigFile(root, env, commandId, configFile = 'gasket.config') {
let gasketConfig = loadConfigFile(root, configFile);
if (gasketConfig) {
Expand All @@ -22,6 +24,8 @@ function loadConfigFile(root, configFile) {
if (resolvedPath) {
return require(resolvedPath);
}

debug(`Failed to resolve Gasket config file ${configFile} from root ${root}`);
}

async function addUserPlugins(gasketConfig) {
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `@gasket/utils`

- force install with npm ([#496])
- Add diagnostic logging for environment/config resolution when a `DEBUG=gasket:*` environment variable is set

### 6.35.2
- add requireWithInstall & tryResolve utils ([#492])
Expand Down
69 changes: 51 additions & 18 deletions packages/gasket-utils/lib/apply-config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const path = require('path');
const defaultsDeep = require('lodash.defaultsdeep');
const tryRequire = require('./try-require');

const debug = require('diagnostics')('gasket:utils');

/**
* Normalize the config by applying any overrides for environments, commands,
* or local-only config file.
Expand All @@ -15,33 +17,64 @@ const tryRequire = require('./try-require');
* @returns {object} config
*/
function applyConfigOverrides(config, { env = '', commandId, root, localFile }) {
return defaultsDeep(
{},
...getPotentialConfigs({ config, env, commandId, root, localFile })
);
}

function *getPotentialConfigs({ config, env, commandId, root, localFile }) {
// Separate environment-specific config from other config
const { environments = {}, commands = {}, ...baseConfig } = config;
const isLocalEnv = env === 'local';
const envParts = env.split('.');

const configs = [

// For git-ignorable changes, merge in optional `.local` file
isLocalEnv && localFile && tryRequire(path.join(root, localFile)),

commandId && commands[commandId],
yield* getLocalOverrides(isLocalEnv, root, localFile);
yield* getCommandOverrides(commands, commandId);
yield* getSubEnvironmentOverrides(env, environments);
yield* getDevOverrides(isLocalEnv, environments);
yield baseConfig;
}

// Iterate over any `.` delimited parts (e.g. `production.subEnv`) and
// merge any corresponding configs within `environments`
...envParts
.map((_, i) => environments[envParts.slice(0, i + 1).join('.')])
.reverse(),
function *getLocalOverrides(isLocalEnv, root, localFile) {
// For git-ignorable changes, merge in optional `.local` file
const localOverrides = isLocalEnv && localFile && tryRequire(path.join(root, localFile));
if (localOverrides) {
debug('Including local config file for overrides', localFile);
yield localOverrides;
}
}

// Special case for the local environment, which inherits from the
// development environment
isLocalEnv && (environments.development || environments.dev),
function *getCommandOverrides(commands, commandId) {
const commandOverrides = commandId && commands[commandId];
if (commandOverrides) {
debug('Including config overrides for command', commandId);
yield commandOverrides;
}
}

baseConfig
function *getSubEnvironmentOverrides(env, environments) {
const envParts = env.split('.');

].filter(Boolean);
// Iterate over any `.` delimited parts (e.g. `production.subEnv`) and
// merge any corresponding configs within `environments`
for (let i = envParts.length - 1; i >= 0; i--) {
const name = envParts.slice(0, i + 1).join('.');
const overrides = environments[name];
if (overrides) {
debug('Including sub-environment override', name);
yield overrides;
}
}
}

return defaultsDeep({}, ...configs);
function *getDevOverrides(isLocalEnv, environments) {
// Special case for the local environment, which inherits from the
// development environment
const devEnv = isLocalEnv && (environments.development || environments.dev);
if (devEnv) {
debug('Including dev/development override due to local environment inheritance');
yield devEnv;
}
}

module.exports = applyConfigOverrides;
1 change: 1 addition & 0 deletions packages/gasket-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"concat-stream": "^2.0.0",
"cross-spawn": "^7.0.1",
"diagnostics": "^2.0.2",
"lodash.defaultsdeep": "^4.6.1"
},
"devDependencies": {
Expand Down