Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
clayrisser committed Nov 30, 2019
1 parent 3400a6c commit eff59eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
11 changes: 5 additions & 6 deletions src/actions/stow.ts
@@ -1,22 +1,21 @@
import ora from 'ora';
import { Options } from '../types';
import { Stow } from '../services';
import { sync } from '.';

export default async function stow(
options: Options = {},
packages: string[]
): Promise<any> {
const spinner = ora();
spinner.start(`stowing packages '${packages.join("' '")}'`);
if (options.sync) await sync(options);
try {
spinner.start(`stowing packages '${packages.join("' '")}'`);
const stow = new Stow(options);
await stow.stow(packages);
spinner.succeed(`stowed packages '${packages.join("' '")}'`);
} catch (err) {
if (err.code?.toString()?.[0] === '4') {
spinner.fail(err.message);
} else {
throw err;
}
if (options.debug) throw err;
spinner.fail(err.message);
}
}
44 changes: 24 additions & 20 deletions src/actions/sync.ts
Expand Up @@ -10,25 +10,29 @@ export default async function sync(options: Options = {}): Promise<any> {
options.dotfiles = dotfilesPath;
const spinner = ora();
const git = new Git(options);
if (await fs.pathExists(dotfilesPath)) {
spinner.start('pulling dotfiles');
await git.pull();
spinner.succeed('pulled dotfiles');
} else {
const remote = options.remote || (await git.guessRemote());
spinner.start('cloning dotfiles');
await git.clone(remote);
spinner.succeed('cloned dotfiles');
try {
if (await fs.pathExists(dotfilesPath)) {
spinner.start('pulling dotfiles');
await git.pull();
spinner.succeed('pulled dotfiles');
} else {
const remote = options.remote || (await git.guessRemote());
spinner.start('cloning dotfiles');
await git.clone(remote);
spinner.succeed('cloned dotfiles');
}
spinner.start('committing dotfiles');
const message = await git.commit();
if (message) {
spinner.succeed(`committed dotfiles with message "${message}"`);
spinner.start('pushing dotfiles');
await git.push();
spinner.succeed('pushed dotfiles');
} else {
spinner.warn('no files to commit');
}
} catch (err) {
if (options.debug) throw err;
spinner.fail(err.message);
}
spinner.start('committing dotfiles');
const message = await git.commit();
if (message) {
spinner.succeed(`committed dotfiles with message "${message}"`);
spinner.start('pushing dotfiles');
await git.push();
spinner.succeed('pushed dotfiles');
} else {
spinner.warn('no files to commit');
}
return options;
}
6 changes: 4 additions & 2 deletions src/commands/stow.ts
Expand Up @@ -11,7 +11,8 @@ export default class Stow extends Command {
debug: flags.boolean({ required: false }),
dotfiles: flags.string({ char: 'd', required: false }),
environment: flags.string({ char: 'e', required: false }),
force: flags.boolean({ char: 'f', required: false })
force: flags.boolean({ char: 'f', required: false }),
sync: flags.boolean({ char: 's', required: false })
};

static strict = false;
Expand All @@ -24,7 +25,8 @@ export default class Stow extends Command {
debug: !!flags.debug,
dotfiles: flags.dotfiles,
environment: flags.environment,
force: !!flags.force
force: !!flags.force,
sync: !!flags.sync
};
return stow(
options,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Expand Up @@ -5,4 +5,5 @@ export interface Options {
environment?: string;
force?: boolean;
remote?: string;
sync?: boolean;
}

0 comments on commit eff59eb

Please sign in to comment.