All files / core/blong-gogo/src loadServer.ts

97.36% Statements 74/76
42.3% Branches 11/26
66.66% Functions 4/6
97.36% Lines 74/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 382x 903x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 125x 125x 30x 30x 30x 30x 30x 30x 6x 6x 6x 6x 6x 6x 6x 6x 6x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x  
import type {IPlatformApi} from '@feasibleone/blong';
import {watch} from 'chokidar';
import type {Dirent} from 'fs';
import {existsSync, mkdirSync, readFileSync, statSync, writeFileSync} from 'fs';
import {readdir} from 'fs/promises';
import minimist from 'minimist';
import {createRequire} from 'node:module';
import os from 'node:os';
import {hrtime} from 'node:process';
import {basename, dirname, extname, join, relative, resolve} from 'path';
import cbc from 'ut-function.cbc';
import merge from 'ut-function.merge';
import ConfigRuntime from './ConfigRuntime.ts';
import './globals.d.ts';
import load from './load.ts';
import timing from './timing.ts';
 
const scan = async (...path: string[]): Promise<Dirent[]> =>
    (await readdir(join(...path), {withFileTypes: true})).sort((a, b) =>
        a < b ? -1 : a > b ? 1 : 0,
    );
 
const context = {
    platform: 'server',
    suite: 'blong',
    user: process.env.USER || os.userInfo().username || 'unknown',
    host: process.env.HOSTNAME || os.hostname() || 'unknown',
    ...(process.env.BLONG_MASTER_KEY && cbc(process.env.BLONG_MASTER_KEY)),
};
 
const loadConfig: IPlatformApi['loadConfig'] = async (baseConfig, parentConfig, loadedConfigs) => {
    // ConfigRuntime is created only at the root call (when no api is provided)
    // and only when parentConfig is a string (suite name) so blong-config can
    // load external files that may change at runtime.
    if (typeof parentConfig === 'string') {
        context.suite = parentConfig;
        const configRuntime = new ConfigRuntime(baseConfig, loadedConfigs, parentConfig);
        return {
            loadedConfig: await configRuntime.load(),
            configRuntime,
        } as Awaited<ReturnType<IPlatformApi['loadConfig']>>;
    } else {
        return {
            loadedConfig: merge({}, baseConfig, ...loadedConfigs, parentConfig),
        } as Awaited<ReturnType<IPlatformApi['loadConfig']>>;
    }
};
 
// Parse CLI intents: first positional arg may be a file/folder target — exclude it from intents.
const allPositional = minimist(process.argv.slice(2))._ as string[];
const [maybeTarget, ...rest] = allPositional;
const targetIsFile = Boolean(maybeTarget && existsSync(resolve(maybeTarget)));
const cliIntents = targetIsFile ? rest : allPositional;
 
export default load.bind(null, {
    platform: 'server',
    readdir: async (path: string) => readdir(path, {withFileTypes: true}),
    existsSync,
    createRequire,
    scan,
    join,
    basename,
    extname,
    dirname,
    loadConfig,
    resolve,
    relative,
    readFileSync,
    writeFileSync,
    mkdirSync,
    statSync,
    watch,
    timing: timing(hrtime),
    configs: ['server', ...cliIntents],
    context,
});