script-creation-rules

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Script Creation Rules

脚本创建规则

Rule (CRITICAL)

核心规则

When creating scripts, follow this priority order:
  1. First choice: Shell script one-liners
  2. Second choice: TypeScript with Deno (only when variables or complex branching are necessary)
  3. Prohibited: Node.js and Python
创建脚本时,请遵循以下优先级顺序:
  1. 首选方案:Shell脚本单行命令
  2. 次选方案:结合Deno使用TypeScript(仅当需要变量或复杂分支逻辑时)
  3. 禁止使用:Node.js和Python

Priority Order

优先级顺序

1. Shell Script One-Liners (PREFERRED)

1. Shell脚本单行命令(首选)

Use shell script one-liners for simple operations:
bash
undefined
对于简单操作,使用Shell脚本单行命令:
bash
undefined

File operations

File operations

find . -name "*.js" -type f | xargs grep "pattern"
find . -name "*.js" -type f | xargs grep "pattern"

Text processing

Text processing

cat file.txt | grep "pattern" | sed 's/old/new/g'
cat file.txt | grep "pattern" | sed 's/old/new/g'

File counting

File counting

ls -1 | wc -l
ls -1 | wc -l

Directory operations

Directory operations

mkdir -p path/to/dir && cd path/to/dir
mkdir -p path/to/dir && cd path/to/dir

Conditional execution

Conditional execution

[ -f file.txt ] && echo "exists" || echo "not found"
undefined
[ -f file.txt ] && echo "exists" || echo "not found"
undefined

2. TypeScript with Deno (When Necessary)

2. 结合Deno使用TypeScript(必要时)

Use TypeScript with Deno only when:
  • Variables are needed for complex logic
  • Complex branching/conditionals are required
  • Error handling beyond simple shell constructs is needed
Execution: Always use
sfw deno run <file>
to execute Deno scripts.
typescript
// Example: Complex script with variables and branching
const files = Deno.readDirSync(".");
const results: string[] = [];

for (const file of files) {
  if (file.isFile && file.name.endsWith(".ts")) {
    const content = Deno.readTextFileSync(file.name);
    if (content.includes("pattern")) {
      results.push(file.name);
    }
  }
}

console.log(results.join("\n"));
Execute with:
bash
sfw deno run --allow-read script.ts
IMPORTANT: Always use
sfw deno run
instead of
deno run
directly.
仅在以下情况下使用TypeScript+Deno:
  • 复杂逻辑需要使用变量时
  • 需要复杂分支/条件判断时
  • 需要超出简单Shell结构的错误处理时
执行方式:必须使用
sfw deno run <file>
来执行Deno脚本。
typescript
// Example: Complex script with variables and branching
const files = Deno.readDirSync(".");
const results: string[] = [];

for (const file of files) {
  if (file.isFile && file.name.endsWith(".ts")) {
    const content = Deno.readTextFileSync(file.name);
    if (content.includes("pattern")) {
      results.push(file.name);
    }
  }
}

console.log(results.join("\n"));
执行命令:
bash
sfw deno run --allow-read script.ts
重要提示:必须使用
sfw deno run
,而非直接使用
deno run

Prohibited Technologies

禁止使用的技术

The following are strictly prohibited:
  • Node.js:
    node script.js
    ,
    npm run
    , etc.
  • Python:
    python script.py
    ,
    python3 script.py
    ,
    pip install
    , etc.
  • Direct Deno execution:
    deno run
    (use
    sfw deno run
    instead)
以下技术严格禁止使用:
  • Node.js
    node script.js
    npm run
  • Python
    python script.py
    python3 script.py
    pip install
  • 直接执行Deno
    deno run
    (请使用
    sfw deno run
    替代)

Decision Flow

决策流程

When creating a script:
  1. Can it be done with a shell one-liner? -> Use shell script
  2. Does it need variables or complex logic? -> Use TypeScript with Deno (execute with
    sfw deno run <file>
    )
  3. Never use Node.js or Python
  4. Never use
    deno run
    directly
    -> Always use
    sfw deno run
创建脚本时:
  1. 是否可以通过Shell单行命令实现? -> 使用Shell脚本
  2. 是否需要变量或复杂逻辑? -> 结合Deno使用TypeScript(使用
    sfw deno run <file>
    执行)
  3. 绝对禁止使用Node.js或Python
  4. 绝对禁止直接使用
    deno run
    -> 必须使用
    sfw deno run

Examples

示例

Good: Shell One-Liner

正确示例:Shell单行命令

bash
undefined
bash
undefined

Find and count TypeScript files

Find and count TypeScript files

find . -name "*.ts" -type f | wc -l
undefined
find . -name "*.ts" -type f | wc -l
undefined

Good: TypeScript with Deno

正确示例:TypeScript+Deno

typescript
// Complex file processing with error handling
try {
  const files = Array.from(Deno.readDirSync(".")).filter((f) => f.isFile && f.name.endsWith(".ts"));

  for (const file of files) {
    const content = Deno.readTextFileSync(file.name);
    // Complex processing...
  }
} catch (error) {
  console.error("Error:", error);
}
typescript
// Complex file processing with error handling
try {
  const files = Array.from(Deno.readDirSync(".")).filter((f) => f.isFile && f.name.endsWith(".ts"));

  for (const file of files) {
    const content = Deno.readTextFileSync(file.name);
    // Complex processing...
  }
} catch (error) {
  console.error("Error:", error);
}

Bad: Node.js

错误示例:Node.js

javascript
// DO NOT USE
const fs = require("fs");
const files = fs.readdirSync(".");
javascript
// DO NOT USE
const fs = require("fs");
const files = fs.readdirSync(".");

Bad: Python

错误示例:Python

python
undefined
python
undefined

DO NOT USE

DO NOT USE

import os files = os.listdir('.')
undefined
import os files = os.listdir('.')
undefined

Common Patterns

常见模式

File Operations

文件操作

bash
undefined
bash
undefined

Shell one-liner

Shell one-liner

find . -type f -name "*.md" -exec wc -l {} ;
undefined
find . -type f -name "*.md" -exec wc -l {} ;
undefined

Text Processing

文本处理

bash
undefined
bash
undefined

Shell one-liner

Shell one-liner

grep -r "pattern" . | sed 's/old/new/g' | sort | uniq
undefined
grep -r "pattern" . | sed 's/old/new/g' | sort | uniq
undefined

Conditional Logic (Simple)

简单条件逻辑

bash
undefined
bash
undefined

Shell one-liner

Shell one-liner

[ -d "dir" ] && echo "exists" || mkdir -p "dir"
undefined
[ -d "dir" ] && echo "exists" || mkdir -p "dir"
undefined

Conditional Logic (Complex)

复杂条件逻辑

typescript
// TypeScript with Deno
const dirs = ["dir1", "dir2", "dir3"];
for (const dir of dirs) {
  try {
    const stat = Deno.statSync(dir);
    if (stat.isDirectory) {
      console.log(`${dir} exists`);
    }
  } catch {
    Deno.mkdirSync(dir, { recursive: true });
    console.log(`Created ${dir}`);
  }
}
Execute with:
bash
sfw deno run --allow-read --allow-write script.ts
typescript
// TypeScript with Deno
const dirs = ["dir1", "dir2", "dir3"];
for (const dir of dirs) {
  try {
    const stat = Deno.statSync(dir);
    if (stat.isDirectory) {
      console.log(`${dir} exists`);
    }
  } catch {
    Deno.mkdirSync(dir, { recursive: true });
    console.log(`Created ${dir}`);
  }
}
执行命令:
bash
sfw deno run --allow-read --allow-write script.ts