From 14753dd5b3e3c6a8687b29d2e78f32fe74f4ccc4 Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Tue, 22 Jun 2021 04:23:57 -0400 Subject: [PATCH] build: run prettier on generated output (#343) Co-authored-by: Ivan Goncharov --- resources/build.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/resources/build.js b/resources/build.js index 019c355..360c16a 100644 --- a/resources/build.js +++ b/resources/build.js @@ -5,6 +5,11 @@ const path = require('path'); const assert = require('assert'); const babel = require('@babel/core'); +const prettier = require('prettier'); + +const prettierConfig = JSON.parse( + fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'), +); const { readdirRecursive, showDirStats } = require('./utils'); @@ -20,10 +25,10 @@ if (require.main === module) { fs.mkdirSync(path.dirname(destPath), { recursive: true }); if (filepath.endsWith('.js')) { const flowBody = '// @flow strict\n' + fs.readFileSync(srcPath, 'utf-8'); - fs.writeFileSync(destPath + '.flow', flowBody); + writeGeneratedFile(destPath + '.flow', flowBody); const cjs = babelBuild(srcPath, { envName: 'cjs' }); - fs.writeFileSync(destPath, cjs); + writeGeneratedFile(destPath, cjs); } else if (filepath.endsWith('.d.ts')) { fs.copyFileSync(srcPath, destPath); } @@ -81,3 +86,8 @@ function buildPackageJSON() { return packageJSON; } + +function writeGeneratedFile(filepath, body) { + const formatted = prettier.format(body, { filepath, ...prettierConfig }); + fs.writeFileSync(filepath, formatted); +}