@alexaegis/workspace-tools
Version:
Tools for working with javascript workspaces
1 lines • 7.06 kB
Source Map (JSON)
{"version":3,"file":"get-workspace-root.function-BKR-FeN5.cjs","names":[],"sources":["../src/npm/collect-file-dirname-paths-up-directory-tree.function.options.ts","../src/package-json/package-json.interface.ts","../src/npm/collect-file-dirname-paths-up-directory-tree.function.ts","../src/npm/get-workspace-root.function.ts"],"sourcesContent":["import type { Defined } from '@alexaegis/common';\nimport {\n\tnormalizeCwdOption,\n\tnormalizeDirectoryDepthOption,\n\ttype CwdOption,\n\ttype DirectoryDepthOption,\n} from '@alexaegis/fs';\n\nexport type CollectFileDirnamesUpDirectoryTreeOptions = CwdOption &\n\tDirectoryDepthOption & {\n\t\t/**\n\t\t * How many packages it should search in at most (A folder is treated\n\t\t * as a package if there is a `package.json` file directly in it)\n\t\t *\n\t\t * Normally workspaces only have 2 layer of packages, so by default\n\t\t * I assume a monorepo. You can change this to 1 if using a\n\t\t * single-project repo, or to something larger like `Infinity` if you\n\t\t * have something more unique. For example searching from inside the\n\t\t * node_modules folder will definitely need you to find more than\n\t\t * 2 package.json files. But normally your `cwd` won't be inside there.\n\t\t *\n\t\t * @default 2\n\t\t */\n\t\tmaxPackages?: number | undefined;\n\n\t\t/**\n\t\t * If the search term is package.json, this has the same effect\n\t\t * as maxPackages. Both conditions can stop the search, the smaller one\n\t\t * will be the stronger.\n\t\t *\n\t\t * @default Infinity\n\t\t */\n\t\tmaxResults?: number | undefined;\n\t};\n\nexport type NormalizedCollectFileDirnamesUpDirectoryTreeOptions =\n\tDefined<CollectFileDirnamesUpDirectoryTreeOptions>;\n\nexport const normalizeCollectFileDirnamesUpDirectoryTreeOptions = (\n\toptions?: CollectFileDirnamesUpDirectoryTreeOptions,\n): NormalizedCollectFileDirnamesUpDirectoryTreeOptions => {\n\treturn {\n\t\t...normalizeCwdOption(options),\n\t\t...normalizeDirectoryDepthOption(options),\n\t\tmaxPackages: options?.maxPackages ?? 2,\n\t\tmaxResults: options?.maxResults ?? Number.POSITIVE_INFINITY,\n\t};\n};\n","import type { Replace } from '@alexaegis/common';\nimport type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';\nimport type { PackageArchetype } from './package-archetype.interface.js';\nimport type { PackageJsonExportConditions } from './package-json-export-conditions.type.js';\n\nexport const PACKAGE_JSON_NAME = 'package.json';\nexport const PNPM_WORKSPACE_FILE_NAME = 'pnpm-workspace.yaml';\nexport const NODE_MODULES_DIRECTORY_NAME = 'node_modules';\nexport const PACKAGE_JSON_DEPENDENCY_FIELDS = [\n\t'dependencies',\n\t'devDependencies',\n\t'optionalDependencies',\n\t'peerDependencies',\n] as const;\n\nexport type PackageJsonExports = Record<string, PackageJsonExportConditions | string>;\n\nexport interface SimplifiedPackageJsonFields {\n\texports?: PackageJsonExports | undefined;\n\tbin?: Record<string, string> | undefined;\n\ttype?: 'commonjs' | 'module' | undefined;\n\ttypes?: string | undefined;\n\tscripts?: Record<string, string | undefined> | undefined;\n\tmain?: string | undefined;\n\tmodule?: string | undefined;\n\tarchetype?: PackageArchetype;\n}\n\n/**\n * This packageJson definition is a bit simplified from the real one\n */\nexport type PackageJson = Replace<JSONSchemaForNPMPackageJsonFiles, SimplifiedPackageJsonFields>;\n\nexport interface PnpmWorkspaceYaml {\n\tpackages?: string[] | undefined;\n}\n","import { existsSync } from 'node:fs';\nimport { join, normalize } from 'node:path';\nimport { PACKAGE_JSON_NAME } from '../package-json/package-json.interface.js';\nimport {\n\tnormalizeCollectFileDirnamesUpDirectoryTreeOptions,\n\ttype CollectFileDirnamesUpDirectoryTreeOptions,\n\ttype NormalizedCollectFileDirnamesUpDirectoryTreeOptions,\n} from './collect-file-dirname-paths-up-directory-tree.function.options.js';\n\nexport const collectFileDirnamePathsUpDirectoryTree = (\n\tfileName: string,\n\trawOptions?: CollectFileDirnamesUpDirectoryTreeOptions,\n): string[] => {\n\tconst options = normalizeCollectFileDirnamesUpDirectoryTreeOptions(rawOptions);\n\treturn collectFileDirnamePathsUpDirectoryTreeInternal(fileName, options, [], []);\n};\n\nconst collectFileDirnamePathsUpDirectoryTreeInternal = (\n\tfileName: string,\n\toptions: NormalizedCollectFileDirnamesUpDirectoryTreeOptions,\n\tresultCollection: string[],\n\tpackageJsonCollection: string[],\n): string[] => {\n\tconst path = normalize(options.cwd);\n\n\tif (\n\t\tpackageJsonCollection.length < options.maxPackages &&\n\t\tresultCollection.length < options.maxResults &&\n\t\texistsSync(join(path, fileName))\n\t) {\n\t\tresultCollection.unshift(path);\n\t}\n\n\tif (\n\t\tpackageJsonCollection.length < options.maxPackages &&\n\t\texistsSync(join(path, PACKAGE_JSON_NAME))\n\t) {\n\t\tpackageJsonCollection.unshift(path);\n\t}\n\n\tconst parentPath = join(path, '..');\n\tif (\n\t\tparentPath !== path &&\n\t\toptions.depth > 0 &&\n\t\tpackageJsonCollection.length < options.maxPackages &&\n\t\tresultCollection.length < options.maxResults\n\t) {\n\t\treturn collectFileDirnamePathsUpDirectoryTreeInternal(\n\t\t\tfileName,\n\t\t\t{ ...options, depth: options.depth - 1, cwd: parentPath },\n\t\t\tresultCollection,\n\t\t\tpackageJsonCollection,\n\t\t);\n\t}\n\n\treturn resultCollection;\n};\n","import { PACKAGE_JSON_NAME } from '../package-json/package-json.interface.js';\nimport { collectFileDirnamePathsUpDirectoryTree } from './collect-file-dirname-paths-up-directory-tree.function.js';\nimport type { CollectFileDirnamesUpDirectoryTreeOptions } from './collect-file-dirname-paths-up-directory-tree.function.options.js';\n\n/**\n * Returns the furthest folder where a package.json file is present\n *\n * (If you're searching for the nearest, use the getWorkspaceRoot function)\n */\nexport const getWorkspaceRoot = (\n\trawOptions?: CollectFileDirnamesUpDirectoryTreeOptions,\n): string | undefined => {\n\treturn collectFileDirnamePathsUpDirectoryTree(PACKAGE_JSON_NAME, rawOptions)[0];\n};\n"],"mappings":";;;;AAsCA,IAAa,sDACZ,YACyD;CACzD,OAAO;EACN,IAAA,GAAA,cAAA,mBAAA,CAAsB,OAAO;EAC7B,IAAA,GAAA,c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