Skip to content

Commit

Permalink
feat: support context file location in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Jul 4, 2023
1 parent a2d375c commit 74f0199
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
10 changes: 5 additions & 5 deletions docs/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ If your use case is that you work with multiple repositories, you might want to

##### Using CLI's `init` command:

`asyncapi config context init [CONTEXT-FILE-PATH] [CONTEXT-NAME] [SPEC-FILE-PATH]`
`asyncapi config context init [CONTEXT-FILE-PATH]`

Where `[CONTEXT-FILE-PATH]` instructs CLI what directory should the context file be created in:
- current directory: `asyncapi config context init . [CONTEXT-NAME] [SPEC-FILE-PATH]`
- root of current repository: `asyncapi config context init ./ [CONTEXT-NAME] [SPEC-FILE-PATH]`
- user's home directory: `asyncapi config context init ~ [CONTEXT-NAME] [SPEC-FILE-PATH]`
- current directory: `asyncapi config context init . (default)`
- root of current repository: `asyncapi config context init ./`
- user's home directory: `asyncapi config context init ~`

The only mandatory switch is `[CONTEXT-FILE-PATH]`, additional switches can be omitted. In case of `asyncapi config context init .|./|~` the [minimal empty context file](#minimalEmptyContextFile) will be created in predefined location.
If `[CONTEXT-FILE-PATH]` is omitted, the context file is created in current directory.

### Context File structure

Expand Down
14 changes: 1 addition & 13 deletions src/commands/config/context/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os from 'os';
import { Flags } from '@oclif/core';
import Command from '../../../base';
import { initContext } from '../../../models/Context';
Expand All @@ -10,20 +9,14 @@ export default class ContextInit extends Command {
};

static contextFilePathMessage = `Specify directory in which context file should be created:
- current directory : asyncapi config context init .
- current directory : asyncapi config context init . (default)
- root of current repository : asyncapi config context init ./
- user's home directory : asyncapi config context init ~`;

static args = [
{
name: 'context-file-path',
description: `${ContextInit.contextFilePathMessage}`,
required: true,
},
{ name: 'context-name', description: 'Context name', required: false },
{
name: 'spec-file-path',
description: 'Filesystem path to the target AsyncAPI document',
required: false,
},
];
Expand All @@ -34,11 +27,6 @@ export default class ContextInit extends Command {
const contextName = args['context-name'];
const specFilePath = args['spec-file-path'];

if (!['.', './', os.homedir()].includes(contextFilePath)) {
this.log(`${ContextInit.contextFilePathMessage}`);
return;
}

const contextWritePath = await initContext(
contextFilePath,
contextName,
Expand Down
10 changes: 5 additions & 5 deletions src/models/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import {

const { readFile, writeFile } = fs;

export const EMPTY_CONTEXT_FILE = {
store: {},
};

const DEFAULT_CONTEXT_FILENAME = '.asyncapi-cli';
const DEFAULT_CONTEXT_FILE_LOCATION = os.homedir();
export const DEFAULT_CONTEXT_FILE_PATH = path.resolve(
Expand Down Expand Up @@ -78,9 +74,12 @@ export async function initContext(
contextName: string,
specFilePath: string
) {
let fileContent: IContextFile = EMPTY_CONTEXT_FILE;
let fileContent: IContextFile = {
store: {},
};
let contextWritePath = '';

// prettier-ignore
switch (contextFilePath) {
/* eslint-disable indent */
case '.':
Expand All @@ -96,6 +95,7 @@ export async function initContext(
contextWritePath = os.homedir() + path.sep + CONTEXT_FILENAME;
break;
default:
contextWritePath = process.cwd() + path.sep + CONTEXT_FILENAME;
}

if (contextName && specFilePath) {
Expand Down

0 comments on commit 74f0199

Please sign in to comment.