Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced some sync methods for their async version #291

Merged
merged 1 commit into from
Dec 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
@@ -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 = {
@@ -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
@@ -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,
});
@@ -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;
},
},
18 changes: 9 additions & 9 deletions src/errors/extractErrors.ts
Original file line number Diff line number Diff line change
@@ -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`.'
@@ -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 = {};
}
@@ -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) {
@@ -116,7 +116,7 @@ export default ErrorDev;
'utf-8'
);

fs.writeFileSync(
await fs.writeFile(
paths.appErrors + '/ErrorProd.js',
`
function ErrorProd(code) {
@@ -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
@@ -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';
Loading