Skip to content

Commit 344cd5e

Browse files
Merge pull request #1287 from opencomponents/reduce-lodash
[INTERNAL] reduce lodash usage on test/code
2 parents 4e145c6 + 9a29eec commit 344cd5e

18 files changed

+74
-66
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import colors from 'colors/safe';
22
import getPortCb from 'getport';
33
import livereload from 'livereload';
44
import path from 'path';
5-
import _ from 'lodash';
65
import { promisify } from 'util';
76
import { fromPromise } from 'universalify';
87

@@ -127,7 +126,7 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
127126
opts.components
128127
);
129128

130-
if (_.isEmpty(components)) {
129+
if (!components.length) {
131130
const err = cliErrors.DEV_FAIL(cliErrors.COMPONENTS_NOT_FOUND);
132131
logger.err(err);
133132
throw err;
@@ -149,7 +148,8 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
149148
await packageComponents(components);
150149

151150
let liveReload: { refresher: () => void; port: number | undefined } = {
152-
refresher: _.noop,
151+
// eslint-disable-next-line @typescript-eslint/no-empty-function
152+
refresher: () => {},
153153
port: undefined
154154
};
155155
if (hotReloading) {

src/cli/facade/publish.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import colors from 'colors/safe';
22
import path from 'path';
33
import fs from 'fs-extra';
44
import readCb from 'read';
5-
import _ from 'lodash';
65
import { promisify } from 'util';
76
import { Logger } from '../logger';
87
import { Component, RegistryCli, Local } from '../../types';
@@ -49,7 +48,8 @@ const publish = ({
4948
}> => {
5049
if (opts.username && opts.password) {
5150
logger.ok(strings.messages.cli.USING_CREDS);
52-
return _.pick(opts, 'username', 'password') as any;
51+
const { username, password } = opts;
52+
return { username, password };
5353
}
5454

5555
logger.warn(strings.messages.cli.ENTER_USERNAME);
@@ -130,7 +130,7 @@ const publish = ({
130130
if (err.message) {
131131
// eslint-disable-next-line no-ex-assign
132132
err = err.message;
133-
} else if (_.isObject(err)) {
133+
} else if (err && typeof err === 'object') {
134134
try {
135135
// eslint-disable-next-line no-ex-assign
136136
err = JSON.stringify(err);

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 { Config, Repository } from '../types';
65

@@ -23,7 +22,8 @@ export default async function appStart(
2322
return;
2423
}
2524

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

2828
logger.log(
2929
colors.yellow(
@@ -40,7 +40,7 @@ export default async function appStart(
4040
)
4141
);
4242

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

4646
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 & 5 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';
@@ -80,7 +79,7 @@ export default function repository(conf: Config): Repository {
8079
]);
8180
}
8281

83-
if (!_.includes(local.getComponents(), componentName)) {
82+
if (!local.getComponents().includes(componentName)) {
8483
return Promise.reject(
8584
strings.errors.registry.COMPONENT_NOT_FOUND(
8685
componentName,
@@ -210,9 +209,7 @@ export default function repository(conf: Config): Repository {
210209

211210
const res = await componentsCache.get();
212211

213-
return _.has(res.components, componentName)
214-
? res.components[componentName]
215-
: [];
212+
return res.components[componentName] ? res.components[componentName] : [];
216213
},
217214
async getDataProvider(componentName: string, componentVersion: string) {
218215
if (conf.local) {

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/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;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const expect = require('chai').expect;
44
const injectr = require('injectr');
55
const path = require('path');
66
const sinon = require('sinon');
7-
const _ = require('lodash');
87

98
const initialise = function () {
109
const fsMock = {
@@ -16,7 +15,7 @@ const initialise = function () {
1615
extname: path.extname,
1716
join: path.join,
1817
resolve: function () {
19-
return _.toArray(arguments).join('/');
18+
return Array.from(arguments).join('/');
2019
}
2120
};
2221

0 commit comments

Comments
 (0)