Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/create-content-sdk-app-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-content-sdk-app': patch
---

[create-content-sdk-app] Add help flag output
60 changes: 59 additions & 1 deletion packages/create-content-sdk-app/src/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { sep } from 'path';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { ParsedArgs } from 'minimist';
import { parseArgs, main, promptDestination, getDestination } from './bin';
import { parseArgs, main, printHelp, promptDestination, getDestination } from './bin';
import * as helpers from './common/utils/helpers';
import * as initialize from './initialize';

Expand Down Expand Up @@ -76,6 +76,41 @@ describe('bin', () => {
expect(args.template).to.equal('nextjs');
expect(args.destination).to.be.undefined;
});

it('should parse help flags', () => {
process.argv = ['node', 'index.ts', '-h'];

const shortArgs = parseArgs();

expect(shortArgs.help).to.equal(true);

process.argv = ['node', 'index.ts', '--help'];

const longArgs = parseArgs();

expect(longArgs.help).to.equal(true);
});
});

describe('printHelp', () => {
let consoleLogStub: SinonStub;

beforeEach(() => {
consoleLogStub = sinon.stub(console, 'log');
});

afterEach(() => {
consoleLogStub?.restore();
});

it('should print all provided templates', () => {
printHelp(['foo', 'bar']);

expect(consoleLogStub).to.have.been.calledOnce;
expect(consoleLogStub.firstCall.args[0]).to.include('Usage:');
expect(consoleLogStub.firstCall.args[0]).to.include('foo');
expect(consoleLogStub.firstCall.args[0]).to.include('bar');
});
});

describe('main', async () => {
Expand Down Expand Up @@ -476,5 +511,28 @@ describe('bin', () => {
expect(consoleLogStub).to.have.been.calledWith(chalk.red('An error occurred:', error));
expect(processExitStub).to.have.been.calledWith(1);
});

it('should print help and skip initialization when help is requested', async () => {
const templates = ['foo', 'bar'];
getAllTemplatesStub.returns(templates);

const args = mockArgs({
help: true,
});

await main(args);

expect(getAllTemplatesStub).to.have.been.calledOnce;
expect(consoleLogStub).to.have.been.calledOnce;
expect(consoleLogStub.firstCall.args[0]).to.include('Usage:');
templates.forEach((template) => {
expect(consoleLogStub.firstCall.args[0]).to.include(template);
});
expect(inquirerPromptStub).to.not.have.been.called;
expect(fsExistsSyncStub).to.not.have.been.called;
expect(fsReaddirSyncStub).to.not.have.been.called;
expect(initializeStub).to.not.have.been.called;
expect(processExitStub).to.not.have.been.called;
});
});
});
31 changes: 30 additions & 1 deletion packages/create-content-sdk-app/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const parseArgs = (): ParsedArgs => {
// to pass to the generator prompts and skip them.
// useful for CI and testing purposes
const options = {
boolean: ['force', 'noInstall', 'yes', 'silent'],
boolean: ['force', 'noInstall', 'yes', 'silent', 'help'],
alias: {
h: 'help',
},
string: ['destination', 'template'],
default: {},
};
Expand All @@ -26,6 +29,27 @@ export const parseArgs = (): ParsedArgs => {
return args;
};

export const printHelp = (templates: string[]) => {
const templatesList = templates.map((template) => ` ${chalk.cyan(template)}`).join('\n');

console.log(`${chalk.bold('Usage:')} create-content-sdk-app ${chalk.cyan('[template]')} ${chalk.yellow('[options]')}

${chalk.bold('Arguments:')}
${chalk.cyan('template')} Template to scaffold

${chalk.bold('Templates:')}
${templatesList}

${chalk.bold('Options:')}
${chalk.yellow('--template')} ${chalk.dim('<name>')} Template to scaffold
${chalk.yellow('--destination')} ${chalk.dim('<path>')} Destination folder
${chalk.yellow('--yes')} Use defaults and skip prompts where possible
${chalk.yellow('--force')} Continue if destination is not empty
${chalk.yellow('--noInstall')} Skip package install and lint fix
${chalk.yellow('--silent')} Suppress normal output
${chalk.yellow('-h, --help')} Show help`);
};

export const getDestination = async (args: ParsedArgs, template: string) => {
if (!template) {
throw new Error('Unable to get destinations, provided template is empty');
Expand Down Expand Up @@ -60,6 +84,11 @@ export const promptDestination = async (prompt: string, defaultDestination: stri
};

export const main = async (args: ParsedArgs) => {
if (args.help) {
printHelp(getAllTemplates());
return;
}

let template: string = '';

// check if template was provided
Expand Down
Loading