create-new-express-app
Version:
A CLI tool to create a TypeScript Express application with best practices
81 lines (80 loc) • 3.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryGitInit = tryGitInit;
const node_child_process_1 = require("node:child_process");
const node_path_1 = require("node:path");
const node_fs_1 = require("node:fs");
const picocolors_1 = __importDefault(require("picocolors"));
function isInGitRepository() {
try {
(0, node_child_process_1.execSync)("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
console.log(picocolors_1.default.green("Already in a Git repository."));
return true;
}
catch (_) { }
return false;
}
function isInMercurialRepository() {
try {
(0, node_child_process_1.execSync)("hg --cwd . root", { stdio: "ignore" });
console.log(picocolors_1.default.green("Already in a Mercurial repository."));
return true;
}
catch (_) { }
return false;
}
function isDefaultBranchSet() {
try {
(0, node_child_process_1.execSync)("git config init.defaultBranch", { stdio: "ignore" });
return true;
}
catch (_) { }
return false;
}
function tryGitInit(root) {
let didInit = false;
try {
(0, node_child_process_1.execSync)("git --version", { stdio: "ignore" });
console.log(picocolors_1.default.green("Git is available."));
if (isInGitRepository() || isInMercurialRepository()) {
return false;
}
console.log(picocolors_1.default.green("Initializing a new Git repository..."));
(0, node_child_process_1.execSync)("git init", { stdio: "ignore" });
didInit = true;
if (!isDefaultBranchSet()) {
(0, node_child_process_1.execSync)("git checkout -b main", { stdio: "ignore" });
}
// Check if .gitignore-copy exists
const gitignoreCopyPath = (0, node_path_1.join)(root, ".gitignore-copy");
const gitignorePath = (0, node_path_1.join)(root, ".gitignore");
if (!(0, node_fs_1.existsSync)(gitignoreCopyPath)) {
console.log(picocolors_1.default.red(`File ${gitignoreCopyPath} does not exist.`));
throw new Error(`File ${gitignoreCopyPath} does not exist.`);
}
// Rename .gitignore-copy to .gitignore using fs.renameSync
(0, node_fs_1.renameSync)(gitignoreCopyPath, gitignorePath);
(0, node_child_process_1.execSync)("git add -A", { stdio: "ignore" });
(0, node_child_process_1.execSync)('git commit -m "Initial commit from Create New Express App"', {
stdio: "ignore",
});
return true;
}
catch (e) {
console.log(picocolors_1.default.red("Git repo not initialized"), e);
if (didInit) {
console.log(picocolors_1.default.red("Removing .git directory..."));
try {
(0, node_fs_1.rmSync)((0, node_path_1.join)(root, ".git"), { recursive: true, force: true });
console.log(picocolors_1.default.green(".git directory removed successfully."));
}
catch (_) {
console.log(picocolors_1.default.red("Failed to remove .git directory."));
}
}
return false;
}
}