Skip to content

Commit ec30604

Browse files
refactor: extract config related utils (#46)
1 parent 4dea321 commit ec30604

File tree

5 files changed

+106
-57
lines changed

5 files changed

+106
-57
lines changed

plugins/plugin-tools/src/config.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import type {JunoConfig, JunoConfigFnOrObject} from '@junobuild/config';
2-
import {
3-
junoConfigExist as junoConfigExistTools,
4-
readJunoConfig as readJunoConfigTools,
5-
type ConfigFilename
6-
} from '@junobuild/config-loader';
71
import {
82
CMC_ID,
93
CYCLES_INDEX_ID,
@@ -20,6 +14,7 @@ import {
2014
SNS_WASM_ID
2115
} from './constants';
2216
import {JunoPluginError} from './error';
17+
import {assertJunoConfig, junoConfigExist, readJunoConfig} from './fs';
2318
import type {ConfigArgs, IcpIds, JunoParams} from './types';
2419

2520
export const useDockerContainer = ({params, mode}: ConfigArgs): boolean =>
@@ -158,27 +153,3 @@ export const container = ({
158153

159154
return undefined;
160155
};
161-
162-
const JUNO_CONFIG_FILE: {filename: ConfigFilename} = {filename: 'juno.config'};
163-
164-
const readJunoConfig = async ({mode}: ConfigArgs): Promise<JunoConfig> => {
165-
const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
166-
typeof userConfig === 'function' ? userConfig({mode}) : userConfig;
167-
168-
return await readJunoConfigTools({
169-
...JUNO_CONFIG_FILE,
170-
config
171-
});
172-
};
173-
174-
export const assertJunoConfig = async () => {
175-
const exist = await junoConfigExist();
176-
177-
if (!exist) {
178-
throw new JunoPluginError(
179-
`No Juno configuration found. Run "juno init" to configure your dapp.`
180-
);
181-
}
182-
};
183-
184-
const junoConfigExist = (): Promise<boolean> => junoConfigExistTools(JUNO_CONFIG_FILE);

plugins/plugin-tools/src/fs.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type {JunoConfig, JunoConfigFnOrObject} from '@junobuild/config';
2+
import {
3+
junoConfigExist as junoConfigExistTools,
4+
readJunoConfig as readJunoConfigTools,
5+
type ConfigFilename
6+
} from '@junobuild/config-loader';
7+
import {JunoPluginError} from './error';
8+
import type {ConfigArgs} from './types';
9+
10+
const JUNO_CONFIG_FILE: {filename: ConfigFilename} = {filename: 'juno.config'};
11+
12+
export const readJunoConfig = async ({mode}: ConfigArgs): Promise<JunoConfig> => {
13+
const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
14+
typeof userConfig === 'function' ? userConfig({mode}) : userConfig;
15+
16+
return await readJunoConfigTools({
17+
...JUNO_CONFIG_FILE,
18+
config
19+
});
20+
};
21+
22+
export const assertJunoConfig = async () => {
23+
const exist = await junoConfigExist();
24+
25+
if (!exist) {
26+
throw new JunoPluginError(
27+
`No Juno configuration found. Run "juno init" to configure your dapp.`
28+
);
29+
}
30+
};
31+
32+
export const junoConfigExist = (): Promise<boolean> => junoConfigExistTools(JUNO_CONFIG_FILE);

plugins/plugin-tools/src/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2-
assertJunoConfig,
32
container as containerConfig,
43
icpIds as icpIdsConfig,
54
orbiterId as orbiterIdConfig,
65
satelliteId as satelliteIdConfig,
76
useDockerContainer
87
} from './config';
8+
import {assertJunoConfig} from './fs';
99
import type {ConfigArgs, IcpIds} from './types';
1010

1111
export const initConfig = async (

plugins/plugin-tools/src/tests/config.spec.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import type {JunoConfig} from '@junobuild/config';
22
import * as configLoader from '@junobuild/config-loader';
33

4-
import {
5-
assertJunoConfig,
6-
container,
7-
icpIds,
8-
orbiterId,
9-
satelliteId,
10-
useDockerContainer
11-
} from '../config';
4+
import {container, icpIds, orbiterId, satelliteId, useDockerContainer} from '../config';
125
import {
136
CMC_ID,
147
CYCLES_INDEX_ID,
@@ -413,22 +406,4 @@ describe('config', () => {
413406
expect(url).toBeUndefined();
414407
});
415408
});
416-
417-
describe('assertJunoConfig', () => {
418-
beforeEach(() => {
419-
vi.clearAllMocks();
420-
});
421-
422-
it('throws if config does not exist', async () => {
423-
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(false);
424-
425-
await expect(assertJunoConfig()).rejects.toThrow(JunoPluginError);
426-
});
427-
428-
it('resolves if config exists', async () => {
429-
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(true);
430-
431-
await expect(assertJunoConfig()).resolves.toBeUndefined();
432-
});
433-
});
434409
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as configLoader from '@junobuild/config-loader';
2+
import {JunoPluginError} from '../error';
3+
import {assertJunoConfig, junoConfigExist, readJunoConfig} from '../fs';
4+
5+
vi.mock('@junobuild/config-loader', async () => {
6+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
7+
const actual = await vi.importActual<typeof import('@junobuild/config-loader')>(
8+
'@junobuild/config-loader'
9+
);
10+
11+
return {
12+
...actual,
13+
junoConfigExist: vi.fn(),
14+
readJunoConfig: vi.fn()
15+
};
16+
});
17+
18+
describe('fs', () => {
19+
beforeEach(() => {
20+
vi.resetAllMocks();
21+
vi.clearAllMocks();
22+
});
23+
24+
describe('readJunoConfig', () => {
25+
it('throws if config does not exist', async () => {
26+
vi.spyOn(configLoader, 'readJunoConfig').mockRejectedValueOnce(new Error());
27+
28+
await expect(assertJunoConfig()).rejects.toThrow();
29+
});
30+
31+
it('resolves if config exists', async () => {
32+
const mockConfig = {
33+
satellite: {ids: {development: 'dev-custom-id'}}
34+
};
35+
36+
vi.spyOn(configLoader, 'readJunoConfig').mockResolvedValue(mockConfig);
37+
38+
const result = await readJunoConfig({mode: 'development'});
39+
40+
expect(result).toEqual(mockConfig);
41+
});
42+
});
43+
44+
describe('assertJunoConfig', () => {
45+
it('throws if config does not exist', async () => {
46+
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(false);
47+
48+
await expect(assertJunoConfig()).rejects.toThrow(JunoPluginError);
49+
});
50+
51+
it('resolves if config exists', async () => {
52+
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(true);
53+
54+
await expect(assertJunoConfig()).resolves.toBeUndefined();
55+
});
56+
});
57+
58+
describe('junoConfigExist', () => {
59+
it('resolves false if config not exist', async () => {
60+
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(false);
61+
62+
await expect(junoConfigExist()).resolves.toBeFalsy();
63+
});
64+
65+
it('resolves true if config exists', async () => {
66+
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(true);
67+
68+
await expect(junoConfigExist()).resolves.toBeTruthy();
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)