tw-runner
Version:
Run and manage Tower Website projects with this utility.
70 lines (57 loc) ⢠2.04 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import Logger from '../lib/models/logger.js';
import installGlobalDependencies from '../lib/utils/install-global-deps.js';
import buildHUGOProject from '../lib/utils/hugo-build.js';
// Main execution
async function run() {
const repoUrl = process.argv[2];
const projectName = process.argv[3] || repoUrl.split('/').pop().replace('.git', '');
const targetDir = path.resolve(projectName);
const logger = new Logger();
console.log('š± Begin cloning:', repoUrl)
if (!repoUrl) {
console.log('Usage: bean clone <repository-url> [custom-name]');
process.exit(1);
}
if (fs.existsSync(targetDir)) {
console.error(`Error: Directory "${projectName}" already exists`);
process.exit(1);
}
console.log(`š± Cloning repository: ${repoUrl}`);
try {
// install global dependencies
logger.log('Installing global dependencies...', 'info');
installGlobalDependencies();
execSync(`git clone ${repoUrl} ${projectName}`, {
stdio: 'inherit',
timeout: 60000
});
// 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());
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' });
}
console.log(`
ā
Successfully cloned repository
Next steps:
1. cd ${projectName}
2. Start working on the project
`);
} catch (error) {
console.error(`ā Error: ${error.message}`);
if (fs.existsSync(targetDir)) {
fs.rmSync(targetDir, { recursive: true, force: true });
}
process.exit(1);
}
}
run();