@dobesv/clean-dest

中文文档
1.3.8发布于 2025/05/07MITTypeScriptCommonJS近 30 天下载 1.5万

A CLI to clean a destination directory given a source directory

原始使用文档
文档目录16 节
  1. Install
  2. Usage
  3. Folder structure
  4. Custom FileMap function
  5. CLI command
  6. CLI
  7. src-root
  8. dest-root
  9. base-pattern
  10. file-map
  11. ignore
  12. permanent
  13. dry-run
  14. verbose
  15. API
  16. Watch Mode

clean-dest

GitHub

A CLI to clean a destination directory given a source directory. This deletes files in the output folder that don't have a corresponding file in the source folder (any more). This helps deal with file renames and deletions when your build tool (e.g. babel or tsc) doesn't automatically delete output files whose input file was removed.

By default it keeps files in the output folder with the same name as files in the input folder, but you can configure this so that additional files are preserved by mapping a source file extension/suffix to output file extensions/suffixes.

Designed initially with typescript in mind, but can be used with any file types.

Install

$ npm install clean-dest

Usage

Folder structure

│
└───scripts
│   │   clean-dest.js
│
└───src
│   │   file1.ts
│   │   file2.ts
│
└───dist
│   │   file1.js
│   │   file1.js.map
│   │   file1.d.ts
│   │   file2.js
│   │   file2.js.map
│   │   file2.d.ts

Custom FileMap function

CLI command

clean-dest -s ./src -d ./dist --file-map ./scripts/clean-dest

CLI

src-root

Glob pattern(s) for source files.

dest-root

Destination root directory.

base-pattern

Glob pattern(s) to delete, default is "dest-root"/**/*. All files that match this pattern will be deleted unless they are matched to a source file or ignored using the ignore option.

file-map

This identifies files in dest-root that should be kept based on the files present in src-root based on a mapping from input file extensions to output file extensions.

The argument can either be a JS module to load for the mapping, or a mapping provided in a special format. If the argument matches the regular pattern /^.[a-z]/i it will be parsed as a mapping string, otherwise it is treated as a module to import.

using a string

It can be written as a series of file suffix mappings separated with semicolons. Each mapping has input file extensions and output file extensions around a colon (:). Multiple file extensions can be separated with commas. If a mapping has no colon then the input and output extensions are considered the same.

The output filename(s) are calculated by replacing a suffix found on the left by all the suffix(es) on the right.

Examples:

  • .js,.ts:.js,.js.map,.d.ts: For any .js or .ts file found in src-root, preserve matching files with suffixes .js, .js.map, .d.ts in dest-root
  • .js:.js,.js.map;.ts:.js,.js.map,.d.ts: For .js files in src-root, preserve .js and .js.map files in the dest-root. For .ts files, also preserve .d.ts files.
  • .js;.ts:.js,.js.map,.d.ts: For .js files in src-root, preserve .js files in the dest-root. For .ts files, preserve .js, .js.map, and .d.ts files.

using a js module

The argument can be a path to a js module that exports information about which files to keep in the output folder. The path will be loaded using require by default, so if it is a local module it should use a relative path.

If the module exports a string or array of strings they will be parsed and processed as if they were provided on the command line as a set of patterns.

If the module exports an object, the object's keys are considered a filename suffix to match. The object's values can be (1) strings or arrays of strings identifying replacement suffixes to use in the output folder, or (2) a function which takes a file path in the output folder but with the original file extension, and returns an array of potential output files.

For example:

// ./scripts/clean-dest.js
module.exports = exports = {
    // Rename the file extensions from the 'ts' from the src folder to what we expect in the dest folder
    '.ts': (destFilePath) => [
        destFilePath.replace(/.ts$/, '.d.ts'),
        destFilePath.replace(/.ts$/, '.js'),
        destFilePath.replace(/.ts$/, '.js.map'),
    ]
};

ignore

permanent

Optional permanent delete using del, otherwise uses trash.

dry-run

Optional test run to not actually delete matched files.

verbose

Optional output logging.

API

See the API docs.

Watch Mode

Use nodemon:

nodemon --watch ./src -e ts --exec clean-dest -s ./src -d ./dist

版本与发布记录

@dobesv/clean-dest 共发布 6 个版本,首次发布于 2022/05/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.8latest2025/05/071.5 年前
1.3.72022/08/094 年前
1.3.62022/06/014 年前
1.3.52022/05/124 年前
1.3.42022/05/124 年前
1.3.32022/05/124 年前

关于 @dobesv/clean-dest 的常见问题

@dobesv/clean-dest 是什么?

A CLI to clean a destination directory given a source directory

如何安装 @dobesv/clean-dest?

在项目目录运行 npm install @dobesv/clean-dest,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 clean-dest,可以用 npx clean-dest 直接运行。

@dobesv/clean-dest 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dobesv/clean-dest 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dobesv/clean-dest 可以商用吗?

@dobesv/clean-dest 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dobesv/clean-dest 的下载量有多少?

根据 npm 官方下载统计,@dobesv/clean-dest 在 2026/10/13 – 2026/11/11 这 30 天内被下载 15,345 次,日均约 512 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dobesv/clean-dest 还在维护吗?

最新版本 1.3.8 发布于 2025/05/07(1.5 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。