SBG Utility
SBG Utility is a collection of helper functions and modules primarily designed to support the `static-blog-generator` ecosystem. It provides functionalities such as file management (reading, writing, checking file stats), date mapping, and logging utilities. The current stable version is `2.0.11`. As a sub-package within a larger monorepo, its release cadence is tied to the main project, though specific updates for `sbg-utility` are less frequent. Key differentiators include its tight integration with the `static-blog-generator`'s internal workings and its CommonJS base with TypeScript type definitions, making it usable in both legacy and modern Node.js environments.
Common errors
-
TypeError: (0, _sbg_utility.someFunction) is not a function
cause Attempting to use a named export as a default import or incorrect destructuring in a CommonJS context.fixEnsure you are using correct named imports for ESM (`import { someFunction } from 'sbg-utility';`) or proper destructuring for CommonJS (`const { someFunction } = require('sbg-utility');`). This package does not have a default export. -
ERR_REQUIRE_ESM: Must use import to load ES Module: ...
cause This error is unlikely for `sbg-utility` itself (as it's CJS), but if you're trying to `require()` an ES module that *depends* on `sbg-utility` in a mixed environment, or incorrectly configure your build, it might surface.fixEnsure that the module causing the error is loaded via `import` if it's an ES module. For `sbg-utility`, stick to `import` in TypeScript/ESM contexts or `require` in CJS contexts, respecting its underlying module type.
Warnings
- gotcha While the package provides TypeScript types and supports ESM `import` syntax, it is fundamentally a CommonJS module. In environments where strict ESM loading is enforced (e.g., Node.js with `type: module` and no bundler), you might encounter issues. Prefer standard `require()` in pure CJS contexts or ensure your build system handles CJS interop for ESM imports.
- gotcha This `sbg-utility` package is part of a larger `static-blog-generator` monorepo. While usable standalone, its API and design choices are often influenced by its parent project's needs. Expect some functionalities to be tailored specifically for the static blog generator workflow.
Install
-
npm install sbg-utility -
yarn add sbg-utility -
pnpm add sbg-utility
Imports
- debug
const debug = require('sbg-utility').log.debug;import { debug } from 'sbg-utility'; - read, write
const { read, write } = require('sbg-utility').filemanager;import { read, write } from 'sbg-utility'; - ReadOptions (Type)
import { ReadOptions } from 'sbg-utility';import type { ReadOptions } from 'sbg-utility';
Quickstart
import { read, write, debug } from 'sbg-utility';
import { join } from 'path';
import { existsSync, mkdirSync } from 'fs';
const outputDir = join(__dirname, 'temp_output');
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
const filePath = join(__dirname, 'example.txt');
const outputPath = join(outputDir, 'output.txt');
async function runUtilityExample() {
debug('Starting SBG Utility example...');
try {
// Create a dummy file for reading
await write(filePath, 'Hello from sbg-utility!', 'utf8');
debug(`Written to ${filePath}`);
const content = await read(filePath, { encoding: 'utf8' });
debug(`Read content: ${content}`);
await write(outputPath, `Processed: ${content.toUpperCase()}`, 'utf8');
debug(`Transformed and written to ${outputPath}`);
} catch (error) {
console.error('An error occurred:', error);
debug('Example failed.', 'error');
}
debug('SBG Utility example finished.');
}
runUtilityExample();