@@ -25,29 +25,28 @@ async function run() {
25
25
`Found ${ subpathHelperFile . subpathNames . length } subpaths and ${ subpathHelperFile . subpathFoldersBarrel . length } subpath barrels` ,
26
26
) ;
27
27
28
- // Check if pkgFile.files already contains the subpaths. This means that the script has already been run and we should exit early
29
- const subpathsAlreadyAdded = subpathHelperFile . subpathNames . some ( name => pkgFile . files . includes ( name ) ) ;
28
+ const allFilesNames = [ ... subpathHelperFile . subpathNames , ... subpathHelperFile . subpathFoldersBarrel , 'dist' ] ;
29
+ const hasAllSubpathsInFiles = pkgFile . files . every ( name => allFilesNames . includes ( name ) ) ;
30
30
31
- if ( subpathsAlreadyAdded ) {
32
- return console . log ( `Subpaths already added to ${ pkgName } package.json. Exiting early` ) ;
31
+ if ( ! hasAllSubpathsInFiles ) {
32
+ throw new Error ( 'Not all subpaths from the package.json "files" array are in the subpaths.mjs' ) ;
33
33
}
34
34
35
- // Add all subpaths to the "files" property on package.json
36
- pkgFile . files = [ ...pkgFile . files , ...subpathHelperFile . subpathNames , ...subpathHelperFile . subpathFoldersBarrel ] ;
37
-
38
- writeJSON ( `../packages/${ pkgName } /package.json` , pkgFile ) ;
39
-
40
- console . log ( `Overwrote package.json for ${ pkgName } with subpaths` ) ;
41
-
42
35
// Create directories for each subpath name using the pkgJsonPlaceholder
43
36
subpathHelperFile . subpathNames . forEach ( name => {
44
- fs . mkdirSync ( new URL ( `../packages/${ pkgName } /${ name } ` , import . meta. url ) ) ;
37
+ const dir = new URL ( `../packages/${ pkgName } /${ name } ` , import . meta. url ) ;
38
+ if ( ! fs . existsSync ( dir ) ) {
39
+ fs . mkdirSync ( dir ) ;
40
+ }
45
41
writeJSON ( `../packages/${ pkgName } /${ name } /package.json` , pkgJsonPlaceholder ( name ) ) ;
46
42
} ) ;
47
43
48
44
// Create directories for each subpath barrel file using the pkgJsonBarrelPlaceholder
49
45
subpathHelperFile . subpathFoldersBarrel . forEach ( name => {
50
- fs . mkdirSync ( new URL ( `../packages/${ pkgName } /${ name } ` , import . meta. url ) ) ;
46
+ const dir = new URL ( `../packages/${ pkgName } /${ name } ` , import . meta. url ) ;
47
+ if ( ! fs . existsSync ( dir ) ) {
48
+ fs . mkdirSync ( dir ) ;
49
+ }
51
50
writeJSON ( `../packages/${ pkgName } /${ name } /package.json` , pkgJsonBarrelPlaceholder ( name ) ) ;
52
51
} ) ;
53
52
0 commit comments