@@ -19,54 +19,47 @@ const ENTRY_POINTS = ['main', 'module', 'types', 'browser'];
19
19
const packageWithBundles = process . argv . includes ( '--bundles' ) ;
20
20
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR ;
21
21
22
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
23
+ const pkgJson : { [ key : string ] : unknown } = require ( path . resolve ( 'package.json' ) ) ;
24
+
22
25
// check if build dir exists
23
- try {
24
- if ( ! fs . existsSync ( path . resolve ( buildDir ) ) ) {
25
- console . error ( `Directory ${ buildDir } DOES NOT exist` ) ;
26
- console . error ( "This script should only be executed after you've run `yarn build`." ) ;
27
- process . exit ( 1 ) ;
28
- }
29
- } catch ( error ) {
30
- console . error ( `Error while looking up directory ${ buildDir } ` ) ;
26
+ if ( ! fs . existsSync ( path . resolve ( buildDir ) ) ) {
27
+ console . error ( `\nERROR: Directory '${ buildDir } ' does not exist in ${ pkgJson . name } .` ) ;
28
+ console . error ( "This script should only be executed after you've run `yarn build`." ) ;
31
29
process . exit ( 1 ) ;
32
30
}
33
31
34
32
// copy non-code assets to build dir
35
33
ASSETS . forEach ( asset => {
36
34
const assetPath = path . resolve ( asset ) ;
37
- try {
38
- if ( ! fs . existsSync ( assetPath ) ) {
39
- console . error ( `Asset ${ asset } does not exist.` ) ;
40
- process . exit ( 1 ) ;
41
- }
42
- const destinationPath = path . resolve ( buildDir , path . basename ( asset ) ) ;
43
- console . log ( `Copying ${ path . basename ( asset ) } to ${ path . relative ( '../..' , destinationPath ) } .` ) ;
44
- fs . copyFileSync ( assetPath , destinationPath ) ;
45
- } catch ( error ) {
46
- console . error ( `Error while copying ${ asset } to ${ buildDir } ` ) ;
35
+ if ( ! fs . existsSync ( assetPath ) ) {
36
+ console . error ( `\nERROR: Asset '${ asset } ' does not exist.` ) ;
47
37
process . exit ( 1 ) ;
48
38
}
39
+ const destinationPath = path . resolve ( buildDir , path . basename ( asset ) ) ;
40
+ console . log ( `Copying ${ path . basename ( asset ) } to ${ path . relative ( '../..' , destinationPath ) } .` ) ;
41
+ fs . copyFileSync ( assetPath , destinationPath ) ;
49
42
} ) ;
50
43
51
44
// package.json modifications
52
- const packageJsonPath = path . resolve ( buildDir , 'package.json' ) ;
45
+ const newPackageJsonPath = path . resolve ( buildDir , 'package.json' ) ;
53
46
// eslint-disable-next-line @typescript-eslint/no-var-requires
54
- const pkgJson : { [ key : string ] : unknown } = require ( packageJsonPath ) ;
47
+ const newPkgJson : { [ key : string ] : unknown } = require ( newPackageJsonPath ) ;
55
48
56
49
// modify entry points to point to correct paths (i.e. strip out the build directory)
57
- ENTRY_POINTS . filter ( entryPoint => pkgJson [ entryPoint ] ) . forEach ( entryPoint => {
58
- pkgJson [ entryPoint ] = ( pkgJson [ entryPoint ] as string ) . replace ( `${ buildDir } /` , '' ) ;
50
+ ENTRY_POINTS . filter ( entryPoint => newPkgJson [ entryPoint ] ) . forEach ( entryPoint => {
51
+ newPkgJson [ entryPoint ] = ( newPkgJson [ entryPoint ] as string ) . replace ( `${ buildDir } /` , '' ) ;
59
52
} ) ;
60
53
61
- delete pkgJson . scripts ;
62
- delete pkgJson . volta ;
63
- delete pkgJson . jest ;
54
+ delete newPkgJson . scripts ;
55
+ delete newPkgJson . volta ;
56
+ delete newPkgJson . jest ;
64
57
65
58
// write modified package.json to file (pretty-printed with 2 spaces)
66
59
try {
67
- fs . writeFileSync ( packageJsonPath , JSON . stringify ( pkgJson , null , 2 ) ) ;
60
+ fs . writeFileSync ( newPackageJsonPath , JSON . stringify ( newPkgJson , null , 2 ) ) ;
68
61
} catch ( error ) {
69
- console . error ( ' Error while writing package.json to disk' ) ;
62
+ console . error ( `\nERROR: Error while writing modified ' package.json' to disk in ${ pkgJson . name } :\n` , error ) ;
70
63
process . exit ( 1 ) ;
71
64
}
72
65
@@ -78,26 +71,28 @@ async function runPackagePrepack(packagePrepackPath: string): Promise<void> {
78
71
process . exit ( 1 ) ;
79
72
}
80
73
} else {
81
- console . error ( `Could not find a prepack function in ${ packagePrepackPath } .` ) ;
74
+ console . error ( `\nERROR: Could not find a \` prepack\` function in './scripts/prepack.ts' in ${ pkgJson . name } .` ) ;
82
75
console . error (
83
- 'Make sure, your package-specific prepack script exports `function prepack(buildDir: string): boolean`.' ,
76
+ 'Make sure your package-specific prepack script exports `function prepack(buildDir: string): boolean`.' ,
84
77
) ;
85
78
process . exit ( 1 ) ;
86
79
}
87
80
}
88
81
89
82
// execute package specific settings
90
- // 1. check if a package called `<package-root>/scripts/prepack.ts` exitsts
83
+ // 1. check if a script called `<package-root>/scripts/prepack.ts` exists
91
84
// if yes, 2.) execute that script for things that are package-specific
92
- void ( async ( ) = > {
85
+ async function runPackageSpecificScripts ( ) : Promise < void > {
93
86
const packagePrepackPath = path . resolve ( 'scripts' , 'prepack.ts' ) ;
94
87
try {
95
88
if ( fs . existsSync ( packagePrepackPath ) ) {
96
89
await runPackagePrepack ( packagePrepackPath ) ;
97
90
}
98
91
} catch ( error ) {
99
- console . error ( `Error while trying to access ${ packagePrepackPath . toString ( ) } ` ) ;
92
+ console . error ( `\nERROR: Error while trying to load and run ./scripts/prepack.ts in ${ pkgJson . name } :\n` , error ) ;
100
93
process . exit ( 1 ) ;
101
94
}
102
95
console . log ( `\nSuccessfully finished prepack commands for ${ pkgJson . name } \n` ) ;
103
- } ) ( ) ;
96
+ }
97
+
98
+ void runPackageSpecificScripts ( ) ;
0 commit comments