tw-runner
Version:
Run and manage Tower Website projects with this utility.
86 lines (71 loc) • 2.42 kB
JavaScript
import inquirer from 'inquirer';
import chalk from 'chalk';
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
const red = chalk.red;
const green = chalk.green;
const blue = chalk.blue;
// Main execution
async function run() {
try {
// Check if we're in a git repo
if (!fs.existsSync('.git')) {
console.log(red('Error: Not a git repository'));
process.exit(1);
}
// Get git status
const branch = execSync('git branch --show-current').toString().trim();
const master = process.argv[2] === 'master';
const branches = execSync('git branch', { encoding: 'utf8' }).split('\n')
.map(b => b.replace('*', '').trim());
if (!branches.includes('staging')) {
console.log(blue('Branch "staging" does not exist. Creating it...'));
execSync('git checkout -b staging', { stdio: 'inherit' });
} else {
execSync('git checkout staging', { stdio: 'inherit' });
}
if (master) {
console.log(blue(`Switching to master branch: ${prod}`));
execSync(`git checkout master`, { stdio: 'inherit' });
}
const status = execSync('git status --porcelain').toString();
if (!status) {
console.log(blue('No changes to commit'));
return;
}
console.log(blue('Changed files:'));
console.log(status);
// Prompt to confirm commit
const { confirmCommit } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmCommit',
message: `You have changes in branch "${branch}". Do you want to commit these changes?`,
default: true
}
]);
if (!confirmCommit) {
console.log(blue('No changes committed.'));
return;
}
// Prompt for commit message
const { commitMessage } = await inquirer.prompt([
{
type: 'input',
name: 'commitMessage',
message: 'Enter commit message:',
validate: input => input.trim() ? true : 'Commit message cannot be empty'
}
]);
execSync('git add .', { stdio: 'inherit' });
execSync(`git commit -m "${commitMessage}"`, { stdio: 'inherit' });
console.log(blue(`Pushing to ${branch}...`));
execSync(`git push origin ${branch}`, { stdio: 'inherit' });
console.log(green('✓ Successfully pushed changes'));
} catch (error) {
console.log(red(`✗ Error: ${error.message}`));
process.exit(1);
}
}
run();