+
11
-

回答

1. AI 提示词 (Prompt)这个提示词的核心是定义一个清晰、严格、易于机器解析的格式。我会使用一种独特的、在代码中几乎不会出现的分隔符。提示词模板 (中文版)
你是一名专业的软件架构师和全栈开发者。你的任务是根据我的要求生成完整的项目源代码。

请严格遵守以下输出格式,不要添加任何额外的解释、介绍或总结。你的整个回复必须是纯粹的代码数据,以便我可以直接用脚本解析。

输出格式规则:

每个文件都必须用一个明确的区块来包裹。

每个文件区块以 ======== BEGIN FILE: [文件的相对路径] ======== 开始。

文件内容紧跟在开始标记之后。

每个文件区块以 ======== END FILE: [文件的相对路径] ======== 结束。

[文件的相对路径] 必须和开始标记中的路径完全一致。

文件和文件之间用一个空行隔开。

路径应该使用正斜杠 / 作为目录分隔符,例如 src/components/Button.js。

确保包含所有必要的文件,例如 .gitignore, package.json, webpack.config.js, README.md 等。

格式示例:

Generated code
======== BEGIN FILE: index.html ========
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Hello World</h1>
<script src="js/main.js"></script>
</body>
</html>
======== END FILE: index.html ========

======== BEGIN FILE: css/style.css ========
body {
font-family: sans-serif;
background-color: #f0f0f0;
}
======== END FILE: css/style.css ========

======== BEGIN FILE: js/main.js ========
console.log("Script loaded!");
======== END FILE: js/main.js ========

======== BEGIN FILE: .gitignore ========
node_modules
dist
.env
======== END FILE: .gitignore ========


你的任务:

现在,请根据以下要求生成项目代码:
[在这里详细描述你的项目需求,例如:一个使用 React 和 Express 的简单待办事项应用,前端包含一个组件用于展示任务列表,一个组件用于添加任务。后端提供两个 API 接口:GET /api/todos 用于获取列表,POST /api/todos 用于添加新任务。]
提示词模板 (英文版 - 推荐)使用英文提示词通常能获得更稳定和高质量的AI代码生成结果。
You are an expert software architect and full-stack developer. Your task is to generate the complete source code for a project based on my request.

You must strictly adhere to the following output format. Do not add any extra explanations, introductions, or summaries. Your entire response must be pure data that I can parse with a script.

**Output Format Rules:**

1. Each file must be enclosed in a clear block.
2. Each file block must start with `======== BEGIN FILE: [relative/path/to/file] ========`.
3. The file content comes immediately after the start marker.
4. Each file block must end with `======== END FILE: [relative/path/to/file] ========`.
5. The path in the end marker must exactly match the path in the start marker.
6. Separate file blocks with a single blank line.
7. Use forward slashes `/` for directory separators (e.g., `src/components/Button.js`).
8. Ensure all necessary files are included, such as `.gitignore`, `package.json`, `webpack.config.js`, `README.md`, etc.

**Format Example:**

======== BEGIN FILE: index.html ========
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Hello World</h1>
<script src="js/main.js"></script>
</body>
</html>
======== END FILE: index.html ========

======== BEGIN FILE: css/style.css ========
body {
font-family: sans-serif;
background-color: #f0f0f0;
}
======== END FILE: css/style.css ========

======== BEGIN FILE: js/main.js ========
console.log("Script loaded!");
======== END FILE: js/main.js ========

======== BEGIN FILE: .gitignore ========
node_modules
dist
.env
======== END FILE: .gitignore ========

**Your Task:**

Now, generate the project code for the following request:
**[Describe your project here. For example: A simple To-Do list application using React and an Express backend. The frontend should have a component to display the list and a component to add a new task. The backend should provide two API endpoints: GET /api/todos to fetch the list, and POST /api/todos to add a new item.]**

如何使用将上述提示词(推荐英文版)复制到 AI 对话框中,并修改最后你的项目需求。AI 会生成一大段文本,完全符合你定义的格式。将 AI 的完整输出复制并粘贴到一个新文件中,例如 project_output.txt。将这个 project_output.txt 文件与下面的 Node.js 或 PHP 脚本放在同一个目录下。运行脚本,它会自动在当前目录下创建项目文件和文件夹。2. Node.js 还原脚本 (rebuild_project.js)这个脚本会读取 project_output.txt,解析内容,并自动创建对应的目录和文件。
// rebuild_project.js
const fs = require('fs');
const path = require('path');

