Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update type definitions #17

Merged
merged 1 commit into from
Feb 7, 2020
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
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
}
}