@alexaegis/workspace-tools
Version:
Tools for working with javascript workspaces
1 lines • 4.17 kB
Source Map (JSON)
{"version":3,"file":"get-root-package-json.function-DWdZuWlE.cjs","names":[],"sources":["../src/npm/get-root-package-json.function.options.ts","../src/npm/normalize-package-json-workspaces-field.function.ts","../src/npm/get-root-package-json.function.ts"],"sourcesContent":["import { normalizeCwdOption, type CwdOption, type NormalizedCwdOption } from '@alexaegis/fs';\nimport {\n\tnormalizeLoggerOption,\n\ttype LoggerOption,\n\ttype NormalizedLoggerOption,\n} from '@alexaegis/logging';\n\nexport type GetRootPackageJsonOptions = CwdOption & LoggerOption;\n\nexport type NormalizedGetRootPackageJsonOptions = NormalizedCwdOption & NormalizedLoggerOption;\n\nexport const normalizeGetRootPackageJsonOptions = (\n\toptions?: GetRootPackageJsonOptions,\n): NormalizedGetRootPackageJsonOptions => {\n\treturn {\n\t\t...normalizeCwdOption(options),\n\t\t...normalizeLoggerOption(options),\n\t};\n};\n","import type { PackageJson } from '../package-json/package-json.interface.js';\n\nexport const normalizePackageJsonWorkspacesField = (\n\tpackageJsonWorkspaces?: PackageJson['workspaces'],\n): string[] => {\n\tif (Array.isArray(packageJsonWorkspaces)) {\n\t\treturn packageJsonWorkspaces;\n\t} else if (packageJsonWorkspaces) {\n\t\treturn [\n\t\t\t...(packageJsonWorkspaces.packages ?? []),\n\t\t\t...(packageJsonWorkspaces.nohoist ?? []),\n\t\t];\n\t} else {\n\t\treturn [];\n\t}\n};\n","import { readJson, readYaml } from '@alexaegis/fs';\nimport { join } from 'node:path';\nimport type { RootWorkspacePackage } from '../monorepo/workspace-package.interface.js';\nimport {\n\tPACKAGE_JSON_NAME,\n\tPNPM_WORKSPACE_FILE_NAME,\n\ttype PackageJson,\n\ttype PnpmWorkspaceYaml,\n} from '../package-json/package-json.interface.js';\nimport {\n\tnormalizeGetRootPackageJsonOptions,\n\ttype GetRootPackageJsonOptions,\n} from './get-root-package-json.function.options.js';\nimport { getWorkspaceRoot } from './get-workspace-root.function.js';\nimport { normalizePackageJsonWorkspacesField } from './normalize-package-json-workspaces-field.function.js';\n\nexport const getRootPackageJson = async (\n\trawOptions: GetRootPackageJsonOptions,\n): Promise<RootWorkspacePackage | undefined> => {\n\tconst options = normalizeGetRootPackageJsonOptions(rawOptions);\n\tconst rootWorkspace = getWorkspaceRoot(options);\n\tif (!rootWorkspace) {\n\t\toptions.logger.error('No package json was found! Cannot collect workspace packages!');\n\n\t\treturn undefined;\n\t}\n\n\tconst packageJsonPath = join(rootWorkspace, PACKAGE_JSON_NAME);\n\tconst packageJson = await readJson<PackageJson>(packageJsonPath).catch(() => undefined);\n\n\tif (!packageJson) {\n\t\toptions.logger.error('Failed to read packageJson!', packageJsonPath);\n\t\treturn undefined;\n\t}\n\n\tlet workspaces = normalizePackageJsonWorkspacesField(packageJson.workspaces);\n\n\tconst pnpmWorkspace = await readYaml<PnpmWorkspaceYaml>(\n\t\tjoin(rootWorkspace, PNPM_WORKSPACE_FILE_NAME),\n\t);\n\n\tif (pnpmWorkspace?.packages) {\n\t\tworkspaces = [...workspaces, ...pnpmWorkspace.packages];\n\t}\n\n\treturn {\n\t\tpackageJson,\n\t\tpackageKind: 'root',\n\t\tworkspacePackagePatterns: workspaces,\n\t\tpackageJsonPath: packageJsonPath,\n\t\tpackagePath: rootWorkspace,\n\t\tpackagePathFromRootPackage: '.',\n\t};\n};\n"],"mappings":";;;;;AAWA,IAAa,sCACZ,YACyC;CACzC,OAAO;EACN,IAAA,GAAA,cAAA,mBAAA,CAAsB,OAAO;EAC7B,IAAA,GAAA,mBAAA,sBAAA,CAAyB,OAAO;CACjC;AACD;;;AChBA,IAAa,uCACZ,0BACc;CACd,IAAI,MAAM,QAAQ,qBAAqB,GACtC,OAAO;MACD,IAAI,uBACV,OAAO,CACN,GAAI,sBAAsB,YAAY,CAAC,GACvC,GAAI,sBAAsB,WAAW,CAAC,CACvC;MAEA,OAAO,CAAC;AAEV;;;ACCA,IAAa,qBAAqB,OACjC,eAC+C;CAC/C,MAAM,UAAU,mCAAmC,UAAU;CAC7D,MAAM,gBAAgB,oCAAA,iBAAiB,OAAO;CAC9C,IAAI,CAAC,eAAe;EACnB,QAAQ,OAAO,MAAM,+DAA+D;EAEpF;CACD;CAEA,MAAM,mBAAA,GAAA,UAAA,KAAA,CAAuB,eAAe,oCAAA,iBAAiB;CAC7D,MAAM,cAAc,OAAA,GAAA,cAAA,SAAA,CAA4B,eAAe,CAAC,CAAC,YAAY,KAAA,CAAS;CAEtF,IAAI,CAAC,aAAa;EACjB,QAAQ,OAAO,MAAM,+BAA+B,eAAe;EACnE;CACD;CAEA,IAAI,aAAa,oCAAoC,YAAY,UAAU;CAE3E,MAAM,gBAAgB,OAAA,GAAA,cAAA,SAAA,EAAA,GAAA,UAAA,KAAA,CAChB,eAAe,oCAAA,wBAAwB,CAC7C;CAEA,IAAI,eAAe,UAClB,aAAa,CAAC,GAAG,YAAY,GAAG,cAAc,QAAQ;CAGvD,OAAO;EACN;EACA,aAAa;EACb,0BAA0B;EACT;EACjB,aAAa;EACb,4BAA4B;CAC7B;AACD"}