Skip to content

Commit

Permalink
(fix/refactor): rewrite some overbroad try/catches
Browse files Browse the repository at this point in the history
- so there's less silent failures that occur but don't error

- replace overbroad try/catch in getProjectPath with just an
  fs.pathExists

- replace overbroad try/catch in cleanDistFolder with just an fs.remove
  - fs.remove is like rimraf and `rm -rf` in that it won't error if the
    file/dir doesn't exist
    - if it does error, it's probably because it *failed* to remove the
      dir, and that should error, because it's potentially a problem,
      especially if you're publishing right after

- rewrite moveTypes() so it doesn't have an overbroad try/catch
  - use fs.pathExists first and early return if it doesn't exist
  - this way if copy or remove actually fail, they will actually error
    - before they would silently fail, which could similarly be pretty
      bad if one were to publish right after a silent failure
  • Loading branch information
agilgur5 committed Feb 7, 2020
1 parent b3632fe commit d643b7c
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import {
} from 'rollup';
import asyncro from 'asyncro';
import chalk from 'chalk';
import util from 'util';
import * as fs from 'fs-extra';
import jest from 'jest';
import { CLIEngine } from 'eslint';
import logError from './logError';
import path from 'path';
import rimraf from 'rimraf';
import execa from 'execa';
import shell from 'shelljs';
import ora from 'ora';
Expand Down Expand Up @@ -102,13 +100,16 @@ async function getInputs(
}

async function moveTypes() {
try {
// Move the typescript types to the base of the ./dist folder
await fs.copy(paths.appDist + '/src', paths.appDist, {
overwrite: true,
});
await fs.remove(paths.appDist + '/src');
} catch (e) {}
const appDistSrc = paths.appDist + '/src';

const pathExists = await fs.pathExists(appDistSrc);
if (!pathExists) return;

// Move the typescript types to the base of the ./dist folder
await fs.copy(appDistSrc, paths.appDist, {
overwrite: true,
});
await fs.remove(appDistSrc);
}

prog
Expand Down Expand Up @@ -140,16 +141,11 @@ prog
// Helper fn to prompt the user for a different
// folder name if one already exists
async function getProjectPath(projectPath: string): Promise<string> {
let exists = true;
try {
// will throw an exception if it does not exists
await util.promisify(fs.access)(projectPath);
} catch {
exists = false;
}
const exists = await fs.pathExists(projectPath);
if (!exists) {
return projectPath;
}

bootSpinner.fail(`Failed to create ${chalk.bold.red(pkg)}`);
const prompt = new Input({
message: `A folder named ${chalk.bold.red(
Expand All @@ -158,6 +154,7 @@ prog
initial: pkg + '-1',
result: (v: string) => v.trim(),
});

pkg = await prompt.run();
projectPath = (await fs.realpath(process.cwd())) + '/' + pkg;
bootSpinner.start(`Creating ${chalk.bold.green(pkg)}...`);
Expand Down Expand Up @@ -453,14 +450,7 @@ async function normalizeOpts(opts: WatchOpts): Promise<NormalizedOpts> {
}

async function cleanDistFolder() {
try {
await util.promisify(fs.access)(paths.appDist);
return util.promisify(rimraf)(paths.appDist);
} catch {
// if an exception is throw, the files does not exists or it is not visible
// either way, we just return
return;
}
await fs.remove(paths.appDist);
}

function writeCjsEntryFile(name: string) {
Expand Down

0 comments on commit d643b7c

Please sign in to comment.