Skip to content

Commit

Permalink
Merge pull request #348 from sparksuite/better-spec-error
Browse files Browse the repository at this point in the history
Better spec error
  • Loading branch information
WesCossick authored Mar 30, 2021
2 parents 19b27a1 + ceffc03 commit 639f68c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waterfall-cli",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.2",
"description": "Effortlessly create CLIs powered by Node.js",
"types": "dist/cjs/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-command-spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('#getCommandSpec()', () => {

it('Complains about invalid spec JS', async () => {
await expect(getCommandSpec(path.join(testFileTrees, 'bad-structure', 'cli', 'invalid-spec-js'))).rejects.toThrow(
'This spec file contains invalid JS'
'Encountered this error while importing the spec file at'
);
});

Expand Down
9 changes: 7 additions & 2 deletions src/utils/get-command-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import chalk from './chalk.js';
import { PrintableError } from './errors.js';
import { ExcludeMe, OmitExcludeMeProperties } from '../types/exclude-matching-properties.js';
import getContext from './get-context.js';

// Define what command input looks like
export interface CommandInput {
Expand Down Expand Up @@ -170,16 +171,20 @@ export default async function getCommandSpec(directory: string): Promise<Generic
);
}

// Get the file path
// Get the context
const context = await getContext();

// Get the file paths
const specFilePath = specFiles[0];
const truncatedPath = specFilePath.replace(`${path.dirname(context.entryFile)}/`, '');

// Return
try {
const spec = (await import(specFilePath)) as { default: GenericCommandSpec } | GenericCommandSpec;
return 'default' in spec ? spec.default : spec;
} catch (error) {
throw new PrintableError(
`This spec file contains invalid JS: ${specFilePath}\n${chalk.bold('JS Error: ')}${String(error)}`
`${String(error)}\n\nEncountered this error while importing the spec file at: ${chalk.bold(truncatedPath)}`
);
}
}

0 comments on commit 639f68c

Please sign in to comment.