UNPKG

@alexaegis/workspace-tools

Version:

Tools for working with javascript workspaces

1 lines 8.39 kB
{"version":3,"file":"index.cjs","names":[],"sources":["../src/ignore/collect-ignore-entries.function.options.ts","../src/ignore/parse-ignore-file.function.ts","../src/ignore/collect-ignore-entries.function.ts","../src/package-json/get-package-json-template-variables.function.ts","../src/package-json/is-package-json-dependency-field.function.ts","../src/package-json/package-archetype.interface.ts"],"sourcesContent":["import type { Defined } from '@alexaegis/common';\nimport {\n\tnormalizeCollectFileDirnamesUpDirectoryTreeOptions,\n\ttype CollectFileDirnamesUpDirectoryTreeOptions,\n} from '../npm/collect-file-dirname-paths-up-directory-tree.function.options.js';\n\nexport const GITIGNORE_FILENAME = '.gitignore';\n\nexport type CollectIgnoreEntriesOptions = CollectFileDirnamesUpDirectoryTreeOptions & {\n\tignoreFileName?: string | undefined;\n};\n\nexport type NormalizedCollectIgnoreEntriesOptions = Defined<CollectIgnoreEntriesOptions>;\n\nexport const normalizeCollectIgnoreEntriesOptions = (\n\toptions?: CollectIgnoreEntriesOptions,\n): NormalizedCollectIgnoreEntriesOptions => {\n\treturn {\n\t\t...normalizeCollectFileDirnamesUpDirectoryTreeOptions(options),\n\t\tignoreFileName: options?.ignoreFileName ?? GITIGNORE_FILENAME,\n\t};\n};\n","import { HASH_COMMENT, NEWLINE } from '@alexaegis/fs';\n\n/**\n * Strips out comments from lines, returns the then non-empty lines.\n * It is meant for ignore files like `.gitignore` but all it does is split\n * a string line by line and strip hash comments off so the name is more\n * generic too\n *\n * @param content content of a file like .gitignore\n *\n * @example a file like\n *\n * ```text\n *\t\tfoo\n *\t\tbar\n *\n *\t\t# comment\n *\n *\n *\t\tzed # comment\n *\t\t#\n * ```\n *\n * will be parsed as ['foo', 'bar', 'zed'] by this function\n */\nexport const splitAndStripHashComments = (content: string): string[] => {\n\treturn content\n\t\t.split(NEWLINE)\n\t\t.map((line) => line.replace(HASH_COMMENT, '').trim())\n\t\t.filter((line) => !!line);\n};\n","import { asyncFilterMap } from '@alexaegis/common';\nimport { readFile } from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { collectFileDirnamePathsUpDirectoryTree, getWorkspaceRoot } from '../index.js';\nimport {\n\tnormalizeCollectIgnoreEntriesOptions,\n\ttype CollectIgnoreEntriesOptions,\n} from './collect-ignore-entries.function.options.js';\nimport { splitAndStripHashComments } from './parse-ignore-file.function.js';\n\n/**\n * Collects every ignore entry within a workspace affecting cwd\n * It will only collect ignore\n *\n * @param rawOptions by default ignoreFile is `.gitignore`\n */\nexport const collectIgnoreEntries = async (\n\trawOptions?: CollectIgnoreEntriesOptions,\n): Promise<string[]> => {\n\tconst options = normalizeCollectIgnoreEntriesOptions(rawOptions);\n\tconst workspaceRoot = getWorkspaceRoot();\n\n\tif (!workspaceRoot) {\n\t\tthrow new Error('Not in a workspace!');\n\t}\n\n\tconst ignoreFileDirnames = collectFileDirnamePathsUpDirectoryTree(\n\t\toptions.ignoreFileName,\n\t\toptions,\n\t);\n\n\tconst ignoreFilePathsWithinWorkspace = ignoreFileDirnames\n\t\t.filter((ignoreFileDirname) => ignoreFileDirname.startsWith(workspaceRoot))\n\t\t.map((ignoreFileDirname) => join(ignoreFileDirname, options.ignoreFileName));\n\n\tconst ignoreFileLines = await asyncFilterMap(\n\t\tignoreFilePathsWithinWorkspace,\n\t\tasync (ignoreFile) => {\n\t\t\tconst gitIgnoreFile = await readFile(ignoreFile, {\n\t\t\t\tencoding: 'utf8',\n\t\t\t});\n\t\t\treturn splitAndStripHashComments(gitIgnoreFile);\n\t\t},\n\t);\n\n\treturn ignoreFileLines.flat();\n};\n","import type { PackageJson } from './package-json.interface.js';\n\nexport type PackageJsonTemplateVariableNames =\n\t'packageName' | 'packageOrg' | 'packageNameWithoutOrg';\n\nexport type PackageJsonTemplateVariables = Record<PackageJsonTemplateVariableNames, string>;\n\nexport const getPackageJsonTemplateVariables = (\n\tpackageJson: PackageJson,\n): PackageJsonTemplateVariables & Record<string, string> => {\n\tconst packageName = packageJson.name ?? '';\n\tlet packageOrg: string | undefined;\n\tlet packageNameWithoutOrg: string = packageName;\n\tif (packageName.includes('/')) {\n\t\tconst [splitPackageOrg, ...splitPackageName] = packageName.split('/');\n\t\tpackageOrg = splitPackageOrg;\n\t\tpackageNameWithoutOrg = splitPackageName.join('/');\n\t}\n\n\treturn {\n\t\tpackageOrg: packageOrg ?? '',\n\t\tpackageName,\n\t\tpackageNameWithoutOrg,\n\t};\n};\n","import { PACKAGE_JSON_DEPENDENCY_FIELDS } from './package-json.interface.js';\n\nexport type PackageJsonDependencyField = (typeof PACKAGE_JSON_DEPENDENCY_FIELDS)[number];\n\n/**\n * Returns true for 'dependencies', 'devDependencies',\n * 'optionalDependencies' and 'peerDependencies'\n */\nexport const isPackageJsonDependencyField = (\n\tfield: unknown,\n): field is PackageJsonDependencyField => {\n\treturn PACKAGE_JSON_DEPENDENCY_FIELDS.includes(field as PackageJsonDependencyField);\n};\n","import { type Nullable } from '@alexaegis/common';\nimport type { JsonMatcherFrom } from '@alexaegis/match';\n/**\n * The archetypical description of a project present in the \"archetype\" field\n * of the package.json. It's not the full description as other package.json\n * fields can be read, most notably the \"private\" field dictates if a package\n * is to be published or not. That information is not duplicated.\n *\n * node-app\n * node-lib\n * web-svelte-app\n * web-svelte-lib\n */\nexport interface PackageArchetype {\n\tplatform?: Nullable<'node' | 'web'>;\n\n\t/**\n\t * @example 'node' | 'svelte' | 'angular'\n\t */\n\tframework?: Nullable<string>;\n\n\t/**\n\t * @example 'ts' | 'js'\n\t */\n\tlanguage?: Nullable<string>;\n\n\tkind?: Nullable<'app' | 'lib' | 'fixture'>;\n\n\t/**\n\t * @example 'vite' | 'rollup'\n\t */\n\tbundler?: Nullable<string>;\n\n\t/**\n\t * @example 'vitest' | 'jest' | 'mocha'\n\t */\n\ttesting?: Nullable<string>;\n\n\t/**\n\t * You can disable specific setup plugins if you wish to skip them in a\n\t * specific package, even if it would otherwise match the rest of the\n\t * archetype.\n\t *\n\t * Like define `[\"@alexaegis/autotool-plugin-vitest\"]` if you don't want it\n\t * to be applied.\n\t *\n\t * Each treated as a RegExp\n\t */\n\tdisabledPlugins?: string[];\n}\n\nexport type PackageJsonArchetypeMatcher = JsonMatcherFrom<PackageArchetype>;\n\n/**\n * This will return names for when you want to denote the archetype in a\n * filename: `tsconfig.web-svelte-lib.json` or `tsconfig.node-lib.json`\n */\nexport const getEncodedArchetype = (\n\tarchetypeMatcher?: PackageJsonArchetypeMatcher | undefined,\n): string => {\n\tif (!archetypeMatcher || typeof archetypeMatcher === 'function') {\n\t\treturn '';\n\t}\n\n\tconst orderedValues = [\n\t\tarchetypeMatcher.platform,\n\t\tarchetypeMatcher.framework,\n\t\tarchetypeMatcher.language,\n\t\tarchetypeMatcher.kind,\n\t\tarchetypeMatcher.bundler,\n\t\tarchetypeMatcher.testing,\n\t];\n\n\treturn orderedValues.filter((value) => typeof value === 'string').join('-');\n};\n"],"mappings":";;;;;;;;;;;AAMA,IAAa,qBAAqB;AAQlC,IAAa,wCACZ,YAC2C;CAC3C,OAAO;EACN,GAAG,oCAAA,mDAAmD,OAAO;EAC7D,gBAAgB,SAAS,kBAAA;CAC1B;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;ACIA,IAAa,6BAA6B,YAA8B;CACvE,OAAO,QACL,MAAM,cAAA,OAAO,CAAC,CACd,KAAK,SAAS,KAAK,QAAQ,cAAA,cAAc,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CACpD,QAAQ,SAAS,CAAC,CAAC,IAAI;AAC1B;;;;;;;;;ACdA,IAAa,uBAAuB,OACnC,eACuB;CACvB,MAAM,UAAU,qCAAqC,UAAU;CAC/D,MAAM,gBAAgB,oCAAA,iBAAiB;CAEvC,IAAI,CAAC,eACJ,MAAM,IAAI,MAAM,qBAAqB;CAsBtC,QAAO,OAAA,GAAA,kBAAA,eAAA,CAnBoB,oCAAA,uCAC1B,QAAQ,gBACR,OAGsC,CAAA,CACrC,QAAQ,sBAAsB,kBAAkB,WAAW,aAAa,CAAC,CAA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