A tool that converts file extensions such as .js or .d.ts to their .cjs and .d.cts counterparts

Description
This is a little tool that simply converts the extensions of filenames such as .js to .cjs, .js.map to .cts.map, .d.ts to .d.cts, and .d.ts.map to .d.cts.map. You might find value in it as part of your build step.
Features
- Converts
.js to .cjs, .js.map to .cts.map, .d.ts to .d.cts, and .d.ts.map to .d.cts.map.
- Can be used as a CLI
- Can be used programmatically as a library
- Is an ESM package with a CommonJS fallback, so you can use it in both types of codebases.
Backers
Patreon

Table of Contents
Install
npm
$ npm install dotcjs
Yarn
$ yarn add dotcjs
pnpm
$ pnpm add dotcjs
Run once with npx
$ npx dotcjs
Usage
dotcjs can be used in a variety of ways. The most straightforward usage is directly from the CLI:
CLI usage
You can use this library as a CLI to convert the extensions of files matching an input glob.
The following command renames all files ending with .js, .js.map, .d.ts, and .d.ts.map matched by the glob **/*.* to their respective .cjs counterparts:
dotcjs **/*.*
You can also pass in second argument, outDir, to copy them to a different directory:
dotcjs dist/** dist/cjs
Here's an overview of the options that can be passed via the CLI:
$ dotcjs --help
Usage: dotcjs transform [options] <input> [outDir]
Cconverts file extensions such as .js or .d.ts to their .cjs and .d.cts counterparts based on the input glob
Options:
-d, --debug [arg] Whether to print debug information
-v, --verbose [arg] Whether to print verbose information
-s, --silent [arg] Whether to not print anything
-c, --cwd [arg] Optionally which directory to use as the current working directory
-m, --dry [arg] If true, no files will be written to disk
-h, --help display help for command
You can also just run cjstoesm without explicitly passing in the transform` command, as the CLI defaults to executing that command.
API Usage
You can also use this library programmatically:
import {transform} from "dotcjs";
await transform({
input: "src/**/*.*"
});
Alternatively, if you don't want the transform function to automatically write files to disk, you can pass write: false as an option and handle it yourself:
import {transform} from "dotcjs";
import fs from "fs";
const result = await transform({
input: "src/**/*.*",
write: false
});
// Write to disk
for (const {filename, oldFilename, text} of result.files) {
fs.copyFileSync(oldFilename, filename);
}
API options
interface TransformOptions {
/**
* The input glob(s) to match against the file system
*/
input: string[] | string;
/**
* The output directory to use. If not given, the source files will be overwritten
*/
outDir?: string;
/**
* If write is false, no files will be written to disk
*/
write?: boolean;
/**
* The FileSystem to use. Useful if you want to work with a virtual file system. Defaults to using the "fs" module
*/
fileSystem?: FileSystem;
/**
* A logger that can print messages of varying severity depending on the log level
*/
logger?: Loggable;
/**
* The base directory (defaults to process.cwd())
*/
cwd?: string;
/**
* If true, debug information will be printed. If a function is provided, it will be invoked for each file name. Returning true from the function
* determines that debug information will be printed related to that file
*/
debug?: boolean;
}
Contributing
Do you want to contribute? Awesome! Please follow these recommendations.
Maintainers
FAQ
Is this really just an over-engineered mv or cp command?
Yes. It truly is all it is.
License
MIT © Frederik Wessberg (@FredWessberg) (Website)