Skip to content

Commit a4fe8bf

Browse files
merge master
2 parents e079a57 + 43d7c46 commit a4fe8bf

File tree

6 files changed

+79
-16
lines changed

6 files changed

+79
-16
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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import path from 'path';
33
import { Component } from '../../types';
44

55
export default function getComponentsByDir() {
6-
return async (componentsDir: string): Promise<string[]> => {
6+
return async (
7+
componentsDir: string,
8+
componentsToRun?: string[]
9+
): Promise<string[]> => {
710
const isOcComponent = (file: string) => {
811
const filePath = path.resolve(componentsDir, file);
912
const packagePath = path.join(filePath, 'package.json');
@@ -25,7 +28,12 @@ export default function getComponentsByDir() {
2528
};
2629

2730
try {
28-
const dirContent = await fs.readdir(componentsDir);
31+
let dirContent = await fs.readdir(componentsDir);
32+
if (componentsToRun) {
33+
dirContent = dirContent.filter(content =>
34+
componentsToRun.includes(content)
35+
);
36+
}
2937

3038
const components = dirContent
3139
.filter(isOcComponent)

src/cli/facade/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
3131
baseUrl: string;
3232
fallbackRegistryUrl: string;
3333
hotReloading?: boolean;
34+
components?: string[];
3435
watch?: boolean;
3536
verbose?: boolean;
3637
production?: boolean;
@@ -120,7 +121,10 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
120121
};
121122

122123
logger.warn(cliMessages.SCANNING_COMPONENTS, true);
123-
const components = await local.getComponentsByDir(componentsDir);
124+
const components = await local.getComponentsByDir(
125+
componentsDir,
126+
opts.components
127+
);
124128

125129
if (_.isEmpty(components)) {
126130
const err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND);
@@ -170,6 +174,7 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
170174
hotReloading,
171175
liveReloadPort: liveReload.port,
172176
local: true,
177+
components: opts.components,
173178
path: path.resolve(componentsDir),
174179
port,
175180
templates: dependencies.templates,

src/registry/domain/repository.ts

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

6971
validComponents.push('oc-client');
7072
return validComponents;

src/types.ts

Lines changed: 5 additions & 1 deletion
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>;
@@ -234,7 +235,10 @@ export interface Local {
234235
};
235236
cleanup: (compressedPackagePath: string) => Promise<void>;
236237
compress: (input: string, output: string) => Promise<void>;
237-
getComponentsByDir: (componentsDir: string) => Promise<string[]>;
238+
getComponentsByDir: (
239+
componentsDir: string,
240+
componentsToRun?: string[]
241+
) => Promise<string[]>;
238242
init: (options: {
239243
componentName: string;
240244
logger: Logger;

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

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

37-
const executeComponentsListingByDir = function (local) {
38-
return local('.');
37+
const executeComponentsListingByDir = function (local, componentsToRun) {
38+
return local('.', componentsToRun);
3939
};
4040

4141
describe('cli : domain : get-components-by-dir', () => {
@@ -140,4 +140,43 @@ describe('cli : domain : get-components-by-dir', () => {
140140
expect(result).to.eql([]);
141141
});
142142
});
143+
144+
describe('when components are filtered', () => {
145+
let error;
146+
let result;
147+
beforeEach(done => {
148+
const data = initialise();
149+
150+
data.fs.readdir
151+
.onCall(0)
152+
.resolves([
153+
'component1',
154+
'component2',
155+
'component3',
156+
'component4',
157+
'package.json'
158+
]);
159+
160+
data.fs.readJsonSync.onCall(0).returns({ oc: {} });
161+
data.fs.readJsonSync.onCall(1).returns({ oc: {} });
162+
data.fs.readJsonSync.onCall(2).returns({ oc: {} });
163+
data.fs.readJsonSync.onCall(3).returns({ oc: {} });
164+
data.fs.readJsonSync
165+
.onCall(4)
166+
.throws(new Error('ENOENT: no such file or directory'));
167+
168+
executeComponentsListingByDir(data.local, ['component1', 'component3'])
169+
.then(res => (result = res))
170+
.catch(err => (error = err))
171+
.finally(done);
172+
});
173+
174+
it('should not error', () => {
175+
expect(error).to.be.undefined;
176+
});
177+
178+
it('should get an the filtered list', () => {
179+
expect(result).to.eql(['./component1', './component3']);
180+
});
181+
});
143182
});

0 commit comments

Comments
 (0)