refactor(node/utils): partial CJS->ESM conversion (~14/26 files)

Converted: customError, Stream, NodeVersion, checkValidRev, AbsolutePaths
(+__dirname shim), run_cmd, Cleanup, ExportHelper, padDiff, ExportTxt,
LibreOffice, UpdateCheck, ImportHtml.

Still CJS in utils/: Settings, Minify, toolbar, ExportEtherpad,
ImportEtherpad, ExportHtml. Their consumers will surface errors until
they're flipped too. ts-check: 530 -> 526 errors.
This commit is contained in:
SamTV12345 2026-04-26 12:49:22 +02:00
parent a26d7d3b63
commit 5c3739f4df
13 changed files with 67 additions and 62 deletions

View file

@ -21,6 +21,12 @@
import log4js from 'log4js';
import path from 'path';
import _ from 'underscore';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import findRoot from 'find-root';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const absPathLogger = log4js.getLogger('AbsolutePaths');
@ -79,7 +85,6 @@ export const findEtherpadRoot = () => {
return etherpadRoot;
}
const findRoot = require('find-root');
const foundRoot = findRoot(__dirname);
const splitFoundRoot = foundRoot.split(path.sep);

View file

@ -1,13 +1,13 @@
'use strict'
import {AChangeSet} from "../types/PadType";
import {Revision} from "../types/Revision";
import {AChangeSet} from "../types/PadType.js";
import {Revision} from "../types/Revision.js";
import {timesLimit, firstSatisfies} from './promises';
const padManager = require('ep_etherpad-lite/node/db/PadManager');
const db = require('ep_etherpad-lite/node/db/DB');
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
const padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler');
import {timesLimit, firstSatisfies} from './promises.js';
import padManager from 'ep_etherpad-lite/node/db/PadManager.js';
import db from 'ep_etherpad-lite/node/db/DB.js';
import * as Changeset from 'ep_etherpad-lite/static/js/Changeset.js';
import padMessageHandler from 'ep_etherpad-lite/node/handler/PadMessageHandler.js';
import log4js from 'log4js';
const logger = log4js.getLogger('cleanup');

View file

