Skip to content

Commit

Permalink
feat(cli): add option to include functional test suite (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Apr 23, 2024
1 parent 1a78aba commit 5655f81
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-camels-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/create-catalyst": minor
---

Adds an option to include the functional test suite as part of the create command. Defaults to false.
6 changes: 3 additions & 3 deletions packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { writeEnv } from '../utils/write-env';
const exec = promisify(execCallback);

export const create = async (options: CreateCommandOptions) => {
const { packageManager, codeEditor } = options;
const { packageManager, codeEditor, includeFunctionalTests } = options;

const URLSchema = z.string().url();
const sampleDataApiUrl = parse(options.sampleDataApiUrl, URLSchema);
Expand Down Expand Up @@ -97,7 +97,7 @@ export const create = async (options: CreateCommandOptions) => {
if (!storeHash || !accessToken) {
console.log(`\nCreating '${projectName}' at '${projectDir}'\n`);

await cloneCatalyst({ projectDir, projectName, ghRef, codeEditor });
await cloneCatalyst({ projectDir, projectName, ghRef, codeEditor, includeFunctionalTests });

console.log(`\nUsing ${chalk.bold(packageManager)}\n`);

Expand Down Expand Up @@ -193,7 +193,7 @@ export const create = async (options: CreateCommandOptions) => {

console.log(`\nCreating '${projectName}' at '${projectDir}'\n`);

await cloneCatalyst({ projectDir, projectName, ghRef, codeEditor });
await cloneCatalyst({ projectDir, projectName, ghRef, codeEditor, includeFunctionalTests });

writeEnv(projectDir, {
channelId: channelId.toString(),
Expand Down
5 changes: 5 additions & 0 deletions packages/create-catalyst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const createCommand = program
.default('vscode')
.hideHelp(),
)
.addOption(
new Option('--include-functional-tests', 'Include the functional test suite')
.default(false)
.hideHelp(),
)
.action((opts) => create(opts));

export type CreateCommandOptions = Options<typeof createCommand>[0];
Expand Down
26 changes: 26 additions & 0 deletions packages/create-catalyst/src/utils/clone-catalyst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import { readFile, writeFile } from 'fs/promises';
import { copySync, ensureDir, readJsonSync, removeSync, writeJsonSync } from 'fs-extra/esm';
import { downloadTemplate } from 'giget';
import merge from 'lodash.merge';
Expand All @@ -9,11 +10,13 @@ import { spinner } from './spinner';

export const cloneCatalyst = async ({
codeEditor,
includeFunctionalTests,
projectDir,
projectName,
ghRef = 'main',
}: {
codeEditor: string;
includeFunctionalTests: boolean;
projectDir: string;
projectName: string;
ghRef?: string;
Expand Down Expand Up @@ -93,6 +96,11 @@ export const cloneCatalyst = async ({
delete packageJson.devDependencies['react-dom']; // will go away
delete packageJson.devDependencies['@bigcommerce/eslint-config-catalyst'];

if (!includeFunctionalTests) {
delete packageJson.devDependencies['@faker-js/faker'];
delete packageJson.devDependencies['@playwright/test'];
}

writeJsonSync(join(projectDir, 'package.json'), packageJson, { spaces: 2 });

const tsConfigJson = z
Expand All @@ -108,6 +116,7 @@ export const cloneCatalyst = async ({
.passthrough(),
})
.passthrough(),
include: z.array(z.string()).optional(),
})
.passthrough()
.parse(
Expand All @@ -123,7 +132,24 @@ export const cloneCatalyst = async ({

tsConfigJson.compilerOptions.paths['@bigcommerce/components/*'] = ['./components/ui/*'];

if (!includeFunctionalTests) {
tsConfigJson.include = tsConfigJson.include?.filter(
(include) => !['tests/**/*', 'playwright.config.ts'].includes(include),
);
}

writeJsonSync(join(projectDir, 'tsconfig.json'), tsConfigJson, { spaces: 2 });

if (!includeFunctionalTests) {
const eslint = (await readFile(join(projectDir, '.eslintrc.cjs'), { encoding: 'utf-8' }))
.replace(/['"]\/playwright-report\/\*\*['"],?/, '')
.replace(/['"]\/test-results\/\*\*['"],?/, '');

await writeFile(join(projectDir, '.eslintrc.cjs'), eslint);

removeSync(join(projectDir, 'tests'));
removeSync(join(projectDir, 'playwright.config.ts'));
}

removeSync(join(projectDir, 'tmp'));
};

0 comments on commit 5655f81

Please sign in to comment.