piral-cli 0.15.0-alpha.4098 → 0.15.0-alpha.4257

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 (154) hide show
  1. package/lib/apps/debug-pilet.js +26 -41
  2. package/lib/apps/debug-pilet.js.map +1 -1
  3. package/lib/apps/debug-piral.js +18 -33
  4. package/lib/apps/debug-piral.js.map +1 -1
  5. package/lib/apps/new-pilet.js +5 -9
  6. package/lib/apps/new-pilet.js.map +1 -1
  7. package/lib/apps/new-piral.js +9 -3
  8. package/lib/apps/new-piral.js.map +1 -1
  9. package/lib/apps/publish-pilet.d.ts +4 -0
  10. package/lib/apps/publish-pilet.js +4 -3
  11. package/lib/apps/publish-pilet.js.map +1 -1
  12. package/lib/apps/publish-piral.d.ts +4 -0
  13. package/lib/apps/publish-piral.js +9 -7
  14. package/lib/apps/publish-piral.js.map +1 -1
  15. package/lib/apps/upgrade-pilet.js +2 -8
  16. package/lib/apps/upgrade-pilet.js.map +1 -1
  17. package/lib/apps/upgrade-piral.js +1 -1
  18. package/lib/apps/upgrade-piral.js.map +1 -1
  19. package/lib/bundler.js +1 -1
  20. package/lib/bundler.js.map +1 -1
  21. package/lib/commands.js +14 -6
  22. package/lib/commands.js.map +1 -1
  23. package/lib/common/browser.d.ts +1 -0
  24. package/lib/common/browser.js +16 -10
  25. package/lib/common/browser.js.map +1 -1
  26. package/lib/common/clients/index.d.ts +19 -0
  27. package/lib/common/clients/index.js +40 -0
  28. package/lib/common/clients/index.js.map +1 -0
  29. package/lib/common/clients/lerna.d.ts +6 -1
  30. package/lib/common/clients/lerna.js +67 -3
  31. package/lib/common/clients/lerna.js.map +1 -1
  32. package/lib/common/clients/npm.d.ts +4 -1
  33. package/lib/common/clients/npm.js +37 -16
  34. package/lib/common/clients/npm.js.map +1 -1
  35. package/lib/common/clients/pnpm.d.ts +4 -0
  36. package/lib/common/clients/pnpm.js +43 -9
  37. package/lib/common/clients/pnpm.js.map +1 -1
  38. package/lib/common/clients/rush.d.ts +6 -0
  39. package/lib/common/clients/rush.js +118 -0
  40. package/lib/common/clients/rush.js.map +1 -0
  41. package/lib/common/clients/yarn.d.ts +4 -0
  42. package/lib/common/clients/yarn.js +45 -11
  43. package/lib/common/clients/yarn.js.map +1 -1
  44. package/lib/common/constants.d.ts +1 -1
  45. package/lib/common/constants.js +1 -1
  46. package/lib/common/constants.js.map +1 -1
  47. package/lib/common/declaration.js +14 -10
  48. package/lib/common/declaration.js.map +1 -1
  49. package/lib/common/emulator.js +2 -2
  50. package/lib/common/emulator.js.map +1 -1
  51. package/lib/common/http.d.ts +1 -1
  52. package/lib/common/http.js +12 -7
  53. package/lib/common/http.js.map +1 -1
  54. package/lib/common/importmap.js +4 -14
  55. package/lib/common/importmap.js.map +1 -1
  56. package/lib/common/info.d.ts +4 -0
  57. package/lib/common/info.js +6 -1
  58. package/lib/common/info.js.map +1 -1
  59. package/lib/common/injectors.d.ts +36 -7
  60. package/lib/common/injectors.js +39 -11
  61. package/lib/common/injectors.js.map +1 -1
  62. package/lib/common/inspect.js +2 -1
  63. package/lib/common/inspect.js.map +1 -1
  64. package/lib/common/interactive.d.ts +9 -0
  65. package/lib/common/interactive.js +31 -1
  66. package/lib/common/interactive.js.map +1 -1
  67. package/lib/common/io.js +1 -1
  68. package/lib/common/io.js.map +1 -1
  69. package/lib/common/npm.d.ts +11 -16
  70. package/lib/common/npm.js +165 -158
  71. package/lib/common/npm.js.map +1 -1
  72. package/lib/common/pack.js +1 -1
  73. package/lib/common/pack.js.map +1 -1
  74. package/lib/common/package.d.ts +6 -7
  75. package/lib/common/package.js +26 -28
  76. package/lib/common/package.js.map +1 -1
  77. package/lib/common/scaffold.js +5 -2
  78. package/lib/common/scaffold.js.map +1 -1
  79. package/lib/common/version.js +4 -4
  80. package/lib/common/version.js.map +1 -1
  81. package/lib/external/index.js +1982 -92
  82. package/lib/helpers.d.ts +4 -4
  83. package/lib/helpers.js +10 -10
  84. package/lib/helpers.js.map +1 -1
  85. package/lib/injectors/{pilet.d.ts → pilet-injector.d.ts} +1 -0
  86. package/lib/injectors/{pilet.js → pilet-injector.js} +29 -31
  87. package/lib/injectors/pilet-injector.js.map +1 -0
  88. package/lib/injectors/{piral.d.ts → piral-injector.d.ts} +1 -0
  89. package/lib/injectors/{piral.js → piral-injector.js} +20 -22
  90. package/lib/injectors/piral-injector.js.map +1 -0
  91. package/lib/messages.d.ts +1 -1
  92. package/lib/messages.js +5 -1
  93. package/lib/messages.js.map +1 -1
  94. package/lib/plugin.js +27 -2
  95. package/lib/plugin.js.map +1 -1
  96. package/lib/release.d.ts +1 -1
  97. package/lib/release.js +7 -2
  98. package/lib/release.js.map +1 -1
  99. package/lib/types/common.d.ts +1 -1
  100. package/lib/types/internal.d.ts +9 -1
  101. package/lib/types/public.d.ts +2 -2
  102. package/package.json +6 -4
  103. package/src/apps/debug-pilet.ts +25 -47
  104. package/src/apps/debug-piral.ts +19 -36
  105. package/src/apps/new-pilet.ts +9 -14
  106. package/src/apps/new-piral.ts +16 -5
  107. package/src/apps/publish-pilet.ts +10 -3
  108. package/src/apps/publish-piral.ts +21 -6
  109. package/src/apps/upgrade-pilet.ts +4 -12
  110. package/src/apps/upgrade-piral.ts +2 -2
  111. package/src/bundler.test.ts +1 -1
  112. package/src/bundler.ts +2 -2
  113. package/src/commands.ts +17 -9
  114. package/src/common/browser.ts +12 -8
  115. package/src/common/clients/index.ts +33 -0
  116. package/src/common/clients/lerna.ts +61 -1
  117. package/src/common/clients/npm.ts +32 -15
  118. package/src/common/clients/pnpm.ts +39 -10
  119. package/src/common/clients/rush.ts +111 -0
  120. package/src/common/clients/yarn.ts +41 -12
  121. package/src/common/constants.ts +1 -1
  122. package/src/common/declaration.ts +17 -10
  123. package/src/common/emulator.ts +3 -3
  124. package/src/common/http.ts +20 -6
  125. package/src/common/importmap.ts +4 -14
  126. package/src/common/info.ts +6 -1
  127. package/src/common/injectors.ts +46 -17
  128. package/src/common/inspect.ts +2 -1
  129. package/src/common/interactive.test.ts +3 -0
  130. package/src/common/interactive.ts +48 -1
  131. package/src/common/io.ts +1 -1
  132. package/src/common/npm.test.ts +122 -76
  133. package/src/common/npm.ts +177 -152
  134. package/src/common/pack.test.ts +1 -1
  135. package/src/common/pack.ts +2 -2
  136. package/src/common/package.test.ts +1 -14
  137. package/src/common/package.ts +31 -29
  138. package/src/common/scaffold.ts +6 -2
  139. package/src/common/version.ts +4 -4
  140. package/src/external/index.ts +2 -1
  141. package/src/helpers.test.ts +5 -5
  142. package/src/helpers.ts +8 -8
  143. package/src/injectors/{pilet.test.ts → pilet-injector.test.ts} +10 -1
  144. package/src/injectors/{pilet.ts → pilet-injector.ts} +36 -28
  145. package/src/injectors/{piral.test.ts → piral-injector.test.ts} +5 -1
  146. package/src/injectors/{piral.ts → piral-injector.ts} +24 -17
  147. package/src/messages.ts +6 -2
  148. package/src/plugin.ts +34 -5
  149. package/src/release.ts +10 -2
  150. package/src/types/common.ts +1 -1
  151. package/src/types/internal.ts +6 -1
  152. package/src/types/public.ts +2 -2
  153. package/lib/injectors/pilet.js.map +0 -1
  154. package/lib/injectors/piral.js.map +0 -1
