Skip to content

Commit

Permalink
Merge pull request #45 from konecty/feat/process-env
Browse files Browse the repository at this point in the history
feat: verify environment for avoid browser error
  • Loading branch information
7sete7 authored Feb 23, 2024
2 parents eebe8bd + bf675bb commit a2d71f0
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"@types/js-cookie": "^3.0.2",
"@types/lodash": "^4.14.182",
"@types/mkdirp": "^1.0.2",
"@types/node": "^17.0.27",
"@types/node": "^20.11.20",
"@types/numeral": "^2.0.2",
"@types/prettier": "^2.6.1",
"@types/qs": "^6.9.7",
Expand Down
2 changes: 0 additions & 2 deletions src/__test__/cli/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ describe('Konecty command line tool login command', () => {

const packageJsonContent = `{ "version": "1.0.0" }`;

process.env.HOME = '/dev/null';

mockedFs.readFileSync.mockReturnValueOnce(packageJsonContent).mockReturnValueOnce(``).mockReturnValueOnce(``);

mockedFs.statSync.mockReturnValueOnce({ isFile: () => true } as Stats);
Expand Down
3 changes: 2 additions & 1 deletion src/cli/docCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import { createDocFromMetadata } from '../lib/createDocFromMetadata';
import { getEnvVariable } from '../lib/getEnv';

export type CreateDocOptions = {
input: string;
output: string;
};

export default function ({ input, output }: CreateDocOptions): void {
const __dirname = path.resolve(process.env.INIT_CWD ?? './');
const __dirname = path.resolve(getEnvVariable('INIT_CWD') ?? './');

const inputFile = path.resolve(__dirname, input);

Expand Down
3 changes: 2 additions & 1 deletion src/cli/exportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import get from 'lodash/get';
import mkdirp from 'mkdirp';
import path from 'path';
import createClientFromCredentialFile from '../lib/createClientFromCredentialFile';
import { getEnvVariable } from '../lib/getEnv';
import { KonectyClient } from '../sdk/Client';

export interface ExportCommandOptions {
Expand All @@ -17,7 +18,7 @@ export interface ExportCommandOptions {
export default async function exportCommand(document?: string, options?: ExportCommandOptions): Promise<void> {
const localOptions = Object.assign({}, options ?? {});

const outputDir = path.resolve(process.env.INIT_CWD ?? './');
const outputDir = path.resolve(getEnvVariable('INIT_CWD') ?? './');

let client = await createClientFromCredentialFile({
credentialsFile: localOptions.credentialsFile,
Expand Down
5 changes: 3 additions & 2 deletions src/cli/loginCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ini from 'ini';
import inquirer from 'inquirer';
import mkdirp from 'mkdirp';
import path from 'path';
import { getEnvVariable } from '../lib/getEnv';
import getHomeDir from '../lib/getHomeDir';
import { KonectyClient } from '../sdk/Client';

Expand Down Expand Up @@ -63,7 +64,7 @@ export default async function loginCommand(options?: LoginCommandOptions): Promi
}

const __dirname =
localOptions?.output != null ? path.resolve(process.env.INIT_CWD ?? './') : path.resolve(getHomeDir() ?? '', '.konecty');
localOptions?.output != null ? path.resolve(getEnvVariable('INIT_CWD') ?? './') : path.resolve(getHomeDir() ?? '', '.konecty');

if (__dirname == null) {
console.error(chalk.red('Unable to get current or home directory'));
Expand All @@ -78,7 +79,7 @@ export default async function loginCommand(options?: LoginCommandOptions): Promi
try {
fs.statSync(outputFile);
originalFile = fs.readFileSync(outputFile, 'utf-8');
} catch (_) {}
} catch (_) { }

const outputIni = ini.parse(originalFile);

Expand Down
3 changes: 2 additions & 1 deletion src/cli/typeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import { createTypeFromMetadata } from '../lib/createTypeFromMetadata';
import { getEnvVariable } from '../lib/getEnv';

export type CreateInterfaceOptions = {
input: string;
output: string;
};

export default function ({ input, output }: CreateInterfaceOptions): void {
const __dirname = path.resolve(process.env.INIT_CWD ?? './');
const __dirname = path.resolve(getEnvVariable('INIT_CWD') ?? './');
const inputFile = path.resolve(__dirname, input);

try {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/createClientFromCredentialFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import get from 'lodash/get';
import path from 'path';

import { KonectyClient, KonectyClientOptions } from '../sdk/Client';
import { getEnvVariable } from './getEnv';
import getHomeDir from './getHomeDir';

function createClientFromCredentialFile(options?: KonectyClientOptions): KonectyClient {
try {
const __dirname = path.resolve(process.env.INIT_CWD ?? './');
const __dirname = path.resolve(getEnvVariable('INIT_CWD') ?? './');
const credentialsFile = options?.credentialsFile ?? path.resolve(getHomeDir() ?? '', '.konecty', 'credentials');

const resolvedFilePath = /^\~/.test(credentialsFile)
Expand Down
7 changes: 7 additions & 0 deletions src/lib/getEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).process = (global as any).process ?? { env: {} };

export function getEnvVariable(name: string): string | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (global as any).process.env[name];
}
7 changes: 4 additions & 3 deletions src/lib/getHomeDir.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os from 'os';
import { getEnvVariable } from './getEnv';

export default function getHomeDir(): string | null {
const home =
process.env.HOME ||
process.env.USERPROFILE ||
(process.env.HOMEPATH ? (process.env.HOMEDRIVE || 'C:/') + process.env.HOMEPATH : null);
getEnvVariable('HOME') ||
getEnvVariable('USERPROFILE') ||
(getEnvVariable('HOMEPATH') ? (getEnvVariable('HOMEDRIVE') || 'C:/') + getEnvVariable('HOMEPATH') : null);

if (home != null) {
return home;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pino from 'pino';
import { getEnvVariable } from './getEnv';

const logger = pino({
level: process.env.LOG_LEVEL || 'info',
level: getEnvVariable('LOG_LEVEL') ?? 'info',
});

export default logger;
12 changes: 6 additions & 6 deletions src/sdk/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class KonectyClient {

async getMenu(menu = 'main'): Promise<KonectyFindResult<Menu>> {
try {
const result = await fetch<Menu[]>(`${this.#options.endpoint}/api/menu/${menu}`, {
const result = await fetch(`${this.#options.endpoint}/api/menu/${menu}`, {
method: 'GET',
headers: {
Authorization: `${this.#options.accessKey}`,
Expand All @@ -318,7 +318,7 @@ export class KonectyClient {

async getListView(module: string, id = 'Default'): Promise<KonectyGetMetaResult<List>> {
try {
const result = await fetch<List>(`${this.#options.endpoint}/api/list-view/${module}/${id}`, {
const result = await fetch(`${this.#options.endpoint}/api/list-view/${module}/${id}`, {
method: 'GET',
headers: {
Authorization: `${this.#options.accessKey}`,
Expand Down Expand Up @@ -347,7 +347,7 @@ export class KonectyClient {

async getDocumentNew(name: string): Promise<KonectyGetMetaResult<DocumentTranslation>> {
try {
const result = await fetch<List[]>(`${this.#options.endpoint}/api/document/${name}`, {
const result = await fetch(`${this.#options.endpoint}/api/document/${name}`, {
method: 'GET',
headers: {
Authorization: `${this.#options.accessKey}`,
Expand Down Expand Up @@ -376,7 +376,7 @@ export class KonectyClient {

async getForm(module: string, id = 'Default'): Promise<KonectyGetMetaResult<any>> {
try {
const result = await fetch<List[]>(`${this.#options.endpoint}/api/form/${module}/${id}`, {
const result = await fetch(`${this.#options.endpoint}/api/form/${module}/${id}`, {
method: 'GET',
headers: {
Authorization: `${this.#options.accessKey}`,
Expand Down Expand Up @@ -405,7 +405,7 @@ export class KonectyClient {

async getMetasByDocument(document: string): Promise<KonectyGetMetaResult<any[]>> {
try {
const result = await fetch<List[]>(`${this.#options.endpoint}/api/metas/${document}`, {
const result = await fetch(`${this.#options.endpoint}/api/metas/${document}`, {
method: 'GET',
headers: {
Authorization: `${this.#options.accessKey}`,
Expand Down Expand Up @@ -645,7 +645,7 @@ function deserializeDates(obj: unknown): unknown {
if (new Date(obj).toISOString() == obj) {
return DateTime.fromISO(obj).toJSDate();
}
} catch (e) {}
} catch (e) { }
}

if (isArray(obj)) {
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2257,11 +2257,18 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/node@*", "@types/node@>=12", "@types/node@^17.0.27":
"@types/node@*", "@types/node@>=12":
version "17.0.27"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.27.tgz#f4df3981ae8268c066e8f49995639f855469081e"
integrity sha512-4/Ke7bbWOasuT3kceBZFGakP1dYN2XFd8v2l9bqF2LNWrmeU07JLpp56aEeG6+Q3olqO5TvXpW0yaiYnZJ5CXg==

"@types/node@^20.11.20":
version "20.11.20"
resolved "https://registry.npmjs.org/@types/node/-/node-20.11.20.tgz#f0a2aee575215149a62784210ad88b3a34843659"
integrity sha512-7/rR21OS+fq8IyHTgtLkDK949uzsa6n8BkziAKtPVpugIkO6D+/ooXMvzXxDnZrmtXVfjb1bKQafYpb8s89LOg==
dependencies:
undici-types "~5.26.4"

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
Expand Down Expand Up @@ -8449,6 +8456,11 @@ undefsafe@^2.0.5:
resolved "https://registry.yarnpkg.com/undefsafe/-/undefsafe-2.0.5.tgz#38733b9327bdcd226db889fb723a6efd162e6e2c"
integrity sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

unicode-canonical-property-names-ecmascript@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz#301acdc525631670d39f6146e0e77ff6bbdebddc"
Expand Down

0 comments on commit a2d71f0

Please sign in to comment.