Skip to content

Commit

Permalink
fix: use messages (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley authored Aug 31, 2022
1 parent 0f77014 commit a4f41d6
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 90 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
module.exports = {
extends: ['eslint-config-salesforce-typescript', 'eslint-config-salesforce-license'],
extends: [
'eslint-config-salesforce-typescript',
'eslint-config-salesforce-license',
'plugin:sf-plugin/recommended'
],
};
8 changes: 4 additions & 4 deletions messages/dev.generate.command.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ Generate a new sf command.

This will generate a basic hello world command, a .md file for messages, and test files.

# flags.force.description
# flags.force.summary

Overwrite existing files.

# flags.nuts.description
# flags.nuts.summary

Generate a NUT test file for the command.

# flags.unit.description
# flags.unit.summary

Generate a unit test file for the command.

# flags.name.description
# flags.name.summary

Name of the new command. Must be separated by colons.

Expand Down
4 changes: 2 additions & 2 deletions messages/dev.generate.hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Generate a new sf hook.

This will generate a basic hook, add the hook to the package.json, and a generate a test file.

# flags.force.description
# flags.force.summary

Overwrite existing files.

# flags.event.description
# flags.event.summary

Event to run hook on.

Expand Down
7 changes: 7 additions & 0 deletions messages/dev.hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# summary

Run a hook. For testing purposes only.

# flags.plugin.summary

Specific plugin from which to execute hook
35 changes: 35 additions & 0 deletions messages/plugin.generator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# info.start

Time to build an sf plugin! Version %s

# question.internal

Are you building a plugin for an internal Salesforce team

# question.internal.name

Name (must start with plugin-)

# question.external.name

Name

# error.InvalidName

Name must start with plugin- and be lowercase

# question.description

Description

# question.author

Author

# question.code-coverage

What % code coverage do you want to enforce

# question.hooks

Which commands do you plan to extend
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@types/shelljs": "^0.8.10",
"@types/yeoman-generator": "^5.2.10",
"@types/yosay": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.33.0",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"chai": "^4.2.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^7.32.0",
Expand All @@ -43,6 +43,7 @@
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jsdoc": "^35.1.2",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-sf-plugin": "^1.1.0",
"husky": "^7.0.4",
"lint-staged": "^11.2.6",
"mocha": "^9.1.3",
Expand Down
19 changes: 14 additions & 5 deletions src/commands/dev/generate/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import { SfCommand } from '@salesforce/sf-plugins-core';
import { fileExists, generate } from '../../../util';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.command');
const messages = Messages.load('@salesforce/plugin-dev', 'dev.generate.command', [
'summary',
'description',
'examples',
'flags.name.summary',
'flags.force.summary',
'flags.nuts.summary',
'flags.unit.summary',
'errors.InvalidDir',
]);

export default class GenerateCommand extends SfCommand<void> {
public static enableJsonFlag = false;
Expand All @@ -21,18 +30,18 @@ export default class GenerateCommand extends SfCommand<void> {
public static flags = {
name: Flags.string({
required: true,
description: messages.getMessage('flags.name.description'),
summary: messages.getMessage('flags.name.summary'),
}),
force: Flags.boolean({
description: messages.getMessage('flags.force.description'),
summary: messages.getMessage('flags.force.summary'),
}),
nuts: Flags.boolean({
description: messages.getMessage('flags.nuts.description'),
summary: messages.getMessage('flags.nuts.summary'),
allowNo: true,
default: true,
}),
unit: Flags.boolean({
description: messages.getMessage('flags.unit.description'),
summary: messages.getMessage('flags.unit.summary'),
allowNo: true,
default: false,
}),
Expand Down
13 changes: 10 additions & 3 deletions src/commands/dev/generate/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import { fileExists, generate } from '../../../util';
import { Hook } from '../../../types';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.hook');
const messages = Messages.load('@salesforce/plugin-dev', 'dev.generate.hook', [
'summary',
'description',
'examples',
'flags.force.summary',
'flags.event.summary',
'errors.InvalidDir',
]);

export default class GenerateHook extends SfCommand<void> {
public static enableJsonFlag = false;
Expand All @@ -21,10 +28,10 @@ export default class GenerateHook extends SfCommand<void> {

public static flags = {
force: Flags.boolean({
description: messages.getMessage('flags.force.description'),
summary: messages.getMessage('flags.force.summary'),
}),
event: Flags.string({
description: messages.getMessage('flags.event.description'),
summary: messages.getMessage('flags.event.summary'),
options: Object.keys(Hook),
required: true,
}),
Expand Down
6 changes: 5 additions & 1 deletion src/commands/dev/generate/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { SfCommand } from '@salesforce/sf-plugins-core';
import { generate } from '../../../util';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.library');
const messages = Messages.load('@salesforce/plugin-dev', 'dev.generate.library', [
'summary',
'description',
'examples',
]);

export default class GenerateLibrary extends SfCommand<void> {
public static enableJsonFlag = false;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/generate/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SfCommand } from '@salesforce/sf-plugins-core';
import { generate } from '../../../util';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.plugin');
const messages = Messages.load('@salesforce/plugin-dev', 'dev.generate.plugin', ['summary', 'description', 'examples']);

export default class GeneratePlugin extends SfCommand<void> {
public static enableJsonFlag = false;
Expand Down
11 changes: 8 additions & 3 deletions src/commands/dev/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/

import { Flags, Hook as OclifHook } from '@oclif/core';
import { Messages } from '@salesforce/core';
import { SfCommand, SfHook } from '@salesforce/sf-plugins-core';
import { AnyJson } from '@salesforce/ts-types';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.load('@salesforce/plugin-dev', 'dev.hook', ['summary', 'flags.plugin.summary']);

export default class Hook extends SfCommand<OclifHook.Result<unknown>> {
public static readonly summary = 'Run a hook. For testing purposes only.';
public static readonly summary = messages.getMessage('summary');
public static readonly examples = [
{
description: 'Execute a hook by name:',
Expand All @@ -24,10 +28,11 @@ export default class Hook extends SfCommand<OclifHook.Result<unknown>> {

public static flags = {
plugin: Flags.string({
description: 'Specific plugin from which to execute hook',
summary: messages.getMessage('flags.plugin.summary'),
char: 'p',
}),
};

public static args = [
{
name: 'hook',
Expand All @@ -51,7 +56,7 @@ export default class Hook extends SfCommand<OclifHook.Result<unknown>> {
if (!this.jsonEnabled()) {
results.successes.forEach(({ result, plugin }) => {
this.styledHeader(plugin.name);
this.styledJSON(result);
this.styledJSON(result as AnyJson);
});

results.failures.forEach(({ error, plugin }) => {
Expand Down
33 changes: 23 additions & 10 deletions src/generators/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ import yosay = require('yosay');
import { exec } from 'shelljs';
import replace = require('replace-in-file');
import { camelCase } from 'change-case';
import { Messages } from '@salesforce/core';
import { Hook, NYC, PackageJson } from '../types';
import { addHookToPackageJson, readJson } from '../util';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.load('@salesforce/plugin-dev', 'plugin.generator', [
'info.start',
'question.internal',
'question.internal.name',
'question.external.name',
'question.description',
'question.author',
'question.code-coverage',
'question.hooks',
'error.InvalidName',
]);

// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const { version } = require('../../package.json');

Expand All @@ -37,60 +51,59 @@ export default class Plugin extends Generator {
}

public async prompting(): Promise<void> {
const msg = 'Time to build an sf plugin!';
this.log(yosay(messages.getMessage('info.start', [version as string])));

this.log(yosay(`${msg} Version: ${version as string}`));
this.githubUsername = await this.getGitUsername();

this.answers = await this.prompt<PluginAnswers>([
{
type: 'confirm',
name: 'internal',
message: 'Are you building a plugin for an internal Salesforce team?',
message: messages.getMessage('question.internal'),
},
{
type: 'input',
name: 'name',
message: 'Name (must start with plugin-)',
message: messages.getMessage('question.internal.name'),
validate: (input: string): boolean | string => {
const result = /plugin-[a-z]+$/.test(input);
if (result) return true;

return 'Name must start with plugin- and be lowercase';
return messages.getMessage('error.InvalidName');
},
when: (answers: { internal: boolean }): boolean => answers.internal,
},
{
type: 'input',
name: 'name',
message: 'Name',
message: messages.getMessage('question.external.name'),
validate: (input: string): boolean => Boolean(input),
when: (answers: { internal: boolean }): boolean => !answers.internal,
},
{
type: 'input',
name: 'description',
message: 'Description',
message: messages.getMessage('question.description'),
},
{
type: 'input',
name: 'author',
message: 'author',
message: messages.getMessage('question.author'),
default: this.githubUsername,
when: (answers: { internal: boolean }): boolean => !answers.internal,
},
{
type: 'list',
name: 'codeCoverage',
message: 'What % code coverage do you want to enforce',
message: messages.getMessage('question.code-coverage'),
default: '50%',
choices: ['0%', '25%', '50%', '75%', '90%', '100%'],
when: (answers: { internal: boolean }): boolean => !answers.internal,
},
{
type: 'checkbox',
name: 'hooks',
message: 'Which commands do you plan to extend',
message: messages.getMessage('question.hooks'),
choices: Object.values(Hook),
when: (answers: { internal: boolean }): boolean => answers.internal,
},
Expand Down
Loading

0 comments on commit a4f41d6

Please sign in to comment.