generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71ce447
commit 861f516
Showing
15 changed files
with
400 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# summary | ||
|
||
Generate a new sf hook. | ||
|
||
# description | ||
|
||
This will generate a basic hook, add the hook to the package.json, and a generate a test file. | ||
|
||
# flags.force.description | ||
|
||
Overwrite existing files. | ||
|
||
# flags.event.description | ||
|
||
Event to run hook on. | ||
|
||
# examples | ||
|
||
- <%= config.bin %> <%= command.id %> my:command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2022, 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 { Flags } from '@oclif/core'; | ||
import { Messages } from '@salesforce/core'; | ||
import { GeneratorCommand } from '../../../generatorCommand'; | ||
|
||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.hook'); | ||
|
||
export default class GenerateHook extends GeneratorCommand { | ||
public static summary = messages.getMessage('summary'); | ||
public static description = messages.getMessage('description'); | ||
public static examples = messages.getMessages('examples'); | ||
|
||
public static flags = { | ||
force: Flags.boolean({ | ||
description: messages.getMessage('flags.force.description'), | ||
}), | ||
event: Flags.string({ | ||
description: messages.getMessage('flags.event.description'), | ||
options: ['sf:env:display', 'sf:env:list', 'sf:deploy', 'sf:logout'], | ||
required: true, | ||
}), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const { flags } = await this.parse(GenerateHook); | ||
await super.generate('hook', { | ||
force: flags.force, | ||
event: flags.event, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2022, 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 * as path from 'path'; | ||
import { camelCase } from 'change-case'; | ||
import * as Generator from 'yeoman-generator'; | ||
import yosay = require('yosay'); | ||
import { PackageJson } from '../types'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment | ||
const { version } = require('../../package.json'); | ||
|
||
export interface CommandGeneratorOptions extends Generator.GeneratorOptions { | ||
name: string; | ||
event: string; | ||
} | ||
|
||
function toArray(item: string | string[]): string[] { | ||
return Array.isArray(item) ? item : [item]; | ||
} | ||
|
||
export default class Command extends Generator { | ||
public options: CommandGeneratorOptions; | ||
public pjson!: PackageJson; | ||
|
||
public constructor(args: string | string[], opts: CommandGeneratorOptions) { | ||
super(args, opts); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
public async prompting(): Promise<void> { | ||
this.pjson = this.fs.readJSON('package.json') as unknown as PackageJson; | ||
this.log(yosay(`Adding a ${this.options.event} hook to ${this.pjson.name} Version: ${version as string}`)); | ||
} | ||
|
||
public writing(): void { | ||
this.sourceRoot(path.join(__dirname, '../../templates')); | ||
const filename = camelCase(this.options.event.replace('sf:', '')); | ||
this.fs.copyTpl( | ||
this.templatePath(`src/hooks/${this.options.event.replace(/:/g, '.')}.ts.ejs`), | ||
this.destinationPath(`src/hooks/${filename}.ts`), | ||
{ year: new Date().getFullYear() } | ||
); | ||
|
||
// TODO | ||
// this.fs.copyTpl( | ||
// this.templatePath('test/hook.test.ts.ejs'), | ||
// this.destinationPath(`test/hooks/${this.options.event}/${this.options.name}.test.ts`), | ||
// this | ||
// ); | ||
|
||
this.pjson.oclif.hooks = this.pjson.oclif.hooks || {}; | ||
const hooks = this.pjson.oclif.hooks; | ||
const p = `./lib/hooks/${filename}`; | ||
if (hooks[this.options.event]) { | ||
hooks[this.options.event] = [...toArray(hooks[this.options.event]), p]; | ||
} else { | ||
this.pjson.oclif.hooks[this.options.event] = p; | ||
} | ||
|
||
this.fs.writeJSON(this.destinationPath('./package.json'), this.pjson); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright (c) 2020, 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 | ||
*/ | ||
|
||
export interface PackageJson { | ||
name: string; | ||
devDependencies: Record<string, string>; | ||
dependencies: Record<string, string>; | ||
oclif: { | ||
bin: string; | ||
dirname: string; | ||
hooks: Record<string, string | string[]>; | ||
}; | ||
repository: string; | ||
homepage: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) <%- year %>, 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 { basename } from 'path'; | ||
import { Deployable, Deployer, generateTableChoices, SfHook } from '@salesforce/sf-plugins-core'; | ||
|
||
class MyEnvDeployable extends Deployable { | ||
public constructor(public myEnvDir: string, private parent: Deployer) { | ||
super(); | ||
} | ||
|
||
public getName(): string { | ||
return basename(this.myEnvDir); | ||
} | ||
|
||
public getType(): string { | ||
return 'myEnv'; | ||
} | ||
|
||
public getPath(): string { | ||
return basename(this.myEnvDir); | ||
} | ||
|
||
public getParent(): Deployer { | ||
return this.parent; | ||
} | ||
} | ||
|
||
export class MyEnvDeployer extends Deployer { | ||
public static NAME = 'Salesforce MyEnvs'; | ||
|
||
private username!: string; | ||
|
||
public constructor(myEnvDir: string) { | ||
super(); | ||
this.deployables = [new MyEnvDeployable(myEnvDir, this)]; | ||
} | ||
|
||
public getName(): string { | ||
return MyEnvDeployer.NAME; | ||
} | ||
|
||
public async setup(flags: Deployer.Flags, options: Deployer.Options): Promise<Deployer.Options> { | ||
if (flags.interactive) { | ||
this.username = await this.promptForUsername(); | ||
} else { | ||
this.username = (options.username as string) || (await this.promptForUsername()); | ||
} | ||
return { username: this.username }; | ||
} | ||
|
||
public async deploy(): Promise<void> { | ||
this.log(`Deploying to ${this.username}.`); | ||
await Promise.resolve(); | ||
} | ||
|
||
public async promptForUsername(): Promise<string> { | ||
const columns = { name: 'Env' }; | ||
const options = [{ name: 'Env1' }, { name: 'Env2' }]; | ||
const { username } = await this.prompt<{ username: string }>([ | ||
{ | ||
name: 'username', | ||
message: 'Select the environment you want to deploy to:', | ||
type: 'list', | ||
choices: generateTableChoices(columns, options, false), | ||
}, | ||
]); | ||
return username; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
const hook: SfHook.Deploy<MyEnvDeployer> = async function () { | ||
return [new MyEnvDeployer('myEnvs/')]; | ||
}; | ||
|
||
export default hook; |
Oops, something went wrong.