@hykocx/nextjs-start
Version:
A Next.js starter template with Tailwind CSS and custom configuration
129 lines (105 loc) ⢠4.2 kB
JavaScript
import { execSync } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the absolute path of the directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Helper function to sleep between steps
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Helper function to log steps
const logStep = async (stepNumber, message) => {
console.log(`\nš Step ${stepNumber}: ${message}`);
};
// Main initialization function
async function initProject() {
try {
console.log('\nš Initializing Next.js project with @hykocx/nextjs-start...\n');
// Step 1: Create the project with create-next-app
await logStep(1, "Creating the Next.js project");
execSync('npx create-next-app@latest ./ --javascript --tailwind --app --empty', { stdio: 'inherit' });
// Step 2: Delete /app directory and its contents
await logStep(2, "Deleting /app directory and its contents");
if (fs.existsSync('./app')) {
fs.removeSync('./app');
console.log('š Deleted /app directory and its contents');
} else {
console.log('ā ļø /app directory not found, skipping deletion');
}
// Step 3: Update jsconfig.json
await logStep(3, "Updating jsconfig.json");
const jsConfigPath = './jsconfig.json';
const jsConfigContent = {
compilerOptions: {
paths: {
"@*": ["./*"]
}
}
};
fs.writeJsonSync(jsConfigPath, jsConfigContent, { spaces: 2 });
console.log('āļø jsconfig.json updated');
// Step 4: Update next.config.mjs
await logStep(4, "Updating next.config.mjs");
const nextConfigPath = './next.config.mjs';
const nextConfigContent = `/** @type {import('next').NextConfig} */
const nextConfig = {
devIndicators: false
};
export default nextConfig;
`;
fs.writeFileSync(nextConfigPath, nextConfigContent);
console.log('āļø next.config.mjs updated');
// Step 5: Update README.md with project name
await logStep(5, "Updating README.md");
const packageJsonPath = './package.json';
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = fs.readJsonSync(packageJsonPath);
const projectName = packageJson.name || 'Next.js Project';
fs.writeFileSync('README.md', `# ${projectName}
A website made with Next.js and Tailwind CSS.
`);
console.log('š README.md updated with project name');
} catch (error) {
console.log('ā ļø Error reading package.json or updating README.md:', error.message);
}
} else {
console.log('ā ļø package.json not found, cannot update README.md');
}
// Step 6: Update package.json scripts
await logStep(6, "Adding make-favicon script to package.json");
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = fs.readJsonSync(packageJsonPath);
// Add make-favicon script to scripts object
if (!packageJson.scripts) {
packageJson.scripts = {};
}
packageJson.scripts["make-favicon"] = "node dev/icons/make-favicon.js";
// Write updated package.json
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
console.log('āļø Added make-favicon script to package.json');
} catch (error) {
console.log('ā ļø Error updating package.json:', error.message);
}
} else {
console.log('ā ļø package.json not found, cannot add make-favicon script');
}
// Step 7: Copy every folder and file from src/ to the root
await logStep(7, "Copying template files to the project");
const srcPath = path.join(__dirname, '..', 'src');
if (fs.existsSync(srcPath)) {
fs.copySync(srcPath, './', { overwrite: true });
console.log('š Template files copied to the project root');
} else {
console.log('ā ļø Template source directory not found');
}
console.log('\nā
Next.js project ready!');
} catch (error) {
console.error('ā An error occurred during initialization:', error.message);
process.exit(1);
}
}
// Run the initialization
initProject();