Skip to content

Commit ae9d420

Browse files
author
Gerard Delmàs
committed
use await on own promises
1 parent dc5b4e7 commit ae9d420

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/DtsCreator.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,43 @@ export class DtsCreator {
4141
this.EOL = options.EOL || os.EOL;
4242
}
4343

44-
create(filePath: string, initialContents?: string, clearCache: boolean = false): Promise<DtsContent> {
45-
return new Promise((resolve, reject) => {
46-
let rInputPath: string;
47-
if(path.isAbsolute(filePath)) {
48-
rInputPath = path.relative(this.inputDirectory, filePath);
49-
}else{
50-
rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath));
51-
}
52-
if(clearCache) {
53-
this.loader.tokensByFile = {};
54-
}
55-
this.loader.fetch(filePath, "/", undefined, initialContents).then((res) => {
56-
if(res) {
57-
var tokens = res;
58-
var keys = Object.keys(tokens);
44+
public async create(filePath: string, initialContents?: string, clearCache: boolean = false): Promise<DtsContent> {
45+
let rInputPath: string;
46+
if(path.isAbsolute(filePath)) {
47+
rInputPath = path.relative(this.inputDirectory, filePath);
48+
}else{
49+
rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath));
50+
}
51+
if(clearCache) {
52+
this.loader.tokensByFile = {};
53+
}
5954

60-
var convertKey = this.getConvertKeyMethod(this.camelCase);
55+
const res = await this.loader.fetch(filePath, "/", undefined, initialContents);
56+
if(res) {
57+
var tokens = res;
58+
var keys = Object.keys(tokens);
6159

62-
var result = keys
63-
.map(k => convertKey(k))
64-
.map(k => 'readonly "' + k + '": string;')
60+
var convertKey = this.getConvertKeyMethod(this.camelCase);
6561

66-
var content = new DtsContent({
67-
dropExtension: this.dropExtension,
68-
rootDir: this.rootDir,
69-
searchDir: this.searchDir,
70-
outDir: this.outDir,
71-
rInputPath,
72-
rawTokenList: keys,
73-
resultList: result,
74-
EOL: this.EOL
75-
});
62+
var result = keys
63+
.map(k => convertKey(k))
64+
.map(k => 'readonly "' + k + '": string;')
7665

77-
resolve(content);
78-
}else{
79-
reject(res);
80-
}
81-
}).catch(err => reject(err));
82-
});
66+
var content = new DtsContent({
67+
dropExtension: this.dropExtension,
68+
rootDir: this.rootDir,
69+
searchDir: this.searchDir,
70+
outDir: this.outDir,
71+
rInputPath,
72+
rawTokenList: keys,
73+
resultList: result,
74+
EOL: this.EOL
75+
});
76+
77+
return content;
78+
}else{
79+
throw res;
80+
}
8381
}
8482

8583
private getConvertKeyMethod(camelCaseOption: CamelCaseOption): (str: string) => string {

src/cli.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import glob from 'glob';
66
import * as yargs from 'yargs';
77
import chalk from 'chalk';
88
import {DtsCreator} from './DtsCreator';
9+
import {DtsContent} from "./DtsContent";
910

1011
let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] <input directory>')
1112
.example('$0 src/styles', '')
@@ -24,15 +25,18 @@ let yarg = yargs.usage('Create .css.d.ts from CSS modules *.css files.\nUsage: $
2425
let argv = yarg.argv;
2526
let creator: DtsCreator;
2627

27-
function writeFile(f: string) {
28-
creator.create(f, undefined, !!argv.w)
29-
.then(content => content.writeFile())
30-
.then(content => {
28+
async function writeFile(f: string): Promise<void> {
29+
try {
30+
const content: DtsContent = await creator.create(f, undefined, !!argv.w);
31+
await content.writeFile();
32+
3133
if (!argv.s) {
3234
console.log('Wrote ' + chalk.green(content.outputFilePath));
3335
}
34-
})
35-
.catch((reason: unknown) => console.error(chalk.red('[Error] ' + reason)));
36+
}
37+
catch (error) {
38+
console.error(chalk.red('[Error] ' + error));
39+
}
3640
};
3741

3842
let main = () => {

0 commit comments

Comments
 (0)