Skip to content

Commit

Permalink
feat(cpa): initialize git repo on project creation (#6342)
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed May 13, 2024
1 parent 662334a commit 40d3078
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/create-payload-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
14 changes: 7 additions & 7 deletions packages/create-payload-app/src/lib/create-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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-')),
Expand Down
5 changes: 5 additions & 0 deletions packages/create-payload-app/src/lib/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions packages/create-payload-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class Main {
'--use-pnpm': Boolean,
'--use-yarn': Boolean,

// Other
'--no-git': Boolean,

// Flags
'--beta': Boolean,
'--debug': Boolean,
Expand Down
1 change: 1 addition & 0 deletions packages/create-payload-app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions packages/create-payload-app/src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 40d3078

Please sign in to comment.