Skip to content

Commit 43d7c46

Browse files
Merge pull request #1276 from opencomponents/dev-filter-components
[CLI-FEATURE] filter components to run when running oc dev
2 parents 05b7276 + ac1cae0 commit 43d7c46

File tree

6 files changed

+174
-94
lines changed

6 files changed

+174
-94
lines changed

src/cli/commands.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export default {
5252
boolean: false,
5353
description: 'Url prefix for registry server',
5454
default: ''
55+
},
56+
components: {
57+
array: true,
58+
description:
59+
'List of component names that you want to be packaged and exposed on dev registry'
5560
}
5661
},
5762
usage: 'Usage: $0 dev <dirPath> [port] [baseUrl] [options]'

src/cli/domain/get-components-by-dir.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@ import { Component } from '../../types';
55
export default function getComponentsByDir() {
66
return (
77
componentsDir: string,
8-
callback: (err: Error | null, data: string[]) => void
8+
componentsToRunOrCb:
9+
| string[]
10+
| ((err: Error | null, data: string[]) => void),
11+
callbackMaybe?: (err: Error | null, data: string[]) => void
912
): void => {
13+
const componentsToRun =
14+
typeof componentsToRunOrCb === 'function'
15+
? undefined
16+
: componentsToRunOrCb;
17+
const callback =
18+
typeof componentsToRunOrCb === 'function'
19+
? componentsToRunOrCb
20+
: callbackMaybe!;
21+
1022
const isOcComponent = function (file: string) {
1123
const filePath = path.resolve(componentsDir, file);
1224
const packagePath = path.join(filePath, 'package.json');
@@ -31,6 +43,11 @@ export default function getComponentsByDir() {
3143

3244
try {
3345
dirContent = fs.readdirSync(componentsDir);
46+
if (componentsToRun) {
47+
dirContent = dirContent.filter(content =>
48+
componentsToRun.includes(content)
49+
);
50+
}
3451
} catch (err) {
3552
return callback(null, []);
3653
}

src/cli/facade/dev.ts

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const dev =
2828
baseUrl: string;
2929
fallbackRegistryUrl: string;
3030
hotReloading?: boolean;
31+
components: string[];
3132
watch?: boolean;
3233
verbose?: boolean;
3334
production?: boolean;
@@ -137,96 +138,101 @@ const dev =
137138
};
138139

139140
logger.warn(cliMessages.SCANNING_COMPONENTS, true);
140-
local.getComponentsByDir(componentsDir, (err, components) => {
141-
if (_.isEmpty(components)) {
142-
err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND) as any;
143-
callback(err, undefined as any);
144-
return logger.err(String(err));
145-
}
141+
local.getComponentsByDir(
142+
componentsDir,
143+
opts.components as any,
144+
(err, components) => {
145+
if (_.isEmpty(components)) {
146+
err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND) as any;
147+
callback(err, undefined as any);
148+
return logger.err(String(err));
149+
}
146150

147-
logger.ok('OK');
148-
_.forEach(components, component =>
149-
logger.log(colors.green('├── ') + component)
150-
);
151+
logger.ok('OK');
152+
_.forEach(components, component =>
153+
logger.log(colors.green('├── ') + component)
154+
);
151155

152-
handleDependencies({ components, logger }, (err, dependencies) => {
153-
if (err) {
154-
logger.err(err);
155-
return callback(err, undefined as any);
156-
}
157-
packageComponents(components, () => {
158-
async.waterfall(
159-
[
160-
(next: any) => {
161-
if (hotReloading) {
162-
getPort(port + 1, (error, otherPort) => {
163-
if (error) {
164-
return next(error);
165-
}
166-
const liveReloadServer = livereload.createServer({
167-
port: otherPort
156+
handleDependencies({ components, logger }, (err, dependencies) => {
157+
if (err) {
158+
logger.err(err);
159+
return callback(err, undefined as any);
160+
}
161+
packageComponents(components, () => {
162+
async.waterfall(
163+
[
164+
(next: any) => {
165+
if (hotReloading) {
166+
getPort(port + 1, (error, otherPort) => {
167+
if (error) {
168+
return next(error);
169+
}
170+
const liveReloadServer = livereload.createServer({
171+
port: otherPort
172+
});
173+
const refresher = () => liveReloadServer.refresh('/');
174+
next(null, { refresher, port: otherPort });
168175
});
169-
const refresher = () => liveReloadServer.refresh('/');
170-
next(null, { refresher, port: otherPort });
171-
});
172-
} else {
173-
next(null, { refresher: _.noop, port: null });
176+
} else {
177+
next(null, { refresher: _.noop, port: null });
178+
}
174179
}
175-
}
176-
],
177-
(err, liveReload: any) => {
178-
if (err) {
179-
logger.err(String(err));
180-
return callback(err, undefined as any);
181-
}
182-
183-
const registry = oc.Registry({
184-
baseUrl,
185-
prefix: opts.prefix || '',
186-
dependencies: dependencies.modules,
187-
discovery: true,
188-
env: { name: 'local' },
189-
fallbackRegistryUrl,
190-
hotReloading,
191-
liveReloadPort: liveReload.port,
192-
local: true,
193-
path: path.resolve(componentsDir),
194-
port,
195-
templates: dependencies.templates,
196-
verbosity: 1
197-
});
198-
199-
registerPlugins(registry);
200-
201-
logger.warn(cliMessages.REGISTRY_STARTING(baseUrl));
202-
if (liveReload.port) {
203-
logger.warn(
204-
cliMessages.REGISTRY_LIVERELOAD_STARTING(liveReload.port)
205-
);
206-
}
207-
registry.start(err => {
180+
],
181+
(err, liveReload: any) => {
208182
if (err) {
209-
if ((err as any).code === 'EADDRINUSE') {
210-
err = cliErrors.PORT_IS_BUSY(port) as any;
211-
}
212-
213183
logger.err(String(err));
214184
return callback(err, undefined as any);
215185
}
216186

217-
if (optWatch) {
218-
watchForChanges(
219-
{ components, refreshLiveReload: liveReload.refresher },
220-
packageComponents
187+
const registry = oc.Registry({
188+
baseUrl,
189+
prefix: opts.prefix || '',
190+
dependencies: dependencies.modules,
191+
discovery: true,
192+
env: { name: 'local' },
193+
fallbackRegistryUrl,
194+
hotReloading,
195+
liveReloadPort: liveReload.port,
196+
local: true,
197+
components: opts.components,
198+
path: path.resolve(componentsDir),
199+
port,
200+
templates: dependencies.templates,
201+
verbosity: 1
202+
});
203+
204+
registerPlugins(registry);
205+
206+
logger.warn(cliMessages.REGISTRY_STARTING(baseUrl));
207+
if (liveReload.port) {
208+
logger.warn(
209+
cliMessages.REGISTRY_LIVERELOAD_STARTING(liveReload.port)
221210
);
222211
}
223-
callback(null, registry);
224-
});
225-
}
226-
);
212+
registry.start(err => {
213+
if (err) {
214+
if ((err as any).code === 'EADDRINUSE') {
215+
err = cliErrors.PORT_IS_BUSY(port) as any;
216+
}
217+
218+
logger.err(String(err));
219+
return callback(err, undefined as any);
220+
}
221+
222+
if (optWatch) {
223+
watchForChanges(
224+
{ components, refreshLiveReload: liveReload.refresher },
225+
packageComponents
226+
);
227+
}
228+
callback(null, registry);
229+
});
230+
}
231+
);
232+
});
227233
});
228-
});
229-
});
234+
}
235+
);
230236
};
231237

232238
export default dev;

src/registry/domain/repository.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ export default function repository(conf: Config): Repository {
5858
.toString();
5959
},
6060
getComponents(): string[] {
61-
const validComponents = fs.readdirSync(conf.path).filter(file => {
62-
const isDir = fs.lstatSync(path.join(conf.path, file)).isDirectory();
63-
const isValidComponent = isDir
64-
? fs
65-
.readdirSync(path.join(conf.path, file))
66-
.filter(file => file === '_package').length === 1
67-
: false;
68-
69-
return isValidComponent;
70-
});
61+
const validComponents =
62+
conf.components ||
63+
fs.readdirSync(conf.path).filter(file => {
64+
const isDir = fs.lstatSync(path.join(conf.path, file)).isDirectory();
65+
const isValidComponent = isDir
66+
? fs
67+
.readdirSync(path.join(conf.path, file))
68+
.filter(file => file === '_package').length === 1
69+
: false;
70+
71+
return isValidComponent;
72+
});
7173

7274
validComponents.push('oc-client');
7375
return validComponents;

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export interface Config {
127127
hotReloading: boolean;
128128
keepAliveTimeout?: number;
129129
liveReloadPort: number;
130+
components?: string[];
130131
local: boolean;
131132
path: string;
132133
plugins: Record<string, (...args: unknown[]) => void>;
@@ -291,6 +292,7 @@ export interface Local {
291292
) => void;
292293
getComponentsByDir: (
293294
componentsDir: string,
295+
componentsToRun: (err: Error | null, data: string[]) => void | string[],
294296
callback: (err: Error | null, data: string[]) => void
295297
) => void;
296298
init: (

test/unit/cli-domain-get-components-by-dir.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ const initialise = function () {
3434
return { local: local, fs: fsMock };
3535
};
3636

37-
const executeComponentsListingByDir = function (local, callback) {
38-
return local('.', callback);
37+
const executeComponentsListingByDir = function (
38+
local,
39+
componentsToRun,
40+
callback
41+
) {
42+
return local('.', componentsToRun, callback);
3943
};
4044

4145
describe('cli : domain : get-components-by-dir', () => {
@@ -65,7 +69,7 @@ describe('cli : domain : get-components-by-dir', () => {
6569
data.fs.readJsonSync.onCall(3).returns({ oc: { packaged: true } });
6670
data.fs.readJsonSync.onCall(4).returns({});
6771

68-
executeComponentsListingByDir(data.local, (err, res) => {
72+
executeComponentsListingByDir(data.local, undefined, (err, res) => {
6973
error = err;
7074
result = res;
7175
done();
@@ -94,7 +98,7 @@ describe('cli : domain : get-components-by-dir', () => {
9498
data.fs.readJsonSync.onCall(0).throws(new Error('syntax error: fubar'));
9599
data.fs.readJsonSync.onCall(1).returns({ oc: {} });
96100

97-
executeComponentsListingByDir(data.local, (err, res) => {
101+
executeComponentsListingByDir(data.local, undefined, (err, res) => {
98102
error = err;
99103
result = res;
100104
done();
@@ -128,7 +132,7 @@ describe('cli : domain : get-components-by-dir', () => {
128132
.onCall(2)
129133
.throws(new Error('ENOENT: no such file or directory'));
130134

131-
executeComponentsListingByDir(data.local, (err, res) => {
135+
executeComponentsListingByDir(data.local, undefined, (err, res) => {
132136
error = err;
133137
result = res;
134138
done();
@@ -143,4 +147,48 @@ describe('cli : domain : get-components-by-dir', () => {
143147
expect(result).to.eql([]);
144148
});
145149
});
150+
151+
describe('when components are filtered', () => {
152+
let error;
153+
let result;
154+
beforeEach(done => {
155+
const data = initialise();
156+
157+
data.fs.readdirSync
158+
.onCall(0)
159+
.returns([
160+
'component1',
161+
'component2',
162+
'component3',
163+
'component4',
164+
'package.json'
165+
]);
166+
167+
data.fs.readJsonSync.onCall(0).returns({ oc: {} });
168+
data.fs.readJsonSync.onCall(1).returns({ oc: {} });
169+
data.fs.readJsonSync.onCall(2).returns({ oc: {} });
170+
data.fs.readJsonSync.onCall(3).returns({ oc: {} });
171+
data.fs.readJsonSync
172+
.onCall(4)
173+
.throws(new Error('ENOENT: no such file or directory'));
174+
175+
executeComponentsListingByDir(
176+
data.local,
177+
['component1', 'component3'],
178+
(err, res) => {
179+
error = err;
180+
result = res;
181+
done();
182+
}
183+
);
184+
});
185+
186+
it('should not error', () => {
187+
expect(error).to.be.null;
188+
});
189+
190+
it('should get an the filtered list', () => {
191+
expect(result).to.eql(['./component1', './component3']);
192+
});
193+
});
146194
});

0 commit comments

Comments
 (0)