Skip to content

Commit

Permalink
Add validation to config middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Dec 17, 2024
1 parent 40f325b commit dc5bf05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const {
getAndLoadConfigIfNeeded,
configFileExists,
getConfigPath,
validateConfig: isValidConfig,
} = require('@hubspot/local-dev-lib/config');
const { NO_VALIDATE_LIST } = require('../lib/constants');
const { logError } = require('../lib/errorHandlers/index');
const {
setLogLevel,
Expand Down Expand Up @@ -149,6 +151,17 @@ const setRequestHeaders = () => {
};

const loadConfigMiddleware = async options => {
const validateConfig = () => {
const shouldValidate = options._.every(
item => !NO_VALIDATE_LIST.includes(item)
);
if (shouldValidate) {
if (!isValidConfig()) {
process.exit(EXIT_CODES.ERROR);
}
}
};

// Load the new config and check for the conflicting config flag
if (configFileExists(true)) {
loadConfig('', options);
Expand All @@ -161,6 +174,7 @@ const loadConfigMiddleware = async options => {
);
process.exit(EXIT_CODES.ERROR);
}
validateConfig();
return;
}

Expand All @@ -169,11 +183,13 @@ const loadConfigMiddleware = async options => {
if (options.config && fs.existsSync(options.config)) {
const { config: configPath } = options;
await loadConfig(configPath, options);
validateConfig();
return;
}

// Load deprecated config without a config flag and with no warnings
getAndLoadConfigIfNeeded(options);
validateConfig();
return;
};

Expand Down
9 changes: 9 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ export const PLATFORM_VERSION_ERROR_TYPES = {
PLATFORM_VERSION_SPECIFIED_DOES_NOT_EXIST:
'PlatformVersionErrorType.PLATFORM_VERSION_SPECIFIED_DOES_NOT_EXIST',
} as const;

export const NO_VALIDATE_LIST = [
'init',
'clean',
'list',
'remove',
'use',
'delete',
] as const;

0 comments on commit dc5bf05

Please sign in to comment.