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

90.52% Statements 86/95
41.66% Branches 10/24
50% Functions 4/8
90.52% Lines 86/95

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1383x 4161x 56x 56x 56x 56x 56x 1x 1x 1x 1x 1x 1x 304x 304x 34x 34x 34x 34x 34x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x     1x 1x 1x 1x  
import type {IPlatformApi} from '@feasibleone/blong';
import * as lukaotVocabulary from '@feasibleone/lukaot';
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 {attachVocabulary} from './responseContext.ts';
import timing from './timing.ts';
 
// The emitter's ambient context is `AsyncLocalStorage`-based, so the shared realm
// machinery reaches it by attachment rather than by import. Only the server has a
// vocabulary; in the browser every helper degrades to "no identity".
attachVocabulary(lukaotVocabulary);
 
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;
 
/**
 * `IPlatformApi.mkdirSync` — create a directory, parents included.
 *
 * Recursive and idempotent on purpose: callers use it to make sure a file's
 * parent exists before writing (kukum's `apply`), so a directory that is
 * already there must be a no-op rather than an `EEXIST`. Node's bare
 * `mkdirSync` throws on both an existing directory and a missing parent.
 */
const mkdirRecursiveSync = (path: string): void => {
    mkdirSync(path, {recursive: true});
};
 
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: mkdirRecursiveSync,
    statSync,
    watch,
    timing: timing(hrtime),
    configs: ['server', ...cliIntents],
    context,
});