Skip to content

Commit 47ff59f

Browse files
committed
build: run prettier on generated files
1 parent 10bd6fe commit 47ff59f

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

resources/build-deno.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const fs = require('fs');
44
const path = require('path');
55

66
const babel = require('@babel/core');
7+
const prettier = require('prettier');
78

89
const { readdirRecursive, showDirStats } = require('./utils');
910

11+
const prettierConfig = JSON.parse(
12+
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
13+
);
14+
1015
if (require.main === module) {
1116
fs.rmdirSync('./denoDist', { recursive: true, force: true });
1217
fs.mkdirSync('./denoDist');
@@ -20,7 +25,7 @@ if (require.main === module) {
2025
if (filepath.endsWith('.js')) {
2126
const options = { babelrc: false, configFile: './.babelrc-deno.json' };
2227
const output = babel.transformFileSync(srcPath, options).code + '\n';
23-
fs.writeFileSync(destPath, output);
28+
writeGeneratedFile(destPath, output);
2429
} else if (filepath.endsWith('.d.ts')) {
2530
fs.copyFileSync(srcPath, destPath);
2631
}
@@ -31,3 +36,8 @@ if (require.main === module) {
3136

3237
showDirStats('./denoDist');
3338
}
39+
40+
function writeGeneratedFile(filepath, body) {
41+
const formatted = prettier.format(body, { filepath, ...prettierConfig });
42+
fs.writeFileSync(filepath, formatted);
43+
}

resources/build-npm.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ const path = require('path');
55
const assert = require('assert');
66

77
const babel = require('@babel/core');
8+
const prettier = require('prettier');
89

910
const { readdirRecursive, showDirStats } = require('./utils');
1011

12+
const prettierConfig = JSON.parse(
13+
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
14+
);
15+
1116
if (require.main === module) {
1217
fs.rmSync('./npmDist', { recursive: true, force: true });
1318
fs.mkdirSync('./npmDist');
@@ -19,14 +24,15 @@ if (require.main === module) {
1924

2025
fs.mkdirSync(path.dirname(destPath), { recursive: true });
2126
if (filepath.endsWith('.js')) {
22-
const flowBody = '// @flow strict\n' + fs.readFileSync(srcPath, 'utf-8');
27+
const flowBody =
28+
'// @flow strict\n\n' + fs.readFileSync(srcPath, 'utf-8');
2329
fs.writeFileSync(destPath + '.flow', flowBody);
2430

2531
const cjs = babelBuild(srcPath, { envName: 'cjs' });
26-
fs.writeFileSync(destPath, cjs);
32+
writeGeneratedFile(destPath, cjs);
2733

2834
const mjs = babelBuild(srcPath, { envName: 'mjs' });
29-
fs.writeFileSync(destPath.replace(/\.js$/, '.mjs'), mjs);
35+
writeGeneratedFile(destPath.replace(/\.js$/, '.mjs'), mjs);
3036
} else if (filepath.endsWith('.d.ts')) {
3137
fs.copyFileSync(srcPath, destPath);
3238
}
@@ -37,14 +43,16 @@ if (require.main === module) {
3743

3844
// Should be done as the last step so only valid packages can be published
3945
const packageJSON = buildPackageJSON();
40-
fs.writeFileSync(
41-
'./npmDist/package.json',
42-
JSON.stringify(packageJSON, null, 2),
43-
);
46+
writeGeneratedFile('./npmDist/package.json', JSON.stringify(packageJSON));
4447

4548
showDirStats('./npmDist');
4649
}
4750

51+
function writeGeneratedFile(filepath, body) {
52+
const formatted = prettier.format(body, { filepath, ...prettierConfig });
53+
fs.writeFileSync(filepath, formatted);
54+
}
55+
4856
function babelBuild(srcPath, options) {
4957
const { code } = babel.transformFileSync(srcPath, {
5058
babelrc: false,

0 commit comments

Comments
 (0)