Skip to content

Commit

Permalink
Merge pull request #17 from tommy351/fix-types
Browse files Browse the repository at this point in the history
Update type definitions
  • Loading branch information
Brian Vaughn authored Feb 7, 2020
2 parents 68edf49 + 98f8667 commit e654f23
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
68 changes: 39 additions & 29 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
import { Chalk } from 'chalk';

export interface Spinner {
interval: number;
frames: string[];
}
declare namespace progressEstimator {
interface Spinner {
interval: number;
frames: string[];
}

export interface ChalkTheme extends Chalk {
asciiCompleted: Chalk;
asciiInProgress: Chalk;
estimate: Chalk;
estimateExceeded: Chalk;
label: Chalk;
percentage: Chalk;
progressBackground: Chalk;
progressForeground: Chalk;
}
interface ChalkTheme extends Chalk {
asciiCompleted: Chalk;
asciiInProgress: Chalk;
estimate: Chalk;
estimateExceeded: Chalk;
label: Chalk;
percentage: Chalk;
progressBackground: Chalk;
progressForeground: Chalk;
}

export interface Configuration {
spinner?: Spinner;
storagePath?: string;
theme?: ChalkTheme;
}
interface LogFunction {
(...text: string[]): void;
clear(): void;
done(): void;
}

interface Configuration {
logFunction?: LogFunction;
spinner?: Spinner;
storagePath?: string;
theme?: ChalkTheme;
}

interface LogOption {
estimate?: number;
id?: string;
}

export interface ProgressEstimator {
<T>(promise: Promise<T>, label: string, estimatedDuration?: number): Promise<
T
>;
configure(options: Configuration): void;
logProgress: ProgressEstimator;
interface ProgressEstimator {
<T>(promise: Promise<T>, label: string, options?: LogOption): Promise<T>;
}
}

export declare const configure: (options: Configuration) => void;
export declare const logProgress: ProgressEstimator;
declare function progressEstimator(
config?: progressEstimator.Configuration
): progressEstimator.ProgressEstimator;

declare const progressEstimator: ProgressEstimator;
export default progressEstimator;
export = progressEstimator;
53 changes: 30 additions & 23 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import progressEstimator, { configure, logProgress, ChalkTheme } from '..';
import createLogger, { ChalkTheme, LogFunction } from '..';

// Test promises
const stringPromise = new Promise<string>(resolve => resolve('hello'));
Expand All @@ -16,35 +16,42 @@ chalkTheme.percentage = chalkTheme;
chalkTheme.progressBackground = chalkTheme;
chalkTheme.progressForeground = chalkTheme;

// Check `logProgress`
const resultOne: Promise<string> = progressEstimator(
stringPromise,
'This promise has no initial estimate'
);
const resultTwo: Promise<number> = progressEstimator(
numberPromise,
'This promise is initially estimated to take 1 second',
1000
);
const resultThree: Promise<number> = logProgress(numberPromise, 'Valid export');
// logFunction
const logFunction: LogFunction = (...text: string[]) => {};
logFunction.done = () => {};
logFunction.clear = () => {};

// Check `configure`
configure({
spinner: { interval: 100, frames: ['.', ''] }
// Check `createLogger`
const logger = createLogger();
createLogger({
spinner: {
interval: 100,
frames: ['.', '']
}
});
configure({
createLogger({
storagePath: 'path/to/dir'
});
configure({
createLogger({
theme: chalkTheme
});
configure({
spinner: { interval: 100, frames: ['.', ''] },
storagePath: 'path/to/dir',
theme: chalkTheme
createLogger({
logFunction: logFunction
});
progressEstimator.configure({
createLogger({
spinner: { interval: 100, frames: ['.', ''] },
storagePath: 'path/to/dir',
theme: chalkTheme
theme: chalkTheme,
logFunction: logFunction
});

const resultOne: Promise<string> = logger(
stringPromise,
'This promise has no initial estimate'
);
const resultTwo: Promise<number> = logger(
numberPromise,
'This promise is initially estimated to take 1 second',
{ estimate: 1000 }
);
const resultThree: Promise<number> = logger(numberPromise, 'Valid export');
3 changes: 1 addition & 2 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": ["es2015"],
"noImplicitAny": true,
"noUnusedLocals": false,
"noEmit": true,
"allowJs": true
"esModuleInterop": true
}
}

0 comments on commit e654f23

Please # to comment.