@@ -499,55 +499,55 @@ async function setPackageJsonsType(
499499 const rootPkgJsonPath = join ( cwd , 'package.json' ) ;
500500 const rootContents = await fse . readFile ( rootPkgJsonPath , 'utf8' ) ;
501501 const rootPkg = JSON . parse ( rootContents ) ;
502+ const workspaces = await getWorkspaces ( rootPkg ) ;
503+ const isSinglePackage = workspaces === null ;
502504
503505 const reverts : ( ( ) => Promise < void > ) [ ] = [ ] ;
504506
505- if ( 'workspaces' in rootPkg ) {
506- for ( const pkgJsonPath of [
507- // we also want to modify the root package.json
508- // TODO: do we?
509- rootPkgJsonPath ,
510- // get all package.jsons from the defined workspaces
511- ...( await globby (
512- rootPkg . workspaces . map ( ( w : string ) => w + '/package.json' ) ,
513- { cwd, absolute : true } ,
514- ) ) ,
515- ] ) {
516- const contents =
517- pkgJsonPath === rootPkgJsonPath
518- ? // no need to re-read the root package.json
519- rootContents
520- : await fse . readFile ( pkgJsonPath , 'utf8' ) ;
521- const endsWithNewline = contents . endsWith ( '\n' ) ;
522-
523- const pkg = JSON . parse ( contents ) ;
524- if ( pkg . type != null && pkg . type !== 'commonjs' && pkg . type !== 'module' ) {
525- throw new Error ( `Invalid "type" property value "${ pkg . type } " in ${ pkgJsonPath } ` ) ;
526- }
507+ for ( const pkgJsonPath of [
508+ // we also want to modify the root package.json TODO: do we?
509+ rootPkgJsonPath ,
510+ ...( isSinglePackage
511+ ? [ ]
512+ : await globby (
513+ workspaces . map ( ( w : string ) => w + '/package.json' ) ,
514+ { cwd, absolute : true } ,
515+ ) ) ,
516+ ] ) {
517+ const contents =
518+ pkgJsonPath === rootPkgJsonPath
519+ ? // no need to re-read the root package.json
520+ rootContents
521+ : await fse . readFile ( pkgJsonPath , 'utf8' ) ;
522+ const endsWithNewline = contents . endsWith ( '\n' ) ;
523+
524+ const pkg = JSON . parse ( contents ) ;
525+ if ( pkg . type != null && pkg . type !== 'commonjs' && pkg . type !== 'module' ) {
526+ throw new Error ( `Invalid "type" property value "${ pkg . type } " in ${ pkgJsonPath } ` ) ;
527+ }
527528
528- const originalType : PackageJsonType | undefined = pkg . type ;
529- const differentType =
530- ( pkg . type ||
531- // default when the type is not defined
532- 'commonjs' ) !== type ;
529+ const originalType : PackageJsonType | undefined = pkg . type ;
530+ const differentType =
531+ ( pkg . type ||
532+ // default when the type is not defined
533+ 'commonjs' ) !== type ;
534+
535+ // change only if the provided type is different
536+ if ( differentType ) {
537+ pkg . type = type ;
538+ await fse . writeFile (
539+ pkgJsonPath ,
540+ JSON . stringify ( pkg , null , ' ' ) + ( endsWithNewline ? '\n' : '' ) ,
541+ ) ;
533542
534- // change only if the provided type is different
535- if ( differentType ) {
536- pkg . type = type ;
543+ // revert change, of course only if we changed something
544+ reverts . push ( async ( ) => {
545+ pkg . type = originalType ;
537546 await fse . writeFile (
538547 pkgJsonPath ,
539548 JSON . stringify ( pkg , null , ' ' ) + ( endsWithNewline ? '\n' : '' ) ,
540549 ) ;
541-
542- // revert change, of course only if we changed something
543- reverts . push ( async ( ) => {
544- pkg . type = originalType ;
545- await fse . writeFile (
546- pkgJsonPath ,
547- JSON . stringify ( pkg , null , ' ' ) + ( endsWithNewline ? '\n' : '' ) ,
548- ) ;
549- } ) ;
550- }
550+ } ) ;
551551 }
552552 }
553553
0 commit comments