Skip to content

Commit

Permalink
Merge branch 'next' into br/lib-ts-port-2
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Dec 20, 2024
2 parents d625070 + 58332a2 commit fedda8a
Show file tree
Hide file tree
Showing 16 changed files with 1,109 additions and 906 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
11 changes: 5 additions & 6 deletions acceptance-tests/tests/workflows/accountManagementFlow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ describe('Account Management Flow', () => {

describe('hs accounts list', () => {
it('should not list the removed authenticated account', async () => {
const output = await testState.cli.executeWithTestConfig([
'accounts',
'list',
]);

expect(output).not.toContain(accountName);
await expect(() =>
testState.cli.executeWithTestConfig(['accounts', 'list'])
).rejects.toThrow(
/There are no accounts defined in the configuration file/
);
});
});

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
47 changes: 24 additions & 23 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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,
} = require('@hubspot/local-dev-lib/config');
const { logError } = require('../lib/errorHandlers/index');
const {
Expand Down Expand Up @@ -148,33 +147,35 @@ const setRequestHeaders = () => {
addUserAgentHeader('HubSpot CLI', pkg.version);
};

const loadConfigMiddleware = async options => {
// Load the new config and check for the conflicting config flag
if (configFileExists(true)) {
loadConfig('', options);
const NO_CONFIG_VALIDATION = {
init: { skip: true },
auth: { skip: true },
};

if (options.config) {
logger.error(
i18n(`${i18nKey}.loadConfigMiddleware.configFileExists`, {
configPath: getConfigPath(),
})
);
const loadConfigMiddleware = async options => {
const maybeValidateConfig = () => {
const shouldValidate = options._.every(
command =>
!(NO_CONFIG_VALIDATION[command] && NO_CONFIG_VALIDATION[command].skip)
);
if (shouldValidate && !validateConfig()) {
process.exit(EXIT_CODES.ERROR);
}
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 if (!options._.includes('init')) {
const { config: configPath } = options;
await loadConfig(configPath, options);
return;
loadConfig(configPath, options);
}

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

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
6 changes: 4 additions & 2 deletions commands/project/__tests__/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-nocheck
const yargs = require('yargs');
const { addUseEnvironmentOptions } = require('../../../lib/commonOpts');
const ProjectLogsManager = require('../../../lib/ProjectLogsManager');
const {
ProjectLogsManager,
} = require('../../../lib/projects/ProjectLogsManager');
const {
projectLogsPrompt,
} = require('../../../lib/prompts/projectsLogsPrompt');
Expand All @@ -16,7 +18,7 @@ jest.mock('@hubspot/local-dev-lib/logger');
jest.mock('../../../lib/commonOpts');
jest.mock('../../../lib/usageTracking');
jest.mock('../../../lib/validation');
jest.mock('../../../lib/ProjectLogsManager');
jest.mock('../../../lib/projects/ProjectLogsManager');
jest.mock('../../../lib/prompts/projectsLogsPrompt');
jest.mock('../../../lib/ui/table');
jest.mock('../../../lib/errorHandlers');
Expand Down
2 changes: 1 addition & 1 deletion commands/project/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { uiBetaTag, uiLine, uiLink } = require('../../lib/ui');
const { projectLogsPrompt } = require('../../lib/prompts/projectsLogsPrompt');
const { i18n } = require('../../lib/lang');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const ProjectLogsManager = require('../../lib/ProjectLogsManager');
const { ProjectLogsManager } = require('../../lib/projects/ProjectLogsManager');

const i18nKey = 'commands.project.subcommands.logs';

Expand Down
2 changes: 2 additions & 0 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ en:
noFunctionsInProject: "There aren't any functions in this project\n\t- Run `{{#orange}}hs project logs --help{{/orange}}` to learn more about logs\n\t- {{link}} to learn more about serverless functions"
noFunctionWithName: "No function with name \"{{ name }}\""
functionNotDeployed: "The function with name \"{{ name }}\" is not deployed"
projectLogsManagerNotInitialized: "Function called on ProjectLogsManager before initialization"
generic: "Error fetching logs"
logs:
showingLogs: "Showing logs for:"
hubspotLogsDirectLink: "View function logs in HubSpot"
Expand Down
15 changes: 7 additions & 8 deletions lib/__tests__/ProjectLogsManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
const ProjectLogsManager = require('../ProjectLogsManager');
const { ProjectLogsManager } = require('../projects/ProjectLogsManager');
const { getProjectConfig, ensureProjectExists } = require('../projects');
const {
fetchProjectComponentsMetadata,
Expand All @@ -8,7 +8,7 @@ const {
jest.mock('../projects');
jest.mock('@hubspot/local-dev-lib/api/projects');

describe('lib/ProjectLogsManager', () => {
describe('lib/projects/ProjectLogsManager', () => {
const accountId = 12345678;
const appId = 999999;
const projectName = 'super cool test project';
Expand Down Expand Up @@ -143,8 +143,8 @@ describe('lib/ProjectLogsManager', () => {
});

describe('getFunctionNames', () => {
it('should return an empty array if functions is nullable', async () => {
ProjectLogsManager.functions = undefined;
it('should return an empty array if functions is empty', async () => {
ProjectLogsManager.functions = [];
expect(ProjectLogsManager.getFunctionNames()).toEqual([]);
});

Expand All @@ -158,8 +158,8 @@ describe('lib/ProjectLogsManager', () => {
});

describe('setFunction', () => {
it('should throw an error when functions is nullable', async () => {
ProjectLogsManager.functions = undefined;
it('should throw an error when functions is empty', async () => {
ProjectLogsManager.functions = [];
expect(() => ProjectLogsManager.setFunction('foo')).toThrow(
`There aren't any functions in this project`
);
Expand All @@ -180,15 +180,14 @@ describe('lib/ProjectLogsManager', () => {
name: 'APP_FUNCTION',
},
deployOutput: {
endpoint: { path: 'yooooooo', method: ['GET'] },
endpoint: { path: 'yooooooo', methods: ['GET'] },
},
};
ProjectLogsManager.functions = [functionToChoose];
ProjectLogsManager.setFunction('function1');
expect(ProjectLogsManager.functionName).toEqual('function1');
expect(ProjectLogsManager.endpointName).toEqual('yooooooo');
expect(ProjectLogsManager.selectedFunction).toEqual(functionToChoose);
expect(ProjectLogsManager.method).toEqual(['GET']);
expect(ProjectLogsManager.isPublicFunction).toEqual(true);
});

Expand Down
Loading

0 comments on commit fedda8a

Please sign in to comment.