tw-runner
Version:
Run and manage Tower Website projects with this utility.
116 lines (97 loc) ⢠3.45 kB
JavaScript
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
import Logger from '../lib/models/logger.js';
import installGlobalDependencies from '../lib/utils/install-global-deps.js';
import buildHUGOProject from '../lib/utils/hugo-build.js';
// Configuration
const WEBSITE_REPO = "git@gitlab.com:tower-web/website-template.git";
const WEBSITE_REPO_HTTPS = "https://gitlab.com/tower-web/website-template.git";
// __dirname replacement for ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Core functions
function cloneTemplate(targetDir, httpsVersion = false) {
const repoUrl = httpsVersion ? WEBSITE_REPO_HTTPS : WEBSITE_REPO;
execSync(`git clone ${repoUrl} ${targetDir}`, {
stdio: 'inherit',
timeout: 60000 // 1 minute timeout
});
}
function setupProject(targetDir) {
// Remove original git history
fs.rmSync(path.join(targetDir, '.git'), {
recursive: true,
force: true
});
// Initialize fresh git repo
execSync('git init', { cwd: targetDir, stdio: 'inherit' });
execSync('git add .', { cwd: targetDir, stdio: 'inherit' });
execSync('git commit -m "Initial commit"', {
cwd: targetDir,
stdio: 'inherit'
});
// Install dependencies
execSync('npm install', {
cwd: targetDir,
stdio: 'inherit',
timeout: 120000 // 2 minute timeout
});
}
// Main execution
async function run() {
const logger = new Logger();
try {
// Argument handling
const projectName = process.argv[2];
const isHttpsVersion = process.argv[3] === '--https';
if (!projectName) {
logger.log('Usage: create-website <project-name>', 'error');
process.exit(1);
}
const targetDir = path.resolve(projectName);
logger.log(`Starting website project setup: ${projectName}`, 'info');
// Validate directory
if (fs.existsSync(targetDir)) {
logger.log(`Directory already exists: ${targetDir}`, 'error');
process.exit(1);
}
// install global dependencies
logger.log('Installing global dependencies...', 'info');
installGlobalDependencies();
// Execute workflow
logger.log('Cloning template repository...', 'info');
await cloneTemplate(targetDir, isHttpsVersion);
logger.log('Setting up new project...', 'info');
setupProject(targetDir);
// Build HUGO project
logger.log('Building HUGO project...', 'info');
await buildHUGOProject();
logger.log('Creating "staging" branch...', 'info');
const branches = execSync('git branch', { cwd: targetDir, encoding: 'utf8' }).split('\n')
.map(b => b.replace('*', '').trim());
console.log(`Available branches: ${branches.join(', ')}`);
if (!branches.includes('staging')) {
console.log('Branch "staging" does not exist. Creating it...');
execSync('git checkout -b staging', { cwd: targetDir, stdio: 'inherit' });
} else {
execSync('git checkout staging', { cwd: targetDir, stdio: 'inherit' });
}
// Success message
logger.log(`
ā
Successfully created website project!
š Next steps:
1. cd ${projectName}
2. Set your git remote: git remote add origin <your-repo-url>
3. Start development: npm run serve
`, 'success');
} catch (error) {
logger.log(`Process failed: ${error.message}`, 'error');
process.exit(1);
} finally {
logger.close();
}
}
run();