@craftnotion/init-project
Version:
A CLI tool to initialize a new project with AdonisJS, NextJS, NestJS, React Native, Strapi, TypeScript, Husky, Git-CZ and more.
172 lines (171 loc) • 7.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitSetup = void 0;
const inquirer_1 = __importDefault(require("inquirer"));
const child_process_1 = require("child_process");
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = __importDefault(require("fs"));
const _1 = require(".");
const path_1 = require("path");
class GitSetup {
projectName;
projectPath;
platform;
constructor(projectName, projectPath, platform) {
this.projectName = projectName;
this.projectPath = projectPath;
this.platform = platform;
}
/**
* Checks if Git is installed on the system.
*/
static isGitInstalled() {
try {
(0, child_process_1.execSync)('git --version', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* Checks if Git is configured with a user name and email.
*/
static isGitConfigured() {
try {
(0, child_process_1.execSync)('git config --get user.name');
(0, child_process_1.execSync)('git config --get user.email');
return true;
}
catch {
return false;
}
}
/**
* Prompts the user for Git configuration details (username and email),
* then sets them globally.
*/
static async configureGit() {
const answers = await inquirer_1.default.prompt([
{
type: 'input',
name: 'name',
message: 'Enter your Git username:',
validate: (input) => input.trim() !== '',
},
{
type: 'input',
name: 'email',
message: 'Enter your Git email:',
validate: (input) => /\S+@\S+\.\S+/.test(input),
},
]);
(0, child_process_1.execSync)(`git config --global user.name "${answers.name}"`);
(0, child_process_1.execSync)(`git config --global user.email "${answers.email}"`);
console.log(chalk_1.default.blue('\nGit configuration updated.'));
}
/**
* Checks if `git-cz` (Commitizen) is installed.
*/
static isGitCzInstalled() {
try {
(0, child_process_1.execSync)('git-cz --version', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* Installs and sets up Husky & Commitizen (if necessary),
* and copies relevant templates for standardized commits.
*/
async standardiseCommits() {
if (!GitSetup.isGitCzInstalled()) {
console.log(chalk_1.default.yellow('\nCommitizen is not installed. Installing globally...'));
// Install commitizen (version unspecified) as a dev dependency
await (0, _1.updatePkg)(this.projectPath, 'devDependencies', { commitizen: '' });
}
// Example: ensures chalk is in devDependencies
await (0, _1.updatePkg)(this.projectPath, 'devDependencies', { chalk: '^4.1.2' });
console.log(chalk_1.default.green('\nCommitizen installed successfully!'));
// Initialize Husky for Git hooks
(0, child_process_1.execSync)('npx husky-init', { stdio: 'inherit', cwd: this.projectPath });
(0, child_process_1.execSync)('chmod ug+x .husky/*', { stdio: 'inherit', cwd: this.projectPath });
console.log(chalk_1.default.green(`\nHusky and commit message template added successfully to ${this.projectName}!`));
console.log(chalk_1.default.green(`\nCopying the templates...`));
await this.copyTemplates();
}
/**
* Copies commit message templates, validation scripts, and changelog configs.
* Also removes the default pre-commit hook if present.
*/
async copyTemplates() {
const preCommitPath = (0, path_1.join)(this.projectPath, '.husky', 'pre-commit');
if (fs_1.default.existsSync(preCommitPath)) {
fs_1.default.unlinkSync(preCommitPath);
}
// Dynamically load the correct commit-msg template
const commitMsgFile = (0, path_1.join)(__dirname, `../templates/commit-msg${this.platform === 'react-native' ? '-react-native' : ''}.txt`);
const commitMessage = fs_1.default.readFileSync(commitMsgFile, 'utf-8');
fs_1.default.writeFileSync((0, path_1.join)(this.projectPath, '.husky', 'commit-msg'), commitMessage, 'utf-8');
// Copy validate script
const validatePath = (0, path_1.join)(__dirname, '../templates/validate.txt');
const validateScript = fs_1.default.readFileSync(validatePath, 'utf-8');
fs_1.default.writeFileSync((0, path_1.join)(this.projectPath, 'validate.js'), validateScript, 'utf-8');
// Copy changelog config
const changelogConfigPath = (0, path_1.join)(__dirname, '../templates/changelog.config.txt');
const changeLogConfig = fs_1.default.readFileSync(changelogConfigPath, 'utf-8');
fs_1.default.writeFileSync((0, path_1.join)(this.projectPath, 'changelog.config.cjs'), changeLogConfig, 'utf-8');
// Make Husky scripts executable on non-Windows systems
if (process.platform !== 'win32') {
(0, child_process_1.execSync)('chmod ug+x .husky/*', { stdio: 'inherit', cwd: this.projectPath });
}
}
/**
* Prompts the user to optionally initialize Git and standardize commit messages.
* If Git is not installed or configured, it helps the user set up Git configuration.
*/
async setupGit() {
// Check if Git is installed
if (!GitSetup.isGitInstalled()) {
console.log(chalk_1.default.red.bold('\nGit is not installed. Please install Git and try again.'));
return false;
}
// Check if Git is configured, if not, configure it
if (!GitSetup.isGitConfigured()) {
console.log(chalk_1.default.blue('Git is not configured. Please enter details to configure Git.'));
await GitSetup.configureGit();
}
// Ask user if they want to initialize Git and set up Husky
const { git, husky } = await inquirer_1.default.prompt([
{
type: 'confirm',
name: 'git',
message: 'Do you want to initialise git?',
default: false,
},
{
type: 'confirm',
name: 'husky',
message: 'Do you want to standardise commit messages?',
default: false,
when: (answers) => answers.git === true,
},
]);
// Initialize Git repository
if (git) {
console.log(chalk_1.default.green(`\nInitializing Git in ${this.projectPath}`));
(0, child_process_1.execSync)('git init', { stdio: 'inherit', cwd: this.projectPath });
}
// Standardize commit messages if chosen
if (husky) {
await this.standardiseCommits();
}
return true;
}
}
exports.GitSetup = GitSetup;