script-creation-rules
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseScript Creation Rules
脚本创建规则
Rule (CRITICAL)
核心规则
When creating scripts, follow this priority order:
- First choice: Shell script one-liners
- Second choice: TypeScript with Deno (only when variables or complex branching are necessary)
- Prohibited: Node.js and Python
创建脚本时,请遵循以下优先级顺序:
- 首选方案:Shell脚本单行命令
- 次选方案:结合Deno使用TypeScript(仅当需要变量或复杂分支逻辑时)
- 禁止使用: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
undefinedFile 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"
undefined2. 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 to execute Deno scripts.
sfw deno run <file>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.tsIMPORTANT: Always use instead of directly.
sfw deno rundeno run仅在以下情况下使用TypeScript+Deno:
- 复杂逻辑需要使用变量时
- 需要复杂分支/条件判断时
- 需要超出简单Shell结构的错误处理时
执行方式:必须使用来执行Deno脚本。
sfw deno run <file>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 rundeno runProhibited Technologies
禁止使用的技术
The following are strictly prohibited:
- Node.js: ,
node script.js, etc.npm run - Python: ,
python script.py,python3 script.py, etc.pip install - Direct Deno execution: (use
deno runinstead)sfw deno run
以下技术严格禁止使用:
- 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:
- Can it be done with a shell one-liner? -> Use shell script
- Does it need variables or complex logic? -> Use TypeScript with Deno (execute with )
sfw deno run <file> - Never use Node.js or Python
- Never use directly -> Always use
deno runsfw deno run
创建脚本时:
- 是否可以通过Shell单行命令实现? -> 使用Shell脚本
- 是否需要变量或复杂逻辑? -> 结合Deno使用TypeScript(使用执行)
sfw deno run <file> - 绝对禁止使用Node.js或Python
- 绝对禁止直接使用-> 必须使用
deno runsfw deno run
Examples
示例
Good: Shell One-Liner
正确示例:Shell单行命令
bash
undefinedbash
undefinedFind and count TypeScript files
Find and count TypeScript files
find . -name "*.ts" -type f | wc -l
undefinedfind . -name "*.ts" -type f | wc -l
undefinedGood: 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
undefinedpython
undefinedDO NOT USE
DO NOT USE
import os
files = os.listdir('.')
undefinedimport os
files = os.listdir('.')
undefinedCommon Patterns
常见模式
File Operations
文件操作
bash
undefinedbash
undefinedShell one-liner
Shell one-liner
find . -type f -name "*.md" -exec wc -l {} ;
undefinedfind . -type f -name "*.md" -exec wc -l {} ;
undefinedText Processing
文本处理
bash
undefinedbash
undefinedShell one-liner
Shell one-liner
grep -r "pattern" . | sed 's/old/new/g' | sort | uniq
undefinedgrep -r "pattern" . | sed 's/old/new/g' | sort | uniq
undefinedConditional Logic (Simple)
简单条件逻辑
bash
undefinedbash
undefinedShell one-liner
Shell one-liner
[ -d "dir" ] && echo "exists" || mkdir -p "dir"
undefined[ -d "dir" ] && echo "exists" || mkdir -p "dir"
undefinedConditional 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.tstypescript
// 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