From 4ff633f5601cbdee01314cf468d3778dcb7cc499 Mon Sep 17 00:00:00 2001 From: Matthias Rolke Date: Tue, 24 Sep 2024 13:55:04 +0200 Subject: [PATCH 1/2] feat: migrate sfdx commands to sf --- package.json | 3 ++- src/commands/hutte/org/authorize.ts | 2 +- src/common.ts | 22 +++++++++++----------- test/commands/org/authorize.test.ts | 12 ++++++------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 9bce8f3..4ef5101 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ ], "homepage": "https://hutte.io", "keywords": [ - "sfdx-plugin" + "sfdx-plugin", + "sf-plugin" ], "license": "MIT", "oclif": { diff --git a/src/commands/hutte/org/authorize.ts b/src/commands/hutte/org/authorize.ts index ca3614a..59f584f 100644 --- a/src/commands/hutte/org/authorize.ts +++ b/src/commands/hutte/org/authorize.ts @@ -69,7 +69,7 @@ export class Authorize extends SfCommand { } private sfdxPull(org: IScratchOrg): IScratchOrg { - const ret = cross_spawn.sync('sfdx', ['force:source:pull', '-f']); + const ret = cross_spawn.sync('sf', ['project', 'retrieve', 'start', '--ignore-conflicts']); if (ret.status !== 0) { throw new Error(ret.output.join('\n')); } diff --git a/src/common.ts b/src/common.ts index 782b16a..7bd923f 100644 --- a/src/common.ts +++ b/src/common.ts @@ -10,27 +10,27 @@ const AUTH_URL_FILE = 'tmp_hutte_login'; export function sfdxLogin(org: IScratchOrg): IScratchOrg { writeFileSync(AUTH_URL_FILE, org.sfdxAuthUrl!); - const response = cross_spawn.sync('sfdx', [ - 'force:auth:sfdxurl:store', - '-f', - AUTH_URL_FILE, - '-a', + const response = cross_spawn.sync('sf', [ + 'org', 'login', 'sfdx-url', + '--alias', `hutte-${org.slug}`, - '--setdefaultusername', + '--set-default', + '--sfdx-url-file', + AUTH_URL_FILE ]); unlinkSync(AUTH_URL_FILE); if (response.status !== 0) { - throw new Error('The sfdx login failed.'); + throw new Error('The login failed.'); } if (org.revisionNumber) { - cross_spawn.sync('sfdx', ['force:source:tracking:reset', '-r', org.revisionNumber, '-p']); + cross_spawn.sync('sf', ['project', 'reset', 'tracking', '--revision', org.revisionNumber, '--no-prompt']); } return org; } export function devHubSfdxLogin(org: IScratchOrg): void { writeFileSync(AUTH_URL_FILE, org.devhubSfdxAuthUrl!); - const result = cross_spawn.sync('sfdx', ['force:auth:sfdxurl:store', '-f', AUTH_URL_FILE, '-a', devHubAlias(org)]); + const result = cross_spawn.sync('sf', ['org', 'login', 'sfdx-url', '--alias', devHubAlias(org), '--sfdx-url-file', AUTH_URL_FILE]); unlinkSync(AUTH_URL_FILE); if (result.status === 0) { return; @@ -39,7 +39,7 @@ export function devHubSfdxLogin(org: IScratchOrg): void { } export function logoutFromDefault(): void { - const result = cross_spawn.sync('sfdx', ['force:auth:logout', '-p']); + const result = cross_spawn.sync('sf', ['org', 'logout', '--no-prompt']); if (result.status === 0) { return; } @@ -61,7 +61,7 @@ interface IDefaultOrgInfo { export function getDefaultOrgInfo(): IDefaultOrgInfo { try { - const data = JSON.parse(execSync('sfdx force:org:display --json').toString()); + const data = JSON.parse(execSync('sf org display --json').toString()); return data.result; } catch { throw new Error('Error reading the default scratch org'); diff --git a/test/commands/org/authorize.test.ts b/test/commands/org/authorize.test.ts index c251620..904e750 100644 --- a/test/commands/org/authorize.test.ts +++ b/test/commands/org/authorize.test.ts @@ -42,7 +42,7 @@ describe('hutte:org:authorize', async () => { it('authorize happy path', async () => { testContext.SANDBOX.stub(cross_spawn, 'sync') - .withArgs('sfdx') + .withArgs('sf') // @ts-expect-error not the full SpawnSyncReturns .returns({ status: 0 }) .withArgs('git') @@ -54,7 +54,7 @@ describe('hutte:org:authorize', async () => { it('authorize fails on unstaged changes', async () => { testContext.SANDBOX.stub(cross_spawn, 'sync') - .withArgs('sfdx') + .withArgs('sf') // @ts-expect-error not the full SpawnSyncReturns .returns({ status: 0 }) .withArgs('git') @@ -71,7 +71,7 @@ describe('hutte:org:authorize', async () => { it('authorize fails on sfdx error', async () => { testContext.SANDBOX.stub(cross_spawn, 'sync') - .withArgs('sfdx') + .withArgs('sf') // @ts-expect-error not the full SpawnSyncReturns .returns({ status: 1 }) .withArgs('git') @@ -83,12 +83,12 @@ describe('hutte:org:authorize', async () => { } catch (e) { err = e; } - expect(err).to.match(/The sfdx login failed/); + expect(err).to.match(/The login failed/); }); it('authorize org by name', async () => { testContext.SANDBOX.stub(cross_spawn, 'sync') - .withArgs('sfdx') + .withArgs('sf') // @ts-expect-error not the full SpawnSyncReturns .returns({ status: 0 }) .withArgs('git') @@ -102,7 +102,7 @@ describe('hutte:org:authorize', async () => { it('authorize fails when name does not exist', async () => { testContext.SANDBOX.stub(cross_spawn, 'sync') - .withArgs('sfdx') + .withArgs('sf') // @ts-expect-error not the full SpawnSyncReturns .returns({ status: 0 }) .withArgs('git') From c4606a5be86eacf512906c85b33b357016fd4a9d Mon Sep 17 00:00:00 2001 From: Matthias Rolke Date: Tue, 24 Sep 2024 14:02:03 +0200 Subject: [PATCH 2/2] feat: login via --sfdx-url-stdin flag to avoid writing temp file BREAKING CHANGE: @salesforce/cli >= 2.28.6 required --- src/common.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/common.ts b/src/common.ts index 7bd923f..0f00bd0 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,24 +1,19 @@ import { execSync } from 'child_process'; import cross_spawn from 'cross-spawn'; -import { readFileSync, unlinkSync, writeFileSync } from 'fs'; +import { readFileSync, writeFileSync } from 'fs'; import { homedir } from 'os'; import { join } from 'path'; import { parse as parseUrl } from 'url'; import { IScratchOrg } from './api'; -const AUTH_URL_FILE = 'tmp_hutte_login'; - export function sfdxLogin(org: IScratchOrg): IScratchOrg { - writeFileSync(AUTH_URL_FILE, org.sfdxAuthUrl!); - const response = cross_spawn.sync('sf', [ - 'org', 'login', 'sfdx-url', - '--alias', - `hutte-${org.slug}`, - '--set-default', - '--sfdx-url-file', - AUTH_URL_FILE - ]); - unlinkSync(AUTH_URL_FILE); + const response = cross_spawn.sync( + 'sf', + ['org', 'login', 'sfdx-url', '--alias', `hutte-${org.slug}`, '--set-default', '--sfdx-url-stdin'], + { + input: org.sfdxAuthUrl!, + }, + ); if (response.status !== 0) { throw new Error('The login failed.'); } @@ -29,9 +24,9 @@ export function sfdxLogin(org: IScratchOrg): IScratchOrg { } export function devHubSfdxLogin(org: IScratchOrg): void { - writeFileSync(AUTH_URL_FILE, org.devhubSfdxAuthUrl!); - const result = cross_spawn.sync('sf', ['org', 'login', 'sfdx-url', '--alias', devHubAlias(org), '--sfdx-url-file', AUTH_URL_FILE]); - unlinkSync(AUTH_URL_FILE); + const result = cross_spawn.sync('sf', ['org', 'login', 'sfdx-url', '--alias', devHubAlias(org), '--sfdx-url-stdin'], { + input: org.devhubSfdxAuthUrl!, + }); if (result.status === 0) { return; }