package/src/common/npm.ts CHANGED
@@ -1,65 +1,60 @@
1
- import { resolve, relative, dirname } from 'path';
2
- import { createReadStream, existsSync, access, constants } from 'fs';
1
+ import { resolve, relative, dirname, isAbsolute, sep } from 'path';
2
+ import { createReadStream, existsSync } from 'fs';
3
3
  import { log, fail } from './log';
4
+ import { clients, detectClients, isWrapperClient } from './clients';
4
5
  import { config } from './config';
5
6
  import { legacyCoreExternals, frameworkLibs } from './constants';
6
7
  import { inspectPackage } from './inspect';
7
- import { readJson, checkExists, findFile } from './io';
8
+ import { readJson, checkExists } from './io';
8
9
  import { clientTypeKeys } from '../helpers';
9
10
  import { PackageType, NpmClientType } from '../types';
10
11
 
11
12
  const gitPrefix = 'git+';
12
13
  const filePrefix = 'file:';
14
+ const pathPrefixes = ['/', './', '../', '.\\', '..\\', '~/', '~\\', filePrefix];
13
15
 
14
16
  function isProjectReference(name: string) {
15
17
  const target = resolve(name, 'package.json');
16
18
  return checkExists(target);
17
19
  }
18
20
 
19
- export function detectPnpm(root: string) {
20
- return new Promise((res) => {
21
- access(resolve(root, 'pnpm-lock.yaml'), constants.F_OK, (noPnpmLock) => {
22
- res(!noPnpmLock);
23
- });
24
- });
21
+ function resolveAbsPath(basePath: string, fullName: string) {
22
+ const prefixed = fullName.startsWith(filePrefix);
23
+ const relPath = !prefixed ? fullName : fullName.replace(filePrefix, '');
24
+ return resolve(basePath, relPath);
25
25
  }
26
26
 
27
- export function detectNpm(root: string) {
28
- return new Promise((res) => {
29
- access(resolve(root, 'package-lock.json'), constants.F_OK, (noPackageLock) => {
30
- res(!noPackageLock);
31
- });
32
- });
33
- }
27
+ async function detectMonorepoRoot(root: string): Promise<[] | [string, NpmClientType]> {
28
+ let previous = root;
34
29
 
35
- export async function detectYarn(root: string) {
36
- return !!(await findFile(root, 'yarn.lock'));
37
- }
30
+ do {
31
+ if (await checkExists(resolve(root, 'lerna.json'))) {
32
+ return [root, 'lerna'];
33
+ }
38
34
 
39
- export async function getLernaConfigPath(root: string) {
40
- log('generalDebug_0003', 'Trying to get the configuration file for Lerna ...');
41
- const file = await findFile(root, 'lerna.json');
35
+ if (await checkExists(resolve(root, 'rush.json'))) {
36
+ return [root, 'rush'];
37
+ }
42
38
 
43
- if (file) {
44
- log('generalDebug_0003', `Found Lerna config in "${file}".`);
45
- return file;
46
- }
39
+ if (await checkExists(resolve(root, 'pnpm-workspace.yaml'))) {
40
+ return [root, 'pnpm'];
41
+ }
47
42
 
48
- return undefined;
49
- }
43
+ const packageJson = await readJson(root, 'package.json');
50
44
 
51
- export async function getLernaNpmClient(root: string): Promise<NpmClientType> {
52
- const file = await getLernaConfigPath(root);
45
+ if (Array.isArray(packageJson?.workspaces)) {
46
+ if (await checkExists(resolve(root, 'yarn.lock'))) {
47
+ return [root, 'yarn'];
48
+ }
53
49
 
54
- if (file) {
55
- try {
56
- return require(file).npmClient;
57
- } catch (err) {
58
- log('generalError_0002', `Could not read lerna.json: ${err}.`);
50
+ return [root, 'npm'];
59
51
  }
60
- }
61
52
 
62
- return undefined;
53
+ previous = root;
54
+ root = dirname(root);
55
+ } while (root !== previous);
56
+
57
+ return [];
63
58
  }
64
59
 
65
60
  /**
@@ -69,36 +64,45 @@ export async function getLernaNpmClient(root: string): Promise<NpmClientType> {
69
64
  */
70
65
  export async function determineNpmClient(root: string, selected?: NpmClientType): Promise<NpmClientType> {
71
66
  if (!selected || !clientTypeKeys.includes(selected)) {
72
- log('generalDebug_0003', 'No npm client selected. Checking for lock files ...');
73
- const [hasNpm, hasYarn, hasPnpm] = await Promise.all([detectNpm(root), detectYarn(root), detectPnpm(root)]);
74
- const found = +hasNpm + +hasYarn + +hasPnpm;
75
- log('generalDebug_0003', `Results of the lock file check: npm = ${hasNpm}, Yarn = ${hasYarn}, Pnpm = ${hasPnpm}`);
67
+ log('generalDebug_0003', 'No npm client selected. Checking for lock or config files ...');
68
+
69
+ const searchedClients = await detectClients(root);
70
+ const foundClients = searchedClients.filter((m) => m.result);
71
+
72
+ log(
73
+ 'generalDebug_0003',
74
+ `Results of the lock file check: ${searchedClients.map((m) => `${m.client}=${m.result}`).join(', ')}`,
75
+ );
76
+
76
77
  const defaultClient = config.npmClient;
77
78
 
78
- if (found !== 1) {
79
- const lernaClient = await getLernaNpmClient(root);
79
+ if (foundClients.length > 1) {
80
+ const wrapperClient = foundClients.find((m) => isWrapperClient(m.client));
80
81
 
81
- if (clientTypeKeys.includes(lernaClient)) {
82
- log('generalDebug_0003', `Found valid npm client via Lerna: ${lernaClient}.`);
83
- return lernaClient;
82
+ if (wrapperClient) {
83
+ const { client } = wrapperClient;
84
+ log('generalDebug_0003', `Found valid wrapper client via lock or config file: "${client}".`);
84
85
  }
85
- } else if (hasNpm) {
86
- log('generalDebug_0003', `Found valid npm client via lockfile.`);
87
- return 'npm';
88
- } else if (hasYarn) {
89
- log('generalDebug_0003', `Found valid Yarn client via lockfile.`);
90
- return 'yarn';
91
- } else if (hasPnpm) {
92
- log('generalDebug_0003', `Found valid pnpm client via lockfile.`);
93
- return 'pnpm';
86
+ }
87
+
88
+ if (foundClients.length > 0) {
89
+ const { client } = foundClients[0];
90
+
91
+ if (foundClients.length > 1) {
92
+ const clientStr = `"${foundClients.map((m) => m.client).join('", "')}"`;
93
+ log('generalWarning_0001', `Found multiple clients via their lock or config files: ${clientStr}.`);
94
+ }
95
+
96
+ log('generalDebug_0003', `Found valid direct client via lock or config file: "${client}".`);
97
+ return client;
94
98
  }
95
99
 
96
100
  if (clientTypeKeys.includes(defaultClient)) {
97
- log('generalDebug_0003', `Found valid Pnpm client the default client: "${defaultClient}".`);
101
+ log('generalDebug_0003', `Using the default client: "${defaultClient}".`);
98
102
  return defaultClient;
99
103
  }
100
104
 
101
- log('generalDebug_0003', 'Using the default npm client.');
105
+ log('generalDebug_0003', 'Using the default "npm" client.');
102
106
  return 'npm';
103
107
  }
104
108
 
@@ -106,103 +110,66 @@ export async function determineNpmClient(root: string, selected?: NpmClientType)
106
110
  }
107
111
 
108
112
  export async function isMonorepoPackageRef(refName: string, root: string): Promise<boolean> {
109
- const c = require(`./clients/npm`);
110
- const newRoot = await detectMonorepoRoot(root);
113
+ const [monorepoRoot, client] = await detectMonorepoRoot(root);
111
114
 
112
- if (newRoot) {
113
- const details = await c.listPackage(refName, newRoot);
114
- return details?.dependencies?.[refName]?.extraneous ?? false;
115
+ if (monorepoRoot) {
116
+ const c = clients[client];
117
+ return await c.isProject(monorepoRoot, refName);
115
118
  }
116
119
 
117
120
  return false;
118
121
  }
119
122
 
120
- export async function detectMonorepoRoot(root: string): Promise<string> {
121
- const file = await getLernaConfigPath(root);
122
-
123
- if (file !== undefined) {
124
- return dirname(file);
125
- }
126
-
127
- let previous = root;
128
-
129
- do {
130
- const packageJson = await readJson(root, 'package.json');
131
-
132
- if (Array.isArray(packageJson?.workspaces)) {
133
- return root;
134
- }
135
-
136
- previous = root;
137
- root = dirname(root);
138
- } while (root !== previous);
139
-
140
- return undefined;
123
+ export function installNpmDependencies(client: NpmClientType, target = '.'): Promise<string> {
124
+ const { installDependencies } = clients[client];
125
+ return installDependencies(target);
141
126
  }
142
127
 
143
- export type MonorepoKind = 'none' | 'lerna' | 'yarn';
144
-
145
- export async function detectMonorepo(root: string): Promise<MonorepoKind> {
146
- const newRoot = await detectMonorepoRoot(root);
147
-
148
- if (newRoot) {
149
- const file = await getLernaConfigPath(newRoot);
150
-
151
- if (file !== undefined) {
152
- return 'lerna';
153
- }
154
-
155
- const packageJson = await readJson(newRoot, 'package.json');
156
-
157
- if (Array.isArray(packageJson?.workspaces)) {
158
- return 'yarn';
159
- }
160
- }
161
-
162
- return 'none';
163
- }
164
-
165
- export function bootstrapMonorepo(target = '.') {
166
- const c = require(`./clients/lerna`);
167
- return c.bootstrap(target);
168
- }
169
-
170
- export function installDependencies(client: NpmClientType, target = '.'): Promise<string> {
171
- const c = require(`./clients/${client}`);
172
- return c.installDependencies(target);
173
- }
174
-
175
- export function installPackage(
128
+ export async function installNpmPackage(
176
129
  client: NpmClientType,
177
130
  packageRef: string,
178
131
  target = '.',
179
132
  ...flags: Array<string>
180
133
  ): Promise<string> {
181
- const c = require(`./clients/${client}`);
182
- return c.installPackage(packageRef, target, ...flags);
134
+ try {
135
+ const { installPackage } = clients[client];
136
+ return await installPackage(packageRef, target, ...flags);
137
+ } catch (ex) {
138
+ log(
139
+ 'generalError_0002',
140
+ `Could not install the package "${packageRef}" using ${client}. Make sure ${client} is correctly installed and accessible: ${ex}`,
141
+ );
142
+ throw ex;
143
+ }
144
+ }
145
+
146
+ export function initNpmProject(client: NpmClientType, projectName: string, target: string) {
147
+ const { initProject } = clients[client];
148
+ return initProject(projectName, target);
183
149
  }
184
150
 
185
- export function publishPackage(target = '.', file = '*.tgz', flags: Array<string> = []): Promise<string> {
186
- const c = require(`./clients/npm`);
187
- return c.publishPackage(target, file, ...flags);
151
+ export function publishNpmPackage(target = '.', file = '*.tgz', flags: Array<string> = []): Promise<string> {
152
+ const { publishPackage } = clients.npm;
153
+ return publishPackage(target, file, ...flags);
188
154
  }
189
155
 
190
- export function createPackage(target = '.'): Promise<string> {
191
- const c = require(`./clients/npm`);
192
- return c.createPackage(target);
156
+ export function createNpmPackage(target = '.'): Promise<string> {
157
+ const { createPackage } = clients.npm;
158
+ return createPackage(target);
193
159
  }
194
160
 
195
- export function findTarball(packageRef: string): Promise<string> {
196
- const c = require(`./clients/npm`);
197
- return c.findTarball(packageRef);
161
+ export function findNpmTarball(packageRef: string): Promise<string> {
162
+ const { findTarball } = clients.npm;
163
+ return findTarball(packageRef);
198
164
  }
199
165
 
200
166
  export function findSpecificVersion(packageName: string, version: string): Promise<string> {
201
- const c = require(`./clients/npm`);
202
- return c.findSpecificVersion(packageName, version);
167
+ const { findSpecificVersion } = clients.npm;
168
+ return findSpecificVersion(packageName, version);
203
169
  }
204
170
 
205
171
  export function findLatestVersion(packageName: string) {
172
+ const { findSpecificVersion } = clients.npm;
206
173
  return findSpecificVersion(packageName, 'latest');
207
174
  }
208
175
 
@@ -210,7 +177,7 @@ export function isLocalPackage(baseDir: string, fullName: string) {
210
177
  log('generalDebug_0003', 'Checking if its a local package ...');
211
178
 
212
179
  if (fullName) {
213
- if (/^[\.\/\~]/.test(fullName)) {
180
+ if (pathPrefixes.some((prefix) => fullName.startsWith(prefix))) {
214
181
  log('generalDebug_0003', 'Found a local package by name.');
215
182
  return true;
216
183
  } else if (fullName.endsWith('.tgz')) {
@@ -249,9 +216,7 @@ export function makeGitUrl(fullName: string) {
249
216
  }
250
217
 
251
218
  export function makeFilePath(basePath: string, fullName: string) {
252
- const prefixed = fullName.startsWith(filePrefix);
253
- const relPath = !prefixed ? fullName : fullName.replace(filePrefix, '');
254
- const absPath = resolve(basePath, relPath);
219
+ const absPath = resolveAbsPath(basePath, fullName);
255
220
  return `${filePrefix}${absPath}`;
256
221
  }
257
222
 
@@ -275,7 +240,7 @@ export async function dissectPackageName(
275
240
  const gitUrl = makeGitUrl(fullName);
276
241
  return [gitUrl, 'latest', false, 'git'];
277
242
  } else if (isLocalPackage(baseDir, fullName)) {
278
- const fullPath = resolve(baseDir, fullName);
243
+ const fullPath = resolveAbsPath(baseDir, fullName);
279
244
  const exists = await checkExists(fullPath);
280
245
 
281
246
  if (!exists) {
@@ -354,12 +319,63 @@ export async function getCurrentPackageDetails(
354
319
  return [combinePackageRef(sourceName, desired, 'registry'), desired];
355
320
  }
356
321
 
322
+ function tryResolve(...args: Parameters<typeof require.resolve>) {
323
+ try {
324
+ return require.resolve(...args);
325
+ } catch {
326
+ return undefined;
327
+ }
328
+ }
329
+
330
+ export function tryResolvePackage(name: string, baseDir: string = undefined) {
331
+ let path = baseDir ? tryResolve(name, { paths: [baseDir] }) : tryResolve(name);
332
+ const root = baseDir || process.cwd();
333
+
334
+ if (!path) {
335
+ if (name.startsWith('.')) {
336
+ path = resolve(root, name);
337
+ } else if (isAbsolute(name)) {
338
+ path = name;
339
+ } else if (name.includes('/', name.startsWith('@') ? name.indexOf('/') + 1 : 0)) {
340
+ const parts = name.split('/');
341
+ const mainPart = name.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
342
+ const mainPath = baseDir ? tryResolve(mainPart, { paths: [baseDir] }) : tryResolve(mainPart);
343
+ const searchStr = `${sep}${mainPart.replace('/', sep)}${sep}`;
344
+
345
+ if (mainPath?.includes(searchStr)) {
346
+ const rest = name.startsWith('@') ? parts.slice(2) : parts.slice(1);
347
+ path = mainPath.substring(0, mainPath.indexOf(searchStr) + searchStr.length) + rest.join(sep);
348
+ }
349
+ } else {
350
+ path = resolve(root, 'node_modules', name);
351
+ }
352
+
353
+ if (!existsSync(path)) {
354
+ path = `${path}.js`;
355
+
356
+ if (!existsSync(path)) {
357
+ path = undefined;
358
+ }
359
+ }
360
+ }
361
+
362
+ if (!path) {
363
+ log('generalDebug_0003', `Could not resolve the package "${name}" in "${root}".`);
364
+ } else {
365
+ log('generalVerbose_0004', `Resolved the package "${name}" (from "${root}") to be "${path}".`);
366
+ }
367
+
368
+ return path;
369
+ }
370
+
371
+ export function findPackageRoot(pck: string, baseDir: string = undefined) {
372
+ return tryResolvePackage(`${pck}/package.json`, baseDir);
373
+ }
374
+
357
375
  export function isLinkedPackage(name: string, type: PackageType, hadVersion: boolean) {
358
376
  if (type === 'registry' && !hadVersion) {
359
- try {
360
- require.resolve(`${name}/package.json`);
361
- return true;
362
- } catch {}
377
+ const root = findPackageRoot(name);
378
+ return typeof root === 'string';
363
379
  }
364
380
 
365
381
  return false;
@@ -367,7 +383,8 @@ export function isLinkedPackage(name: string, type: PackageType, hadVersion: boo
367
383
 
368
384
  export function combinePackageRef(name: string, version: string, type: PackageType) {
369
385
  if (type === 'registry') {
370
- return `${name}@${version || 'latest'}`;
386
+ const tag = version || 'latest';
387
+ return `${name}@${tag}`;
371
388
  }
372
389
 
373
390
  return name;
@@ -421,18 +438,22 @@ export function getPackageVersion(
421
438
  }
422
439
  }
423
440
 
424
- function getExternalsFrom(packageName: string): Array<string> | undefined {
441
+ function getExternalsFrom(root: string, packageName: string): Array<string> | undefined {
425
442
  try {
426
- return require(`${packageName}/package.json`).sharedDependencies;
427
- } catch {
443
+ const target = require.resolve(`${packageName}/package.json`, {
444
+ paths: [root],
445
+ });
446
+ return require(target).sharedDependencies;
447
+ } catch (err) {
448
+ log('generalError_0002', `Could not get externals from "${packageName}": "${err}`);
428
449
  return undefined;
429
450
  }
430
451
  }
431
452
 
432
- function getCoreExternals(dependencies: Record<string, string>): Array<string> {
453
+ function getCoreExternals(root: string, dependencies: Record<string, string>): Array<string> {
433
454
  for (const frameworkLib of frameworkLibs) {
434
455
  if (dependencies[frameworkLib]) {
435
- const deps = getExternalsFrom(frameworkLib);
456
+ const deps = getExternalsFrom(root, frameworkLib);
436
457
 
437
458
  if (deps) {
438
459
  return deps;
@@ -445,24 +466,23 @@ function getCoreExternals(dependencies: Record<string, string>): Array<string> {
445
466
  }
446
467
 
447
468
  export function makePiletExternals(
469
+ root: string,
448
470
  dependencies: Record<string, string>,
449
471
  externals: Array<string>,
450
472
  fromEmulator: boolean,
451
473
  piralInfo: any,
452
- ) {
474
+ ): Array<string> {
453
475
  if (fromEmulator) {
454
- const { sharedDependencies = makeExternals(dependencies, externals, true) } = piralInfo;
476
+ const { sharedDependencies = mergeExternals(externals, legacyCoreExternals) } = piralInfo;
455
477
  return sharedDependencies;
456
478
  } else {
457
- return makeExternals(dependencies, externals);
479
+ return makeExternals(root, dependencies, externals);
458
480
  }
459
481
  }
460
482
 
461
- export function makeExternals(dependencies: Record<string, string>, externals?: Array<string>, legacy = false) {
462
- const coreExternals = legacy ? legacyCoreExternals : getCoreExternals(dependencies);
463
-
464
- if (externals && Array.isArray(externals)) {
465
- const [include, exclude] = externals.reduce<[Array<string>, Array<string>]>(
483
+ export function mergeExternals(customExternals?: Array<string>, coreExternals: Array<string> = []) {
484
+ if (customExternals && Array.isArray(customExternals)) {
485
+ const [include, exclude] = customExternals.reduce<[Array<string>, Array<string>]>(
466
486
  (prev, curr) => {
467
487
  if (typeof curr === 'string') {
468
488
  if (curr.startsWith('!')) {
@@ -482,3 +502,8 @@ export function makeExternals(dependencies: Record<string, string>, externals?:
482
502
 
483
503
  return coreExternals;
484
504
  }
505
+
506
+ export function makeExternals(root: string, dependencies: Record<string, string>, externals?: Array<string>) {
507
+ const coreExternals = getCoreExternals(root, dependencies);
508
+ return mergeExternals(externals, coreExternals);
509
+ }
@@ -13,7 +13,7 @@ jest.mock('./io', () => ({
13
13
  }));
14
14
 
15
15
  jest.mock('./npm', () => ({
16
- createPackage: (target?: string) => {
16
+ createNpmPackage: (target?: string) => {
17
17
  return Promise.resolve(target);
18
18
  },
19
19
  }));
@@ -1,7 +1,7 @@
1
1
  import { resolve, join } from 'path';
2
2
  import { log, progress, fail } from './log';
3
3
  import { readJson, move } from './io';
4
- import { createPackage } from './npm';
4
+ import { createNpmPackage } from './npm';
5
5
  import { ForceOverwrite } from './enums';
6
6
 
7
7
  async function getFile(root: string, name: string, dest: string) {
@@ -38,7 +38,7 @@ export async function createPiletPackage(baseDir: string, source: string, target
38
38
 
39
39
  progress(`Packing pilet in ${dest} ...`);
40
40
  log('generalDebug_0003', 'Creating package ...');
41
- await createPackage(root);
41
+ await createNpmPackage(root);
42
42
  log('generalDebug_0003', 'Successfully created package.');
43
43
  const name = `${pckg.name}-${pckg.version}.tgz`.replace(/@/g, '').replace(/\//g, '-');
44
44
  log('generalDebug_0003', `Assumed package name "${name}".`);
@@ -1,5 +1,4 @@
1
- import { resolve } from 'path';
2
- import { findPackageVersion, findPackageRoot, getPiralPackage, getPiletsInfo, retrievePiletData } from './package';
1
+ import { findPackageVersion, getPiralPackage, getPiletsInfo, retrievePiletData } from './package';
3
2
  import { cliVersion } from './info';
4
3
  import { SourceLanguage } from './enums';
5
4
 
@@ -14,18 +13,6 @@ describe('CLI package module', () => {
14
13
  expect(version).toBe('latest');
15
14
  });
16
15
 
17
- it('findPackageRoot correctly resolves the package root of parcel-bundler', () => {
18
- const dir = process.cwd();
19
- const version = findPackageRoot('webpack', dir);
20
- expect(version).toBe(resolve(dir, 'node_modules', 'webpack', 'package.json'));
21
- });
22
-
23
- it('findPackageRoot returns undefined for invalid package', () => {
24
- const dir = process.cwd();
25
- const version = findPackageRoot('foo-bar-not-exist', dir);
26
- expect(version).toBeUndefined();
27
- });
28
-
29
16
  it('getPiletsInfo returns pilets information about provided piralInfo', () => {
30
17
  const emptyPiletsInfo = {
31
18
  files: [],