diff --git a/messages/connect.md b/messages/connect.md new file mode 100644 index 00000000..e69de29b diff --git a/messages/messages.json b/messages/messages.json deleted file mode 100644 index e06c60e9..00000000 --- a/messages/messages.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "HelpDefaults": "If not supplied, the apiversion, template, and outputdir use default values.\n", - "HelpOutputDirRelative": "The outputdir can be an absolute path or relative to the current working directory.\n", - "HelpOutputDirRelativeLightning": "If you don’t specify an outputdir, we create a subfolder in your current working directory with the name of your bundle. For example, if the current working directory is force-app and your Lightning bundle is called myBundle, we create force-app/myBundle/ to store the files in the bundle.\n", - "HelpExamplesTitle": "\nExamples:\n", - "OutputDirFlagDescription": "folder for saving the created files", - "OutputDirFlagLongDescription": "The directory to store the newly created files. The location can be an absolute path or relative to the current working directory. The default is the current directory.", - "TemplateFlagDescription": "template to use for file creation", - "TemplateFlagLongDescription": "The template to use to create the file. Supplied parameter values or default values are filled into a copy of the template.", - "TargetDirOutput": "target dir = %s", - "App": "app", - "Event": "event", - "Interface": "interface", - "Test": "test", - "Component": "component", - "Page": "page", - "AlphaNumericNameError": "Name must contain only alphanumeric characters.", - "NameMustStartWithLetterError": "Name must start with a letter.", - "EndWithUnderscoreError": "Name can't end with an underscore.", - "DoubleUnderscoreError": "Name can't contain 2 consecutive underscores." -} diff --git a/messages/org.json b/messages/org.json deleted file mode 100644 index b5c9eec0..00000000 --- a/messages/org.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "commandDescription": "print a greeting and your org IDs", - "nameFlagDescription": "name to print", - "forceFlagDescription": "example boolean flag", - "errorNoOrgResults": "No results found for the org '%s'." -} diff --git a/package.json b/package.json index a6bf2336..baaf19bd 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,9 @@ "bugs": "https://github.com/forcedotcom/cli/issues", "dependencies": { "@oclif/core": "^0.5.4", - "@salesforce/core": "^2.20.10" + "@salesforce/core": "3.0.0", + "open": "^8.0.7", + "tslib": "^2" }, "devDependencies": { "@oclif/dev-cli": "^1", diff --git a/src/commands/env/connect.ts b/src/commands/env/connect.ts new file mode 100644 index 00000000..4243acad --- /dev/null +++ b/src/commands/env/connect.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021, 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 os from 'os'; +import * as open from 'open'; + +import { Command, Flags } from '@oclif/core'; +import { AuthFields, AuthInfo, Logger, OAuth2Options, SfdxError, WebOAuthServer } from '@salesforce/core'; + +// TODO: add back once md messages are supported +// Messages.importMessagesDirectory(__dirname); +// const messages = Messages.loadMessages('@salesforce/plugin-env', 'connect'); + +export default class EnvConnect extends Command { + // public static readonly description = messages.getMessage('description'); + // public static readonly examples = messages.getMessage('examples').split(os.EOL); + public static readonly description = 'connect to a Salesforce account or environment'; + public static readonly examples = ''.split(os.EOL); + public static flags = { + 'login-url': Flags.string({ + char: 'r', + description: 'the login URL', + default: 'https://login.salesforce.com', + }), + }; + + public async run(): Promise { + const { flags } = await this.parse(EnvConnect); + const oauthConfig: OAuth2Options = { loginUrl: flags['login-url'] }; + + try { + const authInfo = await this.executeLoginFlow(oauthConfig); + const fields = authInfo.getFields(true); + const successMsg = `Successfully authorized ${fields.username} with ID ${fields.orgId}`; + this.log(successMsg); + return fields; + } catch (err) { + const error = err as Error; + Logger.childFromRoot('auth').debug(error); + if (error.name === 'AuthCodeExchangeError') { + const errMsg = `Invalid client credentials. Verify the OAuth client secret and ID. ${error.message}`; + throw new SfdxError(errMsg); + } + throw error; + } + } + + private async executeLoginFlow(oauthConfig: OAuth2Options): Promise { + const oauthServer = await WebOAuthServer.create({ oauthConfig }); + await oauthServer.start(); + await open(oauthServer.getAuthorizationUrl(), { wait: false }); + return oauthServer.authorizeAndSave(); + } +} diff --git a/src/commands/hello/org.ts b/src/commands/hello/org.ts deleted file mode 100644 index 68ab9f51..00000000 --- a/src/commands/hello/org.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 - */ - -import { Command } from '@oclif/core'; -import { Messages } from '@salesforce/core/lib/messages'; -import { AnyJson } from '@salesforce/ts-types'; - -// Initialize Messages with the current plugin directory -Messages.importMessagesDirectory(__dirname); - -// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, -// or any library that is using the messages framework can also be loaded this way. -const messages = Messages.loadMessages('@salesforce/plugin-env', 'org'); - -export default class Org extends Command { - public static description = messages.getMessage('commandDescription'); - - public static examples = []; - - public static args = [{ name: 'file' }]; - - protected static flagsConfig = {}; - - // eslint-disable-next-line @typescript-eslint/require-await - public async run(): Promise { - this.log('Hello world!'); - - return {}; - } -} diff --git a/test/commands/env/connect.test.ts b/test/commands/env/connect.test.ts new file mode 100644 index 00000000..67ff46a7 --- /dev/null +++ b/test/commands/env/connect.test.ts @@ -0,0 +1,6 @@ +/* + * 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 + */ diff --git a/test/commands/hello/org.test.ts b/test/commands/hello/org.test.ts deleted file mode 100644 index da075732..00000000 --- a/test/commands/hello/org.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 - */ - -import { test } from '@oclif/test'; -import { expect } from 'chai'; - -describe('hello:org', () => { - test - .stdout() - // Test needs to be updated to support spaced commands - .command(['hello:org']) - .it('runs hello:org', (ctx) => { - expect(ctx.stdout).to.contain('Hello world!'); - }); -}); diff --git a/yarn.lock b/yarn.lock index de293866..8fe3e469 100644 --- a/yarn.lock +++ b/yarn.lock @@ -549,7 +549,27 @@ chalk "^2.4.2" cli-ux "^4.9.3" -"@salesforce/core@^2.15.2", "@salesforce/core@^2.2.0", "@salesforce/core@^2.20.10", "@salesforce/core@^2.20.3": +"@salesforce/core@3.0.0": + version "3.0.0" + resolved "https://registry.npmjs.org/@salesforce/core/-/core-3.0.0.tgz#d26e43c35c9711d7fab7caa862fa69048c770d1f" + integrity sha512-Q5gLhlYqrPo+vXAivmWSjneBluo7tgYO6xfJC1IdUnm1OEL9RZBFY8bWXhuoikulP4hczIK13XqDrt2cq/Nvjw== + dependencies: + "@salesforce/bunyan" "^2.0.0" + "@salesforce/kit" "^1.5.0" + "@salesforce/schemas" "^1.0.1" + "@salesforce/ts-types" "^1.0.0" + "@types/graceful-fs" "^4.1.3" + "@types/jsforce" "1.9.23" + "@types/mkdirp" "1.0.0" + debug "^3.1.0" + graceful-fs "^4.2.4" + jsen "0.6.6" + jsforce "^1.10.0" + jsonwebtoken "8.5.0" + mkdirp "1.0.4" + sfdx-faye "^1.0.9" + +"@salesforce/core@^2.15.2", "@salesforce/core@^2.2.0", "@salesforce/core@^2.20.3": version "2.20.10" resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-2.20.10.tgz#b2171043ddaac51b464e8ea3659b4f34ce7bbf73" integrity sha512-ogER5ieNlmHlBt0SfVC0XLHtPtH/WaBAFtsZitHMLV/Zyv0PHwP+5xpSygMmSDzuLZjRooXikH9dFu0z8WEjJw== @@ -2030,6 +2050,11 @@ default-require-extensions@^3.0.0: dependencies: strip-bom "^4.0.0" +define-lazy-prop@^2.0.0: + version "2.0.0" + resolved "https://registry.npmjs.org/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz#3f7ae421129bcaaac9bc74905c98a0009ec9ee7f" + integrity sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og== + define-properties@^1.1.2, define-properties@^1.1.3: version "1.1.3" resolved "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1" @@ -3443,6 +3468,11 @@ is-docker@^2.0.0: resolved "https://registry.npmjs.org/is-docker/-/is-docker-2.1.1.tgz#4125a88e44e450d384e09047ede71adc2d144156" integrity sha512-ZOoqiXfEwtGknTiuDEy8pN2CfE3TxMHprvNer1mXiqwkOT77Rw3YVrUQ52EqAOU3QAWDQ+bQdx7HJzrv7LS2Hw== +is-docker@^2.1.1: + version "2.2.1" + resolved "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz#33eeabe23cfe86f14bde4408a02c0cfb853acdaa" + integrity sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ== + is-extendable@^0.1.0, is-extendable@^0.1.1: version "0.1.1" resolved "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89" @@ -4678,6 +4708,15 @@ onetime@^5.1.0: dependencies: mimic-fn "^2.1.0" +open@^8.0.7: + version "8.0.7" + resolved "https://registry.npmjs.org/open/-/open-8.0.7.tgz#5597eeff14b440f6ff78fb7ced9ede9f69b2122d" + integrity sha512-qoyG0kpdaWVoL5MiwTRQWujSdivwBOgfLadVEdpsZNHOK1+kBvmVtLYdgWr8G4cgBpG9zaxezn6jz6PPdQW5xg== + dependencies: + define-lazy-prop "^2.0.0" + is-docker "^2.1.1" + is-wsl "^2.2.0" + opencollective-postinstall@^2.0.2: version "2.0.3" resolved "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz#7a0fff978f6dbfa4d006238fbac98ed4198c3259"