diff --git a/src/index.ts b/src/index.ts index 78e6c7d..797dca1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,11 +7,20 @@ import { emoji, isTTY } from './cli'; import * as help from './help'; import { getOptions } from './options'; import { gatherDetails } from './prompt'; -import { run as runSubprocess } from './subprocess'; +import { run as runSubprocess, runSilent } from './subprocess'; import { CAPACITOR_VERSION, extractTemplate } from './template'; const debug = Debug('@capacitor/create-plugin'); +const isInsideGitRepo = async (cwd: string): Promise => { + try { + await runSilent('git', ['rev-parse', '--is-inside-work-tree'], { cwd, stdio: 'ignore' }); + return true; + } catch (e) { + return false; + } +}; + process.on('unhandledRejection', (error) => { process.stderr.write(`ERR: ${error}\n`); process.exit(1); @@ -122,15 +131,21 @@ export const run = async (): Promise => { process.stderr.write(`WARN: Could not create test application: ${e.message ?? e.stack ?? e}\n`); } - process.stdout.write('Initializing git...\n'); + const isInGitRepo = await isInsideGitRepo(details.dir); - try { - await runSubprocess('git', ['init'], opts); - await runSubprocess('git', ['checkout', '-b', 'main'], opts); - await runSubprocess('git', ['add', '-A'], opts); - await runSubprocess('git', ['commit', '-m', 'Initial commit', '--no-gpg-sign'], opts); - } catch (e: any) { - process.stderr.write(`WARN: Could not initialize git: ${e.message ?? e.stack ?? e}\n`); + if (isInGitRepo) { + process.stdout.write('Skipping git initialization (already inside a git repository)...\n'); + } else { + process.stdout.write('Initializing git...\n'); + + try { + await runSubprocess('git', ['init'], opts); + await runSubprocess('git', ['checkout', '-b', 'main'], opts); + await runSubprocess('git', ['add', '-A'], opts); + await runSubprocess('git', ['commit', '-m', 'Initial commit', '--no-gpg-sign'], opts); + } catch (e: any) { + process.stderr.write(`WARN: Could not initialize git: ${e.message ?? e.stack ?? e}\n`); + } } const tada = emoji('🎉', '*'); diff --git a/src/subprocess.ts b/src/subprocess.ts index 55ab524..30853a3 100644 --- a/src/subprocess.ts +++ b/src/subprocess.ts @@ -11,6 +11,10 @@ export const run = async (cmd: string, args: readonly string[], options: cp.Spaw await wait(spawn(cmd, args, options)); }; +export const runSilent = async (cmd: string, args: readonly string[], options: cp.SpawnOptions): Promise => { + await wait(spawn(cmd, args, options)); +}; + export const wait = async (p: cp.ChildProcess): Promise => { return new Promise((resolve, reject) => { p.on('error', reject);