Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -156,7 +156,7 @@ FLAG DESCRIPTIONS
The default is the "src" directory in the current working directory.
```

_See code: [src/commands/dev/audit/messages.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/audit/messages.ts)_
_See code: [src/commands/dev/audit/messages.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/audit/messages.ts)_

## `sf dev convert messages`

Expand DownExpand Up@@ -191,7 +191,7 @@ EXAMPLES
$ sf dev convert messages --project-dir ./path/to/plugin --filename my-command.json
```

_See code: [src/commands/dev/convert/messages.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/convert/messages.ts)_
_See code: [src/commands/dev/convert/messages.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/convert/messages.ts)_

## `sf dev convert script`

Expand DownExpand Up@@ -235,7 +235,7 @@ EXAMPLES
$ sf dev convert script --script ./myScript.yml
```

_See code: [src/commands/dev/convert/script.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/convert/script.ts)_
_See code: [src/commands/dev/convert/script.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/convert/script.ts)_

## `sf dev generate command`

Expand DownExpand Up@@ -276,7 +276,7 @@ EXAMPLES
$ sf dev generate command --name my:exciting:command
```

_See code: [src/commands/dev/generate/command.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/generate/command.ts)_
_See code: [src/commands/dev/generate/command.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/generate/command.ts)_

## `sf dev generate flag`

Expand DownExpand Up@@ -316,7 +316,7 @@ EXAMPLES
$ sf dev generate flag --dry-run
```

_See code: [src/commands/dev/generate/flag.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/generate/flag.ts)_
_See code: [src/commands/dev/generate/flag.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/generate/flag.ts)_

## `sf dev generate plugin`

Expand DownExpand Up@@ -350,6 +350,6 @@ EXAMPLES
$ sf dev generate plugin
```

_See code: [src/commands/dev/generate/plugin.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.2/src/commands/dev/generate/plugin.ts)_
_See code: [src/commands/dev/generate/plugin.ts](https://github.com/salesforcecli/plugin-dev/blob/2.5.3-qa.0/src/commands/dev/generate/plugin.ts)_

<!-- commandsstop -->
2 changes: 1 addition & 1 deletion package.json
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/plugin-dev",
"description": "commands for sf plugin development",
"version": "2.5.2",
"version": "2.5.3-qa.0",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/dev/generate/command.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -107,7 +107,9 @@ export default class GenerateCommand extends SfCommand<void> {

if (Object.keys(generator.pjson.devDependencies).includes('@salesforce/plugin-command-reference')) {
// Get a list of all commands in `sf`. We will use this to determine if a topic is internal or external.
const sfCommandsStdout = shelljs.exec('sf commands --json', { silent: true }).stdout;
const sfBin = shelljs.which('sf');
if (!sfBin) throw messages.createError('errors.InvalidDir');
const sfCommandsStdout = shelljs.exec(`${sfBin} commands --json`, { silent: true }).stdout;
const commandsJson = JSON.parse(sfCommandsStdout) as Array<{ id: string }>;
const commands = commandsJson.map((command) => command.id.replace(/:/g, '.').replace(/ /g, '.'));

Expand Down
7 changes: 4 additions & 3 deletions src/commands/dev/generate/flag.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,7 +9,6 @@ import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs/promises';
import select from '@inquirer/select';
import shelljs from 'shelljs';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { toStandardizedId } from '@oclif/core';
Expand All@@ -18,6 +17,7 @@ import { askQuestions } from '../../../prompts/series/flagPrompts.js';
import { fileExists, build, apply, resolveCommandFilePath } from '../../../util.js';
import { FlagAnswers } from '../../../types.js';
import { stringToChoice } from '../../../prompts/functions.js';
import { Generator } from '../../../generator.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
export const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.flag');
Expand DownExpand Up@@ -66,9 +66,10 @@ export default class DevGenerateFlag extends SfCommand<void> {

await updateMarkdownFile(answers, existing, standardizedCommandId);

shelljs.exec(`yarn prettier --write ${commandFilePath}`);
const generator = new Generator({ dryRun: flags['dry-run'] });
generator.execute(`yarn prettier --write ${commandFilePath}`);

shelljs.exec('yarn compile');
generator.execute('yarn compile');

this.log(`Added ${answers.name} flag to ${commandFilePath}`);
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/dev/generate/plugin.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,7 +49,9 @@ async function fetchGithubUserFromAPI(): Promise<{ login: string; name: string }

function fetchGithubUserFromGit(): string | undefined {
try {
const result = shelljs.exec('git config --get user.name', { silent: true });
const gitBin = shelljs.which('git');
if (!gitBin) return undefined;
const result = shelljs.exec(`${gitBin} config --get user.name`, { silent: true });
return result.stdout.trim();
} catch {
// ignore
Expand Down
13 changes: 11 additions & 2 deletions src/generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,8 +88,17 @@ export class Generator {
return;
}

this.logger.debug(`Executing command: ${cmd}`);
shelljs.exec(cmd, { cwd: this.cwd });
const args = cmd.split(' ');
const bin = args[0];
const isBinPath = bin.includes('/') || bin.includes('\\');
const resolved = isBinPath ? bin : shelljs.which(bin);
if (!resolved) {
throw new Error(`Could not find "${bin}" on PATH`);
}
const resolvedCmd = [resolved.toString(), ...args.slice(1)].join(' ');

this.logger.debug(`Executing command: ${resolvedCmd}`);
shelljs.exec(resolvedCmd, { cwd: this.cwd });
}

public async loadPjson(): Promise<PackageJson> {
Expand Down
25 changes: 25 additions & 0 deletions test/generator.test.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { expect } from 'chai';
import { Generator } from '../src/generator.js';

describe('Generator.execute', () => {
it('should throw when binary is not found on PATH', () => {
const generator = new Generator();

expect(() => generator.execute('nonexistent-binary-xyz --help')).to.throw(
'Could not find "nonexistent-binary-xyz" on PATH'
);
});

it('should not execute commands in dry-run mode', () => {
const generator = new Generator({ dryRun: true });

generator.execute('nonexistent-binary-xyz --help');
});
});
Loading