@ -19,16 +19,15 @@
* limitations under the License.
*/
import AttributeMap from '../../static/js/AttributeMap';
import AttributePool from "../../static/js/AttributePool";
import {deserializeOps, splitAttributionLines, subattribution} from '../../static/js/Changeset';
const { checkValidRev } = require('./checkValidRev');
import AttributeMap from '../../static/js/AttributeMap.js';
import AttributePool from "../../static/js/AttributePool.js";
import {deserializeOps, splitAttributionLines, subattribution} from '../../static/js/Changeset.js';
import { checkValidRev } from './checkValidRev.js';
/*
* This method seems unused in core and no plugins depend on it
*/
exports.getPadPlainText = (pad: { getInternalRevisionAText: (arg0: any) => any; atext: any; pool: any; }, revNum: undefined) => {
const _analyzeLine = exports._analyzeLine;
export const getPadPlainText = (pad: { getInternalRevisionAText: (arg0: any) => any; atext: any; pool: any; }, revNum: undefined) => {
const atext = ((revNum !== undefined) ? pad.getInternalRevisionAText(checkValidRev(revNum)) : pad.atext);
const textLines = atext.text.slice(0, -1).split('\n');
const attribLines = splitAttributionLines(atext.attribs, atext.text);
@ -52,7 +51,7 @@ type LineModel = {
[id:string]:string|number|LineModel
}
exports._analyzeLine = (text:string, aline: string, apool: AttributePool) => {
export const _analyzeLine = (text:string, aline: string, apool: AttributePool) => {
const line: LineModel = {};
// identify list
@ -88,5 +87,5 @@ exports._analyzeLine = (text:string, aline: string, apool: AttributePool) => {
};
exports._encodeWhitespace =
export const _encodeWhitespace =
(s:string) => s.replace(/[^\x21-\x7E\s\t\n\r]/gu, (c) => `&#${c.codePointAt(0)};`);

View file

@ -19,15 +19,15 @@
* limitations under the License.
*/
import {AText, PadType} from "../types/PadType";
import {MapType} from "../types/MapType";
import {AText, PadType} from "../types/PadType.js";
import {MapType} from "../types/MapType.js";
import {deserializeOps, splitAttributionLines, subattribution} from '../../static/js/Changeset';
import {StringIterator} from "../../static/js/StringIterator";
import {StringAssembler} from "../../static/js/StringAssembler";
const attributes = require('../../static/js/attributes');
const padManager = require('../db/PadManager');
const _analyzeLine = require('./ExportHelper')._analyzeLine;
import {deserializeOps, splitAttributionLines, subattribution} from '../../static/js/Changeset.js';
import {StringIterator} from "../../static/js/StringIterator.js";
import {StringAssembler} from "../../static/js/StringAssembler.js";
import * as attributes from '../../static/js/attributes.js';
import padManager from '../db/PadManager.js';
import { _analyzeLine } from './ExportHelper.js';
// This is slightly different than the HTML method as it passes the output to getTXTFromAText
const getPadTXT = async (pad: PadType, revNum: string) => {
@ -262,9 +262,9 @@ const getTXTFromAtext = (pad: PadType, atext: AText, authorColors?:string) => {
return pieces.join('');
};
exports.getTXTFromAtext = getTXTFromAtext;
export { getTXTFromAtext };
exports.getPadTXTDocument = async (padId:string, revNum:string) => {
export const getPadTXTDocument = async (padId:string, revNum:string) => {
const pad = await padManager.getPad(padId);
return getPadTXT(pad, revNum);
};

View file

@ -16,16 +16,16 @@
*/
import log4js from 'log4js';
import {deserializeOps} from '../../static/js/Changeset';
const contentcollector = require('../../static/js/contentcollector');
import {deserializeOps} from '../../static/js/Changeset.js';
import * as contentcollector from '../../static/js/contentcollector.js';
import jsdom from 'jsdom';
import {PadType} from "../types/PadType";
import {Builder} from "../../static/js/Builder";
import {PadType} from "../types/PadType.js";
import {Builder} from "../../static/js/Builder.js";
const apiLogger = log4js.getLogger('ImportHtml');
let processor:any;
exports.setPadHTML = async (pad: PadType, html:string, authorId = '') => {
export const setPadHTML = async (pad: PadType, html:string, authorId = '') => {
if (processor == null) {
const [{rehype}, {default: minifyWhitespace}] =
await Promise.all([import('rehype'), import('rehype-minify-whitespace')]);

View file

@ -17,13 +17,13 @@
* limitations under the License.
*/
const async = require('async');
const fs = require('fs').promises;
const log4js = require('log4js');
const os = require('os');
const path = require('path');
const runCmd = require('./run_cmd');
import settings from './Settings';
import async from 'async';
import { promises as fs } from 'fs';
import log4js from 'log4js';
import os from 'os';
import path from 'path';
import runCmd from './run_cmd.js';
import settings from './Settings.js';
const logger = log4js.getLogger('LibreOffice');
@ -89,7 +89,7 @@ const queue = async.queue(doConvertTask, 1);
* @param {String} type The type to convert into
* @param {Function} callback Standard callback function
*/
exports.convertFile = async (srcFile: string, destFile: string, type:string) => {
export const convertFile = async (srcFile: string, destFile: string, type:string) => {
// Used for the moving of the file, not the conversion
const fileExtension = type;

View file

@ -19,7 +19,7 @@
* limitations under the License.
*/
const semver = require('semver');
import semver from 'semver';
/**
* Quits if Etherpad is not running on a given minimum Node version

View file

@ -136,4 +136,4 @@ class Stream {
[Symbol.iterator]() { return this._iter; }
}
module.exports = Stream;
export default Stream;

View file

@ -1,6 +1,6 @@
'use strict';
import semver from 'semver';
import settings, {getEpVersion} from './Settings';
import settings, {getEpVersion} from './Settings.js';
import axios from 'axios';
const headers = {
'User-Agent': 'Etherpad/' + getEpVersion(),

View file

@ -1,6 +1,6 @@
'use strict';
const CustomError = require('../utils/customError');
import CustomError from './customError.js';
// checks if a rev is a legal number
// pre-condition is that `rev` is not undefined
@ -30,5 +30,4 @@ const checkValidRev = (rev: number|string) => {
// checks if a number is an int
const isInt = (value:number) => (parseFloat(String(value)) === parseInt(String(value), 10)) && !isNaN(value);
exports.isInt = isInt;
exports.checkValidRev = checkValidRev;
export { isInt, checkValidRev };

View file

@ -21,4 +21,4 @@ class CustomError extends Error {
}
}
module.exports = CustomError;
export default CustomError;

View file

@ -1,17 +1,17 @@
'use strict';
import {PadAuthor, PadType} from "../types/PadType";
import {MapArrayType} from "../types/MapType";
import {PadAuthor, PadType} from "../types/PadType.js";
import {MapArrayType} from "../types/MapType.js";
import AttributeMap from '../../static/js/AttributeMap';
import {applyToAText, checkRep, compose, deserializeOps, pack, splitAttributionLines, splitTextLines, unpack} from '../../static/js/Changeset';
import {Builder} from "../../static/js/Builder";
import {OpAssembler} from "../../static/js/OpAssembler";
import {numToString} from "../../static/js/ChangesetUtils";
import Op from "../../static/js/Op";
import {StringAssembler} from "../../static/js/StringAssembler";
const attributes = require('../../static/js/attributes');
const exportHtml = require('./ExportHtml');
import AttributeMap from '../../static/js/AttributeMap.js';
import {applyToAText, checkRep, compose, deserializeOps, pack, splitAttributionLines, splitTextLines, unpack} from '../../static/js/Changeset.js';
import {Builder} from "../../static/js/Builder.js";
import {OpAssembler} from "../../static/js/OpAssembler.js";
import {numToString} from "../../static/js/ChangesetUtils.js";
import Op from "../../static/js/Op.js";
import {StringAssembler} from "../../static/js/StringAssembler.js";
import * as attributes from '../../static/js/attributes.js';
import * as exportHtml from './ExportHtml.js';
class PadDiff {
@ -456,4 +456,4 @@ class PadDiff {
// export the constructor
module.exports = PadDiff;
export default PadDiff;

View file

@ -1,14 +1,14 @@
'use strict';
import {ErrorExtended, RunCMDOptions, RunCMDPromise} from "../types/RunCMDOptions";
import {ErrorExtended, RunCMDOptions, RunCMDPromise} from "../types/RunCMDOptions.js";
import {ChildProcess} from "node:child_process";
import {PromiseWithStd} from "../types/PromiseWithStd";
import {PromiseWithStd} from "../types/PromiseWithStd.js";
import {Readable} from "node:stream";
import spawn from 'cross-spawn';
import log4js from 'log4js';
import path from 'path';
import settings from './Settings';
import settings from './Settings.js';
const logger = log4js.getLogger('runCmd');
@ -74,7 +74,7 @@ const logLines = (readable: undefined | Readable | null, logLineFn: (arg0: (stri
* - `stderr`: Similar to `stdout` but for stderr.
* - `child`: The ChildProcess object.
*/
module.exports = exports = (args: string[], opts:RunCMDOptions = {}) => {
const runCmd = (args: string[], opts:RunCMDOptions = {}) => {
logger.debug(`Executing command: ${args.join(' ')}`);
opts = {cwd: settings.root, ...opts};
@ -161,3 +161,5 @@ module.exports = exports = (args: string[], opts:RunCMDOptions = {}) => {
});
return p;
};
export default runCmd;