00badd4d58
* Initialize Git repo on app creation This PR initializes a Git repository after Create Next App runs. We ensure the app was not created in an already-existing Git or Mercurial repo. Failures of setup (no Git installed) do not fail the application bootstrapping. Closes #9077 (replaces) * Add test * Update index.test.js
45 lines
988 B
TypeScript
45 lines
988 B
TypeScript
import { execSync } from 'child_process'
|
|
import path from 'path'
|
|
import rimraf from 'rimraf'
|
|
|
|
function isInGitRepository() {
|
|
try {
|
|
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' })
|
|
return true
|
|
} catch (_) {}
|
|
return false
|
|
}
|
|
|
|
function isInMercurialRepository() {
|
|
try {
|
|
execSync('hg --cwd . root', { stdio: 'ignore' })
|
|
return true
|
|
} catch (_) {}
|
|
return false
|
|
}
|
|
|
|
export function tryGitInit(root: string) {
|
|
let didInit = false
|
|
try {
|
|
execSync('git --version', { stdio: 'ignore' })
|
|
if (isInGitRepository() || isInMercurialRepository()) {
|
|
return false
|
|
}
|
|
|
|
execSync('git init', { stdio: 'ignore' })
|
|
didInit = true
|
|
|
|
execSync('git add -A', { stdio: 'ignore' })
|
|
execSync('git commit -m "Initial commit from Create Next App"', {
|
|
stdio: 'ignore',
|
|
})
|
|
return true
|
|
} catch (e) {
|
|
if (didInit) {
|
|
try {
|
|
rimraf.sync(path.join(root, '.git'))
|
|
} catch (_) {}
|
|
}
|
|
return false
|
|
}
|
|
}
|