Skip to content

Commit e8e4e1d

Browse files
reduce lodash usage on test/code
1 parent 844b1c6 commit e8e4e1d

20 files changed

+75
-65
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from 'fs-extra';
22
import path from 'path';
3-
import _ from 'lodash';
43

54
import settings from '../../resources/settings';
65
import strings from '../../resources/';
76
import { Logger } from '../logger';
7+
import { OcJsonConfig } from '../../types';
88

99
interface MockedPlugin {
1010
register: (options: unknown, dependencies: unknown, next: () => void) => void;
@@ -48,7 +48,7 @@ const registerStaticMocks = (
4848
mocks: Record<string, string>,
4949
logger: Logger
5050
): PluginMock[] =>
51-
_.map(mocks, (mockedValue, pluginName) => {
51+
Object.entries(mocks).map(([pluginName, mockedValue]) => {
5252
logger.ok(`├── ${pluginName} () => ${mockedValue}`);
5353

5454
return {
@@ -65,31 +65,33 @@ const registerDynamicMocks = (
6565
mocks: Record<string, string>,
6666
logger: Logger
6767
) =>
68-
_.map(mocks, (source, pluginName) => {
69-
let pluginMock;
70-
try {
71-
pluginMock = require(path.resolve(ocJsonLocation, source));
72-
} catch (er) {
73-
logger.err(String(er));
74-
return;
75-
}
68+
Object.entries(mocks)
69+
.map(([pluginName, source]) => {
70+
let pluginMock;
71+
try {
72+
pluginMock = require(path.resolve(ocJsonLocation, source));
73+
} catch (er) {
74+
logger.err(String(er));
75+
return;
76+
}
7677

77-
if (!isMockValid(pluginMock)) {
78-
logger.err(`├── ${pluginName} () => Error (skipping)`);
79-
logger.err(strings.errors.cli.MOCK_PLUGIN_IS_NOT_VALID);
80-
return;
81-
}
78+
if (!isMockValid(pluginMock)) {
79+
logger.err(`├── ${pluginName} () => Error (skipping)`);
80+
logger.err(strings.errors.cli.MOCK_PLUGIN_IS_NOT_VALID);
81+
return;
82+
}
8283

83-
const register = (pluginMock as MockedPlugin).register || defaultRegister;
84-
const execute = (pluginMock as MockedPlugin).execute || pluginMock;
84+
const register = (pluginMock as MockedPlugin).register || defaultRegister;
85+
const execute = (pluginMock as MockedPlugin).execute || pluginMock;
8586

86-
logger.ok(`├── ${pluginName} () => [Function]`);
87+
logger.ok(`├── ${pluginName} () => [Function]`);
8788

88-
return {
89-
name: pluginName,
90-
register: { execute, register }
91-
};
92-
}).filter((pluginMock): pluginMock is PluginMock => !!pluginMock);
89+
return {
90+
name: pluginName,
91+
register: { execute, register }
92+
};
93+
})
94+
.filter((pluginMock): pluginMock is PluginMock => !!pluginMock);
9395

9496
const findPath = (
9597
pathToResolve: string,
@@ -128,7 +130,7 @@ export default function getMockedPlugins(
128130
return plugins;
129131
}
130132

131-
const content = fs.readJsonSync(ocJsonPath);
133+
const content: OcJsonConfig = fs.readJsonSync(ocJsonPath);
132134
const ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length);
133135

134136
if (!content.mocks || !content.mocks.plugins) {
@@ -138,10 +140,14 @@ export default function getMockedPlugins(
138140
logger.warn(strings.messages.cli.REGISTERING_MOCKED_PLUGINS);
139141

140142
plugins = plugins.concat(
141-
registerStaticMocks(content.mocks.plugins.static, logger)
143+
registerStaticMocks(content.mocks.plugins.static ?? {}, logger)
142144
);
143145
plugins = plugins.concat(
144-
registerDynamicMocks(ocJsonLocation, content.mocks.plugins.dynamic, logger)
146+
registerDynamicMocks(
147+
ocJsonLocation,
148+
content.mocks.plugins.dynamic ?? {},
149+
logger
150+
)
145151
);
146152

147153
return plugins;

src/cli/facade/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const dev =
150150
}
151151

152152
logger.ok('OK');
153-
_.forEach(components, component =>
153+
components.forEach(component =>
154154
logger.log(colors.green('├── ') + component)
155155
);
156156

src/cli/facade/publish.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import colors from 'colors/safe';
33
import path from 'path';
44
import fs from 'fs-extra';
55
import read from 'read';
6-
import _ from 'lodash';
76
import { Logger } from '../logger';
87
import { Component, RegistryCli, Local } from '../../types';
98

@@ -49,7 +48,8 @@ const publish =
4948
) => {
5049
if (opts.username && opts.password) {
5150
logger.ok(strings.messages.cli.USING_CREDS);
52-
return cb(null, _.pick(opts, 'username', 'password') as any);
51+
const { username, password } = opts;
52+
return cb(null, { username, password });
5353
}
5454

5555
logger.warn(strings.messages.cli.ENTER_USERNAME);
@@ -149,7 +149,7 @@ const publish =
149149
logger.err(errorMessage);
150150
return cb(errorMessage, undefined as any);
151151
} else {
152-
if (_.isObject(err)) {
152+
if (err && typeof err === 'object') {
153153
try {
154154
err = JSON.stringify(err);
155155
} catch (er) {}

src/registry/app-start.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import colors from 'colors/safe';
22
import path from 'path';
3-
import _ from 'lodash';
43
import fs from 'fs-extra';
54
import { ComponentsDetails, Config, Repository } from '../types';
65

@@ -24,7 +23,8 @@ export default function appStart(
2423
return callback(null, 'ok');
2524
}
2625

27-
const logger = options.verbosity ? console : { log: _.noop };
26+
// eslint-disable-next-line @typescript-eslint/no-empty-function
27+
const logger = options.verbosity ? console : { log: () => {} };
2828

2929
logger.log(
3030
colors.yellow(
@@ -43,7 +43,7 @@ export default function appStart(
4343
)
4444
);
4545

46-
if (!_.includes(componentInfo, packageInfo.version)) {
46+
if (!componentInfo.includes(packageInfo.version)) {
4747
logger.log(colors.yellow('Component not found. Publishing it...'));
4848

4949
const pkgInfo = {

src/registry/domain/options-sanitiser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import settings from '../../resources/settings';
32
import { Config } from '../../types';
43
import * as auth from './authentication';
@@ -10,7 +9,7 @@ interface Input extends Partial<Omit<Config, 'beforePublish'>> {
109
}
1110

1211
export default function optionsSanitiser(input: Input): Config {
13-
const options = _.clone(input);
12+
const options = { ...input };
1413

1514
if (!options.publishAuth) {
1615
(options as Config).beforePublish = (_req, _res, next) => next();
@@ -57,9 +56,13 @@ export default function optionsSanitiser(input: Input): Config {
5756
options.templates = [];
5857
}
5958

59+
if (!options.dependencies) {
60+
options.dependencies = [];
61+
}
62+
6063
if (
6164
typeof options.fallbackRegistryUrl !== 'undefined' &&
62-
_.last(options.fallbackRegistryUrl) !== '/'
65+
!options.fallbackRegistryUrl.endsWith('/')
6366
) {
6467
options.fallbackRegistryUrl += '/';
6568
}

src/registry/domain/repository.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs-extra';
22
import getUnixUtcTimestamp from 'oc-get-unix-utc-timestamp';
33
import path from 'path';
4-
import _ from 'lodash';
54

65
import ComponentsCache from './components-cache';
76
import getComponentsDetails from './components-details';
@@ -84,7 +83,7 @@ export default function repository(conf: Config): Repository {
8483
]);
8584
}
8685

87-
if (!_.includes(local.getComponents(), componentName)) {
86+
if (!local.getComponents().includes(componentName)) {
8887
return callback(
8988
strings.errors.registry.COMPONENT_NOT_FOUND(
9089
componentName,
@@ -259,7 +258,7 @@ export default function repository(conf: Config): Repository {
259258
componentsCache.get((err, res) => {
260259
callback(
261260
err as any,
262-
!!res && !!_.has(res.components, componentName)
261+
!!res && !!res.components[componentName]
263262
? res.components[componentName]
264263
: []
265264
);

src/registry/domain/require-wrapper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import coreModules from 'builtin-modules';
22
import path from 'path';
33
import requirePackageName from 'require-package-name';
44
import tryRequire from 'try-require';
5-
import _ from 'lodash';
65

76
import strings from '../../resources';
87

@@ -26,7 +25,7 @@ const throwError = (requirePath: string) => {
2625
export default (injectedDependencies: string[]) =>
2726
<T = unknown>(requirePath: string): T => {
2827
const moduleName = requirePackageName(requirePath);
29-
const isAllowed = _.includes(injectedDependencies, moduleName);
28+
const isAllowed = injectedDependencies.includes(moduleName);
3029

3130
if (!isAllowed) {
3231
return throwError(requirePath);

src/registry/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default function registry(inputOptions: Input) {
5454
// eslint-disable-next-line no-console
5555
const ok = (msg: string) => console.log(colors.green(msg));
5656
if (typeof callback !== 'function') {
57-
callback = _.noop;
57+
// eslint-disable-next-line @typescript-eslint/no-empty-function
58+
callback = () => {};
5859
}
5960
router.create(app, options, repository);
6061
async.waterfall(

src/registry/routes/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function components(
5050

5151
if (!_.isEmpty(components)) {
5252
const errors = _.compact(
53-
_.map(components, (component, index) => {
53+
components.map((component, index) => {
5454
return !component.name
5555
? registryErrors.BATCH_ROUTE_COMPONENT_NAME_MISSING(index)
5656
: '';

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export interface ComponentsList {
4444
lastEdit: number;
4545
}
4646

47+
export interface OcJsonConfig {
48+
registries?: string[];
49+
mocks?: {
50+
plugins?: {
51+
dynamic?: Record<string, string>;
52+
static?: Record<string, string>;
53+
};
54+
};
55+
}
56+
4757
export interface OcParameter {
4858
default?: string | boolean | number;
4959
description?: string;

0 commit comments

Comments
 (0)