create-director-app
Version:
NPX package to clone director scripts from script generation service
57 lines (51 loc) • 1.8 kB
JavaScript
import fs from "fs-extra";
import * as path from "path";
export async function setupProject(projectName, templateData, googleApiKey) {
const projectPath = path.resolve(process.cwd(), projectName);
if (await fs.pathExists(projectPath)) {
throw new Error(`Directory ${projectName} already exists`);
}
await fs.ensureDir(projectPath);
try {
for (const file of templateData.files) {
const filePath = path.join(projectPath, file.path);
if (file.type === "directory") {
await fs.ensureDir(filePath);
}
else {
await fs.ensureDir(path.dirname(filePath));
await fs.outputFile(filePath, file.content, "utf-8");
}
}
if (templateData.packageJson) {
const packageJsonPath = path.join(projectPath, "package.json");
await fs.outputJSON(packageJsonPath, templateData.packageJson, {
spaces: 2,
});
}
if (templateData.instructions) {
const readmePath = path.join(projectPath, "README.md");
const readmeContent = `
${templateData.instructions}
1. Install dependencies: pnpm install
2. Run the script: pnpm start
---
_Generated with create-director-app_
`;
await fs.outputFile(readmePath, readmeContent, "utf-8");
}
if (googleApiKey) {
const envPath = path.join(projectPath, ".env");
const envContent = `
GOOGLE_API_KEY=${googleApiKey}
`;
await fs.outputFile(envPath, envContent, "utf-8");
}
}
catch (error) {
await fs.remove(projectPath);
throw error;
}
}
//# sourceMappingURL=project-setup.js.map