Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
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);
Expand Down Expand Up @@ -122,15 +131,21 @@ export const run = async (): Promise<void> => {
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('🎉', '*');
Expand Down
4 changes: 4 additions & 0 deletions src/subprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
await wait(spawn(cmd, args, options));
};

export const wait = async (p: cp.ChildProcess): Promise<void> => {
return new Promise<void>((resolve, reject) => {
p.on('error', reject);
Expand Down