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
17 changes: 17 additions & 0 deletions original_program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ... (skip down to line 179)
program.on('command:*', async function (this: Command, op) {
const msg = intl.formatMessage(
{
defaultMessage: 'Unknown command "clasp {command}"',
},
{
command: op[0],
},
);
this.error(msg as string);
});

program.error;

return program;
}
8 changes: 4 additions & 4 deletions src/commands/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function makeProgram(exitOverride?: (err: CommanderError) => void) {
cmd.copyInheritedSettings(program);
}

program.on('command:*', async function (this: Command, op) {
program.on('command:*', function (this: Command, op) {
const msg = intl.formatMessage(
{
defaultMessage: 'Unknown command "clasp {command}"',
Expand All @@ -188,10 +188,10 @@ export function makeProgram(exitOverride?: (err: CommanderError) => void) {
command: op[0],
},
);
this.error(msg as string);
console.error(msg as string);
process.exitCode = 1;
program.help();
});

program.error;

return program;
}
3 changes: 2 additions & 1 deletion src/core/clasp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export async function initClaspInstance(options: InitOptions): Promise<Clasp> {
const rootDirReal = await fs.realpath(projectRoot.rootDir).catch(() => projectRoot.rootDir);

// Strict validation: resolved path must be rootDir or properly inside it
const isValid = contentDir === projectRoot.rootDir ||
const isValid =
contentDir === projectRoot.rootDir ||
(contentDir.startsWith(rootDirReal + path.sep) && isInside(projectRoot.rootDir, contentDir));

if (!isValid) {
Expand Down
14 changes: 14 additions & 0 deletions test/commands/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {expect} from 'chai';
import {describe, it} from 'mocha';

import {makeProgram} from '../../src/commands/program.js';
import {runCommand} from './utils.js';

describe('Consistency between imported and registered commands', () => {
const expectedCommands = [
Expand Down Expand Up @@ -67,3 +68,16 @@ describe('Consistency between imported and registered commands', () => {
expect(program.commands).to.length(expectedCommands.length);
});
});

describe('Unknown commands', () => {
it('should print help when an unknown command is provided', async () => {
const result = await runCommand(['nonexistentcommand'], false);

expect(result.stderr).to.contain('Unknown command "clasp nonexistentcommand"');
expect(result.stdout).to.contain('Usage: clasp <command> [options]');

// Also the global process.exitCode should be set to 1 by our patch
expect(process.exitCode).to.equal(1);
process.exitCode = 0; // reset for subsequent tests
});
});
Loading