SuperAPI AI Directory

backup-project

General & other

View repository

Updated 10-16 08:49

About this skill

Generic JS project code backup

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 detects main.js / index.js / app.js
  • ✅ Automatic project root discovery: specified via the -d parameter, or by searching upward for package.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 out require("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:

  1. "back up the code", "back up the project", "save the current version", "back up the code, name is xxx"
  2. "back up before modifying", "save a checkpoint first"
  3. "create a snapshot", "project-backup"
  4. 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: true
  • command_type: short_running_process
  • requires_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

  1. -e file specified on the command line
  2. package.json the main field in
  3. detect in order main.js → index.js → app.js

Dependency detection

Regex:/require\(["'](\.\.?\/[^"']+|[^"']+\.js)["']\)/g

  • Matches require("./xxx")、require("../xxx") as well as require("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