Skip to content

Commit 9a31a71

Browse files
Merge pull request #1435 from opencomponents/fix-oc-config
Fix loading info correctly
2 parents 1c90373 + 27067dc commit 9a31a71

File tree

6 files changed

+135
-104
lines changed

6 files changed

+135
-104
lines changed

src/cli/domain/get-mocked-plugins.ts

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import path from 'node:path';
2-
import fs from 'fs-extra';
32

43
import strings from '../../resources/';
5-
import settings from '../../resources/settings';
64
import type { Logger } from '../logger';
75
import { getOcConfig } from './ocConfig';
86

@@ -98,44 +96,18 @@ const registerDynamicMocks = (
9896
})
9997
.filter((pluginMock): pluginMock is PluginMock => !!pluginMock);
10098

101-
const findPath = (
102-
pathToResolve: string,
103-
fileName: string
104-
): string | undefined => {
105-
const rootDir = fs.realpathSync('.');
106-
const fileToResolve = path.join(pathToResolve, fileName);
107-
108-
if (!fs.existsSync(fileToResolve)) {
109-
if (pathToResolve === rootDir) {
110-
return undefined;
111-
}
112-
const getParent = (pathToResolve: string) =>
113-
pathToResolve.split('/').slice(0, -1).join('/');
114-
115-
const parentDir = pathToResolve ? getParent(pathToResolve) : rootDir;
116-
117-
return findPath(parentDir, fileName);
118-
}
119-
120-
return fileToResolve;
121-
};
122-
12399
export default function getMockedPlugins(
124100
logger: Logger,
125-
componentsDir: string
101+
componentsDir?: string
126102
): PluginMock[] {
127103
componentsDir = path.resolve(componentsDir || '.');
128104

129105
let plugins: PluginMock[] = [];
130-
const ocJsonFileName = settings.configFile.src.replace('./', '');
131-
const ocJsonPath = findPath(componentsDir, ocJsonFileName);
132-
133-
if (!ocJsonPath) {
134-
return plugins;
135-
}
136106

137-
const content = getOcConfig(ocJsonPath);
138-
const ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length);
107+
const content = getOcConfig(componentsDir);
108+
const ocJsonLocation = content.sourcePath
109+
? path.dirname(content.sourcePath)
110+
: componentsDir;
139111

