Skip to content

Commit

Permalink
replace lodash with lodash-es
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 8, 2023
1 parent fbae307 commit 7fa5597
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 35 deletions.
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"doc_path": "../yeoman-generator-doc/"
},
"dependencies": {
"@types/lodash-es": "^4.17.7",
"@types/node": "^16.18.3",
"@yeoman/namespace": "^1.0.0",
"chalk": "^5.1.2",
Expand All @@ -59,7 +60,7 @@
"github-username": "^7.0.0",
"json-schema": "^0.4.0",
"latest-version": "^7.0.0",
"lodash": "^4.17.11",
"lodash-es": "^4.17.21",
"mem-fs-editor": "^10.0.1",
"minimist": "^1.2.8",
"read-pkg-up": "^9.1.0",
Expand All @@ -71,7 +72,6 @@
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/inquirer": "^9.0.3",
"@types/lodash": "^4.14.194",
"@types/semver": "^7.3.13",
"@types/sinon": "^10.0.14",
"@types/text-table": "^0.2.2",
Expand Down
1 change: 0 additions & 1 deletion src/actions/help.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import path from 'node:path';
import fs from 'node:fs';
import _ from 'lodash';
import table from 'text-table';
import type { ArgumentSpec, CliOptionSpec } from '../types.js';
import type BaseGenerator from '../generator.js';
Expand Down
7 changes: 2 additions & 5 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path, { dirname, resolve as pathResolve, join as pathJoin } from 'node:pa
import os from 'node:os';
import EventEmitter from 'node:events';
import { fileURLToPath } from 'node:url';
import _ from 'lodash';
import * as _ from 'lodash-es';
import semver from 'semver';
import { readPackageUpSync } from 'read-pkg-up';
import chalk from 'chalk';
Expand Down Expand Up @@ -60,7 +60,7 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
readonly env: Environment;
readonly fs!: MemFsEditor;
readonly log!: Logger;
readonly _: _.LoDashStatic;
readonly _ = _;
appname!: string;
args!: string[];
/** @deprecated */
Expand Down Expand Up @@ -192,9 +192,6 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
// Add convenience debug object
this._debug = createDebug(this._namespace);

// Expose utilities for dependency-less generators.
this._ = _;

if (actualOptions.help) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/deprecate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import { template } from 'lodash-es';
import chalk from 'chalk';

const deprecate = (message, fn) => {
Expand All @@ -13,7 +13,7 @@ deprecate.log = message => {
};

deprecate.object = (message, object) => {
const messageTpl = _.template(message);
const messageTpl = template(message);
const mirror = [];

for (const name of Object.keys(object)) {
Expand Down
1 change: 0 additions & 1 deletion src/util/prompt-suggestion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'node:assert';
import _ from 'lodash';
import type { JSONSchema7Object } from 'json-schema';
import type { PromptAnswers, PromptQuestion } from '../questions.js';
import type Storage from './storage.js';
Expand Down
22 changes: 11 additions & 11 deletions src/util/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert';
import _ from 'lodash';
import { cloneDeep, defaults as setDefaults, merge, get, set } from 'lodash-es';
import sortKeys from 'sort-keys';
import type { MemFsEditor } from 'mem-fs-editor';
import type { StorageRecord, StorageValue } from '../types.js';
Expand Down Expand Up @@ -177,7 +177,7 @@ class Storage {
return store;
}

return ((this.lodashPath ? _.get(store, this.name) : store[this.name]) ?? {}) as StorageRecord;
return ((this.lodashPath ? get(store, this.name) : store[this.name]) ?? {}) as StorageRecord;
}

/**
Expand All @@ -194,7 +194,7 @@ class Storage {
if (this.name) {
fullStore = this.readContent();
if (this.lodashPath) {
_.set(fullStore, this.name, value);
set(fullStore, this.name, value);
} else {
fullStore[this.name] = value;
}
Expand Down Expand Up @@ -227,15 +227,15 @@ class Storage {
* @return The stored value. Any JSON valid type could be returned
*/
getPath<T extends StorageValue = StorageValue>(path: string): T {
return _.get(this._store, path) as T;
return get(this._store, path) as T;
}

/**
* Get all the stored values
* @return key-value object
*/
getAll(): StorageRecord {
return _.cloneDeep(this._store);
return cloneDeep(this._store);
}

/**
Expand Down Expand Up @@ -268,10 +268,10 @@ class Storage {
* @return val Whatever was passed in as val.
*/
setPath(path: string | number, value: StorageValue) {
assert(!_.isFunction(value), "Storage value can't be a function");
assert(typeof value !== 'function', "Storage value can't be a function");

const store = this._store;
_.set(store, path, value);
set(store, path, value);
this._persist(store);
return value;
}
Expand All @@ -294,8 +294,8 @@ class Storage {
* @return val Returns the merged options.
*/
defaults(defaults: StorageRecord): StorageRecord {
assert(_.isObject(defaults), 'Storage `defaults` method only accept objects');
const store = _.defaults({}, this._store, defaults);
assert(typeof defaults === 'object', 'Storage `defaults` method only accept objects');
const store = setDefaults({}, this._store, defaults);
this._persist(store);
return this.getAll();
}
Expand All @@ -305,8 +305,8 @@ class Storage {
* @return val Returns the merged object.
*/
merge(source: StorageRecord) {
assert(_.isObject(source), 'Storage `merge` method only accept objects');
const value = _.merge({}, this._store, source);
assert(typeof source === 'object', 'Storage `merge` method only accept objects');
const value = merge({}, this._store, source);
this._persist(value);
return this.getAll();
}
Expand Down
12 changes: 6 additions & 6 deletions test/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import { createRequire } from 'node:module';
import process from 'node:process';
import { Buffer } from 'node:buffer';
import _ from 'lodash';
import { extend } from 'lodash-es';
import { spy as sinonSpy, fake as sinonFake, assert as sinonAssert } from 'sinon';
import { passthrough } from '@yeoman/transform';
import assert from 'yeoman-assert';
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Base', () => {
let execSpy;
beforeEach(function () {
TestGenerator = class extends Base {};
_.extend(TestGenerator.prototype, {
extend(TestGenerator.prototype, {
_beforeQueue: sinonSpy(),
exec: sinonSpy(),
exec2: sinonSpy(),
Expand Down Expand Up @@ -507,7 +507,7 @@ describe('Base', () => {

beforeEach(function () {
TestGenerator = class extends Base {};
_.extend(TestGenerator.prototype, {
extend(TestGenerator.prototype, {
beforeQueue: sinonSpy(),
_private: sinonSpy(),
'#composed': sinonSpy(),
Expand Down Expand Up @@ -1509,7 +1509,7 @@ describe('Base', () => {
});

it('generates correct _queues and runLoop queueNames', function () {
_.extend(TestGenerator.prototype, {
extend(TestGenerator.prototype, {
assert() {
assert.deepStrictEqual(this._queues, {
initializing: {
Expand Down Expand Up @@ -1591,7 +1591,7 @@ describe('Base', () => {
const configuring = sinonSpy();
const end = sinonSpy();

_.extend(TestGenerator.prototype, {
extend(TestGenerator.prototype, {
prePrompting1,
preConfiguring1,
preConfiguring2,
Expand Down Expand Up @@ -1625,7 +1625,7 @@ describe('Base', () => {
const customPreConfiguring1 = sinonSpy();
const customPreConfiguring2 = sinonSpy();

_.extend(TestGenerator.prototype, {
extend(TestGenerator.prototype, {
get preConfiguring1() {
return { commonPreConfiguring };
},
Expand Down

0 comments on commit 7fa5597

Please sign in to comment.