Skip to content

Commit c1e6e04

Browse files
committed
export getLocalConfigFilePath and getGlobalConfigFilePath
1 parent 56ace63 commit c1e6e04

File tree

6 files changed

+67
-68
lines changed

6 files changed

+67
-68
lines changed

config/__tests__/config.test.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
updateAllowUsageTracking,
2525
updateDefaultCmsPublishMode,
2626
isConfigFlagEnabled,
27+
getGlobalConfigFilePath,
28+
getLocalConfigFilePathIfExists,
2729
} from '../index';
2830
import { HubSpotConfigAccount } from '../../types/Accounts';
2931
import { HubSpotConfig } from '../../types/Config';
@@ -37,13 +39,13 @@ import {
3739
OAUTH_AUTH_METHOD,
3840
API_KEY_AUTH_METHOD,
3941
} from '../../constants/auth';
40-
import {
41-
getGlobalConfigFilePath,
42-
getLocalConfigDefaultFilePath,
43-
formatConfigForWrite,
44-
} from '../utils';
42+
import { getLocalConfigDefaultFilePath, formatConfigForWrite } from '../utils';
4543
import { getDefaultAccountOverrideAccountId } from '../defaultAccountOverride';
46-
import { CONFIG_FLAGS, ENVIRONMENT_VARIABLES } from '../../constants/config';
44+
import {
45+
CONFIG_FLAGS,
46+
ENVIRONMENT_VARIABLES,
47+
HUBSPOT_CONFIGURATION_FOLDER,
48+
} from '../../constants/config';
4749
import * as utils from '../utils';
4850
import { CmsPublishMode } from '../../types/Files';
4951

@@ -128,6 +130,32 @@ describe('config/index', () => {
128130
cleanup();
129131
});
130132

