Skip to content

Commit 4deb7e2

Browse files
committed
Replaced some sync methods for their async version
1 parent 6252b70 commit 4deb7e2

File tree

6 files changed

+140
-121
lines changed

6 files changed

+140
-121
lines changed

src/createEslintConfig.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import util from 'util';
34
import { CLIEngine } from 'eslint';
45
import { PackageJson } from './types';
56
import { getReactVersion } from './utils';
@@ -9,11 +10,11 @@ interface CreateEslintConfigArgs {
910
rootDir: string;
1011
writeFile: boolean;
1112
}
12-
export function createEslintConfig({
13+
export async function createEslintConfig({
1314
pkg,
1415
rootDir,
1516
writeFile,
16-
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
17+
}: CreateEslintConfigArgs): Promise<CLIEngine.Options['baseConfig']> {
1718
const isReactLibrary = Boolean(getReactVersion(pkg));
1819

1920
const config = {
@@ -30,24 +31,27 @@ export function createEslintConfig({
3031
},
3132
};
3233

33-
if (writeFile) {
34-
const file = path.join(rootDir, '.eslintrc.js');
35-
if (fs.existsSync(file)) {
34+
if (!writeFile) {
35+
return config;
36+
}
37+
38+
const file = path.join(rootDir, '.eslintrc.js');
39+
try {
40+
await util.promisify(fs.writeFile)(
41+
file,
42+
`module.exports = ${JSON.stringify(config, null, 2)}`,
43+
{ flag: 'wx' }
44+
);
45+
} catch (e) {
46+
if (e.code === 'EEXIST') {
3647
console.error(
3748
'Error trying to save the Eslint configuration file:',
3849
`${file} already exists.`
3950
);
4051
} else {
41-
try {
42-
fs.writeFileSync(
43-
file,
44-
`module.exports = ${JSON.stringify(config, null, 2)}`
45-
);
46-
} catch (e) {
47-
console.error(e);
48-
}
52+
console.error(e);
4953
}
50-
}
5154

52-
return config;
55+
return config;
56+
}
5357
}

src/createRollupConfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const errorCodeOpts = {
2020
// shebang cache map thing because the transform only gets run once
2121
let shebang: any = {};
2222

23-
export function createRollupConfig(opts: TsdxOptions) {
24-
const findAndRecordErrorCodes = extractErrors({
23+
export async function createRollupConfig(opts: TsdxOptions) {
24+
const findAndRecordErrorCodes = await extractErrors({
2525
...errorCodeOpts,
2626
...opts,
2727
});
@@ -88,8 +88,8 @@ export function createRollupConfig(opts: TsdxOptions) {
8888
},
8989
plugins: [
9090
!!opts.extractErrors && {
91-
transform(source: any) {
92-
findAndRecordErrorCodes(source);
91+
async transform(source: any) {
92+
await findAndRecordErrorCodes(source);
9393
return source;
9494
},
9595
},

src/errors/extractErrors.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const babylonOptions = {
2828
],
2929
};
3030

31-
export function extractErrors(opts: any) {
31+
export async function extractErrors(opts: any) {
3232
if (!opts || !('errorMapFilePath' in opts)) {
3333
throw new Error(
3434
'Missing options. Ensure you pass an object with `errorMapFilePath`.'
@@ -45,7 +45,7 @@ export function extractErrors(opts: any) {
4545
// Using `fs.readFileSync` instead of `require` here, because `require()`
4646
// calls are cached, and the cache map is not properly invalidated after
4747
// file changes.
48-
existingErrorMap = JSON.parse(fs.readFileSync(errorMapFilePath, 'utf8'));
48+
existingErrorMap = JSON.parse(await fs.readFile(errorMapFilePath, 'utf8'));
4949
} catch (e) {
5050
existingErrorMap = {};
5151
}
@@ -89,20 +89,20 @@ export function extractErrors(opts: any) {
8989
existingErrorMap[errorMsgLiteral] = '' + currentID++;
9090
}
9191

92-
function flush(cb?: any) {
92+
async function flush() {
9393
const prettyName = pascalCase(safeVariableName(opts.name));
9494
// Ensure that the ./src/errors directory exists or create it
95-
fs.ensureDirSync(paths.appErrors);
95+
await fs.ensureDir(paths.appErrors);
9696

9797
// Output messages to ./errors/codes.json
98-
fs.writeFileSync(
98+
await fs.writeFile(
9999
errorMapFilePath,
100100
JSON.stringify(invertObject(existingErrorMap), null, 2) + '\n',
101101
'utf-8'
102102
);
103103

104104
// Write the error files, unless they already exist
105-
fs.writeFileSync(
105+
await fs.writeFile(
106106
paths.appErrors + '/ErrorDev.js',
107107
`
108108
function ErrorDev(message) {
@@ -116,7 +116,7 @@ export default ErrorDev;
116116
'utf-8'
117117
);
118118

119-
fs.writeFileSync(
119+
await fs.writeFile(
120120
paths.appErrors + '/ErrorProd.js',
121121
`
122122
function ErrorProd(code) {
@@ -138,8 +138,8 @@ export default ErrorProd;
138138
);
139139
}
140140

141-
return function extractErrors(source: any) {
141+
return async function extractErrors(source: any) {
142142
transform(source);
143-
flush();
143+
await flush();
144144
};
145145
}

src/getInstallCmd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ let cmd: InstallCommand;
44

55
export type InstallCommand = 'yarn' | 'npm';
66

7-
export default function getInstallCmd(): InstallCommand {
7+
export default async function getInstallCmd(): Promise<InstallCommand> {
88
if (cmd) {
99
return cmd;
1010
}
1111

1212
try {
13-
execa.sync('yarnpkg', ['--version']);
13+
await execa('yarnpkg', ['--version']);
1414
cmd = 'yarn';
1515
} catch (e) {
1616
cmd = 'npm';

0 commit comments

Comments
 (0)