@e-mc/module 0.8.6 → 0.8.7

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.
Files changed (3) hide show
  1. package/README.md +7 -7
  2. package/index.js +94 -32
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- - https://www.unpkg.com/@e-mc/types@0.8.6/lib/index.d.ts
12
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/index.d.ts
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -213,8 +213,8 @@ interface ModuleConstructor {
213
213
  getMemUsage(format: true): string;
214
214
  getMemUsage(format?: boolean): number;
215
215
  formatCpuMem(start: CpuUsage, all?: boolean): string;
216
- getPackageVersion(name: string | [string, string], startDir: string): string;
217
- getPackageVersion(name: string | [string, string], unstable?: boolean, startDir?: string): string;
216
+ getPackageVersion(name: string | [string, string], startDir: string, baseDir?: string): string;
217
+ getPackageVersion(name: string | [string, string], unstable?: boolean, startDir?: string, baseDir?: string): string;
218
218
  checkSemVer(name: string | [string, string], options: CheckSemVerOptions): boolean;
219
219
  checkSemVer(name: string | [string, string], min: number | string, max: number | string, options: CheckSemVerOptions): boolean;
220
220
  checkSemVer(name: string | [string, string], min: number | string, max?: number | string, unstable?: boolean, startDir?: string): boolean;
@@ -232,10 +232,10 @@ interface ModuleConstructor {
232
232
 
233
233
  ## References
234
234
 
235
- - https://www.unpkg.com/@e-mc/types@0.8.6/lib/core.d.ts
236
- - https://www.unpkg.com/@e-mc/types@0.8.6/lib/logger.d.ts
237
- - https://www.unpkg.com/@e-mc/types@0.8.6/lib/module.d.ts
238
- - https://www.unpkg.com/@e-mc/types@0.8.6/lib/node.d.ts
235
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/core.d.ts
236
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/logger.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/module.d.ts
238
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/node.d.ts
239
239
 
240
240
  ## LICENSE
241
241
 
package/index.js CHANGED
@@ -132,28 +132,84 @@ const REGEXP_TORRENT = /^(?:magnet:\?xt=|(?:https?|s?ftp):\/\/[^/][^\n]*?\.(?:to
132
132
  let LOG_NEWLINE = true;
133
133
  let LOG_EMPTYLINE = false;
134
134
  let TEMP_DIR = path.join(PROCESS_CWD, "tmp");
135
- const PNPM_VER = (function () {
136
- try {
137
- const pathname = path.resolve('./node_modules/.modules.yaml');
138
- if (fs.existsSync(pathname)) {
139
- return fs.readFileSync(pathname, 'utf-8');
135
+ let PNPM_VER;
136
+ let YARN_VER;
137
+ function parseYaml(pathname) {
138
+ return require('js-yaml').load(fs.readFileSync(pathname, 'utf-8'));
139
+ }
140
+ function setPnpmVer() {
141
+ if (PNPM_VER === undefined) {
142
+ PNPM_VER = false;
143
+ const items = [];
144
+ let baseDir;
145
+ try {
146
+ let pathname = path.resolve("node_modules/.modules.yaml"), config;
147
+ if (fs.existsSync(pathname) && (0, types_1.isPlainObject)(config = parseYaml(pathname))) {
148
+ if (config.nodeLinker === 'hoisted') {
149
+ return PNPM_VER = true;
150
+ }
151
+ if (config.nodeLinker === 'pnp') {
152
+ return false;
153
+ }
154
+ const addPackage = (value) => {
155
+ let index = value.indexOf('(');
156
+ if (index !== -1) {
157
+ value = value.substring(0, index);
158
+ }
159
+ index = value.lastIndexOf(value.indexOf('@', 2) !== -1 ? '@' : '/');
160
+ const name = value.substring(1, index);
161
+ const version = value.substring(index + 1);
162
+ if (!items.find(item => item[0] === name && item[1] === version)) {
163
+ items.push([name, version]);
164
+ }
165
+ };
166
+ for (const name in config.hoistedDependencies) {
167
+ addPackage(name);
168
+ }
169
+ if (!path.isAbsolute(baseDir = config.virtualStoreDir)) {
170
+ baseDir = path.resolve('node_modules', baseDir);
171
+ }
172
+ if (!fs.existsSync(pathname = path.resolve(baseDir, "lock.yaml"))) {
173
+ baseDir = undefined;
174
+ }
175
+ else if ((0, types_1.isPlainObject)(config = parseYaml(pathname))) {
176
+ for (const name in config.packages) {
177
+ addPackage(name);
178
+ }
179
+ }
180
+ }
140
181
  }
141
- }
142
- catch {
143
- }
144
- return '';
145
- })();
146
- const YARN_VER = (function () {
147
- try {
148
- const pathname = path.resolve('./.pnp.cjs');
149
- if (fs.existsSync(pathname)) {
150
- return require(pathname);
182
+ catch {
183
+ }
184
+ if (items.length) {
185
+ PNPM_VER = { baseDir, items };
151
186
  }
152
187
  }
153
- catch {
188
+ return PNPM_VER;
189
+ }
190
+ function setYarnVer() {
191
+ if (YARN_VER === undefined) {
192
+ YARN_VER = false;
193
+ try {
194
+ const pathname = path.resolve(".yarnrc.yml");
195
+ let config;
196
+ if (fs.existsSync(pathname) && (0, types_1.isPlainObject)(config = parseYaml(pathname)) && config.nodeLinker === 'node-modules') {
197
+ return YARN_VER = true;
198
+ }
199
+ }
200
+ catch {
201
+ }
202
+ try {
203
+ const pathname = path.resolve(".pnp.cjs");
204
+ if (fs.existsSync(pathname)) {
205
+ return YARN_VER = require(pathname);
206
+ }
207
+ }
208
+ catch {
209
+ }
154
210
  }
155
- return null;
156
- })();
211
+ return YARN_VER;
212
+ }
157
213
  function applyStyle(options, style) {
158
214
  var _h;
159
215
  for (const attr in style) {
@@ -627,7 +683,7 @@ class Module extends EventEmitter {
627
683
  this[_f] = new AbortController();
628
684
  this[_g] = null;
629
685
  }
630
- static get VERSION() { return "0.8.6"; }
686
+ static get VERSION() { return "0.8.7"; }
631
687
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
632
688
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
633
689
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -1191,7 +1247,7 @@ class Module extends EventEmitter {
1191
1247
  }
1192
1248
  }
1193
1249
  catch (err) {
1194
- if (err instanceof Error && err.code === 'ENOENT') {
1250
+ if (this.isErrorCode(err, 'ENOENT')) {
1195
1251
  throw err;
1196
1252
  }
1197
1253
  }
@@ -1811,8 +1867,11 @@ class Module extends EventEmitter {
1811
1867
  }
1812
1868
  return result;
1813
1869
  }
1814
- static getPackageVersion(value, unstable, startDir) {
1870
+ static getPackageVersion(value, unstable, startDir, baseDir) {
1815
1871
  if (typeof unstable === 'string') {
1872
+ if (typeof startDir === 'string') {
1873
+ baseDir = startDir;
1874
+ }
1816
1875
  startDir = unstable;
1817
1876
  unstable = false;
1818
1877
  }
@@ -1834,7 +1893,10 @@ class Module extends EventEmitter {
1834
1893
  folders.push(startDir);
1835
1894
  folders.reverse();
1836
1895
  }
1837
- for (const folder of folders) {
1896
+ for (let folder of folders) {
1897
+ if (baseDir) {
1898
+ folder = path.join(baseDir, folder.substring(PROCESS_CWD.length));
1899
+ }
1838
1900
  try {
1839
1901
  const pkg = path.join(folder, `node_modules/${value}/package.json`);
1840
1902
  if (fs.existsSync(pkg)) {
@@ -1887,18 +1949,18 @@ class Module extends EventEmitter {
1887
1949
  }
1888
1950
  return result[0];
1889
1951
  };
1890
- if (PNPM_VER) {
1891
- const result = [];
1892
- const pattern = new RegExp(`/${(0, types_1.escapePattern)(value)}/([^:]+):`, 'g');
1893
- let match;
1894
- while (match = pattern.exec(PNPM_VER)) {
1895
- result.push(match[1]);
1896
- }
1897
- if (result.length) {
1898
- return latest(result);
1952
+ if (setPnpmVer()) {
1953
+ if ((0, types_1.isObject)(PNPM_VER)) {
1954
+ if (startDir && !baseDir) {
1955
+ return this.getPackageVersion(value, unstable, startDir, PNPM_VER.baseDir || path.join(PROCESS_CWD, 'node_modules/.pnpm'));
1956
+ }
1957
+ const result = PNPM_VER.items.filter(item => item[0] === value).map(item => item[1]);
1958
+ if (result.length) {
1959
+ return latest(result);
1960
+ }
1899
1961
  }
1900
1962
  }
1901
- if (YARN_VER) {
1963
+ else if (setYarnVer() && (0, types_1.isObject)(YARN_VER)) {
1902
1964
  folders.forEach((folder, index) => {
1903
1965
  folder = ensureDir(folder).replace(path.sep + 'node_modules' + path.sep, path.sep + 'packages' + path.sep);
1904
1966
  folders[index] = PLATFORM_WIN32 ? folder.toLowerCase() : folder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,11 +20,12 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/types": "0.8.6",
23
+ "@e-mc/types": "0.8.7",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",
27
27
  "file-type": "16.5.4",
28
+ "js-yaml": "^4.1.0",
28
29
  "mime-types": "^2.1.35",
29
30
  "picomatch": "^3.0.1",
30
31
  "strip-ansi": "6.0.1"