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 May 26, 2023
1 parent ee0a68a commit 674692b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@oclif/plugin-not-found": "^2.3.22",
"@stoplight/spectral-cli": "6.6.0",
"ajv": "^8.12.0",
"app-root-path": "^3.1.0",
"chalk": "^4.1.0",
"chokidar": "^3.5.2",
"fs-extra": "^11.1.0",
Expand Down Expand Up @@ -174,7 +175,7 @@
"release": "semantic-release",
"pretest": "npm run build",
"test": "npm run test:unit",
"test:unit": "cross-env NODE_ENV=development TEST=1 CONTEXT_FILENAME=\"./test.asyncapi\" CONTEXT_FILE_PATH=\"./\" jest --coverage -i",
"test:unit": "cross-env NODE_ENV=development TEST=1 CUSTOM_CONTEXT_FILENAME=\"./test.asyncapi\" CUSTOM_CONTEXT_FILE_LOCATION=\"./\" jest --coverage -i",
"get-version": "echo $npm_package_version"
},
"types": "lib/index.d.ts"
Expand Down
8 changes: 8 additions & 0 deletions src/errors/context-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const NO_CONTEXTS_SAVED = `These are your options to specify in the CLI w
- In case you did not specify a context that you want to use, the CLI checks if there is a default context and uses it. To set default context run: asyncapi config context use mycontext
- In case you did not provide any reference to AsyncAPI file and there is no default context, the CLI detects if in your current working directory you have files like asyncapi.json, asyncapi.yaml, asyncapi.yml. Just rename your file accordingly.
`;
const CONTEXT_WRONG_FORMAT = 'Context file has wrong format';

class ContextError extends Error {
constructor() {
Expand Down Expand Up @@ -35,3 +36,10 @@ export class ContextNotFound extends ContextError {
this.message = CONTEXT_NOT_FOUND(contextName);
}
}

export class ContextWrongFormat extends ContextError {
constructor() {
super();
this.message = CONTEXT_WRONG_FORMAT;
}
}
36 changes: 32 additions & 4 deletions src/models/Context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { promises as fs } from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as repoRoot from 'app-root-path';

import { ContextNotFound, MissingContextFileError, MissingCurrentContextError } from '../errors/context-error';

const { readFile, writeFile } = fs;
const CONTEXT_FILENAME = process.env.CONTEXT_FILENAME || '.asyncapi';
export const DEFAULT_CONTEXT_FILE_PATH = path.resolve(process.env.CONTEXT_FILE_PATH || os.homedir(), CONTEXT_FILENAME);

const DEFAULT_CONTEXT_FILENAME = '.asyncapi';
const DEFAULT_CONTEXT_FILE_LOCATION = os.homedir();
const DEFAULT_CONTEXT_FILE_PATH = path.resolve(DEFAULT_CONTEXT_FILE_LOCATION, DEFAULT_CONTEXT_FILENAME);

const CONTEXT_FILENAME = process.env.CUSTOM_CONTEXT_FILENAME || DEFAULT_CONTEXT_FILENAME;
const CONTEXT_FILE_LOCATION = process.env.CUSTOM_CONTEXT_FILE_LOCATION || DEFAULT_CONTEXT_FILE_LOCATION;
const CONTEXT_FILE_PATH = path.resolve(CONTEXT_FILE_LOCATION, CONTEXT_FILENAME);

export interface IContextFile {
current?: string,
Expand Down Expand Up @@ -87,15 +94,15 @@ export async function setCurrentContext(contextName: string) {

export async function loadContextFile(): Promise<IContextFile> {
try {
return JSON.parse(await readFile(DEFAULT_CONTEXT_FILE_PATH, { encoding: 'utf8' })) as IContextFile;
return JSON.parse(await readFile(await getContextFilePath(), { encoding: 'utf8' })) as IContextFile;
} catch (e) {
throw new MissingContextFileError();
}
}

async function saveContextFile(fileContent: IContextFile) {
try {
writeFile(DEFAULT_CONTEXT_FILE_PATH, JSON.stringify({
writeFile(CONTEXT_FILE_PATH, JSON.stringify({
current: fileContent.current,
store: fileContent.store
}), { encoding: 'utf8' });
Expand All @@ -104,3 +111,24 @@ async function saveContextFile(fileContent: IContextFile) {
return;
}
}

async function getContextFilePath(): Promise<string> {
const currentPath = process.cwd().slice(repoRoot.path.length + 1).split(path.sep);
currentPath.unshift(repoRoot.path);

for (let i = currentPath.length; i >= 0; i--) {
const currentPathString = currentPath[0]
? currentPath.join(path.sep) + path.sep + CONTEXT_FILENAME
: os.homedir() + path.sep + CONTEXT_FILENAME;

try {
if (JSON.parse(await readFile(currentPathString + path.sep + CONTEXT_FILENAME, { encoding: 'utf8' })) as IContextFile) {
return currentPathString;
}
} catch (e) {}

currentPath.pop();
}

return '';
}

0 comments on commit 674692b

Please sign in to comment.