Skip to content

Commit 9e45d45

Browse files
committed
build: run prettier on generated files
1 parent 10bd6fe commit 9e45d45

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-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: 14 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,14 @@ 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 = '// @flow strict\n\n' + fs.readFileSync(srcPath, 'utf-8');
2328
fs.writeFileSync(destPath + '.flow', flowBody);
2429

2530
const cjs = babelBuild(srcPath, { envName: 'cjs' });
26-
fs.writeFileSync(destPath, cjs);
31+
writeGeneratedFile(destPath, cjs);
2732

2833
const mjs = babelBuild(srcPath, { envName: 'mjs' });
29-
fs.writeFileSync(destPath.replace(/\.js$/, '.mjs'), mjs);
34+
writeGeneratedFile(destPath.replace(/\.js$/, '.mjs'), mjs);
3035
} else if (filepath.endsWith('.d.ts')) {
3136
fs.copyFileSync(srcPath, destPath);
3237
}
@@ -37,14 +42,16 @@ if (require.main === module) {
3742

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

4547
showDirStats('./npmDist');
4648
}
4749

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

0 commit comments

Comments
 (0)