Skip to content

Commit

Permalink
fix: dont override existing topic description
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 22, 2022
1 parent e22d552 commit 6dee8d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/generators/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export interface CommandGeneratorOptions extends Generator.GeneratorOptions {
unit: boolean;
}

export function addTopics(newCmd: string, commands: string[] = []): Record<string, Topic> {
export function addTopics(
newCmd: string,
existingTopics: Record<string, Topic>,
commands: string[] = []
): Record<string, Topic> {
const updated: Record<string, Topic> = {};

const paths: string[] = [];
Expand All @@ -45,12 +49,14 @@ export function addTopics(newCmd: string, commands: string[] = []): Record<strin
subtopics: existing,
}
: {
description: `description for ${p}`,
description: get(existingTopics, `${p}.description`, `description for ${p}`),
subtopics: existing,
};
set(updated, p, merged);
} else {
const entry = isExternal ? { external: true } : { description: `description for ${p}` };
const entry = isExternal
? { external: true }
: { description: get(existingTopics, `${p}.description`, `description for ${p}`) };
set(updated, p, entry);
}
}
Expand All @@ -77,11 +83,11 @@ export default class Command extends Generator {
const commandSnapshotUrl = 'https://raw.githubusercontent.com/salesforcecli/cli/main/command-snapshot.json';
const commandSnapshot = await got(commandSnapshotUrl).json<Array<{ command: string }>>();
const commands = commandSnapshot.map((c) => c.command.replace(/:/g, '.'));
const newTopics = addTopics(this.options.name, commands);
const newTopics = addTopics(this.options.name, this.pjson.oclif.topics, commands);
this.pjson.oclif.topics = { ...this.pjson.oclif.topics, ...newTopics };
this.internalPlugin = true;
} else {
const newTopics = addTopics(this.options.name);
const newTopics = addTopics(this.options.name, this.pjson.oclif.topics);
this.pjson.oclif.topics = { ...this.pjson.oclif.topics, ...newTopics };
}

Expand Down
27 changes: 23 additions & 4 deletions test/commands/dev/generate/command.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,30 @@ describe('dev generate command NUTs', () => {
});

describe('generated command under existing topic', () => {
const name = 'deploy:awesome:stuff';
const command = `dev generate command --name ${name} --force --nuts --unit`;

before(async () => {
execCmd(command, { ensureExitCode: 0, cli: 'sf', cwd: session.project.dir });
execCmd('dev generate command --name deploy:awesome:stuff --force --nuts --unit', {
ensureExitCode: 0,
cli: 'sf',
cwd: session.project.dir,
});

execCmd('dev generate command --name hello:every:one --force --nuts --unit', {
ensureExitCode: 0,
cli: 'sf',
cwd: session.project.dir,
});
});

it('should add new subtopics in package.json', async () => {
const packageJson = readJson<PackageJson>(path.join(session.project.dir, 'package.json'));
expect(packageJson.oclif.topics.hello).to.deep.equal({
description: 'Commands to say hello.',
subtopics: {
every: {
description: 'description for hello.every',
},
},
});
});

it('should add new topics in package.json', async () => {
Expand Down

0 comments on commit 6dee8d8

Please sign in to comment.