Skip to content

Commit f2e2b6d

Browse files
committed
Feat: add types to typechooser
1 parent 66f97ce commit f2e2b6d

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

lib/process/process.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,36 @@ import defaults from './defaults';
2121

2222
const { fileExt, availableTypes, optionsDefault } = defaults;
2323

24-
interface BaseOptions {
24+
export interface BaseOptions {
2525
cwd?: string;
2626
newPath?: string;
2727
overwrite?: boolean;
2828
}
2929

30+
export interface AllOptions {
31+
pug: BaseOptions & ReplacePugOptions;
32+
any: BaseOptions & ReplaceStringOptions;
33+
js: BaseOptions & ReplaceJsOptions;
34+
html: BaseOptions & FillLibrariesOptions & ReplaceHtmlOptions;
35+
css: BaseOptions & FillLibrariesOptions & ReplaceCssOptions;
36+
auto: (
37+
& BaseOptions
38+
& FillLibrariesOptions
39+
& ReplaceCssOptions
40+
& ReplacePugOptions
41+
& ReplaceHtmlOptions
42+
& ReplaceStringOptions
43+
& ReplaceJsOptions
44+
);
45+
}
46+
3047
export type Options =
31-
| BaseOptions & ReplacePugOptions & { type: 'pug' }
32-
| BaseOptions & ReplaceStringOptions & { type: 'any' }
33-
| BaseOptions & ReplaceJsOptions & { type: 'js' }
34-
| BaseOptions & FillLibrariesOptions & ReplaceHtmlOptions & { type: 'html' }
35-
| BaseOptions & FillLibrariesOptions & ReplaceCssOptions & { type: 'css' }
36-
| BaseOptions & FillLibrariesOptions & ReplaceCssOptions & ReplacePugOptions & ReplaceHtmlOptions & ReplaceStringOptions & ReplaceJsOptions & { type: 'auto' }
48+
| AllOptions['pug'] & { type: 'pug' }
49+
| AllOptions['any'] & { type: 'any' }
50+
| AllOptions['js'] & { type: 'js' }
51+
| AllOptions['html'] & { type: 'html' }
52+
| AllOptions['css'] & { type: 'css' }
53+
| AllOptions['auto'] & { type: 'auto' }
3754

3855
const rcsProcess = (pathString: string, opts: Options | Callback, cb?: Callback): void => {
3956
let globString = pathString;
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { fromCallback } from 'universalify';
2-
import rcsProcess from '../process';
2+
import rcsProcess, { AllOptions } from '../process';
33

4-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
5-
const typeChooser = (processType: 'css' | 'js' | 'html' | 'pug' | 'any' | 'auto') => (
6-
fromCallback((pathString, opts, cb) => {
4+
function typeChooser(processType: 'pug'): (pathString: string, opts: AllOptions['html']) => Promise<void>;
5+
function typeChooser(processType: 'any'): (pathString: string, opts: AllOptions['any']) => Promise<void>;
6+
function typeChooser(processType: 'js'): (pathString: string, opts: AllOptions['js']) => Promise<void>;
7+
function typeChooser(processType: 'html'): (pathString: string, opts: AllOptions['html']) => Promise<void>;
8+
function typeChooser(processType: 'css'): (pathString: string, opts: AllOptions['css']) => Promise<void>;
9+
function typeChooser(processType: 'auto'): (pathString: string, opts: AllOptions['auto']) => Promise<void>;
10+
function typeChooser(processType: any): any {
11+
return fromCallback((pathString, opts, cb) => {
712
let callback = cb;
813
let options = opts;
914

@@ -16,7 +21,7 @@ const typeChooser = (processType: 'css' | 'js' | 'html' | 'pug' | 'any' | 'auto'
1621
options.type = processType;
1722

1823
return rcsProcess(pathString, options, callback);
19-
})
20-
); // /typeChooser
24+
});
25+
} // /typeChooser
2126

2227
export default typeChooser;
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import rcsProcessSync from '../processSync';
2+
import { AllOptions } from '../process';
23

3-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4-
const typeChooserSync = (processType: 'css' | 'js' | 'html' | 'pug' | 'any' | 'auto') => (
5-
(pathString: string, opts: any) => {
4+
function typeChooserSync(processType: 'pug'): (pathString: string, opts: AllOptions['html']) => void;
5+
function typeChooserSync(processType: 'any'): (pathString: string, opts: AllOptions['any']) => void;
6+
function typeChooserSync(processType: 'js'): (pathString: string, opts: AllOptions['js']) => void;
7+
function typeChooserSync(processType: 'html'): (pathString: string, opts: AllOptions['html']) => void;
8+
function typeChooserSync(processType: 'css'): (pathString: string, opts: AllOptions['css']) => void;
9+
function typeChooserSync(processType: 'auto'): (pathString: string, opts: AllOptions['auto']) => void;
10+
function typeChooserSync(processType: any): any {
11+
return (pathString: string, opts: any) => {
612
const options = opts || {};
713
// set the type for process
8-
options.type = processType;
14+
(options as any).type = processType;
915

10-
return rcsProcessSync(pathString, options);
11-
}
12-
); // /typeChooserSync
16+
rcsProcessSync(pathString, options as any);
17+
};
18+
} // /typeChooserSync
1319

1420
export default typeChooserSync;

0 commit comments

Comments
 (0)