| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { dirname } from 'node:path';
- import fs from 'node:fs';
- import type { PathLike } from 'node:fs';
- import fsp from 'node:fs/promises';
- import { appendArrayInPlace } from 'foxts/append-array-in-place';
- export type MaybePromise<T> = T | Promise<T>;
- interface Write {
- (
- destination: string,
- input: NodeJS.TypedArray | string,
- ): Promise<void>
- }
- export type VoidOrVoidArray = void | VoidOrVoidArray[];
- export function mkdirp(dir: string) {
- if (fs.existsSync(dir)) {
- return;
- }
- return fsp.mkdir(dir, { recursive: true });
- }
- export const writeFile: Write = async (destination: string, input, dir = dirname(destination)): Promise<void> => {
- const p = mkdirp(dir);
- if (p) {
- await p;
- }
- return fsp.writeFile(destination, input, { encoding: 'utf-8' });
- };
- export function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
- const result: string[] = [
- '#########################################',
- `# ${title}`,
- `# Last Updated: ${date.toISOString()}`,
- `# Size: ${content.length}`
- ];
- appendArrayInPlace(result, description.map(line => (line ? `# ${line}` : '#')));
- result.push('#########################################');
- appendArrayInPlace(result, content);
- result.push('################## EOF ##################', '');
- return result;
- };
- export function notSupported(name: string) {
- return (...args: unknown[]) => {
- console.error(`${name}: not supported.`, args);
- throw new Error(`${name}: not implemented.`);
- };
- }
- export function withIdentityContent(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
- return content;
- };
- export function isDirectoryEmptySync(path: PathLike) {
- const directoryHandle = fs.opendirSync(path);
- try {
- return directoryHandle.readSync() === null;
- } finally {
- directoryHandle.closeSync();
- }
- }
|