From 743f42b6ef6006d35bf9e0b45e3b70d6e9100596 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 15 Aug 2022 20:16:46 +0300 Subject: [PATCH] resources: Move `mkdirSync` into `writeGeneratedFile` (#3697) --- resources/build-deno.ts | 1 - resources/build-npm.ts | 5 +---- resources/diff-npm-package.ts | 10 +++------- resources/utils.ts | 1 + 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/resources/build-deno.ts b/resources/build-deno.ts index c0b3e895c4..0d7ba254c8 100644 --- a/resources/build-deno.ts +++ b/resources/build-deno.ts @@ -33,7 +33,6 @@ for (const filepath of srcFiles) { transformed.dispose(); const destPath = path.join('./denoDist', filepath); - fs.mkdirSync(path.dirname(destPath), { recursive: true }); writeGeneratedFile(destPath, newContent); } } diff --git a/resources/build-npm.ts b/resources/build-npm.ts index badbecc906..91874c29eb 100644 --- a/resources/build-npm.ts +++ b/resources/build-npm.ts @@ -48,10 +48,7 @@ assert( ); const tsHost = ts.createCompilerHost(tsOptions); -tsHost.writeFile = (filepath, body) => { - fs.mkdirSync(path.dirname(filepath), { recursive: true }); - writeGeneratedFile(filepath, body); -}; +tsHost.writeFile = writeGeneratedFile; const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost); const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, { diff --git a/resources/diff-npm-package.ts b/resources/diff-npm-package.ts index 47ec08a883..a2b144132b 100644 --- a/resources/diff-npm-package.ts +++ b/resources/diff-npm-package.ts @@ -3,7 +3,7 @@ import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; -import { exec, execOutput, localRepoPath } from './utils'; +import { exec, execOutput, localRepoPath, writeGeneratedFile } from './utils'; const LOCAL = 'local'; const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff'); @@ -32,12 +32,8 @@ const diff = execOutput(`npm diff --diff=${fromPackage} --diff=${toPackage}`); if (diff === '') { console.log('No changes found!'); } else { - const reportsDir = localRepoPath('reports'); - if (!fs.existsSync(reportsDir)) { - fs.mkdirSync(reportsDir); - } - const reportPath = path.join(reportsDir, 'npm-dist-diff.html'); - fs.writeFileSync(reportPath, generateReport(diff)); + const reportPath = localRepoPath('reports', 'npm-dist-diff.html'); + writeGeneratedFile(reportPath, generateReport(diff)); console.log('Report saved to: ', reportPath); } diff --git a/resources/utils.ts b/resources/utils.ts index c8d3bb7123..ca7918c4ad 100644 --- a/resources/utils.ts +++ b/resources/utils.ts @@ -106,6 +106,7 @@ const prettierConfig = JSON.parse( export function writeGeneratedFile(filepath: string, body: string): void { const formatted = prettier.format(body, { filepath, ...prettierConfig }); + fs.mkdirSync(path.dirname(filepath), { recursive: true }); fs.writeFileSync(filepath, formatted); }