🎥 Make videos programmatically with React
disallow creating a remotion project if already inside a repo
Jonny Burger3a0f8df6bd1f
diff --git a/packages/create-video/src/init.ts b/packages/create-video/src/init.tsindex 09d86f894e..9596706271 100644--- a/packages/create-video/src/init.ts+++ b/packages/create-video/src/init.ts@@ -15,19 +15,33 @@ import {import {resolveProjectRoot} from './resolve-project-root';import {selectTemplate} from './select-template';-const isGitExecutableAvailable = async () => {+export const checkGitAvailability = async (+ cwd: string,+ command: string+): Promise<+ | {type: 'no-git-repo'}+ | {type: 'is-git-repo'; location: string}+ | {type: 'git-not-installed'}+> => {try {- await execa('git', ['--version']);- return true;+ const result = await execa(command, ['rev-parse', '--show-toplevel'], {+ cwd,+ });+ return {type: 'is-git-repo', location: result.stdout};} catch (e) {- if ((e as {errno: string}).errno === 'ENOENT') {- Log.warn('Unable to find `git` command. `git` not in PATH.');- return false;+ if ((e as Error).message.includes('not a git repository')) {+ return {type: 'no-git-repo'};+ }++ if ((e as {code: string}).code === 'ENOENT') {+ return {type: 'git-not-installed'};}++ throw e;}};-const initGitRepoAsync = async (root: string): Promise<void> => {+const getGitStatus = async (root: string): Promise<void> => {// not in git tree, so let's inittry {await execa('git', ['init'], {cwd: root});@@ -48,8 +62,22 @@ const initGitRepoAsync = async (root: string): Promise<void> => {};export const init = async () => {+ const result = await checkGitAvailability(process.cwd(), 'git');+ if (result.type === 'git-not-installed') {+ Log.error(+ 'Git is not installed or not in the path. Install Git to continue.'+ );+ process.exit(1);+ }++ if (result.type === 'is-git-repo') {+ Log.error(`You are already inside a Git repo (${result.location}).`);+ Log.error('Create a Remotion project somewhere else.');+ process.exit(1);+ }+const [projectRoot, folderName] = await resolveProjectRoot();- await isGitExecutableAvailable();+const latestRemotionVersionPromise = getLatestRemotionVersion();const selectedTemplate = await selectTemplate();@@ -117,7 +145,7 @@ export const init = async () => {await promise;}- await initGitRepoAsync(projectRoot);+ await getGitStatus(projectRoot);Log.info();Log.info(`Welcome to ${chalk.blueBright('Remotion')}!`);diff --git a/packages/create-video/src/test/git-status.test.ts b/packages/create-video/src/test/git-status.test.tsnew file mode 100644index 0000000000..f1cff1df83--- /dev/null+++ b/packages/create-video/src/test/git-status.test.ts@@ -0,0 +1,27 @@+import path from 'path';+import {expect, test} from 'vitest';+import {checkGitAvailability} from '../init';++test('Get git status', async () => {+ const status = await checkGitAvailability(process.cwd(), 'git');+ expect(status).toEqual({+ type: 'is-git-repo',+ location: path.join(__dirname, '../../../..'),+ });++ if (status.type !== 'is-git-repo') {+ throw new Error('is git repo');+ }++ const status2 = await checkGitAvailability(+ path.dirname(status.location),+ 'git'+ );+ expect(status2).toEqual({type: 'no-git-repo'});++ const status3 = await checkGitAvailability(+ path.dirname(status.location),+ 'wronggitbinary'+ );+ expect(status3).toEqual({type: 'git-not-installed'});+});