// AI生成内容的文件名
const inputFile = 'project_output.txt';

// 正则表达式,用于匹配文件区块
// 关键点:
// 1. `(.*?)`: 捕获文件路径 (非贪婪)
// 2. `([\s\S]*?)`: 捕获文件内容,包括换行符 (非贪婪)
// 3. `\1`: 反向引用,确保开始和结束的路径一致
// 4. `g` 标志: 全局匹配,找到所有文件区块
const fileBlockRegex = /======== BEGIN FILE: (.*?) ========([\s\S]*?)======== END FILE: \1 ========/g;

function rebuildProject() {
console.log(`Reading from ${inputFile}...`);

let content;
try {
content = fs.readFileSync(inputFile, 'utf8');
} catch (error) {
console.error(`Error: Could not read the input file '${inputFile}'.`);
console.error(error.message);
return;
}

let match;
let fileCount = 0;

// 使用 while 循环和 exec 来遍历所有匹配项
while ((match = fileBlockRegex.exec(content)) !== null) {
const filePath = match[1].trim();
// 移除文件内容前后可能存在的空行
const fileContent = match[2].trim();

if (!filePath) {
console.warn('Found a file block with an empty path. Skipping.');
continue;
}

try {
// 获取文件所在的目录
const dir = path.dirname(filePath);

// 如果目录不存在,则递归创建它
if (dir && !fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log(`Created directory: ${dir}`);
}

// 写入文件
fs.writeFileSync(filePath, fileContent);
console.log(`Created file: ${filePath}`);
fileCount++;

} catch (error) {
console.error(`Error creating file or directory for: ${filePath}`);
console.error(error.message);
}
}

if (fileCount > 0) {
console.log(`\nProject rebuild complete. ${fileCount} files were created.`);
} else {
console.warn('\nNo valid file blocks were found in the input file. Please check the format.');
}
}

rebuildProject();

如何运行 Node.js 脚本:确保你已安装 Node.js。将上述代码保存为 rebuild_project.js。在终端中,进入该文件所在的目录。运行命令:node rebuild_project.js3. PHP 还原脚本 (rebuild_project.php)如果你更喜欢用 PHP,这个脚本的功能和 Node.js 版本完全相同。
<?php
// rebuild_project.php

// AI生成内容的文件名
$inputFile = 'project_output.txt';

// 正则表达式,与JS版本类似
// s 修饰符: 让 . 匹配换行符,等同于 [\s\S]
$fileBlockRegex = '/======== BEGIN FILE: (.*?) ========([\s\S]*?)======== END FILE: \1 ========/s';

function rebuildProject(string $inputFile, string $regex): void
{
echo "Reading from {$inputFile}...\n";

if (!file_exists($inputFile)) {
echo "Error: Could not find the input file '{$inputFile}'.\n";
return;
}

$content = file_get_contents($inputFile);
if ($content === false) {
echo "Error: Could not read the input file '{$inputFile}'.\n";
return;
}

$matches = [];
$fileCount = preg_match_all($regex, $content, $matches);

if ($fileCount > 0) {
for ($i = 0; $i < $fileCount; $i++) {
$filePath = trim($matches[1][$i]);
$fileContent = trim($matches[2][$i]);

if (empty($filePath)) {
echo "Warning: Found a file block with an empty path. Skipping.\n";
continue;
}

try {
// 获取文件所在的目录
$dir = dirname($filePath);

// 如果目录不存在,则递归创建它
if ($dir !== '.' && !is_dir($dir)) {
// 0755 是常用的目录权限,true 表示允许递归创建
mkdir($dir, 0755, true);
echo "Created directory: {$dir}\n";
}

// 写入文件
file_put_contents($filePath, $fileContent);
echo "Created file: {$filePath}\n";

} catch (Exception $e) {
echo "Error creating file or directory for: {$filePath}\n";
echo $e->getMessage() . "\n";
}
}
echo "\nProject rebuild complete. {$fileCount} files were created.\n";
} else {
echo "\nNo valid file blocks were found in the input file. Please check the format.\n";
}
}

rebuildProject($inputFile, $fileBlockRegex);
?>
如何运行 PHP 脚本:确保你已安装 PHP。将上述代码保存为 rebuild_project.php。在终端中,进入该文件所在的目录。运行命令:php rebuild_project.php现在,你拥有了一套完整的、自动化的工作流程,可以高效地利用 AI 生成和部署项目代码了。

网友回复

我知道答案,我要回答