133+
describe('getGlobalConfigFilePath()', () => {
134+
it('returns the global config file path', () => {
135+
const globalConfigFilePath = getGlobalConfigFilePath();
136+
expect(globalConfigFilePath).toBeDefined();
137+
expect(globalConfigFilePath).toContain(
138+
`${HUBSPOT_CONFIGURATION_FOLDER}/config.yml`
139+
);
140+
});
141+
});
142+
143+
describe('getLocalConfigFilePathIfExists()', () => {
144+
it('returns the nearest config file path', () => {
145+
const mockConfigPath = '/mock/path/hubspot.config.yml';
146+
mockFindup.mockReturnValue(mockConfigPath);
147+
148+
const localConfigPath = getLocalConfigFilePathIfExists();
149+
expect(localConfigPath).toBe(mockConfigPath);
150+
});
151+
152+
it('returns null if no config file found', () => {
153+
mockFindup.mockReturnValue(null);
154+
const localConfigPath = getLocalConfigFilePathIfExists();
155+
expect(localConfigPath).toBeNull();
156+
});
157+
});
158+
131159
describe('localConfigFileExists()', () => {
132160
it('returns true when local config exists', () => {
133161
mockFindup.mockReturnValueOnce(getLocalConfigDefaultFilePath());
@@ -243,15 +271,21 @@ describe('config/index', () => {
243271
describe('deleteConfigFileIfEmpty()', () => {
244272
it('deletes the config file if it is empty', () => {
245273
mockFs.existsSync.mockReturnValue(true);
246-
mockFs.readFileSync.mockReturnValueOnce('');
274+
mockFs.readFileSync.mockReturnValueOnce(yaml.dump({ accounts: [] }));
275+
jest
276+
.spyOn(utils, 'parseConfig')
277+
.mockReturnValueOnce({ accounts: [] } as HubSpotConfig);
247278
deleteConfigFileIfEmpty();
248279

249280
expect(mockFs.unlinkSync).toHaveBeenCalledWith(getConfigFilePath());
250281
});
251282

252283
it('does not delete the config file if it is not empty', () => {
253284
mockFs.existsSync.mockReturnValue(true);
254-
mockFs.readFileSync.mockReturnValueOnce('test-config-content');
285+
mockFs.readFileSync.mockReturnValueOnce(yaml.dump(CONFIG));
286+
jest
287+
.spyOn(utils, 'parseConfig')
288+
.mockReturnValueOnce(structuredClone(CONFIG));
255289
deleteConfigFileIfEmpty();
256290

257291
expect(mockFs.unlinkSync).not.toHaveBeenCalled();

config/__tests__/migrate.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import {
99
mergeConfigAccounts,
1010
} from '../migrate';
1111
import { HubSpotConfig } from '../../types/Config';
12-
import {
13-
getGlobalConfigFilePath,
14-
readConfigFile,
15-
writeConfigFile,
16-
} from '../utils';
12+
import { readConfigFile, writeConfigFile } from '../utils';
1713
import {
1814
DEFAULT_CMS_PUBLISH_MODE,
1915
HTTP_TIMEOUT,
@@ -25,7 +21,7 @@ import {
2521
import { ENVIRONMENTS } from '../../constants/environments';
2622
import { PERSONAL_ACCESS_KEY_AUTH_METHOD } from '../../constants/auth';
2723
import { PersonalAccessKeyConfigAccount } from '../../types/Accounts';
28-
import { createEmptyConfigFile } from '../index';
24+
import { createEmptyConfigFile, getGlobalConfigFilePath } from '../index';
2925

3026
jest.mock('fs', () => ({
3127
...jest.requireActual('fs'),
@@ -36,12 +32,12 @@ jest.mock('../utils', () => ({
3632
...jest.requireActual('../utils'),
3733
readConfigFile: jest.fn(),
3834
writeConfigFile: jest.fn(),
39-
getGlobalConfigFilePath: jest.fn(),
4035
}));
4136

4237
jest.mock('../index', () => ({
4338
...jest.requireActual('../index'),
4439
createEmptyConfigFile: jest.fn(),
40+
getGlobalConfigFilePath: jest.fn(),
4541
}));
4642

4743
describe('config/migrate', () => {

config/__tests__/utils.test.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import findup from 'findup-sync';
22
import fs from 'fs-extra';
33
import {
4-
getGlobalConfigFilePath,
5-
getLocalConfigFilePath,
64
getLocalConfigDefaultFilePath,
75
getConfigPathEnvironmentVariables,
86
readConfigFile,
@@ -134,32 +132,6 @@ describe('config/utils', () => {
134132
cleanupEnvironmentVariables();
135133
});
136134

137-
describe('getGlobalConfigFilePath()', () => {
138-
it('returns the global config file path', () => {
139-
const globalConfigFilePath = getGlobalConfigFilePath();
140-
expect(globalConfigFilePath).toBeDefined();
141-
expect(globalConfigFilePath).toContain(
142-
`${HUBSPOT_CONFIGURATION_FOLDER}/config.yml`
143-
);
144-
});
145-
});
146-
147-
describe('getLocalConfigFilePath()', () => {
148-
it('returns the nearest config file path', () => {
149-
const mockConfigPath = '/mock/path/hubspot.config.yml';
150-
mockFindup.mockReturnValue(mockConfigPath);
151-
152-
const localConfigPath = getLocalConfigFilePath();
153-
expect(localConfigPath).toBe(mockConfigPath);
154-
});
155-
156-
it('returns null if no config file found', () => {
157-
mockFindup.mockReturnValue(null);
158-
const localConfigPath = getLocalConfigFilePath();
159-
expect(localConfigPath).toBeNull();
160-
});
161-
});
162-
163135
describe('getLocalConfigDefaultFilePath()', () => {
164136
it('returns the default config path in current directory', () => {
165137
const mockCwdPath = '/mock/cwd';

config/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import fs from 'fs-extra';
2+
import findup from 'findup-sync';
23

34
import {
45
ACCOUNT_IDENTIFIERS,
6+
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME,
7+
GLOBAL_CONFIG_PATH,
58
HUBSPOT_CONFIG_OPERATIONS,
69
MIN_HTTP_TIMEOUT,
710
} from '../constants/config';
@@ -10,8 +13,6 @@ import { HubSpotConfig, ConfigFlag } from '../types/Config';
1013
import { CmsPublishMode } from '../types/Files';
1114
import { logger } from '../lib/logger';
1215
import {
13-
getGlobalConfigFilePath,
14-
getLocalConfigFilePath,
1516
readConfigFile,
1617
parseConfig,
1718
buildConfigFromEnvironment,
@@ -33,11 +34,26 @@ import { getValidEnv } from '../lib/environment';
3334
import { HubSpotConfigError } from '../models/HubSpotConfigError';
3435
import { HUBSPOT_CONFIG_ERROR_TYPES } from '../constants/config';
3536
import { isDeepEqual } from '../lib/isDeepEqual';
37+
import { getCwd } from '../lib/path';
3638

3739
const EMPTY_CONFIG = { accounts: [] };
3840

41+
export function getGlobalConfigFilePath(): string {
42+
return GLOBAL_CONFIG_PATH;
43+
}
44+
45+
export function getLocalConfigFilePathIfExists(): string | null {
46+
return findup(
47+
[
48+
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME,
49+
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME.replace('.yml', '.yaml'),
50+
],
51+
{ cwd: getCwd() }
52+
);
53+
}
54+
3955
export function localConfigFileExists(): boolean {
40-
return Boolean(getLocalConfigFilePath());
56+
return Boolean(getLocalConfigFilePathIfExists());
4157
}
4258

4359
export function globalConfigFileExists(): boolean {
@@ -59,7 +75,7 @@ function getConfigDefaultFilePath(): string {
5975
return globalConfigFilePath;
6076
}
6177

62-
const localConfigFilePath = getLocalConfigFilePath();
78+
const localConfigFilePath = getLocalConfigFilePathIfExists();
6379

6480
if (!localConfigFilePath) {
6581
throw new HubSpotConfigError(

config/migrate.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22

33
import { HubSpotConfig } from '../types/Config';
4-
import { createEmptyConfigFile } from './index';
4+
import { createEmptyConfigFile, getGlobalConfigFilePath } from './index';
55
import {
66
DEFAULT_CMS_PUBLISH_MODE,
77
HTTP_TIMEOUT,
@@ -12,12 +12,7 @@ import {
1212
AUTO_OPEN_BROWSER,
1313
ALLOW_AUTO_UPDATES,
1414
} from '../constants/config';
15-
import {
16-
getGlobalConfigFilePath,
17-
parseConfig,
18-
readConfigFile,
19-
writeConfigFile,
20-
} from './utils';
15+
import { parseConfig, readConfigFile, writeConfigFile } from './utils';
2116
import { ValueOf } from '../types/Utils';
2217

2318
export function getConfigAtPath(path: string): HubSpotConfig {

config/utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ import { i18n } from '../utils/lang';
3737
import { ValueOf } from '../types/Utils';
3838
import { HubSpotConfigError } from '../models/HubSpotConfigError';
3939

40-
export function getGlobalConfigFilePath(): string {
41-
return GLOBAL_CONFIG_PATH;
42-
}
43-
44-
export function getLocalConfigFilePath(): string | null {
45-
return findup(
46-
[
47-
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME,
48-
DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME.replace('.yml', '.yaml'),
49-
],
50-
{ cwd: getCwd() }
51-
);
52-
}
53-
5440
export function getLocalConfigDefaultFilePath(): string {
5541
return `${getCwd()}/${DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME}`;
5642
}

0 commit comments

Comments
 (0)