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 Jun 27, 2023
1 parent 2cd988f commit ad39d0f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
7 changes: 1 addition & 6 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<<<<<<< HEAD
sonar.exclusions=test/**/*
=======
sonar.sources = src/
sonar.tests = test/
>>>>>>> 639d864 (feat: support context file location in repository)
sonar.exclusions=test/**/*
1 change: 0 additions & 1 deletion sonar-project.properties

This file was deleted.

10 changes: 9 additions & 1 deletion src/errors/context-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const NO_CONTEXTS_SAVED = `These are your options to specify in the CLI w
`;
const CONTEXT_WRONG_FORMAT = (contextFileName: string) => `Context file "${contextFileName}" has wrong format.`;
const CONTEXT_ALREADY_EXISTS = (contextName: string, contextFileName: string) => `Context with name "${contextName}" already exists in context file "${contextFileName}".`;
const CONTEXT_WRITE_ERROR = (contextFileName: string) => `Error writing context file "${contextFileName}".`;

class ContextError extends Error {
constructor() {
Expand Down Expand Up @@ -38,7 +39,7 @@ export class MissingCurrentContextError extends ContextError {
}
}

export class ContextNotFound extends ContextError {
export class ContextNotFoundError extends ContextError {
constructor(contextName: string) {
super();
this.message = CONTEXT_NOT_FOUND(contextName);
Expand All @@ -51,3 +52,10 @@ export class ContextAlreadyExistsError extends ContextError {
this.message = CONTEXT_ALREADY_EXISTS(contextName, contextFileName);
}
}

export class ContextFileWriteError extends ContextError {
constructor(contextFileName: string) {
super();
this.message = CONTEXT_WRITE_ERROR(contextFileName);
}
}
20 changes: 10 additions & 10 deletions src/models/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as os from 'os';
import * as repoRoot from 'app-root-path';

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

const { readFile, writeFile } = fs;
Expand All @@ -17,8 +18,8 @@ const DEFAULT_CONTEXT_FILENAME = '.asyncapi-cli';
const DEFAULT_CONTEXT_FILE_LOCATION = os.homedir();
export 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_FILENAME = process.env.CUSTOM_CONTEXT_FILENAME || DEFAULT_CONTEXT_FILENAME;
const CONTEXT_FILE_LOCATION = process.env.CUSTOM_CONTEXT_FILE_LOCATION || DEFAULT_CONTEXT_FILE_LOCATION;

// Usage of promises for assignment of their resolved values to constants is
// known to be troublesome:
Expand Down Expand Up @@ -63,11 +64,11 @@ export async function loadContext(contextName?: string): Promise<string> {
const fileContent: IContextFile = await loadContextFile();
if (contextName) {
const context = fileContent.store[String(contextName)];
if (!context) {throw new ContextNotFound(contextName);}
if (!context) {throw new ContextNotFoundError(contextName);}
return context;
} else if (fileContent.current) {
const context = fileContent.store[fileContent.current];
if (!context) {throw new ContextNotFound(fileContent.current);}
if (!context) {throw new ContextNotFoundError(fileContent.current);}
return context;
}

Expand Down Expand Up @@ -103,7 +104,7 @@ export async function addContext(contextName: string, pathToFile: string) {
export async function removeContext(contextName: string) {
const fileContent: IContextFile = await loadContextFile();
if (!fileContent.store[String(contextName)]) {
throw new ContextNotFound(contextName);
throw new ContextNotFoundError(contextName);
}
if (fileContent.current === contextName) {
delete fileContent.current;
Expand All @@ -124,7 +125,7 @@ export async function getCurrentContext(): Promise<ICurrentContext> {
export async function setCurrentContext(contextName: string) {
const fileContent: IContextFile = await loadContextFile();
if (!fileContent.store[String(contextName)]) {
throw new ContextNotFound(contextName);
throw new ContextNotFoundError(contextName);
}
fileContent.current = contextName;
await saveContextFile(fileContent);
Expand Down Expand Up @@ -162,9 +163,8 @@ async function saveContextFile(fileContent: IContextFile) {
current: fileContent.current,
store: fileContent.store
}), { encoding: 'utf8' });
return fileContent;
} catch (e) {
return;
throw new ContextFileWriteError(CONTEXT_FILE_PATH);
}
}

Expand Down Expand Up @@ -203,7 +203,7 @@ async function getContextFilePath(): Promise<string | null> {
return null;
}

export async function isContextFileValid(
async function isContextFileValid(
fileContent: IContextFile
): Promise<boolean> {
// Validation of context file's format against interface `IContextFile`.
Expand Down
5 changes: 4 additions & 1 deletion test/commands/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable sonarjs/no-duplicate-string */
import path from 'path';
import { test } from '@oclif/test';

Expand Down Expand Up @@ -91,6 +90,10 @@ describe('config:context, correct format', () => {
});
});

// On direct execution of `chmodSync(CONTEXT_FILE_PATH, '444')` context file's
// permissions get changed to 'read only' in file system, but `@oclif/test`'s
// test still passes. Thus there is no sense in implementation of
// `writeFile()` faulty scenario with `@oclif/test` framework.
describe('config:context:remove', () => {
test
.stderr()
Expand Down
2 changes: 1 addition & 1 deletion test/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class ContextTestingHelper {
writeFileSync(CONTEXT_FILE_PATH, JSON.stringify(this._context), { encoding: 'utf8' });
}

createDummyContextFileWrong(data): void {
createDummyContextFileWrong(data: string): void {
writeFileSync(CONTEXT_FILE_PATH, data, { encoding: 'utf8' });
}

Expand Down

0 comments on commit ad39d0f

Please sign in to comment.