CLI: Support to initialize git repo

This commit is contained in:
fuma-nama
2024-08-11 23:43:16 +08:00
parent 96c9ddac06
commit 70f15af238
5 changed files with 100 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'create-fumadocs-app': patch
---
Support to initialize git repo
+18 -2
View File
@@ -1,5 +1,6 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { tryGitInit } from '@/git';
import localVersions from '../versions.json';
import versionPkg from '../../create-app-versions/package.json';
import type { PackageManager } from './auto-install';
@@ -12,11 +13,19 @@ export interface Options {
outputDir: string;
template: Template;
tailwindcss: boolean;
installDeps: boolean;
packageManager: PackageManager;
installDeps?: boolean;
initializeGit?: boolean;
log?: (message: string) => void;
}
export async function create(options: Options): Promise<void> {
const {
installDeps = true,
initializeGit = true,
log = console.log,
} = options;
const projectName = path.basename(options.outputDir);
const dest = path.resolve(cwd, options.outputDir);
await copy(path.join(sourceDir, `template/+shared`), dest, (name) => {
@@ -29,9 +38,11 @@ export async function create(options: Options): Promise<void> {
});
await copy(path.join(sourceDir, `template/${options.template}`), dest);
log('Configured Typescript');
if (options.tailwindcss) {
await copy(path.join(sourceDir, `template/+tailwindcss`), dest);
log('Configured Tailwind CSS');
}
const packageJson = createPackageJson(projectName, options);
@@ -40,8 +51,13 @@ export async function create(options: Options): Promise<void> {
const readMe = await getReadme(dest, projectName);
await fs.writeFile(path.join(dest, 'README.md'), readMe);
if (options.installDeps) {
if (installDeps) {
await autoInstall(options.packageManager, dest);
log('Installed dependencies');
}
if (initializeGit && tryGitInit(dest)) {
log('Initialized Git repository');
}
}
+71
View File
@@ -0,0 +1,71 @@
import { execSync } from 'node:child_process';
import { rmSync } from 'node:fs';
import { join } from 'node:path';
/*
Initialize a Git repo on the project.
Based on https://github.com/vercel/next.js/blob/canary/packages/create-next-app/helpers/git.ts
*/
function isInGitRepository(cwd: string): boolean {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore', cwd });
return true;
} catch (_) {
return false;
}
}
function isInMercurialRepository(cwd: string): boolean {
try {
execSync('hg --cwd . root', { stdio: 'ignore', cwd });
return true;
} catch (_) {
return false;
}
}
function isDefaultBranchSet(cwd: string): boolean {
try {
execSync('git config init.defaultBranch', { stdio: 'ignore', cwd });
return true;
} catch (_) {
return false;
}
}
export function tryGitInit(root: string): boolean {
let didInit = false;
try {
execSync('git --version', { stdio: 'ignore' });
if (isInGitRepository(root) || isInMercurialRepository(root)) {
return false;
}
execSync('git init', { stdio: 'ignore', cwd: root });
didInit = true;
if (!isDefaultBranchSet(root)) {
execSync('git checkout -b main', { stdio: 'ignore', cwd: root });
}
execSync('git add -A', { stdio: 'ignore', cwd: root });
execSync('git commit -m "Initial commit from Create Fumadocs App"', {
stdio: 'ignore',
cwd: root,
});
return true;
} catch (e) {
if (didInit) {
try {
rmSync(join(root, '.git'), { recursive: true, force: true });
} catch (_) {
// do nothing
}
}
return false;
}
}
+5 -6
View File
@@ -8,6 +8,7 @@ import {
group,
intro,
isCancel,
log,
outro,
select,
spinner,
@@ -89,18 +90,16 @@ async function main(): Promise<void> {
template: options.template,
outputDir: dest,
installDeps: options.installDeps,
log: (message) => {
log.info(message);
},
});
info.stop('Project Generated');
outro(pc.bgGreen(pc.bold('Done')));
if (options.tailwindcss) {
console.log('✔ Tailwind CSS');
}
console.log('✔ Typescript');
console.log(pc.bold('\nOpen the project'));
console.log(pc.cyan(`cd ${projectName}`));
+1 -1
View File
@@ -1 +1 @@
{"fumadocs-core":"12.5.6","fumadocs-ui":"12.5.6","fumadocs-mdx":"8.2.34","@fumadocs/content-collections":"1.1.0"}
{"fumadocs-core":"13.1.0","fumadocs-ui":"13.1.0","fumadocs-mdx":"9.0.0","@fumadocs/content-collections":"1.1.0"}