feat(cpa): initialize git repo on project creation (#6342)

This commit is contained in:
Elliot DeNolf
2024-05-13 14:59:39 -04:00
committed by GitHub
parent 662334abfb
commit 40d3078bf2
7 changed files with 71 additions and 8 deletions

View File

@@ -55,6 +55,7 @@
"@types/esprima": "^4.0.6",
"@types/fs-extra": "^9.0.12",
"@types/jest": "^27.0.3",
"@types/node": "20.12.5"
"@types/node": "20.12.5",
"temp-dir": "2.0.0"
}
}

View File

@@ -2,25 +2,22 @@ import fse from 'fs-extra'
import path from 'path'
import type { CliArgs, DbType, ProjectTemplate } from '../types.js'
import { createProject } from './create-project.js'
import { fileURLToPath } from 'node:url'
import { dbReplacements } from './packages.js'
import { getValidTemplates } from './templates.js'
import globby from 'globby'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
import tempDirectory from 'temp-dir'
const projectDir = path.resolve(dirname, './tmp')
describe('createProject', () => {
let projectDir: string
beforeAll(() => {
console.log = jest.fn()
})
beforeEach(() => {
if (fse.existsSync(projectDir)) {
fse.rmdirSync(projectDir, { recursive: true })
}
projectDir = `${tempDirectory}/${Math.random().toString(36).substring(7)}`
})
afterEach(() => {
if (fse.existsSync(projectDir)) {
fse.rmSync(projectDir, { recursive: true })
@@ -100,6 +97,9 @@ describe('createProject', () => {
const packageJsonPath = path.resolve(projectDir, 'package.json')
const packageJson = fse.readJsonSync(packageJsonPath)
// Verify git was initialized
expect(fse.existsSync(path.resolve(projectDir, '.git'))).toBe(true)
// Should only have one db adapter
expect(
Object.keys(packageJson.dependencies).filter((n) => n.startsWith('@payloadcms/db-')),

View File

@@ -8,6 +8,7 @@ import path from 'path'
import type { CliArgs, DbDetails, PackageManager, ProjectTemplate } from '../types.js'
import { tryInitRepoAndCommit } from '../utils/git.js'
import { debug, error, warning } from '../utils/log.js'
import { configurePayloadConfig } from './configure-payload-config.js'
@@ -108,6 +109,10 @@ export async function createProject(args: {
} else {
spinner.stop('Dependency installation skipped')
}
if (!cliArgs['--no-git']) {
tryInitRepoAndCommit({ cwd: projectDir })
}
}
export async function updatePackageJSON(args: {

View File

@@ -53,6 +53,9 @@ export class Main {
'--use-pnpm': Boolean,
'--use-yarn': Boolean,
// Other
'--no-git': Boolean,
// Flags
'--beta': Boolean,
'--debug': Boolean,

View File

@@ -12,6 +12,7 @@ export interface Args extends arg.Spec {
'--local-template': StringConstructor
'--name': StringConstructor
'--no-deps': BooleanConstructor
'--no-git': BooleanConstructor
'--secret': StringConstructor
'--template': StringConstructor
'--template-branch': StringConstructor

View File

@@ -0,0 +1,50 @@
import type { ExecSyncOptions } from 'child_process'
import { execSync } from 'child_process'
import { warning } from './log.js'
export function tryInitRepoAndCommit(args: { cwd: string }): void {
const execOpts: ExecSyncOptions = { cwd: args.cwd, stdio: 'ignore' }
try {
// Check if git is available
execSync('git -v', execOpts)
// Do nothing if already in a git repo
if (isGitRepo(execOpts)) {
return
}
// Initialize
execSync('git init', execOpts)
if (!ensureHasDefaultBranch(execOpts)) {
execSync('git checkout -b main', execOpts)
}
// Add and commit files
execSync('git add -A', execOpts)
execSync('git commit -m "feat: initial commit"', execOpts)
} catch (_) {
warning('Failed to initialize git repository.')
}
}
function isGitRepo(opts: ExecSyncOptions): boolean {
try {
execSync('git rev-parse --is-inside-work-tree', opts)
return true
} catch (_) {
// Ignore errors
}
return false
}
function ensureHasDefaultBranch(opts: ExecSyncOptions): boolean {
try {
execSync(`git config init.defaultBranch`, opts)
return true
} catch (_) {
// Ignore errros
}
return false
}

3
pnpm-lock.yaml generated
View File

@@ -328,6 +328,9 @@ importers:
'@types/node':
specifier: 20.12.5
version: 20.12.5
temp-dir:
specifier: 2.0.0
version: 2.0.0
packages/db-mongodb:
dependencies: