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 CHANGED
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ const ErrorStackParser = require('error-stack-parser');
3
4
  const path = require('pathe');
5
+ const process = require('node:process');
6
+ const path$1 = require('node:path');
4
7
  const node_fs = require('node:fs');
5
8
  const execa = require('execa');
6
9
  const asyncCacheFn = require('async-cache-fn');
@@ -10,8 +13,8 @@ const mlly = require('mlly');
10
13
  const globby = require('globby');
11
14
  const gitignoreParser = require('parse-gitignore');
12
15
  const WST = require('workspace-tools');
13
- const process = require('node:process');
14
- const path$1 = require('node:path');
16
+ const utils = require('pathe/utils');
17
+ const os = require('node:os');
15
18
 
16
19
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
20
 
@@ -27,11 +30,13 @@ function _interopNamespaceCompat(e) {
27
30
  return n;
28
31
  }
29
32
 
33
+ const ErrorStackParser__default = /*#__PURE__*/_interopDefaultCompat(ErrorStackParser);
30
34
  const path__default$1 = /*#__PURE__*/_interopDefaultCompat(path);
31
- const gitignoreParser__default = /*#__PURE__*/_interopDefaultCompat(gitignoreParser);
32
- const WST__namespace = /*#__PURE__*/_interopNamespaceCompat(WST);
33
35
  const process__default = /*#__PURE__*/_interopDefaultCompat(process);
34
36
  const path__default = /*#__PURE__*/_interopDefaultCompat(path$1);
37
+ const gitignoreParser__default = /*#__PURE__*/_interopDefaultCompat(gitignoreParser);
38
+ const WST__namespace = /*#__PURE__*/_interopNamespaceCompat(WST);
39
+ const os__default = /*#__PURE__*/_interopDefaultCompat(os);
35
40
 
36
41
  const toArray = (data) => Array.isArray(data) ? data : [data];
37
42
  const entriesOf = (o) => Object.entries(o);
@@ -59,6 +64,115 @@ const invariant = (predicate, message) => {
59
64
  throw new Error(message);
60
65
  }
61
66
  };
67
+ function getArrayItemAtOffset(arr, index, offset = 0) {
68
+ if (!index || !arr)
69
+ return void 0;
70
+ return arr[index + offset];
71
+ }
72
+ function isMatching(a, b) {
73
+ if (!a || !b)
74
+ return false;
75
+ return a === b;
76
+ }
77
+
78
+ function findCallerStackFrame(options) {
79
+ return findErrorStackFrame(options, (frame) => frame.isParentOfRootFunction);
80
+ }
81
+ function findErrorStackFrame(options, find) {
82
+ return getErrorStackFrames(options, find)[0];
83
+ }
84
+ function getErrorStackFrames(options, filter) {
85
+ const { error, rootFunctionName, startFrom = "top" } = options;
86
+ const frames = startFrom === "bottom" ? ErrorStackParser__default.parse(error).reverse() : ErrorStackParser__default.parse(error);
87
+ const { parsedFrames } = frames.reduce(
88
+ (acc, frame, index) => {
89
+ const parsed = parseFrame({
90
+ frame,
91
+ index,
92
+ rootFunctionName,
93
+ frames
94
+ });
95
+ const isValid = filter ? filter(parsed) : true;
96
+ if (isValid) {
97
+ acc.parsedFrames.push(parsed);
98
+ }
99
+ return acc;
100
+ },
101
+ { parsedFrames: [] }
102
+ );
103
+ return parsedFrames ?? [];
104
+ }
105
+ function parseFrame(options) {
106
+ const { frame, frames, index, rootFunctionName, cwd, debug } = options ?? {};
107
+ const functionName = parseFunctionName(frame.functionName);
108
+ const isRootFunction = isMatching(functionName, rootFunctionName);
109
+ const beforeFrameFunctionName = parseFunctionName(
110
+ getArrayItemAtOffset(frames, index, -1)?.functionName
111
+ );
112
+ const isParentOfRootFunction = isMatching(
113
+ beforeFrameFunctionName,
114
+ rootFunctionName
115
+ );
116
+ const fileData = frame.fileName ? getFilePathData({ filepath: frame.fileName, cwd }) : void 0;
117
+ const { isFileInCwd: isFrameInScope, ...restFileData } = fileData ?? {};
118
+ const data = {
119
+ ...restFileData,
120
+ functionName,
121
+ source: frame.source,
122
+ sourceFunctionName: frame.functionName,
123
+ isFrameInScope,
124
+ place: placeFormatter(index, frames?.length),
125
+ isRootFunction,
126
+ isParentOfRootFunction,
127
+ rootFunctionName
128
+ };
129
+ debug && console.log(data);
130
+ return data;
131
+ }
132
+ function getFilePathData({
133
+ filepath,
134
+ cwd
135
+ }) {
136
+ const workingDir = cwd ?? process__default.cwd();
137
+ const filePath = path.normalize(filepath);
138
+ const fileBasename = path.basename(filePath);
139
+ const dirPath = path.dirname(filePath);
140
+ const dirBasename = path.basename(dirPath);
141
+ const relativeFilePath = path.relative(workingDir, filePath);
142
+ const relativeDirPath = path.dirname(relativeFilePath);
143
+ const isFileInCwd = filePath.startsWith(workingDir);
144
+ return {
145
+ filePath,
146
+ dirPath,
147
+ relativeFilePath,
148
+ relativeDirPath,
149
+ fileBasename,
150
+ dirBasename,
151
+ isFileInCwd
152
+ };
153
+ }
154
+ function parseFunctionName(functionName) {
155
+ if (!functionName)
156
+ return void 0;
157
+ return removeStart(functionName, "Module.");
158
+ }
159
+ function removeStart(input, value) {
160
+ return input.startsWith(value) ? input.slice(value.length) : input;
161
+ }
162
+ function placeFormatter(index, total) {
163
+ if (!index || !total)
164
+ return void 0;
165
+ return [index, total].join("/");
166
+ }
167
+
168
+ const _filename = (options) => {
169
+ const { filePath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
170
+ return filePath;
171
+ };
172
+ const _dirname = (options) => {
173
+ const { dirPath } = findCallerStackFrame({ error: new Error(), ...options }) ?? {};
174
+ return dirPath;
175
+ };
62
176
 
63
177
  async function resolveModule(module) {
64
178
  const resolved = await module;
@@ -681,8 +795,191 @@ function isPackageDependency(packageName) {
681
795
  return toArray(packageName).every((name) => isDependencyInPackageJson(name));
682
796
  }
683
797
 
798
+ function definePathAliases(aliasDefinitions) {
799
+ const aliasMap = getAliasMap(aliasDefinitions);
800
+ function getFilePath(options, aliases) {
801
+ return getAliasedFilePath(
802
+ { ...aliasMap, ...aliases },
803
+ options
804
+ );
805
+ }
806
+ return {
807
+ aliasDefinitions,
808
+ aliasMap,
809
+ getFilePath
810
+ };
811
+ }
812
+ function getAliasedFilePath(aliasMap, options) {
813
+ try {
814
+ if (typeof options === "string" || Array.isArray(options)) {
815
+ return resolvePathTo(options, { aliasMap });
816
+ }
817
+ const opts = { ...options, aliasMap };
818
+ if (opts.startingFrom) {
819
+ return resolveRelativePathTo(opts.to, opts.startingFrom, opts);
820
+ }
821
+ return resolvePathTo(opts.to, opts);
822
+ } catch (e) {
823
+ return void 0;
824
+ }
825
+ }
826
+ function resolveRelativePathTo(to, from, options) {
827
+ const pathFrom = resolvePathTo(from, options);
828
+ const pathTo = resolvePathTo(to, options);
829
+ return path__default$1.relative(pathFrom, pathTo);
830
+ }
831
+ function resolvePathTo(pathTo, { cwd, checkExistence, glob, aliasMap }) {
832
+ const normalized = normalizePathTo(pathTo, { cwd, aliasMap });
833
+ if (glob) {
834
+ const globPaths = globby.globbySync(normalized, { cwd });
835
+ if (!globPaths[0])
836
+ throw new Error(`No paths found for glob: ${normalized}`);
837
+ return globPaths[0];
838
+ }
839
+ if (!glob && checkExistence && !node_fs.existsSync(normalized))
840
+ throw new Error(`Path does not exist: ${normalized}`);
841
+ return normalized;
842
+ }
843
+ function normalizePathTo(pathTo, options) {
844
+ const { cwd, aliasMap } = options ?? {};
845
+ if (Array.isArray(pathTo)) {
846
+ const [baseDir] = pathTo;
847
+ const aliasedPath = path__default$1.join(...pathTo.filter(isNotNull));
848
+ return utils.resolveAlias(aliasedPath, {
849
+ [baseDir]: executeMapFn(aliasMap, baseDir, [{ cwd }])
850
+ });
851
+ }
852
+ return pathTo;
853
+ }
854
+ function executeMapFn(map, key, args) {
855
+ if (!map || !key || !(key in map))
856
+ return void 0;
857
+ const fnArgs = Array.isArray(args) ? args : [args];
858
+ const resolver = map?.[key];
859
+ return typeof resolver === "function" ? resolver?.(...fnArgs) : resolver;
860
+ }
861
+ function getAliasMap(aliasDefs) {
862
+ return Object.fromEntries(
863
+ Object.entries(aliasDefs).flatMap(([alias, v]) => {
864
+ const { resolve, subpaths = [] } = v;
865
+ return [
866
+ [alias, resolve],
867
+ ...subpaths.map(({ to }) => {
868
+ const subpathAlias = path__default$1.join(alias, to);
869
+ return [
870
+ subpathAlias,
871
+ (opts) => utils.resolveAlias(subpathAlias, { [alias]: resolve(opts) })
872
+ ];
873
+ })
874
+ ];
875
+ })
876
+ );
877
+ }
878
+ function isNotNull(value) {
879
+ return value !== null && value !== void 0;
880
+ }
881
+
882
+ const predefinedPathAliases = {
883
+ "<workspace_folder>": {
884
+ resolve: (opts) => getWorkspaceFolder(opts),
885
+ subpaths: [
886
+ {
887
+ to: "node_modules"
888
+ },
889
+ {
890
+ to: "node_modules/.bin"
891
+ }
892
+ ]
893
+ },
894
+ "<workspace_folder?>": {
895
+ resolve: (opts) => getWorkspaceFolder(opts),
896
+ subpaths: [
897
+ {
898
+ to: "node_modules"
899
+ },
900
+ {
901
+ to: "node_modules/.bin"
902
+ }
903
+ ]
904
+ },
905
+ "<package_folder>": {
906
+ resolve: (opts) => getPackageFolder(opts),
907
+ subpaths: [
908
+ {
909
+ to: "node_modules"
910
+ },
911
+ {
912
+ to: "node_modules/.bin"
913
+ },
914
+ {
915
+ to: "src"
916
+ }
917
+ ]
918
+ },
919
+ "<gitroot_folder>": {
920
+ resolve: (opts) => getGitRootFolder(opts),
921
+ subpaths: [
922
+ {
923
+ to: "node_modules"
924
+ },
925
+ {
926
+ to: "node_modules/.bin"
927
+ },
928
+ {
929
+ to: ".vscode"
930
+ }
931
+ ]
932
+ },
933
+ "<user_home>": {
934
+ resolve: () => os__default.homedir(),
935
+ subpaths: []
936
+ },
937
+ "<user_tmpdir>": {
938
+ resolve: () => os__default.tmpdir(),
939
+ subpaths: []
940
+ },
941
+ "<cwd>": {
942
+ resolve: () => process__default.cwd(),
943
+ subpaths: []
944
+ },
945
+ "<current_file>": {
946
+ // resolve: () => fileURLToPath(import.meta.url),
947
+ resolve: () => _filename({ rootFunctionName: "getFilePath" }) ?? "",
948
+ subpaths: []
949
+ },
950
+ "<current_folder>": {
951
+ // resolve: () => fileURLToPath(import.meta.url),
952
+ resolve: () => _dirname({ rootFunctionName: "getFilePath" }) ?? "",
953
+ subpaths: []
954
+ }
955
+ };
956
+
957
+ const getPath = definePathAliases(predefinedPathAliases).getFilePath;
958
+ getPath(["<workspace_folder>/node_modules/.bin"]);
959
+
960
+ function getFolderByPackageName(name, options) {
961
+ const infoMap = getWorkspacePackageInfoMap({ ...options, includeRoot: true });
962
+ const info = infoMap?.[name];
963
+ return info?.dirpath;
964
+ }
965
+
966
+ function getGitRootFolder(options) {
967
+ const output = execa.execaSync(`git rev-parse --show-toplevel`, {
968
+ ...options,
969
+ reject: false
970
+ });
971
+ if (!output.stdout) {
972
+ console.warn(`Directory "${output.cwd}" is not in a git repository`);
973
+ return void 0;
974
+ }
975
+ return path__default$1.normalize(output.stdout);
976
+ }
977
+
978
+ exports._dirname = _dirname;
979
+ exports._filename = _filename;
684
980
  exports.definePackage = definePackage;
685
981
  exports.definePackageManagerClient = definePackageManagerClient;
982
+ exports.definePathAliases = definePathAliases;
686
983
  exports.detectGlobalPackageManagers = detectGlobalPackageManagers;
687
984
  exports.detectLockfilePackageManagers = detectLockfilePackageManagers;
688
985
  exports.detectPackageManagers = detectPackageManagers;
@@ -690,11 +987,19 @@ exports.filterPackageManagers = filterPackageManagers;
690
987
  exports.findPackageManager = findPackageManager;
691
988
  exports.findPackageManagerSafely = findPackageManagerSafely;
692
989
  exports.findResolvedModulePath = findResolvedModulePath;
990
+ exports.getAliasMap = getAliasMap;
991
+ exports.getFolderByPackageName = getFolderByPackageName;
992
+ exports.getGitRootFolder = getGitRootFolder;
993
+ exports.getPackageFolder = getPackageFolder;
994
+ exports.getPath = getPath;
995
+ exports.getWorkspaceFolder = getWorkspaceFolder;
693
996
  exports.importMap = importMap;
694
997
  exports.importer = importer;
695
998
  exports.isPackageDependency = isPackageDependency;
696
999
  exports.isPackageModuleFound = isPackageModuleFound;
697
1000
  exports.packageManagerConfigs = packageManagerConfigs;
1001
+ exports.predefinedPathAliases = predefinedPathAliases;
698
1002
  exports.resolveModule = resolveModule;
699
1003
  exports.resolveModulePath = resolveModulePath;
700
1004
  exports.resolvePackageModulePath = resolvePackageModulePath;
1005
+ exports.workspace = workspace;