Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { promises as fs } from 'fs';
import { parse, stringify } from 'ini';
import { join } from 'path';
import * as z from 'zod';
import args from './cli/args';
import { configDir, ensureFolderExists, filter, loadFile } from './utils';

export const Config = z.object({
Expand All @@ -28,11 +29,22 @@ const defaultConfig: Config = {
renewTime: 1000 * 60 * 60 * 24 * 15
};

export const defaultPath = configDir(join('metacall', 'deploy'));
export const defaultPath = (): string => configDir(join('metacall', 'deploy'));

export const configFilePath = (path = defaultPath) => join(path, 'config.ini');
let configDirFound: string | undefined;

export const load = async (path = defaultPath): Promise<Config> => {
export const setConfigDir = (path: string) => {
configDirFound = path;
};

export const getConfigDir = () =>
configDirFound || args['confDir'] || defaultPath();

export const configFilePath = (path = getConfigDir()) =>
join(path, 'config.ini');

export const load = async (path = getConfigDir()): Promise<Config> => {
setConfigDir(path);
const data = parse(
await loadFile(configFilePath(await ensureFolderExists(path)))
);
Expand All @@ -45,7 +57,7 @@ export const load = async (path = defaultPath): Promise<Config> => {

export const save = async (
data: Partial<Config>,
path = defaultPath
path = getConfigDir()
): Promise<void> =>
fs.writeFile(
configFilePath(await ensureFolderExists(path)),
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ void (async () => {

if (args['logout']) return logout();

const config = await startup(args['confDir']);

if (args['serverUrl']) {
config.baseURL = args['serverUrl'];
}
const config = await startup();
const api: APIInterface = API(
config.token as string,
args['dev'] ? config.devURL : config.baseURL
Expand Down Expand Up @@ -177,4 +173,8 @@ void (async () => {
error(String(e));
}
}

if (args['serverUrl']) {
config.baseURL = args['serverUrl'];
}
})();
3 changes: 1 addition & 2 deletions src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from '@metacall/protocol/deployment';
import { RunnerToDisplayName } from '@metacall/protocol/language';
import API, { isProtocolError } from '@metacall/protocol/protocol';
import args from './cli/args';
import { error, info } from './cli/messages';
import { listSelection } from './cli/selection';
import { startup } from './startup';
Expand All @@ -17,7 +16,7 @@ const showLogs = async (
type: LogType,
dev: boolean
): Promise<void> => {
const config = await startup(args['confDir']);
const config = await startup();
const api = API(
config.token as string,
dev ? config.devURL : config.baseURL
Expand Down
6 changes: 3 additions & 3 deletions src/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import { auth } from './auth';
import args from './cli/args';
import { Config, defaultPath, load } from './config';
import { Config, load } from './config';

const devToken = 'local'; // Use some random token in order to proceed

export const startup = async (confDir: string | undefined): Promise<Config> => {
const config = await load(confDir || defaultPath);
export const startup = async (): Promise<Config> => {
const config = await load();
const token = args['dev'] ? devToken : await auth(config);

return Object.assign(config, { token });
Expand Down
27 changes: 18 additions & 9 deletions src/test/cli.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
keys,
runCLI
} from './cli';
import { exists } from '../utils';

describe('Integration CLI (Deploy)', function () {
this.timeout(2000000);
Expand Down Expand Up @@ -75,14 +76,10 @@ describe('Integration CLI (Deploy)', function () {
}
});

// TODO: --confDir
// --confDir
it('Should be able to login using --confDir flag', async function () {
const file = await load();
const token = file.token || '';

notStrictEqual(token, '');

// await clearCache();
const token = file.token || 'dummy_token';

const confDir = await createTmpDirectory();
const configPath = join(confDir, 'config.ini');
Expand All @@ -96,13 +93,25 @@ describe('Integration CLI (Deploy)', function () {
[keys.enter, keys.enter]
).promise;
} catch (err) {
strictEqual(
err,
`X The directory you specified (${workdir}) is empty.\n`
ok(
String(err).includes(
`X The directory you specified (${workdir}) is empty.\n`
) || String(err).includes('Token validation failed')
);
}
});

// --confDir logout
it('Should be able to logout using --confDir flag', async function () {
const confDir = await createTmpDirectory();
const configPath = join(confDir, 'config.ini');
await writeFile(configPath, 'token=abc', 'utf8');

await runCLI([`--confDir=${confDir}`, '--logout'], [keys.enter])
.promise;

strictEqual(await exists(configPath), false);
});
// --help
it('Should be able to print help guide using --help flag', async () => {
const result = await runCLI(['--help'], [keys.enter]).promise;
Expand Down
5 changes: 2 additions & 3 deletions src/test/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import fs from 'fs/promises';
import inspector from 'inspector';
import os from 'os';
import { join } from 'path';
import args from '../cli/args';
import { configFilePath } from '../config';
import { startup } from '../startup';
import { exists } from '../utils';
Expand Down Expand Up @@ -129,7 +128,7 @@ export const keys = Object.freeze({
});

export const deployed = async (suffix: string): Promise<boolean> => {
const config = await startup(args['confDir']);
const config = await startup();
const api = API(config.token as string, config.baseURL);

const sleep = (ms: number): Promise<ReturnType<typeof setTimeout>> =>
Expand Down Expand Up @@ -161,7 +160,7 @@ export const deployed = async (suffix: string): Promise<boolean> => {
};

export const deleted = async (suffix: string): Promise<boolean> => {
const config = await startup(args['confDir']);
const config = await startup();
const api = API(config.token as string, config.baseURL);

const sleep = (ms: number): Promise<ReturnType<typeof setTimeout>> =>
Expand Down