Skip to content

Commit

Permalink
Replaced some sync methods for their async version (#291)
Browse files Browse the repository at this point in the history
Replaced some sync methods for their async version
  • Loading branch information
swyxio authored Dec 4, 2019
2 parents 5aa5eda + 4deb7e2 commit c5f65bc
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 121 deletions.
34 changes: 19 additions & 15 deletions src/createEslintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import util from 'util';
import { CLIEngine } from 'eslint';
import { PackageJson } from './types';
import { getReactVersion } from './utils';
Expand All @@ -9,11 +10,11 @@ interface CreateEslintConfigArgs {
rootDir: string;
writeFile: boolean;
}
export function createEslintConfig({
export async function createEslintConfig({
pkg,
rootDir,
writeFile,
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
}: CreateEslintConfigArgs): Promise<CLIEngine.Options['baseConfig']> {
const isReactLibrary = Boolean(getReactVersion(pkg));

const config = {
Expand All @@ -30,24 +31,27 @@ export function createEslintConfig({
},
};

if (writeFile) {
const file = path.join(rootDir, '.eslintrc.js');
if (fs.existsSync(file)) {
if (!writeFile) {
return config;
}

const file = path.join(rootDir, '.eslintrc.js');
try {
await util.promisify(fs.writeFile)(
file,
`module.exports = ${JSON.stringify(config, null, 2)}`,
{ flag: 'wx' }
);
} catch (e) {
if (e.code === 'EEXIST') {
console.error(
'Error trying to save the Eslint configuration file:',
`${file} already exists.`
);
} else {
try {
fs.writeFileSync(
file,
`module.exports = ${JSON.stringify(config, null, 2)}`
);
} catch (e) {
console.error(e);
}
console.error(e);
}
}

return config;
return config;
}
}
8 changes: 4 additions & 4 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const errorCodeOpts = {
// shebang cache map thing because the transform only gets run once
let shebang: any = {};

export function createRollupConfig(opts: TsdxOptions) {
const findAndRecordErrorCodes = extractErrors({
export async function createRollupConfig(opts: TsdxOptions) {
const findAndRecordErrorCodes = await extractErrors({
...errorCodeOpts,
...opts,
});
Expand Down Expand Up @@ -88,8 +88,8 @@ export function createRollupConfig(opts: TsdxOptions) {
},
plugins: [
!!opts.extractErrors && {
transform(source: any) {
findAndRecordErrorCodes(source);
async transform(source: any) {
await findAndRecordErrorCodes(source);
return source;
},
},
Expand Down
18 changes: 9 additions & 9 deletions src/errors/extractErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const babylonOptions = {
],
};

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

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

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

// Write the error files, unless they already exist
fs.writeFileSync(
await fs.writeFile(
paths.appErrors + '/ErrorDev.js',
`
function ErrorDev(message) {
Expand All @@ -116,7 +116,7 @@ export default ErrorDev;
'utf-8'
);

fs.writeFileSync(
await fs.writeFile(
paths.appErrors + '/ErrorProd.js',
`
function ErrorProd(code) {
Expand All @@ -138,8 +138,8 @@ export default ErrorProd;
);
}

return function extractErrors(source: any) {
return async function extractErrors(source: any) {
transform(source);
flush();
await flush();
};
}
4 changes: 2 additions & 2 deletions src/getInstallCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ let cmd: InstallCommand;

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

export default function getInstallCmd(): InstallCommand {
export default async function getInstallCmd(): Promise<InstallCommand> {
if (cmd) {
return cmd;
}

try {
execa.sync('yarnpkg', ['--version']);
await execa('yarnpkg', ['--version']);
cmd = 'yarn';
} catch (e) {
cmd = 'npm';
Expand Down
Loading

0 comments on commit c5f65bc

Please sign in to comment.