Skip to content

Commit cfb2caa

Browse files
committed
chore(async): replace promises with async/await
1 parent 09ec8b3 commit cfb2caa

File tree

5 files changed

+109
-92
lines changed

5 files changed

+109
-92
lines changed

.eslintrc.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ module.exports = {
33
root: true,
44
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
55
parser: "@typescript-eslint/parser",
6-
plugins: ["@typescript-eslint"],
6+
plugins: ["@typescript-eslint", "promise"],
77
ignorePatterns: ["dist/**"],
88
overrides: [
99
{
1010
files: ["*.ts", "*.tsx"],
11-
1211
extends: [
1312
"plugin:@typescript-eslint/recommended-requiring-type-checking",
1413
],
15-
1614
parserOptions: {
1715
project: ["./tsconfig.json"],
1816
},
1917
},
2018
],
19+
rules: {
20+
"promise/prefer-await-to-then": "error",
21+
},
2122
};

lib/core/list-different.ts

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,49 @@ export const listDifferent = async (
1515
const files = listFilesAndPerformSanityChecks(pattern, options);
1616

1717
// Wait for all the files to be checked.
18-
await Promise.all(files.map((file) => checkFile(file, options))).then(
19-
(results) => {
20-
results.includes(false) && process.exit(1);
21-
}
18+
const validChecks = await Promise.all(
19+
files.map((file) => checkFile(file, options))
2220
);
21+
if (validChecks.includes(false)) {
22+
process.exit(1);
23+
}
2324
};
2425

25-
export const checkFile = (
26+
export const checkFile = async (
2627
file: string,
2728
options: ConfigOptions
2829
): Promise<boolean> => {
29-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
30-
return new Promise((resolve) =>
31-
fileToClassNames(file, options)
32-
.then(async (classNames) => {
33-
const typeDefinition = await classNamesToTypeDefinitions({
34-
classNames: classNames,
35-
...options,
36-
});
37-
38-
if (!typeDefinition) {
39-
// Assume if no type defs are necessary it's fine
40-
resolve(true);
41-
return;
42-
}
43-
44-
const path = getTypeDefinitionPath(file, options);
45-
46-
if (!fs.existsSync(path)) {
47-
alerts.error(
48-
`[INVALID TYPES] Type file needs to be generated for ${file} `
49-
);
50-
resolve(false);
51-
return;
52-
}
30+
try {
31+
const classNames = await fileToClassNames(file, options);
32+
const typeDefinition = await classNamesToTypeDefinitions({
33+
classNames: classNames,
34+
...options,
35+
});
36+
37+
if (!typeDefinition) {
38+
// Assume if no type defs are necessary it's fine
39+
return true;
40+
}
5341

54-
const content = fs.readFileSync(path, { encoding: "utf8" });
42+
const path = getTypeDefinitionPath(file, options);
43+
if (!fs.existsSync(path)) {
44+
alerts.error(
45+
`[INVALID TYPES] Type file needs to be generated for ${file} `
46+
);
47+
return false;
48+
}
5549

56-
if (content !== typeDefinition) {
57-
alerts.error(`[INVALID TYPES] Check type definitions for ${file}`);
58-
resolve(false);
59-
return;
60-
}
50+
const content = fs.readFileSync(path, { encoding: "utf8" });
51+
if (content !== typeDefinition) {
52+
alerts.error(`[INVALID TYPES] Check type definitions for ${file}`);
53+
return false;
54+
}
6155

62-
resolve(true);
63-
})
64-
.catch((error) => {
65-
alerts.error(
66-
`An error occurred checking ${file}:\n${JSON.stringify(error)}`
67-
);
68-
resolve(false);
69-
})
70-
);
56+
return true;
57+
} catch (error) {
58+
alerts.error(
59+
`An error occurred checking ${file}:\n${JSON.stringify(error)}`
60+
);
61+
return false;
62+
}
7163
};

lib/core/write-file.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,61 @@ import { CLIOptions } from "./types";
1616
* @param file the SCSS file to generate types for
1717
* @param options the CLI options
1818
*/
19-
export const writeFile = (file: string, options: CLIOptions): Promise<void> => {
20-
return fileToClassNames(file, options)
21-
.then(async (classNames) => {
22-
const typeDefinition = await classNamesToTypeDefinitions({
23-
classNames,
24-
...options,
25-
});
19+
export const writeFile = async (
20+
file: string,
21+
options: CLIOptions
22+
): Promise<void> => {
23+
try {
24+
const classNames = await fileToClassNames(file, options);
25+
const typeDefinition = await classNamesToTypeDefinitions({
26+
classNames,
27+
...options,
28+
});
2629

27-
const typesPath = getTypeDefinitionPath(file, options);
28-
const typesExist = fs.existsSync(typesPath);
30+
const typesPath = getTypeDefinitionPath(file, options);
31+
const typesExist = fs.existsSync(typesPath);
2932

30-
// Avoid outputting empty type definition files.
31-
// If the file exists and the type definition is now empty, remove the file.
32-
if (!typeDefinition) {
33-
if (typesExist) {
34-
removeSCSSTypeDefinitionFile(file, options);
35-
} else {
36-
alerts.notice(`[NO GENERATED TYPES] ${file}`);
37-
}
38-
return;
33+
// Avoid outputting empty type definition files.
34+
// If the file exists and the type definition is now empty, remove the file.
35+
if (!typeDefinition) {
36+
if (typesExist) {
37+
removeSCSSTypeDefinitionFile(file, options);
38+
} else {
39+
alerts.notice(`[NO GENERATED TYPES] ${file}`);
3940
}
41+
return;
42+
}
4043

41-
// Avoid re-writing the file if it hasn't changed.
42-
// First by checking the file modification time, then
43-
// by comparing the file contents.
44-
if (options.updateStaleOnly && typesExist) {
45-
const fileModified = fs.statSync(file).mtime;
46-
const typeDefinitionModified = fs.statSync(typesPath).mtime;
44+
// Avoid re-writing the file if it hasn't changed.
45+
// First by checking the file modification time, then
46+
// by comparing the file contents.
47+
if (options.updateStaleOnly && typesExist) {
48+
const fileModified = fs.statSync(file).mtime;
49+
const typeDefinitionModified = fs.statSync(typesPath).mtime;
4750

48-
if (fileModified < typeDefinitionModified) {
49-
return;
50-
}
51-
52-
const existingTypeDefinition = fs.readFileSync(typesPath, "utf8");
53-
if (existingTypeDefinition === typeDefinition) {
54-
return;
55-
}
51+
if (fileModified < typeDefinitionModified) {
52+
return;
5653
}
5754

58-
// Files can be written to arbitrary directories and need to
59-
// be nested to match the project structure so it's possible
60-
// there are multiple directories that need to be created.
61-
const dirname = path.dirname(typesPath);
62-
if (!fs.existsSync(dirname)) {
63-
fs.mkdirSync(dirname, { recursive: true });
55+
const existingTypeDefinition = fs.readFileSync(typesPath, "utf8");
56+
if (existingTypeDefinition === typeDefinition) {
57+
return;
6458
}
59+
}
6560

66-
fs.writeFileSync(typesPath, typeDefinition);
67-
alerts.success(`[GENERATED TYPES] ${typesPath}`);
68-
})
69-
.catch(({ message, file, line, column }: SassError) => {
70-
const location = file ? `(${file}[${line}:${column}])` : "";
71-
alerts.error(`${message} ${location}`);
72-
});
61+
// Files can be written to arbitrary directories and need to
62+
// be nested to match the project structure so it's possible
63+
// there are multiple directories that need to be created.
64+
const dirname = path.dirname(typesPath);
65+
if (!fs.existsSync(dirname)) {
66+
fs.mkdirSync(dirname, { recursive: true });
67+
}
68+
69+
fs.writeFileSync(typesPath, typeDefinition);
70+
alerts.success(`[GENERATED TYPES] ${typesPath}`);
71+
} catch (error) {
72+
const { message, file, line, column } = error as SassError;
73+
const location = file ? ` (${file}[${line}:${column}])` : "";
74+
alerts.error(`${message}${location}`);
75+
}
7376
};

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"babel-jest": "^29.6.2",
6060
"babel-plugin-transform-import-meta": "^2.2.1",
6161
"eslint": "^8.46.0",
62+
"eslint-plugin-promise": "^6.1.1",
6263
"husky": "^7.0.4",
6364
"jest": "^29.6.2",
6465
"lint-staged": "^12.3.4",

0 commit comments

Comments
 (0)