Skip to content

Commit c3712b3

Browse files
committed
refactor: rid project of fs-extra
1 parent b1168d9 commit c3712b3

4 files changed

Lines changed: 16 additions & 52 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"@salesforce/sf-plugins-core": "^2.2.9",
1212
"@salesforce/ts-types": "^1.7.1",
1313
"chalk": "^3.0.0",
14-
"fs-extra": "^10.0.1",
1514
"handlebars": "^4.7.7",
1615
"lodash.uniqby": "^4.7.0",
1716
"mkdirp": "^1.0.4",

src/commands/commandreference/generate.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8+
import * as fs from 'fs/promises';
89
import * as os from 'os';
910
import * as path from 'path';
10-
import { pathExists, readJSON } from 'fs-extra';
1111
import { SfCommand } from '@salesforce/sf-plugins-core';
1212
import { Command, Flags, Interfaces } from '@oclif/core';
1313
import { Messages, SfError } from '@salesforce/core';
14-
import { AnyJson, Dictionary, ensure, ensureString, JsonMap } from '@salesforce/ts-types';
14+
import { AnyJson, ensure, ensureString, JsonMap } from '@salesforce/ts-types';
1515
import chalk = require('chalk');
16+
import { parseJsonMap } from '@salesforce/kit';
1617
import { Ditamap } from '../../ditamap/ditamap';
1718
import { Docs } from '../../docs';
18-
import { CommandClass, events, mergeDeep } from '../../utils';
19+
import { CommandClass, events } from '../../utils';
1920

2021
// Initialize Messages with the current plugin directory
2122
Messages.importMessagesDirectory(__dirname);
2223
const messages = Messages.loadMessages('@salesforce/plugin-command-reference', 'main');
2324

25+
const fileExists = async (filePath: string): Promise<boolean> =>
26+
fs
27+
.stat(filePath)
28+
.then(() => true)
29+
.catch(() => false);
30+
2431
export default class CommandReferenceGenerate extends SfCommand<AnyJson> {
2532
public static description = messages.getMessage('commandDescription');
2633

@@ -58,8 +65,8 @@ export default class CommandReferenceGenerate extends SfCommand<AnyJson> {
5865
let pluginNames: string[];
5966
if (!flags.plugins && !flags.all) {
6067
const pJsonPath = path.join(process.cwd(), 'package.json');
61-
if (await pathExists(pJsonPath)) {
62-
const packageJson = (await readJSON(pJsonPath)) as JsonMap;
68+
if (await fileExists(pJsonPath)) {
69+
const packageJson = parseJsonMap(await fs.readFile(pJsonPath, 'utf-8'));
6370
pluginNames = [ensureString(packageJson.name)];
6471
} else {
6572
throw new SfError(
@@ -177,7 +184,7 @@ export default class CommandReferenceGenerate extends SfCommand<AnyJson> {
177184
const commandClass = await this.loadCommand(cmd);
178185

179186
if (commandClass.plugin?.pjson.oclif.topics) {
180-
mergeDeep(topicsMeta, commandClass.plugin.pjson.oclif.topics as Dictionary);
187+
Object.assign(topicsMeta, commandClass.plugin.pjson.oclif.topics);
181188
plugins[commandClass.plugin.name] = true;
182189
}
183190
}
@@ -203,10 +210,10 @@ export default class CommandReferenceGenerate extends SfCommand<AnyJson> {
203210

204211
return obj as unknown as CommandClass;
205212
} catch (error) {
206-
return Object.assign({}, cmd) as unknown as CommandClass;
213+
return cmd as unknown as CommandClass;
207214
}
208215
});
209-
const commands = (await Promise.all(promises)) as CommandClass[];
216+
const commands = await Promise.all(promises);
210217
return Array.from(
211218
commands
212219
.reduce((acc: Map<string, CommandClass>, cmd: CommandClass) => {

src/utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,12 @@
77

88
import { EventEmitter } from 'events';
99
import { EOL } from 'os';
10-
import { Dictionary, isObject } from '@salesforce/ts-types';
1110
import { Command } from '@oclif/core';
1211

1312
export type CommandClass = Command.Class & { topic: string; subtopic: string } & Record<string, unknown>;
1413

1514
export const events = new EventEmitter();
1615

17-
export function mergeDeep(target: Dictionary, source: Dictionary): Dictionary {
18-
Object.keys(source).forEach((key) => {
19-
if (isObject(target[key]) && isObject(source[key])) {
20-
mergeDeep(target[key] as Dictionary, source[key] as Dictionary);
21-
} else {
22-
target[key] = source[key];
23-
}
24-
});
25-
return target;
26-
}
27-
2816
export function punctuate(description?: string): string | undefined {
2917
if (!description) return description;
3018

test/unit/utils.test.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,7 @@
77

88
import { EOL } from 'os';
99
import { expect } from 'chai';
10-
import { punctuate, mergeDeep } from '../../src/utils';
11-
12-
it('merge two shallow objects', () => {
13-
expect(mergeDeep({ a: 1 }, { b: 2 })).to.deep.equal({ a: 1, b: 2 });
14-
});
15-
16-
it('merge two deeply nested objects', () => {
17-
expect(
18-
mergeDeep(
19-
{
20-
a: {
21-
b: 1,
22-
c: { d: 3 },
23-
},
24-
},
25-
{
26-
a: {
27-
c: { e: 4 },
28-
},
29-
}
30-
)
31-
).to.deep.equal({
32-
a: {
33-
b: 1,
34-
c: {
35-
d: 3,
36-
e: 4,
37-
},
38-
},
39-
});
40-
});
10+
import { punctuate } from '../../src/utils';
4111

4212
describe('punctuate', () => {
4313
it('description to longDescription', () => {

0 commit comments

Comments
 (0)