Skip to content

Commit

Permalink
Merge loadAndValidateOptions refactor p3
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Dec 20, 2024
2 parents ad5b50d + a85ce94 commit c377c98
Show file tree
Hide file tree
Showing 50 changed files with 2,786 additions and 2,246 deletions.
27 changes: 18 additions & 9 deletions acceptance-tests/tests/commands/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('hs create', () => {
});

it('creates a module', async () => {
await testState.cli.execute(
await testState.cli.executeWithTestConfig(
['create', 'module', FOLDERS.module.name],
['label', ENTER, ENTER, ENTER, 'y', ENTER, ENTER]
);
Expand All @@ -80,7 +80,7 @@ describe('hs create', () => {
});

it('creates a template', async () => {
await testState.cli.execute(
await testState.cli.executeWithTestConfig(
['create', 'template', FOLDERS.template.name],
[ENTER]
);
Expand All @@ -90,35 +90,44 @@ describe('hs create', () => {
});

it('website-theme', async () => {
await testState.cli.execute(['create', FOLDERS.websiteTheme.name]);
await testState.cli.executeWithTestConfig([
'create',
FOLDERS.websiteTheme.name,
]);
expect(
testState.existsInTestOutputDirectory(FOLDERS.websiteTheme.folder)
).toBe(true);
});

it('react-app', async () => {
await testState.cli.execute(['create', FOLDERS.reactApp.name]);
await testState.cli.executeWithTestConfig([
'create',
FOLDERS.reactApp.name,
]);
expect(testState.existsInTestOutputDirectory(FOLDERS.reactApp.folder)).toBe(
true
);
});

it('vue-app', async () => {
await testState.cli.execute(['create', FOLDERS.vueApp.name]);
await testState.cli.executeWithTestConfig(['create', FOLDERS.vueApp.name]);
expect(testState.existsInTestOutputDirectory(FOLDERS.vueApp.folder)).toBe(
true
);
});

it('webpack-serverless', async () => {
await testState.cli.execute(['create', FOLDERS.webpackServerless.name]);
await testState.cli.executeWithTestConfig([
'create',
FOLDERS.webpackServerless.name,
]);
expect(
testState.existsInTestOutputDirectory(FOLDERS.webpackServerless.folder)
).toBe(true);
});

it('api-sample', async () => {
await testState.cli.execute(
await testState.cli.executeWithTestConfig(
['create', FOLDERS.apiSample.name, FOLDERS.apiSample.name],
[ENTER, ENTER]
);
Expand All @@ -129,14 +138,14 @@ describe('hs create', () => {
});

it('app', async () => {
await testState.cli.execute(['create', FOLDERS.app.name]);
await testState.cli.executeWithTestConfig(['create', FOLDERS.app.name]);
expect(testState.existsInTestOutputDirectory(FOLDERS.app.folder)).toBe(
true
);
});

it('function', async () => {
await testState.cli.execute(
await testState.cli.executeWithTestConfig(
['create', 'function'],
[
FOLDERS.function.name,
Expand Down
2 changes: 1 addition & 1 deletion acceptance-tests/tests/workflows/cmsTemplateFlow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('CMS Template Flow', () => {

describe('hs create', () => {
it('should create a CMS template', async () => {
await testState.cli.execute(
await testState.cli.executeWithTestConfig(
['create', 'template', TEMPLATE_NAME],
[ENTER]
);
Expand Down
45 changes: 15 additions & 30 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
const yargs = require('yargs');
const updateNotifier = require('update-notifier');
const chalk = require('chalk');
const fs = require('fs');

const { logger } = require('@hubspot/local-dev-lib/logger');
const { addUserAgentHeader } = require('@hubspot/local-dev-lib/http');
const {
loadConfig,
getAndLoadConfigIfNeeded,
configFileExists,
getConfigPath,
validateConfig,
Expand Down Expand Up @@ -175,42 +173,29 @@ const NO_CONFIG_VALIDATION = {

const loadConfigMiddleware = async options => {
const maybeValidateConfig = () => {
if (shouldValidate(options, NO_CONFIG_VALIDATION)) {
if (!validateConfig()) {
process.exit(EXIT_CODES.ERROR);
}
if (shouldValidate(options, NO_CONFIG_VALIDATION) && !validateConfig()) {
process.exit(EXIT_CODES.ERROR);
}
};

// Load the new config and check for the conflicting config flag
if (configFileExists(true)) {
loadConfig('', options);

if (options.config) {
logger.error(
i18n(`${i18nKey}.loadConfigMiddleware.configFileExists`, {
configPath: getConfigPath(),
})
);
process.exit(EXIT_CODES.ERROR);
}
maybeValidateConfig();
return;
}

// We need to load the config when options.config exists,
// so that getAccountIdFromConfig() in injectAccountIdMiddleware reads from the right config
if (options.config && fs.existsSync(options.config)) {
if (configFileExists(true) && options.config) {
logger.error(
i18n(`${i18nKey}.loadConfigMiddleware.configFileExists`, {
configPath: getConfigPath(),
})
);
process.exit(EXIT_CODES.ERROR);
} else {
// We need to load the config when options.config exists,
// so that getAccountIdFromConfig() in injectAccountIdMiddleware reads from the right config
const { config: configPath } = options;
loadConfig(configPath, options);
maybeValidateConfig();
return;
if (!options._.includes('init')) {
loadConfig(configPath, options);
}
}

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

const checkAndWarnGitInclusionMiddleware = () => {
Expand Down
2 changes: 1 addition & 1 deletion commands/__tests__/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('commands/create', () => {
it('should support the correct options', () => {
createCommand.builder(yargs);

expect(yargs.option).toHaveBeenCalledTimes(2);
expect(yargs.option).toHaveBeenCalledTimes(3);
expect(yargs.option).toHaveBeenCalledWith(
'internal',
expect.objectContaining({ type: 'boolean', hidden: true })
Expand Down
7 changes: 6 additions & 1 deletion commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
const fs = require('fs-extra');
const { logError } = require('../lib/errorHandlers/index');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { setLogLevel, addGlobalOptions } = require('../lib/commonOpts');
const {
setLogLevel,
addGlobalOptions,
addConfigOptions,
} = require('../lib/commonOpts');
const { resolveLocalPath } = require('../lib/filesystem');
const { trackCommandUsage } = require('../lib/usageTracking');
const assets = require('./create/index');
Expand Down Expand Up @@ -117,6 +121,7 @@ exports.builder = yargs => {
hidden: true,
});

addConfigOptions(yargs);
addGlobalOptions(yargs);

return yargs;
Expand Down
10 changes: 5 additions & 5 deletions commands/project/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ const {
addConfigOptions,
addUseEnvironmentOptions,
} = require('../../../lib/commonOpts');
const {
getProjectConfig,
pollDeployStatus,
getProjectDetailUrl,
} = require('../../../lib/projects');
const { getProjectConfig } = require('../../../lib/projects');
const { getProjectDetailUrl } = require('../../../lib/projects/urls');
const { pollDeployStatus } = require('../../../lib/projects/buildAndDeploy');
const { projectNamePrompt } = require('../../../lib/prompts/projectNamePrompt');
const { promptUser } = require('../../../lib/prompts/promptUtils');
const { trackCommandUsage } = require('../../../lib/usageTracking');
Expand All @@ -34,6 +32,8 @@ jest.mock('@hubspot/local-dev-lib/config');
jest.mock('../../../lib/commonOpts');
jest.mock('../../../lib/validation');
jest.mock('../../../lib/projects');
jest.mock('../../../lib/projects/urls');
jest.mock('../../../lib/projects/buildAndDeploy');
jest.mock('../../../lib/prompts/projectNamePrompt');
jest.mock('../../../lib/prompts/promptUtils');
jest.mock('../../../lib/usageTracking');
Expand Down
8 changes: 3 additions & 5 deletions commands/project/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const {
deployProject,
fetchProject,
} = require('@hubspot/local-dev-lib/api/projects');
const {
getProjectConfig,
pollDeployStatus,
getProjectDetailUrl,
} = require('../../lib/projects');
const { getProjectConfig } = require('../../lib/projects');
const { pollDeployStatus } = require('../../lib/projects/buildAndDeploy');
const { getProjectDetailUrl } = require('../../lib/projects/urls');
const { projectNamePrompt } = require('../../lib/prompts/projectNamePrompt');
const { promptUser } = require('../../lib/prompts/promptUtils');
const { i18n } = require('../../lib/lang');
Expand Down
2 changes: 1 addition & 1 deletion commands/project/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {
findProjectComponents,
getProjectComponentTypes,
COMPONENT_TYPES,
} = require('../../lib/projectStructure');
} = require('../../lib/projects/structure');
const {
confirmDefaultAccountIsTarget,
suggestRecommendedNestedAccount,
Expand Down
2 changes: 1 addition & 1 deletion commands/project/listBuilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const { getTableContents, getTableHeader } = require('../../lib/ui/table');
const { uiBetaTag, uiLink } = require('../../lib/ui');
const {
getProjectConfig,
getProjectDetailUrl,
validateProjectConfig,
} = require('../../lib/projects');
const { getProjectDetailUrl } = require('../../lib/projects/urls');
const moment = require('moment');
const { promptUser } = require('../../lib/prompts/promptUtils');
const { isHubSpotHttpError } = require('@hubspot/local-dev-lib/errors/index');
Expand Down
7 changes: 2 additions & 5 deletions commands/project/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ const {
const { trackCommandUsage } = require('../../lib/usageTracking');
const { i18n } = require('../../lib/lang');
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
getProjectConfig,
getProjectDetailUrl,
ensureProjectExists,
} = require('../../lib/projects');
const { getProjectConfig, ensureProjectExists } = require('../../lib/projects');
const { getProjectDetailUrl } = require('../../lib/projects/urls');
const { projectNamePrompt } = require('../../lib/prompts/projectNamePrompt');
const { uiBetaTag } = require('../../lib/ui');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
Expand Down
8 changes: 5 additions & 3 deletions commands/project/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const { trackCommandUsage } = require('../../lib/usageTracking');
const {
ensureProjectExists,
getProjectConfig,
handleProjectUpload,
logFeedbackMessage,
validateProjectConfig,
pollProjectBuildAndDeploy,
displayWarnLogs,
} = require('../../lib/projects');
const { handleProjectUpload } = require('../../lib/projects/upload');
const {
displayWarnLogs,
pollProjectBuildAndDeploy,
} = require('../../lib/projects/buildAndDeploy');
const { i18n } = require('../../lib/lang');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { isSpecifiedError } = require('@hubspot/local-dev-lib/errors/index');
Expand Down
10 changes: 6 additions & 4 deletions commands/project/watch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
const { i18n } = require('../../lib/lang');
const { createWatcher } = require('../../lib/projectsWatch');
const { createWatcher } = require('../../lib/projects/watch');
const { logError, ApiErrorContext } = require('../../lib/errorHandlers/index');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { PROJECT_ERROR_TYPES } = require('../../lib/constants');
Expand All @@ -14,12 +14,14 @@ const { uiBetaTag } = require('../../lib/ui');
const {
ensureProjectExists,
getProjectConfig,
handleProjectUpload,
pollBuildStatus,
pollDeployStatus,
validateProjectConfig,
logFeedbackMessage,
} = require('../../lib/projects');
const { handleProjectUpload } = require('../../lib/projects/upload');
const {
pollBuildStatus,
pollDeployStatus,
} = require('../../lib/projects/buildAndDeploy');
const {
cancelStagedBuild,
fetchProjectBuilds,
Expand Down
2 changes: 1 addition & 1 deletion commands/theme/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const { getProjectConfig } = require('../../lib/projects');
const {
findProjectComponents,
COMPONENT_TYPES,
} = require('../../lib/projectStructure');
} = require('../../lib/projects/structure');
const { preview } = require('@hubspot/theme-preview-dev-server');
const { hasFeature } = require('../../lib/hasFeature');
const i18nKey = 'commands.theme.subcommands.preview';
Expand Down
32 changes: 18 additions & 14 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -1094,16 +1094,6 @@ en:
checkIfParentAccountIsAuthed:
notAuthedError: "To develop this project locally, run {{ authCommand }} to authenticate the App Developer Account {{ accountId }} associated with {{ accountIdentifier }}."
projects:
uploadProjectFiles:
add: "Uploading {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
fail: "Failed to upload {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
succeed: "Uploaded {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
buildCreated: "Project \"{{ projectName }}\" uploaded and build #{{ buildId }} created"
handleProjectUpload:
emptySource: "Source directory \"{{ srcDir }}\" is empty. Add files to your project and rerun `{{#yellow}}hs project upload{{/yellow}}` to upload them to HubSpot."
compressed: "Project files compressed: {{ byteCount }} bytes"
compressing: "Compressing build files to \"{{ path }}\""
fileFiltered: "Ignore rule triggered for \"{{ filename }}\""
validateProjectConfig:
configNotFound: "Unable to locate a project configuration file. Try running again from a project directory, or run {{ createCommand }} to create a new project."
configMissingFields: "The project configuruation file is missing required fields."
Expand All @@ -1116,6 +1106,11 @@ en:
notFound: "Your project {{#bold}}{{ projectName }}{{/bold}} could not be found in {{#bold}}{{ accountIdentifier }}{{/bold}}."
pollFetchProject:
checkingProject: "Checking if project exists in {{ accountIdentifier }}"
unableToFindAutodeployStatus: "Unable to find the auto deploy for build #{{ buildId }}. This deploy may have been skipped. {{ viewDeploysLink }}."
logFeedbackMessage:
feedbackHeader: "We'd love to hear your feedback!"
feedbackMessage: "How are you liking the new projects and developer tools? \n > Run `{{#yellow}}hs feedback{{/yellow}}` to let us know what you think!\n"
projectBuildAndDeploy:
makePollTaskStatusFunc:
componentCountSingular: "Found 1 component in this project"
componentCount: "Found {{ numComponents }} components in this project"
Expand All @@ -1127,10 +1122,17 @@ en:
buildSucceededAutomaticallyDeploying: "Build #{{ buildId }} succeeded. {{#bold}}Automatically deploying{{/bold}} to {{ accountIdentifier }}\n"
cleanedUpTempFile: "Cleaned up temporary file {{ path }}"
viewDeploys: "View all deploys for this project in HubSpot"
unableToFindAutodeployStatus: "Unable to find the auto deploy for build #{{ buildId }}. This deploy may have been skipped. {{ viewDeploysLink }}."
logFeedbackMessage:
feedbackHeader: "We'd love to hear your feedback!"
feedbackMessage: "How are you liking the new projects and developer tools? \n > Run `{{#yellow}}hs feedback{{/yellow}}` to let us know what you think!\n"
projectUpload:
uploadProjectFiles:
add: "Uploading {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
fail: "Failed to upload {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
succeed: "Uploaded {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
buildCreated: "Project \"{{ projectName }}\" uploaded and build #{{ buildId }} created"
handleProjectUpload:
emptySource: "Source directory \"{{ srcDir }}\" is empty. Add files to your project and rerun `{{#yellow}}hs project upload{{/yellow}}` to upload them to HubSpot."
compressed: "Project files compressed: {{ byteCount }} bytes"
compressing: "Compressing build files to \"{{ path }}\""
fileFiltered: "Ignore rule triggered for \"{{ filename }}\""
ui:
betaTag: "{{#bold}}[BETA]{{/bold}}"
betaWarning:
Expand Down Expand Up @@ -1551,6 +1553,8 @@ en:
counts:
errors: '{{#bold}}Errors:{{/bold}} {{ count }}'
warnings: "{{#bold}}Warning:{{/bold}} {{ count }}"
oauth:
missingClientId: "Error building oauth URL: missing client ID."



2 changes: 1 addition & 1 deletion lib/DevServerManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
const { logger } = require('@hubspot/local-dev-lib/logger');
const { COMPONENT_TYPES } = require('./projectStructure');
const { COMPONENT_TYPES } = require('./projects/structure');
const { i18n } = require('./lang');
const { promptUser } = require('./prompts/promptUtils');
const { DevModeInterface } = require('@hubspot/ui-extensions-dev-server');
Expand Down
Loading

0 comments on commit c377c98

Please sign in to comment.