package-management 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +309 -4
- package/dist/index.d.cts +417 -1
- package/dist/index.d.mts +417 -1
- package/dist/index.d.ts +417 -1
- package/dist/index.mjs +298 -6
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import ErrorStackParser from 'error-stack-parser';
|
|
2
|
+
import path$1, { normalize, basename, dirname, relative } from 'pathe';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
6
|
+
import { execa, execaSync } from 'execa';
|
|
4
7
|
import { asyncCacheFn } from 'async-cache-fn';
|
|
5
8
|
import { findUp } from 'find-up';
|
|
6
9
|
import { readFile } from 'node:fs/promises';
|
|
@@ -8,8 +11,8 @@ import { resolvePathSync } from 'mlly';
|
|
|
8
11
|
import { globbySync } from 'globby';
|
|
9
12
|
import gitignoreParser from 'parse-gitignore';
|
|
10
13
|
import * as WST from 'workspace-tools';
|
|
11
|
-
import
|
|
12
|
-
import
|
|
14
|
+
import { resolveAlias } from 'pathe/utils';
|
|
15
|
+
import os from 'node:os';
|
|
13
16
|
|
|
14
17
|
const toArray = (data) => Array.isArray(data) ? data : [data];
|
|
15
18
|
const entriesOf = (o) => Object.entries(o);
|
|
@@ -37,6 +40,115 @@ const invariant = (predicate, message) => {
|
|
|
37
40
|
throw new Error(message);
|
|
38
41
|
}
|
|
39
42
|
};
|
|
43
|
+
function getArrayItemAtOffset(arr, index, offset = 0) {
|
|
44
|
+
if (!index || !arr)
|
|
45
|
+
return void 0;
|
|
46
|
+
return arr[index + offset];
|
|
47
|
+
}
|
|
48
|
+
function isMatching(a, b) {
|
|
49
|
+
if (!a || !b)
|
|
50
|
+
return false;
|
|
51
|
+
return a === b;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function findCallerStackFrame(options) {
|
|
55
|
+
return findErrorStackFrame(options, (frame) => frame.isParentOfRootFunction);
|
|
56
|
+
}
|
|
57
|
+
function findErrorStackFrame(options, find) {
|
|
58
|
+
return getErrorStackFrames(options, find)[0];
|
|
59
|
+
}
|
|
60
|
+
function getErrorStackFrames(options, filter) {
|
|
61
|
+
const { error, rootFunctionName, startFrom = "top" } = options;
|
|
62
|
+
const frames = startFrom === "bottom" ? ErrorStackParser.parse(error).reverse() : ErrorStackParser.parse(error);
|
|
63
|
+
const { parsedFrames } = frames.reduce(
|
|
64
|
+
(acc, frame, index) => {
|
|
65
|
+
const parsed = parseFrame({
|
|
66
|
+
frame,
|
|
67
|
+
index,
|
|
68
|
+
rootFunctionName,
|
|
69
|
+
frames
|
|
70
|
+
});
|
|
71
|
+
const isValid = filter ? filter(parsed) : true;
|
|
72
|
+
if (isValid) {
|
|
73
|
+
acc.parsedFrames.push(parsed);
|
|
74
|
+
}
|
|
75
|
+
return acc;
|
|
76
|
+
},
|
|
77
|
+
{ parsedFrames: [] }
|
|
78
|
+
);
|
|
79
|
+
return parsedFrames ?? [];
|
|
80
|
+
}
|
|
81
|
+
function parseFrame(options) {
|
|
82
|
+
const { frame, frames, index, rootFunctionName, cwd, debug } = options ?? {};
|
|
83
|
+
const functionName = parseFunctionName(frame.functionName);
|
|
84
|
+
const isRootFunction = isMatching(functionName, rootFunctionName);
|
|
85
|
+
const beforeFrameFunctionName = parseFunctionName(
|
|
86
|
+
getArrayItemAtOffset(frames, index, -1)?.functionName
|
|
87
|
+
);
|
|
88
|
+
const isParentOfRootFunction = isMatching(
|
|
89
|
+
beforeFrameFunctionName,
|
|
90
|
+
rootFunctionName
|
|
91
|
+
);
|
|
92
|
+
const fileData = frame.fileName ? getFilePathData({ filepath: frame.fileName, cwd }) : void 0;
|
|
93
|
+
const { isFileInCwd: isFrameInScope, ...restFileData } = fileData ?? {};
|
|
94
|
+
const data = {
|
|
95
|
+
...restFileData,
|
|
96
|
+
functionName,
|
|
97
|
+
source: frame.source,
|
|
98
|
+
sourceFunctionName: frame.functionName,
|
|
99
|
+
isFrameInScope,
|
|
100
|
+
place: placeFormatter(index, frames?.length),
|
|
101
|
+
isRootFunction,
|
|
102
|
+
isParentOfRootFunction,
|
|
103
|
+
rootFunctionName
|
|
104
|
+
};
|
|
105
|
+
debug && console.log(data);
|
|
106
|
+
return data;
|
|
107
|
+
}
|
|
108
|
+
function getFilePathData({
|
|
109
|
+
filepath,
|
|
110
|
+
cwd
|
|
111
|
+
}) {
|
|
112
|
+
const workingDir = cwd ?? process.cwd();
|
|
113
|
+
const filePath = normalize(filepath);
|
|
114
|
+
const fileBasename = basename(filePath);
|
|
115
|
+
const dirPath = dirname(filePath);
|
|
116
|
+
const dirBasename = basename(dirPath);
|
|
117
|
+
const relativeFilePath = relative(workingDir, filePath);
|
|
118
|
+
const relativeDirPath = dirname(relativeFilePath);
|
|
119
|
+
const isFileInCwd = filePath.startsWith(workingDir);
|
|
120
|
+
return {
|
|
121
|
+
filePath,
|
|
122
|
+
dirPath,
|
|
123
|
+
relativeFilePath,
|
|
124
|
+
relativeDirPath,
|
|
125
|
+
fileBasename,
|
|
126
|
+
dirBasename,
|
|
127
|
+
isFileInCwd
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function parseFunctionName(functionName) {
|
|
131
|
+
if (!functionName)
|
|
132
|
+
return void 0;
|
|
133
|
+
return removeStart(functionName, "Module.");
|
|
134
|
+
}
|
|
135
|
+
function removeStart(input, value) {
|
|
136
|
+
return input.startsWith(value) ? input.slice(value.length) : input;
|
|
137
|
+
}
|
|
138
|
+
function placeFormatter(index, total) {
|
|
139
|
+
if (!index || !total)
|
|
140
|
+
return void 0;
|
|
141
|
+
return [index, total].join("/");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const _filename = (options) => {
|
|
145
|
+
const { filePath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
|
|
146
|
+
return filePath;
|
|
147
|
+
};
|
|
148
|
+
const _dirname = (options) => {
|
|
149
|
+
const { dirPath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
|
|
150
|
+
return dirPath;
|
|
151
|
+
};
|
|
40
152
|
|
|
41
153
|
async function resolveModule(module) {
|
|
42
154
|
const resolved = await module;
|
|
@@ -659,4 +771,184 @@ function isPackageDependency(packageName) {
|
|
|
659
771
|
return toArray(packageName).every((name) => isDependencyInPackageJson(name));
|
|
660
772
|
}
|
|
661
773
|
|
|
662
|
-
|
|
774
|
+
function definePathAliases(aliasDefinitions) {
|
|
775
|
+
const aliasMap = getAliasMap(aliasDefinitions);
|
|
776
|
+
function getFilePath(options, aliases) {
|
|
777
|
+
return getAliasedFilePath(
|
|
778
|
+
{ ...aliasMap, ...aliases },
|
|
779
|
+
options
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
return {
|
|
783
|
+
aliasDefinitions,
|
|
784
|
+
aliasMap,
|
|
785
|
+
getFilePath
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
function getAliasedFilePath(aliasMap, options) {
|
|
789
|
+
try {
|
|
790
|
+
if (typeof options === "string" || Array.isArray(options)) {
|
|
791
|
+
return resolvePathTo(options, { aliasMap });
|
|
792
|
+
}
|
|
793
|
+
const opts = { ...options, aliasMap };
|
|
794
|
+
if (opts.startingFrom) {
|
|
795
|
+
return resolveRelativePathTo(opts.to, opts.startingFrom, opts);
|
|
796
|
+
}
|
|
797
|
+
return resolvePathTo(opts.to, opts);
|
|
798
|
+
} catch (e) {
|
|
799
|
+
return void 0;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function resolveRelativePathTo(to, from, options) {
|
|
803
|
+
const pathFrom = resolvePathTo(from, options);
|
|
804
|
+
const pathTo = resolvePathTo(to, options);
|
|
805
|
+
return path$1.relative(pathFrom, pathTo);
|
|
806
|
+
}
|
|
807
|
+
function resolvePathTo(pathTo, { cwd, checkExistence, glob, aliasMap }) {
|
|
808
|
+
const normalized = normalizePathTo(pathTo, { cwd, aliasMap });
|
|
809
|
+
if (glob) {
|
|
810
|
+
const globPaths = globbySync(normalized, { cwd });
|
|
811
|
+
if (!globPaths[0])
|
|
812
|
+
throw new Error(`No paths found for glob: ${normalized}`);
|
|
813
|
+
return globPaths[0];
|
|
814
|
+
}
|
|
815
|
+
if (!glob && checkExistence && !existsSync(normalized))
|
|
816
|
+
throw new Error(`Path does not exist: ${normalized}`);
|
|
817
|
+
return normalized;
|
|
818
|
+
}
|
|
819
|
+
function normalizePathTo(pathTo, options) {
|
|
820
|
+
const { cwd, aliasMap } = options ?? {};
|
|
821
|
+
if (Array.isArray(pathTo)) {
|
|
822
|
+
const [baseDir] = pathTo;
|
|
823
|
+
const aliasedPath = path$1.join(...pathTo.filter(isNotNull));
|
|
824
|
+
return resolveAlias(aliasedPath, {
|
|
825
|
+
[baseDir]: executeMapFn(aliasMap, baseDir, [{ cwd }])
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
return pathTo;
|
|
829
|
+
}
|
|
830
|
+
function executeMapFn(map, key, args) {
|
|
831
|
+
if (!map || !key || !(key in map))
|
|
832
|
+
return void 0;
|
|
833
|
+
const fnArgs = Array.isArray(args) ? args : [args];
|
|
834
|
+
const resolver = map?.[key];
|
|
835
|
+
return typeof resolver === "function" ? resolver?.(...fnArgs) : resolver;
|
|
836
|
+
}
|
|
837
|
+
function getAliasMap(aliasDefs) {
|
|
838
|
+
return Object.fromEntries(
|
|
839
|
+
Object.entries(aliasDefs).flatMap(([alias, v]) => {
|
|
840
|
+
const { resolve, subpaths = [] } = v;
|
|
841
|
+
return [
|
|
842
|
+
[alias, resolve],
|
|
843
|
+
...subpaths.map(({ to }) => {
|
|
844
|
+
const subpathAlias = path$1.join(alias, to);
|
|
845
|
+
return [
|
|
846
|
+
subpathAlias,
|
|
847
|
+
(opts) => resolveAlias(subpathAlias, { [alias]: resolve(opts) })
|
|
848
|
+
];
|
|
849
|
+
})
|
|
850
|
+
];
|
|
851
|
+
})
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
function isNotNull(value) {
|
|
855
|
+
return value !== null && value !== void 0;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const predefinedPathAliases = {
|
|
859
|
+
"<workspace_folder>": {
|
|
860
|
+
resolve: (opts) => getWorkspaceFolder(opts),
|
|
861
|
+
subpaths: [
|
|
862
|
+
{
|
|
863
|
+
to: "node_modules"
|
|
864
|
+
},
|
|
865
|
+
{
|
|
866
|
+
to: "node_modules/.bin"
|
|
867
|
+
}
|
|
868
|
+
]
|
|
869
|
+
},
|
|
870
|
+
"<workspace_folder?>": {
|
|
871
|
+
resolve: (opts) => getWorkspaceFolder(opts),
|
|
872
|
+
subpaths: [
|
|
873
|
+
{
|
|
874
|
+
to: "node_modules"
|
|
875
|
+
},
|
|
876
|
+
{
|
|
877
|
+
to: "node_modules/.bin"
|
|
878
|
+
}
|
|
879
|
+
]
|
|
880
|
+
},
|
|
881
|
+
"<package_folder>": {
|
|
882
|
+
resolve: (opts) => getPackageFolder(opts),
|
|
883
|
+
subpaths: [
|
|
884
|
+
{
|
|
885
|
+
to: "node_modules"
|
|
886
|
+
},
|
|
887
|
+
{
|
|
888
|
+
to: "node_modules/.bin"
|
|
889
|
+
},
|
|
890
|
+
{
|
|
891
|
+
to: "src"
|
|
892
|
+
}
|
|
893
|
+
]
|
|
894
|
+
},
|
|
895
|
+
"<gitroot_folder>": {
|
|
896
|
+
resolve: (opts) => getGitRootFolder(opts),
|
|
897
|
+
subpaths: [
|
|
898
|
+
{
|
|
899
|
+
to: "node_modules"
|
|
900
|
+
},
|
|
901
|
+
{
|
|
902
|
+
to: "node_modules/.bin"
|
|
903
|
+
},
|
|
904
|
+
{
|
|
905
|
+
to: ".vscode"
|
|
906
|
+
}
|
|
907
|
+
]
|
|
908
|
+
},
|
|
909
|
+
"<user_home>": {
|
|
910
|
+
resolve: () => os.homedir(),
|
|
911
|
+
subpaths: []
|
|
912
|
+
},
|
|
913
|
+
"<user_tmpdir>": {
|
|
914
|
+
resolve: () => os.tmpdir(),
|
|
915
|
+
subpaths: []
|
|
916
|
+
},
|
|
917
|
+
"<cwd>": {
|
|
918
|
+
resolve: () => process.cwd(),
|
|
919
|
+
subpaths: []
|
|
920
|
+
},
|
|
921
|
+
"<current_file>": {
|
|
922
|
+
// resolve: () => fileURLToPath(import.meta.url),
|
|
923
|
+
resolve: () => _filename({ rootFunctionName: "getFilePath" }) ?? "",
|
|
924
|
+
subpaths: []
|
|
925
|
+
},
|
|
926
|
+
"<current_folder>": {
|
|
927
|
+
// resolve: () => fileURLToPath(import.meta.url),
|
|
928
|
+
resolve: () => _dirname({ rootFunctionName: "getFilePath" }) ?? "",
|
|
929
|
+
subpaths: []
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
const getPath = definePathAliases(predefinedPathAliases).getFilePath;
|
|
934
|
+
getPath(["<workspace_folder>/node_modules/.bin"]);
|
|
935
|
+
|
|
936
|
+
function getFolderByPackageName(name, options) {
|
|
937
|
+
const infoMap = getWorkspacePackageInfoMap({ ...options, includeRoot: true });
|
|
938
|
+
const info = infoMap?.[name];
|
|
939
|
+
return info?.dirpath;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function getGitRootFolder(options) {
|
|
943
|
+
const output = execaSync(`git rev-parse --show-toplevel`, {
|
|
944
|
+
...options,
|
|
945
|
+
reject: false
|
|
946
|
+
});
|
|
947
|
+
if (!output.stdout) {
|
|
948
|
+
console.warn(`Directory "${output.cwd}" is not in a git repository`);
|
|
949
|
+
return void 0;
|
|
950
|
+
}
|
|
951
|
+
return path$1.normalize(output.stdout);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
export { _dirname, _filename, definePackage, definePackageManagerClient, definePathAliases, detectGlobalPackageManagers, detectLockfilePackageManagers, detectPackageManagers, filterPackageManagers, findPackageManager, findPackageManagerSafely, findResolvedModulePath, getAliasMap, getFolderByPackageName, getGitRootFolder, getPackageFolder, getPath, getWorkspaceFolder, importMap, importer, isPackageDependency, isPackageModuleFound, packageManagerConfigs, predefinedPathAliases, resolveModule, resolveModulePath, resolvePackageModulePath, workspace };
|