Skip to content

Commit

Permalink
Merge pull request #2105 from Azure/ant/fix_async_code
Browse files Browse the repository at this point in the history
Use fs/promises instead of promisify
  • Loading branch information
anthony-c-martin authored Dec 16, 2021
2 parents 1aff1d9 + 8ce64dd commit 5adb72c
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 49 deletions.
4 changes: 2 additions & 2 deletions generator/cmd/generateall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ executeSynchronous(async () => {

for (const basePath of basePaths) {
try {
const readme = await validateAndReturnReadmePath(localPath, basePath);
const readme = validateAndReturnReadmePath(localPath, basePath);
const namespaces = keys(await getApiVersionsByNamespace(readme));
let filteredAutoGenList = findOrGenerateAutogenEntries(basePath, namespaces)
.filter(x => x.disabledForAutogen !== true);
Expand All @@ -88,7 +88,7 @@ executeSynchronous(async () => {
path: ['schemas']
} as Package;
try {
const readme = await validateAndReturnReadmePath(localPath, autoGenConfig.readmeFile || autoGenConfig.basePath);
const readme = validateAndReturnReadmePath(localPath, autoGenConfig.readmeFile || autoGenConfig.basePath);
pkg.packageName = getPackageString(readme);

const newConfigs = await generateSchemas(readme, autoGenConfig);
Expand Down
2 changes: 1 addition & 1 deletion generator/cmd/generateonboardedreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ executeSynchronous(async () => {
const allBasePaths = [];

for (const basePath of basePaths) {
const readme = await validateAndReturnReadmePath(constants.specsRepoPath, basePath);
const readme = validateAndReturnReadmePath(constants.specsRepoPath, basePath);
const namespaces = keys(await getApiVersionsByNamespace(readme));
const autogenlistEntries = findOrGenerateAutogenEntries(basePath, namespaces);

Expand Down
2 changes: 1 addition & 1 deletion generator/cmd/generatesingle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ executeSynchronous(async () => {

let readme = '';
try {
readme = await validateAndReturnReadmePath(localPath, basePath);
readme = validateAndReturnReadmePath(localPath, basePath);
} catch {
throw new Error(`Unable to find a readme under '${basePath}'. Please try running 'npm run list-basepaths' to find the list of valid paths.`);
}
Expand Down
2 changes: 1 addition & 1 deletion generator/cmd/listbasepaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ executeSynchronous(async () => {
const basePaths = await cloneAndGenerateBasePaths(constants.specsRepoPath, constants.specsRepoUri, constants.specsRepoCommitHash);

for (const basePath of basePaths) {
const readme = await validateAndReturnReadmePath(constants.specsRepoPath, basePath);
const readme = validateAndReturnReadmePath(constants.specsRepoPath, basePath);
const namespaces = keys(await getApiVersionsByNamespace(readme));
const autogenlistEntries = findOrGenerateAutogenEntries(basePath, namespaces);

Expand Down
6 changes: 1 addition & 5 deletions generator/cmd/listresources.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
import { readFile, writeFile } from 'fs/promises';
import * as constants from '../constants';
import { lowerCaseCompare, executeSynchronous } from '../utils';

Expand All @@ -26,9 +25,6 @@ const rootSchemaPaths = [
'https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json',
];

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

async function readSchema(schemaUri: string) {
if (!schemaUri.toLowerCase().startsWith(constants.schemasBaseUri.toLowerCase() + '/')) {
throw new Error(`Invalid schema Uri ${schemaUri}`);
Expand Down
2 changes: 1 addition & 1 deletion generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function handleGeneratedSchema(readme: string, schemaPath: string, autoGen

async function execAutoRest(tmpFolder: string, params: string[]) {
await executeCmd(__dirname, `${__dirname}/node_modules/.bin/${autorestBinary}`, params);
if (!await fileExists(tmpFolder)) {
if (!fileExists(tmpFolder)) {
return [];
}

Expand Down
9 changes: 3 additions & 6 deletions generator/git.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { executeCmd, rmdirRecursive } from './utils';
import fs from 'fs';
import { promisify } from 'util';

const mkdir = promisify(fs.mkdir);
const exists = promisify(fs.exists);
import { existsSync } from 'fs';
import { mkdir } from 'fs/promises';

export async function resetGitDirectory(localPath: string, includeGc: boolean) {
if (await exists(localPath)) {
if (existsSync(localPath)) {
await executeCmd(localPath, 'git', ['reset', '-q', '.']);
await executeCmd(localPath, 'git', ['checkout', '-q', '--', '.']);
await executeCmd(localPath, 'git', ['clean', '-q', '-fd']);
Expand Down
14 changes: 6 additions & 8 deletions generator/specs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
import { cloneGitRepo } from './git';
import { findRecursive, lowerCaseEquals } from './utils';
import { ReadmeTag, AutoGenConfig, CodeBlock } from './models';
import * as constants from './constants'
import * as cm from '@ts-common/commonmark-to-markdown'
import * as yaml from 'js-yaml'

const exists = promisify(fs.exists);
import { existsSync } from 'fs';
import { readFile, writeFile } from 'fs/promises';

export async function resolveAbsolutePath(localPath: string) {
if (path.isAbsolute(localPath)) {
Expand All @@ -19,15 +17,15 @@ export async function resolveAbsolutePath(localPath: string) {
return path.resolve(constants.generatorRoot, localPath);
}

export async function validateAndReturnReadmePath(localPath: string, basePath: string) {
export function validateAndReturnReadmePath(localPath: string, basePath: string) {
let readme = '';
if (basePath.toLowerCase().endsWith('readme.md')) {
readme = path.resolve(localPath, basePath);
} else {
readme = path.resolve(localPath, 'specification', basePath, 'readme.md')
}

if (!await exists(readme)) {
if (!existsSync(readme)) {
throw new Error(`Unable to find readme '${readme}' in specs repo`);
}

Expand Down Expand Up @@ -79,7 +77,7 @@ function isExcludedBasePath(basePath: string) {
}

export async function prepareReadme(readme: string, autoGenConfig?: AutoGenConfig) {
const content = fs.readFileSync(readme).toString();
const content = (await readFile(readme)).toString();
const markdownEx = cm.parse(content);
const fileSet = new Set<string>();
for (const node of cm.iterate(markdownEx.markDown)) {
Expand Down Expand Up @@ -125,7 +123,7 @@ export async function prepareReadme(readme: string, autoGenConfig?: AutoGenConfi

const schemaReadme = readme.replace(/\.md$/i, '.azureresourceschema.md');

fs.writeFileSync(schemaReadme, schemaReadmeContent);
await writeFile(schemaReadme, schemaReadmeContent);
}

function compositeSchemaReadme(readmeTag: ReadmeTag): string {
Expand Down
24 changes: 7 additions & 17 deletions generator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@
// Licensed under the MIT License.
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
import chalk from 'chalk';
import { series } from 'async';

const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const unlink = promisify(fs.unlink);
const rmdir = promisify(fs.rmdir);
const exists = promisify(fs.exists);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const mkdir = promisify(fs.mkdir);

import { existsSync } from 'fs';
import { readdir, stat, unlink, rmdir, readFile, writeFile, mkdir } from 'fs/promises';

export function executeCmd(cwd: string, cmd: string, args: string[]) : Promise<number> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -90,7 +80,7 @@ export async function findRecursive(basePath: string, filter: (name: string) =>
}

export async function rmdirRecursive(basePath: string) {
if (!await exists(basePath)) {
if (!existsSync(basePath)) {
return;
}

Expand Down Expand Up @@ -184,19 +174,19 @@ export async function writeJsonFile(filePath: string, json: any) {
}

export async function safeMkdir(filePath: string) {
if (!await exists(filePath)) {
if (!existsSync(filePath)) {
await mkdir(filePath, { recursive: true });
}
}

export async function safeUnlink(filePath: string) {
if (await exists(filePath)) {
if (existsSync(filePath)) {
await unlink(filePath);
}
}

export async function fileExists(filePath: string) {
return await exists(filePath);
export function fileExists(filePath: string) {
return existsSync(filePath);
}

export function executeSynchronous<T>(asyncFunc: () => Promise<T>) {
Expand Down
5 changes: 1 addition & 4 deletions tools/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// Licensed under the MIT License.
import express = require('express');

import { promisify } from 'util';
import { hostname } from 'os';
import fs from 'fs';

const readFile = promisify(fs.readFile);
import { readFile } from 'fs/promises';

const app = express();
const port = 3000;
Expand Down
4 changes: 1 addition & 3 deletions tools/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import Ajv from 'ajv';
import * as url from 'url';
import path from 'path';
import fs from 'fs';
import { promisify } from 'util';
import { readFile } from 'fs/promises';
import { getLanguageService } from 'vscode-json-languageservice';
import { TextDocument } from 'vscode-languageserver-types';
import draft4MetaSchema from 'ajv/lib/refs/json-schema-draft-04.json';
import * as schemaTestsRunner from './schemaTestsRunner';
import 'mocha';
import { findCycle } from './cycleCheck';

const readFile = promisify(fs.readFile);

const schemasFolder = __dirname + '/../schemas/';
const schemaTestsFolder = __dirname + '/../tests/';
const testSchemasFolder = __dirname + '/schemas/';
Expand Down

0 comments on commit 5adb72c

Please sign in to comment.