@@ -25,29 +25,28 @@ async function run() {
2525 `Found ${ subpathHelperFile . subpathNames . length } subpaths and ${ subpathHelperFile . subpathFoldersBarrel . length } subpath barrels` ,
2626 ) ;
2727
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 ) ) ;
3030
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' ) ;
3333 }
3434
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-
4235 // Create directories for each subpath name using the pkgJsonPlaceholder
4336 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+ }
4541 writeJSON ( `../packages/${ pkgName } /${ name } /package.json` , pkgJsonPlaceholder ( name ) ) ;
4642 } ) ;
4743
4844 // Create directories for each subpath barrel file using the pkgJsonBarrelPlaceholder
4945 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+ }
5150 writeJSON ( `../packages/${ pkgName } /${ name } /package.json` , pkgJsonBarrelPlaceholder ( name ) ) ;
5251 } ) ;
5352
0 commit comments