@@ -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,14 @@ 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 = '// @flow strict\n\n ' + fs . readFileSync ( srcPath , 'utf-8' ) ;
23
28
fs . writeFileSync ( destPath + '.flow' , flowBody ) ;
24
29
25
30
const cjs = babelBuild ( srcPath , { envName : 'cjs' } ) ;
26
- fs . writeFileSync ( destPath , cjs ) ;
31
+ writeGeneratedFile ( destPath , cjs ) ;
27
32
28
33
const mjs = babelBuild ( srcPath , { envName : 'mjs' } ) ;
29
- fs . writeFileSync ( destPath . replace ( / \. j s $ / , '.mjs' ) , mjs ) ;
34
+ writeGeneratedFile ( destPath . replace ( / \. j s $ / , '.mjs' ) , mjs ) ;
30
35
} else if ( filepath . endsWith ( '.d.ts' ) ) {
31
36
fs . copyFileSync ( srcPath , destPath ) ;
32
37
}
@@ -37,14 +42,16 @@ if (require.main === module) {
37
42
38
43
// Should be done as the last step so only valid packages can be published
39
44
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 ) ) ;
44
46
45
47
showDirStats ( './npmDist' ) ;
46
48
}
47
49
50
+ function writeGeneratedFile ( filepath , body ) {
51
+ const formatted = prettier . format ( body , { filepath, ...prettierConfig } ) ;
52
+ fs . writeFileSync ( filepath , formatted ) ;
53
+ }
54
+
48
55
function babelBuild ( srcPath , options ) {
49
56
const { code } = babel . transformFileSync ( srcPath , {
50
57
babelrc : false ,
0 commit comments