Skip to content

Commit 127e9be

Browse files
authored
feat: created hook to load spec file (#28)
* feat: created hook to load spec file * feat: auto loading spec path * feat: added file flag to load spec path * feat: changes suggested by @magicmatatjahu * feat: deleted duplicated code * test: trying to fix the test failing issue * test: fixed issue with file flag. * feat: updated parser to latest * feat: updated the package-lock
1 parent ee258c1 commit 127e9be

File tree

11 files changed

+146
-44
lines changed

11 files changed

+146
-44
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Commands
2525
Options
2626
-c --context context-name saved in the store
2727
-w --watch Enable watchMode (not implemented yet)
28+
-f --file File path of the specification file
2829
2930
context
3031
current show the current set context
@@ -35,7 +36,8 @@ Commands
3536
3637
Examples
3738
$ asyncapi context add dummy ./asyncapi.yml
38-
$ asyncapi validate --context=dummy
39+
$ asyncapi validate --context=dummy
40+
$ asyncapi validate --file=./asyncapi.yml
3941
```
4042

4143
> For now --context flag is requried to run validate command

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dist"
2626
],
2727
"dependencies": {
28-
"@asyncapi/parser": "^1.6.0",
28+
"@asyncapi/parser": "^1.7.0",
2929
"ink": "^3.0.8",
3030
"meow": "^9.0.0",
3131
"react": "^17.0.1",

src/CliModels.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export type HelpMessage = string;
44
export type Arguments = string[];
55

66
export interface Options {
7-
context: string,
8-
watch: boolean
7+
context?: string,
8+
watch: boolean,
9+
file?: string
910
}
1011

1112
export class CliInput {
@@ -34,8 +35,8 @@ export class CliInput {
3435

3536
static createFromMeow(meowOutput: any): CliInput {
3637
const [command, ...args] = meowOutput.input;
37-
const { context, watch } = meowOutput.flags;
38-
return new CliInput(command || 'help', { context, watch }, args);
38+
const { context, watch, file } = meowOutput.flags;
39+
return new CliInput(command || 'help', { context, watch, file }, args);
3940
}
4041

4142
static createSubCommand(cliInput: CliInput): CliInput {

src/cli.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const cli = meow(`
1414
Options
1515
-c --context context-name saved in the store
1616
-w --watch Enable watchMode (not implemented yet)
17+
-f --file File path of the specification file
1718
context
1819
current show the current set context
1920
list show the list of all stored context
@@ -22,8 +23,9 @@ const cli = meow(`
2223
add <context-name> <filepath> add/update new context
2324
2425
Examples
25-
$ asyncapi context add dummy ./asyncapi.yml
26-
$ asyncapi validate --context=dummy
26+
$ asyncapi context add dummy ./asyncapi.yml
27+
$ asyncapi validate --context=dummy
28+
$ asyncapi validate --file=./asyncapi.yml
2729
`, {
2830
flags: {
2931
context: {
@@ -36,6 +38,11 @@ const cli = meow(`
3638
type: 'boolean',
3739
isRequired: false,
3840
default: false
41+
},
42+
file: {
43+
alias: 'f',
44+
type: 'string',
45+
isRequired: false
3946
}
4047
}
4148
});

src/components/Context/context.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('rendering current context', () => {
2626
test('showing error if now current context is found', () => {
2727
testing.deleteDummyContextFile();
2828
let { lastFrame } = render(<ShowCurrentContext />);
29-
expect(lastFrame()).toMatch('No contexts saved yet.');
29+
let message = lastFrame();
30+
expect(message).toMatch('No contexts saved yet.');
3031
})
3132

3233
test('showing current context ', () => {

src/components/Validate/Validate.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ import { Newline, Text } from 'ink';
33
import { Options } from "../../CliModels";
44
import { SpecificationFile, useValidate } from "../../hooks/validation";
55
import { UseValidateResponse } from "../../hooks/validation/models";
6-
import { useContextFile } from '../../hooks/context';
6+
import { useSpecfile } from '../../hooks/context';
77

88
interface ValidateInput {
99
options: Options,
1010
}
1111

1212
const Validate: FunctionComponent<ValidateInput> = ({ options }) => {
13-
let { response, error } = useContextFile().getContext(options.context);
14-
if (error || !response) {
15-
if(error) return <Text color="red">{error.message}</Text>
13+
let { specFile, error } = useSpecfile({ context: options.context, file: options.file });
14+
if (error) {
15+
return <Text color="red">{error.message}</Text>
16+
}
17+
18+
if (!specFile) {
19+
return <Text />
1620
}
1721

1822
const validationInput = {
19-
file: new SpecificationFile(response.getSpecificationName()),
23+
file: new SpecificationFile(specFile.getSpecificationName()),
2024
watchMode: options.watch,
2125
};
2226

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ export class ContextTestingHelper {
3939
getPath(key: string) {
4040
return this._context.store[key];
4141
}
42+
43+
createSpecFileAtWorkingDir() {
44+
if (!fs.existsSync(path.resolve(process.cwd(), 'asyncapi.yml'))) fs.writeFileSync(path.resolve(process.cwd(), 'asyncapi.yml'), '');
45+
}
46+
47+
deleteSpecFileAtWorkingDir() {
48+
if (fs.existsSync(path.resolve(process.cwd(), 'asyncapi.yml'))) fs.unlinkSync(path.resolve(process.cwd(), 'asyncapi.yml'));
49+
}
4250
}

src/hooks/context/contextService.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('ContextService.autoDetect', () => {
5555
const contextService = new ContextService();
5656

5757
test("Should return undefined when does not detects anyfile", () => {
58+
testing.deleteSpecFileAtWorkingDir();
5859
expect(contextService.autoDetectSpecFile()).toBeUndefined();
5960
});
6061

src/hooks/context/hook.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContextFile } from './hooks';
1+
import { useContextFile, useSpecfile } from './hooks';
22
import { ContextFileNotFoundError, KeyNotFoundError, ContextNotFoundError } from './models';
33
import { ContextTestingHelper } from '../../constants';
44
import { SpecificationFile } from '../validation';
@@ -131,3 +131,30 @@ describe('useContextFile().getContext', () => {
131131
expect(response instanceof SpecificationFile).toBeTruthy();
132132
});
133133
})
134+
135+
describe('useSpecFile should ', () => {
136+
it('Load spec file from --file flag', () => {
137+
const { specFile, error } = useSpecfile({ file: './test/specification.yml' });
138+
expect(error).toBeUndefined();
139+
expect(specFile instanceof SpecificationFile).toBeTruthy();
140+
});
141+
it('Load spec file from --context flag', () => {
142+
testingVariables.createDummyContextFile();
143+
const { specFile, error } = useSpecfile({ context: 'home' });
144+
expect(error).toBeUndefined();
145+
expect(specFile instanceof SpecificationFile).toBeTruthy();
146+
});
147+
it('Load spec file from current context', () => {
148+
testingVariables.createDummyContextFile();
149+
const { specFile, error } = useSpecfile({});
150+
expect(error).toBeUndefined();
151+
expect(specFile).toBeDefined();
152+
});
153+
154+
it('Throw error when nothing found', () => {
155+
testingVariables.deleteDummyContextFile()
156+
testingVariables.deleteDummyContextFile();
157+
const { error } = useSpecfile({});
158+
expect(error).toBeDefined();
159+
});
160+
})

0 commit comments

Comments
 (0)