140112
if (!content.development?.plugins) {
141113
return plugins;

src/cli/domain/ocConfig.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import path from 'node:path';
23
import settings from '../../resources/settings';
34

45
export interface OpenComponentsConfig {
@@ -37,6 +38,7 @@ export interface OpenComponentsConfig {
3738
}
3839

3940
type ParsedConfig = {
41+
sourcePath?: string;
4042
registries: string[];
4143
development: {
4244
plugins: {
@@ -50,6 +52,28 @@ type ParsedConfig = {
5052
};
5153
};
5254

55+
const findPath = (
56+
pathToResolve: string,
57+
fileName: string
58+
): string | undefined => {
59+
const rootDir = fs.realpathSync('.');
60+
const fileToResolve = path.join(pathToResolve, fileName);
61+
62+
if (!fs.existsSync(fileToResolve)) {
63+
if (pathToResolve === rootDir) {
64+
return undefined;
65+
}
66+
const getParent = (pathToResolve: string) =>
67+
pathToResolve.split('/').slice(0, -1).join('/');
68+
69+
const parentDir = pathToResolve ? getParent(pathToResolve) : rootDir;
70+
71+
return findPath(parentDir, fileName);
72+
}
73+
74+
return fileToResolve;
75+
};
76+
5377
function parseConfig(config: OpenComponentsConfig): ParsedConfig {
5478
const plugins = {
5579
...(config.mocks?.plugins || {}),
@@ -68,12 +92,15 @@ function parseConfig(config: OpenComponentsConfig): ParsedConfig {
6892
return parsedConfig;
6993
}
7094

71-
export function getOcConfig(path?: string): ParsedConfig {
95+
export function getOcConfig(folder?: string): ParsedConfig {
96+
const configPath = folder
97+
? findPath(folder, settings.configFile.src.replace('./', '')) ||
98+
settings.configFile.src
99+
: settings.configFile.src;
100+
72101
try {
73-
const config = JSON.parse(
74-
fs.readFileSync(path || settings.configFile.src, 'utf8')
75-
);
76-
return parseConfig(config);
102+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
103+
return { ...parseConfig(config), sourcePath: configPath };
77104
} catch {
78105
return {
79106
registries: [],
@@ -85,8 +112,9 @@ export function getOcConfig(path?: string): ParsedConfig {
85112
}
86113

87114
export function setOcConfig(config: ParsedConfig, path?: string) {
115+
const { sourcePath, ...rest } = config;
88116
fs.writeFileSync(
89-
path || settings.configFile.src,
90-
JSON.stringify(parseConfig(config), null, 2)
117+
path || sourcePath || settings.configFile.src,
118+
JSON.stringify(rest, null, 2)
91119
);
92120
}

src/cli/facade/dev.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import path from 'node:path';
22
import { promisify } from 'node:util';
33
import colors from 'colors/safe';
4-
import fs from 'fs-extra';
54
import getPortCb from 'getport';
65
import livereload from 'livereload';
76
import { fromPromise } from 'universalify';
87

98
import * as oc from '../../index';
109
import strings from '../../resources/index';
11-
import settings from '../../resources/settings';
1210
import getMockedPlugins from '../domain/get-mocked-plugins';
1311
import handleDependencies from '../domain/handle-dependencies';
1412
import type { Local } from '../domain/local';
13+
import { getOcConfig } from '../domain/ocConfig';
1514
import watch from '../domain/watch';
1615
import type { Logger } from '../logger';
1716

@@ -46,7 +45,7 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
4645
let fallbackClient = false;
4746
if (!fallbackRegistryUrl) {
4847
try {
49-
const localConfig = await fs.readJson(settings.configFile.src);
48+
const localConfig = getOcConfig(componentsDir);
5049
if (
5150
!fallbackRegistryUrl &&
5251
typeof localConfig.development?.fallback?.url === 'string'

src/registry/routes/component-preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function componentPreview(
3333
previewView({
3434
component,
3535
fallbackClient: res.conf.fallbackClient
36-
? res.conf.fallbackRegistryUrl
36+
? `${res.conf.fallbackRegistryUrl.replace(/\/$/, '')}/oc-client/client.dev.js`
3737
: undefined,
3838
href: res.conf.baseUrl,
3939
liveReload,

test/unit/cli-domain-get-mocked-plugins.js

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,32 @@ describe('cli : domain : get-mocked-plugins', () => {
5151

5252
describe('when setting up mocked plugins', () => {
5353
describe('when componentsDir parameter is undefined', () => {
54-
const joinStub = sinon.stub();
54+
let getOcConfigMock;
5555

5656
beforeEach(() => {
57-
initialise({ pathJoin: joinStub });
57+
getOcConfigMock = sinon.stub().returns({ registries: [], mocks: { plugins: {} } });
58+
initialise({ getOcConfig: getOcConfigMock });
5859
getMockedPlugins(logMock, undefined);
5960
});
6061

6162
it('should use . as default', () => {
62-
expect(joinStub.args[0][0]).to.equal('.');
63+
expect(getOcConfigMock.called).to.be.true;
64+
expect(getOcConfigMock.args[0][0]).to.equal('.');
6365
});
6466
});
6567

6668
describe('when componentsDir parameter is omitted', () => {
67-
const joinStub = sinon.stub();
69+
let getOcConfigMock;
6870

6971
beforeEach(() => {
70-
initialise({ pathJoin: joinStub });
72+
getOcConfigMock = sinon.stub().returns({ registries: [], mocks: { plugins: {} } });
73+
initialise({ getOcConfig: getOcConfigMock });
7174
getMockedPlugins(logMock);
7275
});
7376

7477
it('should use . as default', () => {
75-
expect(joinStub.args[0][0]).to.equal('.');
78+
expect(getOcConfigMock.called).to.be.true;
79+
expect(getOcConfigMock.args[0][0]).to.equal('.');
7680
});
7781
});
7882

@@ -90,65 +94,26 @@ describe('cli : domain : get-mocked-plugins', () => {
9094
const getOcConfigMock = sinon.stub().returns(ocJsonComponent);
9195

9296
beforeEach(() => {
93-
initialise({fs: {
94-
existsSync: sinon.stub().returns(true),
95-
}, getOcConfig: getOcConfigMock});
97+
initialise({ getOcConfig: getOcConfigMock });
9698
result = getMockedPlugins(logMock, '/root/components/');
9799
});
98100

99-
it('should use components folder oc.json as default', () => {
101+
it('should return plugins from the provided components folder config', () => {
100102
expect(getOcConfigMock.calledOnce).to.be.true;
101-
expect(getOcConfigMock.args[0][0]).to.equal('/root/components/oc.json');
103+
expect(getOcConfigMock.args[0][0]).to.equal('/root/components/');
102104
expect(result.length).to.equal(2);
103105
});
104106
});
105107

106-
describe('when oc.json is in root folder', () => {
107-
let result;
108-
const ocJsonComponent = {
109-
registries: [],
110-
development: {
111-
plugins: {
112-
static: { foo: 1, bar: 2 }
113-
}
114-
}
115-
};
116-
const ocJsonRoot = {
117-
registries: [],
118-
development: {
119-
plugins: {
120-
static: { foo: 1, bar: 2, baz: 3 }
121-
}
122-
}
123-
};
124-
125-
const getOcConfigMock = sinon.stub();
126-
const existsMock = sinon.stub();
127-
128-
getOcConfigMock.withArgs('/root/components/oc.json').returns(ocJsonComponent);
129-
getOcConfigMock.withArgs('/root/oc.json').returns(ocJsonRoot);
130-
131-
existsMock.withArgs('/root/components/oc.json').returns(false);
132-
existsMock.withArgs('/root/oc.json').returns(true);
133-
134-
beforeEach(() => {
135-
initialise({fs:{
136-
existsSync: existsMock,
137-
}, getOcConfig: getOcConfigMock});
138-
result = getMockedPlugins(logMock, '/root/components/');
139-
});
140-
141-
it('should use root oc.json', () => {
142-
expect(result.length).to.equal(3);
143-
});
144-
});
145108

146109
describe('when oc.json is missing', () => {
147110
let result;
148111
beforeEach(() => {
149-
initialise({fs:{
150-
existsSync: sinon.stub().returns(false)
151-
}});
112+
const getOcConfigMock = sinon.stub().returns({
113+
registries: [],
114+
development: { plugins: {} }
115+
});
116+
initialise({ getOcConfig: getOcConfigMock });
152117
result = getMockedPlugins(logMock, '/root/components/');
153118
});
154119

@@ -161,16 +126,13 @@ describe('cli : domain : get-mocked-plugins', () => {
161126
let result;
162127
const ocJson = {
163128
registries: [],
164-
mocks: {
129+
development: {
165130
plugins: {}
166131
}
167132
};
168133

169134
beforeEach(() => {
170-
initialise({fs:{
171-
existsSync: sinon.stub().returns(true),
172-
readJsonSync: sinon.stub().returns(ocJson)
173-
}});
135+
initialise({ getOcConfig: sinon.stub().returns(ocJson) });
174136
result = getMockedPlugins(logMock, '/root/components/');
175137
});
176138

0 commit comments

Comments
 (0)