2020-05-28 10:23:10 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
2019-11-11 06:42:51 +01:00
|
|
|
import { execSync } from 'child_process'
|
|
|
|
import path from 'path'
|
|
|
|
import rimraf from 'rimraf'
|
|
|
|
|
2020-05-10 12:55:46 +02:00
|
|
|
function isInGitRepository(): boolean {
|
2019-11-11 06:42:51 +01:00
|
|
|
try {
|
|
|
|
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' })
|
|
|
|
return true
|
|
|
|
} catch (_) {}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:55:46 +02:00
|
|
|
function isInMercurialRepository(): boolean {
|
2019-11-11 06:42:51 +01:00
|
|
|
try {
|
|
|
|
execSync('hg --cwd . root', { stdio: 'ignore' })
|
|
|
|
return true
|
|
|
|
} catch (_) {}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:55:46 +02:00
|
|
|
export function tryGitInit(root: string): boolean {
|
2019-11-11 06:42:51 +01:00
|
|
|
let didInit = false
|
|
|
|
try {
|
|
|
|
execSync('git --version', { stdio: 'ignore' })
|
|
|
|
if (isInGitRepository() || isInMercurialRepository()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
execSync('git init', { stdio: 'ignore' })
|
|
|
|
didInit = true
|
|
|
|
|
2020-11-07 15:46:17 +01:00
|
|
|
execSync('git checkout -b main', { stdio: 'ignore' })
|
|
|
|
|
2019-11-11 06:42:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|