ume.ready

Note

You need version 1.3.0 or newer.

umejs, since v1.3.0, no longer blocks the main thread. This means that if any of your code relied on firing after umejs is done loading, that will now be executed immediately. To make sure your code is excuted after umejs is ready, use ume.ready. This can be integrated both with await as well as with .then().

ume.ready will be an array of the file names that have been built.

Examples for both:

// using await
// since top-level await is only supported in esm, that's what we're using

// --- Import everything ---
import { default as ume } from "@dantenl/umejs"
import { fileURLToPath } from 'url';
import express from 'express';
import path from 'path';

const app = express();
const PORT = 9418

// --- Define __dirname ---
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// --- Create the ume middleware ---
const umeConfig = ume({
    contentDir: path.join(__dirname, 'docs'),
    templatePath: path.join(__dirname, 'template', 'layout-v1.ume.html'),
})

// --- Initialise documentation ---
app.use('/docs/{*slug}', umeConfig);

// ----- Start the server -----
const server = app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

// --- Wait for umejs to be ready ---
const markdownFiles = await umeConfig.ready
console.log(`umejs is ready! ${markdownFiles.length} files have been built!`)

// --- Properly close the server ---
process.on('SIGINT', async () => {
    server.close();
    await umeConfig.close();
    process.exit(0);
});
// using .then()

// --- Import everything ---
const ume = require('@dantenl/umejs');
const express = require('express');
const path = require('path');

const app = express();
const PORT = 9418


// --- Create the ume middleware ---
const umeConfig = ume({
    parallel: true,

    contentDir: path.join(__dirname, 'docs'),
    templatePath: path.join(__dirname, 'template', 'layout-v1.ume.html')
});

// --- Initialise documentation ---
app.use('/docs/{*slug}', umeConfig);

// --- Start the server ---
const server = app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

// --- Wait for umejs to be ready ---
umeConfig.ready.then(markdownFiles => {
    console.log(`umejs is ready! ${markdownFiles.length} files have been built!`)
})

// --- Close the file watcher ---
process.on('SIGINT', async () => {
    server.close();
    await umeConfig.close();
    process.exit(0);
});

If we take a look at the logs of either of these files (they're identical), we can clearly see it working! As expected, the server is ready before umejs is, but our little console.log() is always executed last!

[🌸 umejs] Initialising project at /Users/dante_nl/Projects/ume-docs/v1
Server running at http://localhost:9418
[🌸 umejs] 23 Markdown file(s) in /Users/dante_nl/Projects/ume-docs/v1 have been generated.
[🌸 umejs] All files built - took 22ms
umejs is ready! 23 files have been built!

On a side note, if you want to recreate the blocking behaviour from before v1.3.0; that is also possible. Simply await ume.ready before starting the server, or put app.listen() inside of the ume.ready.then().