create-auth-js-boiler
Version:
Create a new auth-js-boiler project
132 lines (107 loc) • 3.76 kB
JavaScript
#!/usr/bin/env node
/**
* This script prepares the create-auth-js-boiler package for publishing
* by copying the latest files from the main app to the template directory.
*/
import fs from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
// Get the current file path and directory in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Directories to copy
const srcDirs = ["src", "public", "prisma"];
// Individual files to copy
const rootFiles = [
"components.json",
"next.config.mjs",
"package.json",
"postcss.config.mjs",
"README.md",
"tailwind.config.ts",
"tsconfig.json",
".env.example",
];
// Files to exclude from copying
const excludedItems = [
"CONTRIBUTING.md",
"docker-compose.yml",
"dockerfile",
".git",
"node_modules",
".next",
"create-auth-js-boiler",
];
const mainDir = path.resolve(__dirname, "..");
const templateDir = path.resolve(__dirname, "template");
// Ensure template dir exists
fs.ensureDirSync(templateDir);
console.log("🔄 Copying app files to template...");
// Copy specified root files
rootFiles.forEach((file) => {
const src = path.join(mainDir, file);
const dest = path.join(templateDir, file);
if (fs.existsSync(src)) {
console.log(`Copying file: ${file}`);
fs.copySync(src, dest, { overwrite: true });
} else {
console.warn(`⚠️ Warning: Source file not found: ${file}`);
}
});
// Copy directories
srcDirs.forEach((dir) => {
const src = path.join(mainDir, dir);
const dest = path.join(templateDir, dir);
if (fs.existsSync(src)) {
console.log(`Copying directory: ${dir}`);
// Remove destination first to ensure deleted files are actually removed
if (fs.existsSync(dest)) {
fs.removeSync(dest);
}
// Copy directory with filter for excluded items
fs.copySync(src, dest, {
filter: (src) => {
const relativePath = path.relative(mainDir, src);
return !excludedItems.some(
(excluded) =>
relativePath.startsWith(excluded) ||
path.basename(src) === excluded,
);
},
});
} else {
console.warn(`⚠️ Warning: Source directory not found: ${dir}`);
}
});
// Check if package.json exists in template and make any required adjustments
const templatePkgPath = path.join(templateDir, "package.json");
if (fs.existsSync(templatePkgPath)) {
console.log("📦 Adjusting template package.json...");
const pkg = fs.readJSONSync(templatePkgPath);
// Update template package.json if needed
pkg.private = false;
pkg.description = "Auth JS Boiler - Complete authentication solution";
fs.writeJSONSync(templatePkgPath, pkg, { spaces: 2 });
}
// Create or update .env.example if it doesn't exist in the template
const envExamplePath = path.join(mainDir, ".env.example");
const templateEnvPath = path.join(templateDir, ".env.example");
if (!fs.existsSync(templateEnvPath) && fs.existsSync(envExamplePath)) {
console.log("📄 Creating .env.example in template...");
fs.copySync(envExamplePath, templateEnvPath);
} else if (!fs.existsSync(envExamplePath)) {
console.log("📝 Creating default .env.example...");
const defaultEnv = `# Database
DATABASE_URL="postgresql://user:password@localhost:5432/auth_js_boiler?schema=public"
# Auth
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="your-secret-key" # Generate with: openssl rand -base64 32
# Email
EMAIL="your-email@gmail.com"
EMAIL_PASS_KEY="your-gmail-app-password" # App password from Google Account settings
# OAuth Providers (optional)
GOOGLE_CLIENT_ID=""
GOOGLE_CLIENT_SECRET=""`;
fs.writeFileSync(templateEnvPath, defaultEnv);
}
console.log("✅ Template preparation completed successfully!");