Skip to content

Commit

Permalink
fix: allow edited files to persist as a command instead of hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ksiemer committed May 19, 2022
1 parent 584b101 commit 2d1502b
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 99 deletions.
48 changes: 13 additions & 35 deletions command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@
{
"command": "commerce:examples:convert",
"plugin": "@salesforce/commerce",
"flags": [
"definitionfile",
"json",
"loglevel",
"outputdir",
"sourcepath",
"store-name",
"type"
]
"flags": ["definitionfile", "json", "loglevel", "outputdir", "prompt", "sourcepath", "store-name", "type"]
},
{
"command": "commerce:files:copy",
"plugin": "@salesforce/commerce",
"flags": ["copysourcepath", "dirstocopy", "filestocopy", "json", "loglevel", "prompt"]
},
{
"command": "commerce:payments:quickstart:setup",
"plugin": "@salesforce/commerce",
"flags": [
"apiversion",
"json",
"loglevel",
"payment-adapter",
"store-name",
"targetusername"
]
"flags": ["apiversion", "json", "loglevel", "payment-adapter", "store-name", "targetusername"]
},
{
"command": "commerce:products:import",
Expand All @@ -33,6 +23,7 @@
"json",
"loglevel",
"products-file-csv",
"prompt",
"store-name",
"targetdevhubusername",
"targetusername",
Expand All @@ -48,6 +39,7 @@
"definitionfile",
"json",
"loglevel",
"prompt",
"store-name",
"targetdevhubusername",
"targetusername",
Expand All @@ -72,27 +64,12 @@
{
"command": "commerce:store:open",
"plugin": "@salesforce/commerce",
"flags": [
"all",
"apiversion",
"json",
"loglevel",
"store-name",
"targetdevhubusername",
"targetusername"
]
"flags": ["all", "apiversion", "json", "loglevel", "store-name", "targetdevhubusername", "targetusername"]
},
{
"command": "commerce:store:quickstart:create",
"plugin": "@salesforce/commerce",
"flags": [
"apiversion",
"json",
"loglevel",
"store-name",
"targetusername",
"templatename"
]
"flags": ["apiversion", "json", "loglevel", "store-name", "targetusername", "templatename"]
},
{
"command": "commerce:store:quickstart:setup",
Expand All @@ -102,9 +79,10 @@
"definitionfile",
"json",
"loglevel",
"prompt",
"store-name",
"targetdevhubusername",
"targetusername"
]
}
]
]
12 changes: 5 additions & 7 deletions messages/commerce.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"nameFlagDescription": "name to print",
"errorNoOrgResults": "No results found for the org '%s'.",
"files": {
"fileDifference": "Local file %s does not match plugin file %s.",
"cmdDescription": "Copy files from source to destination folder",
"fileDifference": "Local file %s does not match source file %s.",
"copyPluginVersionYN": "Would you like replace %s with %s? Y/N",
"backingUpExistingFile": "Backing up existing local file as: %s\n",
"overwritingFile": "Overwriting file %s with %s\n",
"skippingFile": "Skipping replacing existing file: %s\n"
"backingUpExistingFile": "Backing up existing local file as: %s",
"overwritingFile": "Overwriting file %s with %s",
"skippingFile": "Skipping replacing existing file: %s"
},
"setup": {
"cmdDescription": "Setup a devhub and scratch org from start to finish with one command",
Expand All @@ -18,9 +19,6 @@
"errorStoreCreate": "Store %s with Scratch Org Admin %s failed for StoreCreate",
"errorPaymentsQuickstartSetup": "Store %s with Scratch Org Admin %s failed for PaymentsQuickstartSetup"
},
"downloads": {
"hookRunning": "example init hook running before %s"
},
"shell": {
"errorCheck": "TypeError: Converting circular structure to JSON",
"noResultsForCommand": "No results for command: \n"
Expand Down
10 changes: 7 additions & 3 deletions src/commands/commerce/examples/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { fs, Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import { allFlags } from '../../../lib/flags/commerce/all.flags';
import { exampleFlags } from '../../../lib/flags/commerce/convert.flags';
import { filterFlags, removeFlagBeforeAll } from '../../../lib/utils/args/flagsUtils';
import { BASE_DIR, CONFIG_DIR, EXAMPLE_DIR } from '../../../lib/utils/constants/properties';
import { addAllowedArgs, filterFlags, modifyArgFlag, removeFlagBeforeAll } from '../../../lib/utils/args/flagsUtils';
import { BASE_DIR, CONFIG_DIR, EXAMPLE_DIR, FILE_COPY_ARGS } from '../../../lib/utils/constants/properties';
import { copyFileSync, mkdirSync, readFileSync, renameRecursive } from '../../../lib/utils/fsUtils';
import { shell } from '../../../lib/utils/shell';
import { convertStoreScratchDefToExamples, parseStoreScratchDef } from '../../../lib/utils/jsonUtils';
import { FilesCopy } from '../files/copy';

const TOPIC = 'examples';
const CMD = `commerce:${TOPIC}:convert`;
Expand All @@ -29,10 +30,13 @@ export class ExamplesConvert extends SfdxCommand {

protected static flagsConfig = {
...exampleFlags,
...filterFlags(['store-name'], allFlags),
...filterFlags(['store-name', 'prompt'], allFlags),
};

public async run(): Promise<AnyJson> {
// Copy all example files
FILE_COPY_ARGS.forEach((v) => modifyArgFlag(v.args, v.value, this.argv));
await FilesCopy.run(addAllowedArgs(this.argv, FilesCopy), this.config);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
if (!this.flags.definitionfile || !fs.existsSync(this.flags.definitionfile)) {
this.flags.definitionfile = CONFIG_DIR + '/store-scratch-def.json';
Expand Down
77 changes: 77 additions & 0 deletions src/commands/commerce/files/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 { fs, Messages, SfdxError } from '@salesforce/core';
import { SfdxCommand } from '@salesforce/command';
import { BASE_DIR } from '../../../lib/utils/constants/properties';
import { copyFolderRecursiveWithConfirm, mkdirSync } from '../../../lib/utils/fsUtils';
import { filesFlags } from '../../../lib/flags/commerce/files.flags';

const TOPIC = 'files';
const CMD = `commerce:${TOPIC}:copy`;
const msgs = Messages.loadMessages('@salesforce/commerce', 'commerce');

export class FilesCopy extends SfdxCommand {
public static description = msgs.getMessage('files.cmdDescription', [BASE_DIR]);
public static examples = [
`sfdx ${CMD} -y --copySourcePath "~/myexamplefilesdirectory" --filestocopy "file1.txt,file2.txt" --dirstocopy "dir1,dir2,dir3"`,
`sfdx ${CMD} --prompt --copySourcePath "~/myexamplefilesdirectory" --filestocopy "file1.txt,file2.txt" --dirstocopy "dir1,dir2,dir3"`,
`sfdx ${CMD} --copySourcePath "~/myexamplefilesdirectory" --filestocopy "file1.txt,file2.txt" --dirstocopy "dir1,dir2,dir3"`,
];
public static varargs = {
required: false,
validator: (name: string): void => {
// Whitelist varargs parameter names
if (!FilesCopy.vargsAllowList.includes(name)) {
const errMsg = `Invalid parameter [${name}] found`;
const errName = 'InvalidVarargName';
const errAction = `Choose one of these parameter names: ${FilesCopy.vargsAllowList.join()}`;
throw new SfdxError(errMsg, errName, [errAction]);
}
},
};

public static get vargsAllowList(): string[] {
return ['prompt', 'copysourcepath', 'filestocopy', 'dirstocopy'];
}

protected static flagsConfig = {
...filesFlags,
};

public async run(): Promise<void> {
// const passedArgs = getPassedArgs(this.argv, this.flags);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const prompt = this.flags.prompt;
mkdirSync(BASE_DIR);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const sourceBaseDir: string = this.flags.copysourcepath;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const dirsToCopy = this.flags.dirstocopy;
let dirs: string[];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
if (Array.isArray(dirsToCopy)) dirs = dirsToCopy;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
else dirs = dirsToCopy !== null ? dirsToCopy.split(',') : null;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const filesToCopy = this.flags.filestocopy;
let files: string[];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
if (Array.isArray(filesToCopy)) files = filesToCopy;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
else files = filesToCopy !== null ? filesToCopy.split(',') : null;
if (sourceBaseDir !== null) {
for (const d of dirs) {
await copyFolderRecursiveWithConfirm(`${sourceBaseDir}/${d}`, BASE_DIR, prompt);
}
files
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
.filter((f) => !fs.existsSync(`${BASE_DIR}/${f}`))
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
.forEach((f) => fs.copyFileSync(`${sourceBaseDir}/${f}`, `${BASE_DIR}/${f}`));
}
}
}
10 changes: 7 additions & 3 deletions src/commands/commerce/payments/quickstart/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { Messages, Org, SfdxError } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import { paymentsFlags } from '../../../../lib/flags/commerce/payments.flags';
import { storeFlags } from '../../../../lib/flags/commerce/store.flags';
import { filterFlags } from '../../../../lib/utils/args/flagsUtils';
import { EXAMPLE_DIR } from '../../../../lib/utils/constants/properties';
import { addAllowedArgs, filterFlags, modifyArgFlag } from '../../../../lib/utils/args/flagsUtils';
import { EXAMPLE_DIR, FILE_COPY_ARGS } from '../../../../lib/utils/constants/properties';
import { forceDataRecordCreate, forceDataRecordDelete, forceDataSoql } from '../../../../lib/utils/sfdx/forceDataSoql';
import { shell, shellJsonSfdx } from '../../../../lib/utils/shell';
import { StoreQuickstartSetup } from '../../store/quickstart/setup';
import { FilesCopy } from '../../files/copy';

Messages.importMessagesDirectory(__dirname);

Expand All @@ -28,12 +29,15 @@ export class PaymentsQuickstartSetup extends SfdxCommand {
public static examples = [`sfdx ${CMD} -p Stripe`]; // TODO documentation including examples and descriptions
protected static flagsConfig = {
...paymentsFlags,
...filterFlags(['store-name'], storeFlags),
...filterFlags(['store-name', 'prompt'], storeFlags),
};
public org: Org;

// eslint-disable-next-line @typescript-eslint/require-await
public async run(): Promise<AnyJson> {
// Copy all example files
FILE_COPY_ARGS.forEach((v) => modifyArgFlag(v.args, v.value, this.argv));
await FilesCopy.run(addAllowedArgs(this.argv, FilesCopy), this.config);
const selection = this.flags['payment-adapter'] as string;
const namedCredentialMasterLabel = selection;
const paymentGatewayAdapterName = `${selection}Adapter`;
Expand Down
10 changes: 8 additions & 2 deletions src/commands/commerce/products/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import chalk from 'chalk';
import { AnyJson } from '@salesforce/ts-types';
import { productsFlags } from '../../../lib/flags/commerce/products.flags';
import { storeFlags } from '../../../lib/flags/commerce/store.flags';
import { filterFlags } from '../../../lib/utils/args/flagsUtils';
import { BASE_DIR, CONFIG_DIR, JSON_DIR, STORE_DIR } from '../../../lib/utils/constants/properties';
import { addAllowedArgs, filterFlags, modifyArgFlag } from '../../../lib/utils/args/flagsUtils';
import { BASE_DIR, CONFIG_DIR, FILE_COPY_ARGS, JSON_DIR, STORE_DIR } from '../../../lib/utils/constants/properties';
import { ImportResult, parseStoreScratchDef, replaceErrors } from '../../../lib/utils/jsonUtils';
import { forceDataRecordCreate, forceDataSoql } from '../../../lib/utils/sfdx/forceDataSoql';
import { shellJsonSfdx } from '../../../lib/utils/shell';
import { StatusFileManager } from '../../../lib/utils/statusFileManager';
import { StoreCreate } from '../store/create';
import { exampleFlags } from '../../../lib/flags/commerce/convert.flags';
import { FilesCopy } from '../files/copy';
import { filesFlags } from '../../../lib/flags/commerce/files.flags';

Messages.importMessagesDirectory(__dirname);

Expand All @@ -35,6 +37,7 @@ export class ProductsImport extends SfdxCommand {
...productsFlags,
...filterFlags(['definitionfile', 'type'], exampleFlags),
...filterFlags(['store-name'], storeFlags),
...filterFlags(['prompt'], filesFlags),
};

public org: Org;
Expand All @@ -44,6 +47,9 @@ export class ProductsImport extends SfdxCommand {

// tslint:disable-next-line:no-any
public async run(): Promise<AnyJson> {
// Copy all example files
FILE_COPY_ARGS.forEach((v) => modifyArgFlag(v.args, v.value, this.argv));
await FilesCopy.run(addAllowedArgs(this.argv, FilesCopy), this.config);
this.devHubUsername = (await this.org.getDevHubOrg()).getUsername();
this.statusFileManager = new StatusFileManager(
this.devHubUsername,
Expand Down
12 changes: 8 additions & 4 deletions src/commands/commerce/store/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CONFIG_DIR,
SCRATCH_ORG_DIR,
STORE_DIR,
FILE_COPY_ARGS,
} from '../../../lib/utils/constants/properties';
import { BuyerUserDef, parseStoreScratchDef, replaceErrors, UserInfo } from '../../../lib/utils/jsonUtils';
import { Requires } from '../../../lib/utils/requires';
Expand All @@ -25,6 +26,7 @@ import { shell, shellJsonSfdx } from '../../../lib/utils/shell';
import { sleep } from '../../../lib/utils/sleep';
import { StatusFileManager } from '../../../lib/utils/statusFileManager';
import { mkdirSync } from '../../../lib/utils/fsUtils';
import { FilesCopy } from '../files/copy';
import { StoreQuickstartCreate } from './quickstart/create';
import { StoreQuickstartSetup } from './quickstart/setup';
import { StoreOpen } from './open';
Expand Down Expand Up @@ -55,14 +57,15 @@ export class StoreCreate extends SfdxCommand {
public static get vargsAllowList(): string[] {
return ['buyerEmail', 'existingBuyerAuthentication', 'buyerAlias'].concat(
StoreQuickstartSetup.vargsAllowList,
StoreQuickstartCreate.vargsAllowList
StoreQuickstartCreate.vargsAllowList,
FilesCopy.vargsAllowList
);
}

public static description = msgs.getMessage('create.cmdDescription');
public static examples = [`sfdx ${CMD} --store-name test-store`];
protected static flagsConfig = filterFlags(
['store-name', 'templatename', 'definitionfile', 'type', 'buyer-username'],
['store-name', 'templatename', 'definitionfile', 'type', 'buyer-username', 'prompt'],
allFlags
);
public org: Org;
Expand Down Expand Up @@ -162,9 +165,10 @@ export class StoreCreate extends SfdxCommand {
}
}
public async run(): Promise<AnyJson> {
// Copy all example files
await this.config.runHook('files', {});
this.devhubUsername = (await this.org.getDevHubOrg()).getUsername();
// Copy all example files
FILE_COPY_ARGS.forEach((v) => modifyArgFlag(v.args, v.value, this.argv));
await FilesCopy.run(addAllowedArgs(this.argv, FilesCopy), this.config);
const passedArgs = getPassedArgs(this.argv, this.flags);
if (!this.flags.type || (this.flags.type !== 'b2c' && this.flags.type !== 'b2b')) this.flags.type = 'b2c';
if (!Object.keys(passedArgs).includes('definitionfile') && Object.keys(passedArgs).includes('type'))
Expand Down
9 changes: 7 additions & 2 deletions src/commands/commerce/store/quickstart/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { fs, Messages, SfdxError, Org as SfdxOrg } from '@salesforce/core';
import chalk from 'chalk';
import { AnyJson } from '@salesforce/ts-types';
import { allFlags } from '../../../../lib/flags/commerce/all.flags';
import { addAllowedArgs, filterFlags } from '../../../../lib/utils/args/flagsUtils';
import { addAllowedArgs, filterFlags, modifyArgFlag } from '../../../../lib/utils/args/flagsUtils';
import {
BASE_DIR,
BUYER_USER_DEF,
EXAMPLE_DIR,
FILE_COPY_ARGS,
PACKAGE_RETRIEVE,
PACKAGE_RETRIEVE_TEMPLATE,
QUICKSTART_CONFIG,
Expand All @@ -31,6 +32,7 @@ import { shell, shellJsonSfdx } from '../../../../lib/utils/shell';
import { StatusFileManager } from '../../../../lib/utils/statusFileManager';
import { ProductsImport } from '../../products/import';
import { StoreCreate } from '../create';
import { FilesCopy } from '../../files/copy';

Messages.importMessagesDirectory(__dirname);

Expand Down Expand Up @@ -67,7 +69,7 @@ export class StoreQuickstartSetup extends SfdxCommand {
public static examples = [`sfdx ${CMD} --definitionfile store-scratch-def.json`];

protected static flagsConfig = {
...filterFlags(['store-name', 'definitionfile'], allFlags),
...filterFlags(['store-name', 'definitionfile', 'prompt'], allFlags),
};

private static storeType: string;
Expand Down Expand Up @@ -99,6 +101,9 @@ export class StoreQuickstartSetup extends SfdxCommand {
}

public async run(): Promise<AnyJson> {
// Copy all example files
FILE_COPY_ARGS.forEach((v) => modifyArgFlag(v.args, v.value, this.argv));
await FilesCopy.run(addAllowedArgs(this.argv, FilesCopy), this.config);
this.devHubUsername = (await this.org.getDevHubOrg()).getUsername();
this.statusFileManager = new StatusFileManager(
this.devHubUsername,
Expand Down
Loading

0 comments on commit 2d1502b

Please sign in to comment.