Feature Overview
Automatically back up the entry file of a JS project and all its require() dependencies, creating a complete project snapshot.
Core Features
- ✅ Automatic entry file discovery: prioritizes reading
package.json#main, then detectsmain.js/index.js/app.js - ✅ Automatic project root discovery: specified via the
-dparameter, or by searching upward forpackage.json - ✅ Recursive nested detection: automatically detects multi-level nested dependencies (A→B→C→D), using a BFS algorithm
- ✅ Automatic numbering management: scans existing backup folders and automatically generates an incrementing two-digit number
- ✅ Smart naming: the user provides a descriptive name, and the number prefix is automatically prepended (e.g. "09第一次重构")
- ✅ Circular dependency handling: correctly handles A→B→A circular dependencies without interrupting the backup
- ✅ Filter npm packages: only matches relative paths
require("./xxx")/require("../xxx"), automatically filtering outrequire("fs")etc.
Environment Requirements
- Node.js: version 14.x or higher
- Script Location: the
backup.mjs
in this skill directory
node backup.mjs "备份名称" # 使用 cwd 自动发现项目
node backup.mjs "备份名称" -d <项目根目录> # 指定项目根
node backup.mjs "备份名称" -e <入口文件> # 指定入口文件
node backup.mjs # 纯编号备份
Trigger Conditions
When the user encounters any of the followingimmediately invoke this skill:
- "back up the code", "back up the project", "save the current version", "back up the code, name is xxx"
- "back up before modifying", "save a checkpoint first"
- "create a snapshot", "project-backup"
- Before preparing to carry out a major code refactor
AI Execution Flow
Step 1: Determine the project root directory
Obtain the working directory of the current session (the project root) for later use with the -d parameter.
Step 2: Execute the backup command
Use the RunCommand tool to execute the backup script,be sure to include the -d parameter to specify the project root:
# cwd 可以是任意目录;必须通过 -d 指定项目根
node "<skill目录>/backup.mjs" "备份名称" -d "<项目根目录绝对路径>"
Key parameters:
blocking: truecommand_type: short_running_processrequires_approval: false
Example:
node "C:\Users\Administrator\.trae-cn\skills\backup-project\backup.mjs" "第一次重构" -d "D:\script\work-sop\...\"
Step 3: Verify the backup result
Use the LS tool to list the project-backup/<备份名称>/ directory and show the user that the backup succeeded.
Script Core Logic (backup.mjs)
Argument parsing
-d / --dir 项目根目录(默认: 从 cwd 向上查找 package.json)
-e / --entry 入口文件名(默认: 自动检测)
第一个非选项参数 = 备份描述名称
Entry file detection priority
-efile specified on the command linepackage.jsonthemainfield in- detect in order
main.js→index.js→app.js
Dependency detection
Regex:/require\(["'](\.\.?\/[^"']+|[^"']+\.js)["']\)/g
- Matches
require("./xxx")、require("../xxx")as well asrequire("xxx.js")bare filenames - Automatically skips
require("fs")/require("lodash")and other npm packages (no extension, no path) - Uses BFS breadth-first search, maximum depth 10 levels
- Dependency files retain their original relative path structure in the backup directory
Numbering rules
- Scan all subfolders under
project-backup/all subfolders under it - Extract the numeric prefix of folder names, take the maximum value +1
- Pad to two digits with zeros (01, 02, ...)
- Concatenation format:
{编号}{用户名称}, e.g.09第一次重构
Troubleshooting
| Problem | Cause | Solution |
|---|---|---|
| Entry file does not exist | Incorrect project root | Use -d to explicitly specify the project root |
| Missing dependency files | Dynamic require / ESM import used | The script currently only supports static require() |
| Backup directory already exists | Backup with the same name | fs.mkdirSync recursive mode does not report an error; files will be overwritten |
| Copy failed | File is locked | Close the editor with the file open |