@@ -5,9 +5,14 @@ const path = require('path');
5
5
const assert = require ( 'assert' ) ;
6
6
7
7
const babel = require ( '@babel/core' ) ;
8
+ const prettier = require ( 'prettier' ) ;
8
9
9
10
const { readdirRecursive, showDirStats } = require ( './utils' ) ;
10
11
12
+ const prettierConfig = JSON . parse (
13
+ fs . readFileSync ( require . resolve ( '../.prettierrc' ) , 'utf-8' ) ,
14
+ ) ;
15
+
11
16
if ( require . main === module ) {
12
17
fs . rmSync ( './npmDist' , { recursive : true , force : true } ) ;
13
18
fs . mkdirSync ( './npmDist' ) ;
@@ -19,14 +24,15 @@ if (require.main === module) {
19
24
20
25
fs . mkdirSync ( path . dirname ( destPath ) , { recursive : true } ) ;
21
26
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' ) ;
23
29
fs . writeFileSync ( destPath + '.flow' , flowBody ) ;
24
30
25
31
const cjs = babelBuild ( srcPath , { envName : 'cjs' } ) ;
26
- fs . writeFileSync ( destPath , cjs ) ;
32
+ writeGeneratedFile ( destPath , cjs ) ;
27
33
28
34
const mjs = babelBuild ( srcPath , { envName : 'mjs' } ) ;
29
- fs . writeFileSync ( destPath . replace ( / \. j s $ / , '.mjs' ) , mjs ) ;
35
+ writeGeneratedFile ( destPath . replace ( / \. j s $ / , '.mjs' ) , mjs ) ;
30
36
} else if ( filepath . endsWith ( '.d.ts' ) ) {
31
37
fs . copyFileSync ( srcPath , destPath ) ;
32
38
}
@@ -37,14 +43,16 @@ if (require.main === module) {
37
43
38
44
// Should be done as the last step so only valid packages can be published
39
45
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 ) ) ;
44
47
45
48
showDirStats ( './npmDist' ) ;
46
49
}
47
50
51
+ function writeGeneratedFile ( filepath , body ) {
52
+ const formatted = prettier . format ( body , { filepath, ...prettierConfig } ) ;
53
+ fs . writeFileSync ( filepath , formatted ) ;
54
+ }
55
+
48
56
function babelBuild ( srcPath , options ) {
49
57
const { code } = babel . transformFileSync ( srcPath , {
50
58
babelrc : false ,
0 commit comments