piral-cli 0.15.0-alpha.4122 → 0.15.0-alpha.4231

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 (124) hide show
  1. package/lib/apps/new-pilet.js +5 -9
  2. package/lib/apps/new-pilet.js.map +1 -1
  3. package/lib/apps/new-piral.js +5 -3
  4. package/lib/apps/new-piral.js.map +1 -1
  5. package/lib/apps/publish-pilet.d.ts +4 -0
  6. package/lib/apps/publish-pilet.js +4 -3
  7. package/lib/apps/publish-pilet.js.map +1 -1
  8. package/lib/apps/publish-piral.d.ts +4 -0
  9. package/lib/apps/publish-piral.js +9 -7
  10. package/lib/apps/publish-piral.js.map +1 -1
  11. package/lib/apps/upgrade-pilet.js +2 -8
  12. package/lib/apps/upgrade-pilet.js.map +1 -1
  13. package/lib/apps/upgrade-piral.js +1 -1
  14. package/lib/apps/upgrade-piral.js.map +1 -1
  15. package/lib/bundler.js +1 -1
  16. package/lib/bundler.js.map +1 -1
  17. package/lib/commands.js +8 -0
  18. package/lib/commands.js.map +1 -1
  19. package/lib/common/browser.d.ts +1 -0
  20. package/lib/common/browser.js +16 -10
  21. package/lib/common/browser.js.map +1 -1
  22. package/lib/common/clients/index.d.ts +19 -0
  23. package/lib/common/clients/index.js +40 -0
  24. package/lib/common/clients/index.js.map +1 -0
  25. package/lib/common/clients/lerna.d.ts +6 -1
  26. package/lib/common/clients/lerna.js +67 -3
  27. package/lib/common/clients/lerna.js.map +1 -1
  28. package/lib/common/clients/npm.d.ts +4 -1
  29. package/lib/common/clients/npm.js +37 -16
  30. package/lib/common/clients/npm.js.map +1 -1
  31. package/lib/common/clients/pnpm.d.ts +4 -0
  32. package/lib/common/clients/pnpm.js +43 -9
  33. package/lib/common/clients/pnpm.js.map +1 -1
  34. package/lib/common/clients/rush.d.ts +6 -0
  35. package/lib/common/clients/rush.js +118 -0
  36. package/lib/common/clients/rush.js.map +1 -0
  37. package/lib/common/clients/yarn.d.ts +4 -0
  38. package/lib/common/clients/yarn.js +45 -11
  39. package/lib/common/clients/yarn.js.map +1 -1
  40. package/lib/common/declaration.js +12 -9
  41. package/lib/common/declaration.js.map +1 -1
  42. package/lib/common/emulator.js +2 -2
  43. package/lib/common/emulator.js.map +1 -1
  44. package/lib/common/http.d.ts +1 -1
  45. package/lib/common/http.js +12 -7
  46. package/lib/common/http.js.map +1 -1
  47. package/lib/common/info.d.ts +4 -0
  48. package/lib/common/info.js +6 -1
  49. package/lib/common/info.js.map +1 -1
  50. package/lib/common/inspect.js +2 -1
  51. package/lib/common/inspect.js.map +1 -1
  52. package/lib/common/interactive.d.ts +9 -0
  53. package/lib/common/interactive.js +31 -1
  54. package/lib/common/interactive.js.map +1 -1
  55. package/lib/common/io.js +1 -1
  56. package/lib/common/io.js.map +1 -1
  57. package/lib/common/npm.d.ts +9 -16
  58. package/lib/common/npm.js +110 -152
  59. package/lib/common/npm.js.map +1 -1
  60. package/lib/common/pack.js +1 -1
  61. package/lib/common/pack.js.map +1 -1
  62. package/lib/common/package.d.ts +6 -6
  63. package/lib/common/package.js +13 -4
  64. package/lib/common/package.js.map +1 -1
  65. package/lib/common/scaffold.js +5 -2
  66. package/lib/common/scaffold.js.map +1 -1
  67. package/lib/common/version.js +4 -4
  68. package/lib/common/version.js.map +1 -1
  69. package/lib/external/index.js +1982 -92
  70. package/lib/helpers.js +1 -1
  71. package/lib/helpers.js.map +1 -1
  72. package/lib/injectors/pilet.js +2 -2
  73. package/lib/injectors/pilet.js.map +1 -1
  74. package/lib/messages.d.ts +1 -1
  75. package/lib/messages.js.map +1 -1
  76. package/lib/plugin.js +27 -2
  77. package/lib/plugin.js.map +1 -1
  78. package/lib/release.d.ts +1 -1
  79. package/lib/release.js +7 -2
  80. package/lib/release.js.map +1 -1
  81. package/lib/types/common.d.ts +1 -1
  82. package/lib/types/internal.d.ts +9 -1
  83. package/lib/types/public.d.ts +1 -1
  84. package/package.json +5 -3
  85. package/src/apps/new-pilet.ts +9 -14
  86. package/src/apps/new-piral.ts +9 -5
  87. package/src/apps/publish-pilet.ts +10 -3
  88. package/src/apps/publish-piral.ts +21 -6
  89. package/src/apps/upgrade-pilet.ts +4 -12
  90. package/src/apps/upgrade-piral.ts +2 -2
  91. package/src/bundler.test.ts +1 -1
  92. package/src/bundler.ts +2 -2
  93. package/src/commands.ts +8 -0
  94. package/src/common/browser.ts +12 -8
  95. package/src/common/clients/index.ts +33 -0
  96. package/src/common/clients/lerna.ts +61 -1
  97. package/src/common/clients/npm.ts +32 -15
  98. package/src/common/clients/pnpm.ts +39 -10
  99. package/src/common/clients/rush.ts +111 -0
  100. package/src/common/clients/yarn.ts +41 -12
  101. package/src/common/declaration.ts +15 -9
  102. package/src/common/emulator.ts +3 -3
  103. package/src/common/http.ts +20 -6
  104. package/src/common/info.ts +6 -1
  105. package/src/common/inspect.ts +2 -1
  106. package/src/common/interactive.test.ts +3 -0
  107. package/src/common/interactive.ts +48 -1
  108. package/src/common/io.ts +1 -1
  109. package/src/common/npm.test.ts +109 -76
  110. package/src/common/npm.ts +119 -146
  111. package/src/common/pack.test.ts +1 -1
  112. package/src/common/pack.ts +2 -2
  113. package/src/common/package.ts +19 -10
  114. package/src/common/scaffold.ts +6 -2
  115. package/src/common/version.ts +4 -4
  116. package/src/external/index.ts +2 -1
  117. package/src/helpers.ts +1 -1
  118. package/src/injectors/pilet.ts +3 -3
  119. package/src/messages.ts +1 -1
  120. package/src/plugin.ts +34 -5
  121. package/src/release.ts +10 -2
  122. package/src/types/common.ts +1 -1
  123. package/src/types/internal.ts +6 -1
  124. package/src/types/public.ts +1 -1
@@ -2,7 +2,7 @@ import { resolve } from 'path';
2
2
  import { LogLevels, NpmClientType } from '../types';
3
3
  import {
4
4
  readJson,
5
- installPackage,
5
+ installNpmPackage,
6
6
  checkExistingDirectory,
7
7
  patchPiletPackage,
8
8
  copyPiralFiles,
@@ -10,7 +10,7 @@ import {
10
10
  readPiralPackage,
11
11
  getPiletsInfo,
12
12
  runScript,
13
- installDependencies,
13
+ installNpmDependencies,
14
14
  getCurrentPackageDetails,
15
15
  checkAppShellPackage,
16
16
  setLogLevel,
@@ -22,8 +22,6 @@ import {
22
22
  ForceOverwrite,
23
23
  copyScaffoldingFiles,
24
24
  getPiralPath,
25
- detectMonorepo,
26
- bootstrapMonorepo,
27
25
  isMonorepoPackageRef,
28
26
  getPiletScaffoldData,
29
27
  SourceLanguage,
@@ -131,7 +129,7 @@ export async function upgradePilet(baseDir = process.cwd(), options: UpgradePile
131
129
  if (!monorepoRef) {
132
130
  // only install the latest if the shell does come from remote
133
131
  progress(`Updating npm package to %s ...`, packageRef);
134
- await installPackage(npmClient, packageRef, root, '--no-save');
132
+ await installNpmPackage(npmClient, packageRef, root, '--no-save');
135
133
  }
136
134
 
137
135
  const piralInfo = await readPiralPackage(root, sourceName);
@@ -164,13 +162,7 @@ export async function upgradePilet(baseDir = process.cwd(), options: UpgradePile
164
162
 
165
163
  if (install) {
166
164
  progress(`Updating dependencies ...`);
167
- const monorepoKind = await detectMonorepo(root);
168
-
169
- if (monorepoKind === 'lerna') {
170
- await bootstrapMonorepo(root);
171
- } else {
172
- await installDependencies(npmClient, root);
173
- }
165
+ await installNpmDependencies(npmClient, root);
174
166
  }
175
167
 
176
168
  if (postUpgrade) {
@@ -3,7 +3,7 @@ import { LogLevels, NpmClientType } from '../types';
3
3
  import {
4
4
  readJson,
5
5
  checkExistingDirectory,
6
- installDependencies,
6
+ installNpmDependencies,
7
7
  setLogLevel,
8
8
  progress,
9
9
  fail,
@@ -113,7 +113,7 @@ export async function upgradePiral(baseDir = process.cwd(), options: UpgradePira
113
113
 
114
114
  if (install) {
115
115
  progress(`Updating the npm packages to %s ...`, version);
116
- await installDependencies(npmClient, root);
116
+ await installNpmDependencies(npmClient, root);
117
117
  }
118
118
 
119
119
  logDone('Piral instance upgraded successfully!');
@@ -48,7 +48,7 @@ jest.mock('./common', () => ({
48
48
  bundler: 'parcel',
49
49
  },
50
50
  cliVersion: '1.0.0',
51
- installPackage: jest.fn(),
51
+ installNpmPackage: jest.fn(),
52
52
  fail(msg) {
53
53
  throw new Error(msg);
54
54
  },
package/src/bundler.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { callDynamic, callStatic } from './build/bundler-calls';
2
2
  import { availableBundlers } from './helpers';
3
3
  import {
4
- installPackage,
4
+ installNpmPackage,
5
5
  cliVersion,
6
6
  fail,
7
7
  progress,
@@ -39,7 +39,7 @@ async function installDefaultBundler(root: string) {
39
39
  const client = await determineNpmClient(root);
40
40
  log('generalDebug_0003', `Prepare to install ${selectedPackage}@${cliVersion} using "${client}" into "${root}".`);
41
41
  progress(`Installing ${selectedPackage} ...`);
42
- await installPackage(client, `${selectedPackage}@${cliVersion}`, root, '--save-dev', '--save-exact');
42
+ await installNpmPackage(client, `${selectedPackage}@${cliVersion}`, root, '--save-dev', '--save-exact');
43
43
  log('generalDebug_0003', `Installed bundler from "${selectedPackage}".`);
44
44
 
45
45
  require('./inject').inject(selectedPackage);
package/src/commands.ts CHANGED
@@ -208,6 +208,9 @@ const allCommands: Array<ToolCommand<any>> = [
208
208
  .option('fields', undefined)
209
209
  .describe('fields', 'Sets additional fields to be included in the feed service request.')
210
210
  .default('fields', apps.publishPiralDefaults.fields)
211
+ .boolean('interactive')
212
+ .describe('interactive', 'Defines if authorization tokens can be retrieved interactively.')
213
+ .default('interactive', apps.publishPiralDefaults.interactive)
211
214
  .string('base')
212
215
  .default('base', process.cwd())
213
216
  .describe('base', 'Sets the base directory. By default the current directory is used.');
@@ -219,6 +222,7 @@ const allCommands: Array<ToolCommand<any>> = [
219
222
  type: args.type as PiralBuildType,
220
223
  provider: args.provider as string,
221
224
  fields: args.fields as Record<string, string>,
225
+ interactive: args.interactive as boolean,
222
226
  });
223
227
  },
224
228
  },
@@ -623,6 +627,9 @@ const allCommands: Array<ToolCommand<any>> = [
623
627
  .option('headers', undefined)
624
628
  .describe('headers', 'Sets additional headers to be included in the feed service request.')
625
629
  .default('headers', apps.publishPiletDefaults.headers)
630
+ .boolean('interactive')
631
+ .describe('interactive', 'Defines if authorization tokens can be retrieved interactively.')
632
+ .default('interactive', apps.publishPiletDefaults.interactive)
626
633
  .string('base')
627
634
  .default('base', process.cwd())
628
635
  .describe('base', 'Sets the base directory. By default the current directory is used.');
@@ -641,6 +648,7 @@ const allCommands: Array<ToolCommand<any>> = [
641
648
  fields: args.fields as Record<string, string>,
642
649
  headers: args.headers as Record<string, string>,
643
650
  mode: args.mode as PiletPublishScheme,
651
+ interactive: args.interactive as boolean,
644
652
  _: args,
645
653
  });
646
654
  },
@@ -1,15 +1,19 @@
1
1
  import { log } from './log';
2
- import { open } from '../external';
3
2
  import { config } from './config';
3
+ import { open } from '../external';
4
+
5
+ export async function openBrowserAt(address: string) {
6
+ try {
7
+ await open(address, undefined);
8
+ } catch (err) {
9
+ log('failedToOpenBrowser_0170', err);
10
+ }
11
+ }
4
12
 
5
13
  export async function openBrowser(shouldOpen: boolean, port: number, path: string, https?: boolean) {
6
14
  if (shouldOpen) {
7
- try {
8
- const scheme = https ? 'https' : 'http';
9
- const address = `${scheme}://${config.host}:${port}${path}`;
10
- await open(address, undefined);
11
- } catch (err) {
12
- log('failedToOpenBrowser_0170', err);
13
- }
15
+ const scheme = https ? 'https' : 'http';
16
+ const address = `${scheme}://${config.host}:${port}${path}`;
17
+ await openBrowserAt(address);
14
18
  }
15
19
  }
@@ -0,0 +1,33 @@
1
+ import * as lerna from './lerna';
2
+ import * as npm from './npm';
3
+ import * as pnpm from './pnpm';
4
+ import * as rush from './rush';
5
+ import * as yarn from './yarn';
6
+
7
+ export const clients = {
8
+ lerna,
9
+ npm,
10
+ pnpm,
11
+ rush,
12
+ yarn,
13
+ };
14
+
15
+ type ClientName = keyof typeof clients;
16
+
17
+ const directClients = ['npm', 'yarn', 'pnpm'];
18
+
19
+ export function isWrapperClient(client: ClientName) {
20
+ return !directClients.includes(client);
21
+ }
22
+
23
+ export function detectClients(root: string) {
24
+ return Promise.all(
25
+ Object.keys(clients).map(async (client: ClientName) => {
26
+ const result = await clients[client].detectClient(root);
27
+ return {
28
+ client,
29
+ result,
30
+ };
31
+ }),
32
+ );
33
+ }
@@ -1,17 +1,77 @@
1
1
  import { resolve } from 'path';
2
2
  import { log } from '../log';
3
+ import { findFile } from '../io';
3
4
  import { runCommand } from '../scripts';
4
5
  import { MemoryStream } from '../MemoryStream';
5
6
 
7
+ // Helpers:
8
+
6
9
  function runLernaProcess(args: Array<string>, target: string, output?: NodeJS.WritableStream) {
7
10
  log('generalDebug_0003', 'Starting the Lerna process ...');
8
11
  const cwd = resolve(process.cwd(), target);
9
12
  return runCommand('lerna', args, cwd, output);
10
13
  }
11
14
 
12
- export async function bootstrap(target = '.', ...flags: Array<string>) {
15
+ function convert(flags: Array<string>) {
16
+ return flags.map((flag) => {
17
+ switch (flag) {
18
+ case '--save-exact':
19
+ return '--exact';
20
+ case '--save-dev':
21
+ return '--dev';
22
+ case '--no-save':
23
+ // unfortunately no
24
+ return '';
25
+ default:
26
+ return flag;
27
+ }
28
+ });
29
+ }
30
+
31
+ // Client interface functions:
32
+
33
+ export async function installDependencies(target = '.', ...flags: Array<string>) {
13
34
  const ms = new MemoryStream();
14
35
  await runLernaProcess(['bootstrap', ...flags], target, ms);
15
36
  log('generalDebug_0003', `Lerna bootstrap result: ${ms.value}`);
16
37
  return ms.value;
17
38
  }
39
+
40
+ export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
41
+ const ms = new MemoryStream();
42
+ await runLernaProcess(['add', packageRef, ...convert(flags)], target, ms);
43
+ log('generalDebug_0003', `Lerna install package result: ${ms.value}`);
44
+ return ms.value;
45
+ }
46
+
47
+ export async function detectClient(root: string) {
48
+ return !!(await findFile(root, 'lerna.json'));
49
+ }
50
+
51
+ export async function initProject(projectName: string, target: string) {}
52
+
53
+ export async function isProject(root: string, packageRef: string) {
54
+ const projects = await listProjects(root);
55
+
56
+ if (Array.isArray(projects)) {
57
+ return projects?.some((p) => p.name === packageRef) ?? false;
58
+ }
59
+
60
+ return false;
61
+ }
62
+
63
+ // Functions to exclusively use from lerna client:
64
+
65
+ export async function listProjects(target: string) {
66
+ const ms = new MemoryStream();
67
+
68
+ try {
69
+ await runLernaProcess(['list', '--json', '-p'], target, ms);
70
+ } catch (e) {
71
+ log('generalDebug_0003', `lerna list error: ${e}`);
72
+ return [];
73
+ }
74
+
75
+ log('generalDebug_0003', `lerna list project result: ${ms.value}`);
76
+ return JSON.parse(ms.value);
77
+ }
@@ -1,14 +1,19 @@
1
1
  import { resolve } from 'path';
2
2
  import { log } from '../log';
3
+ import { findFile } from '../io';
3
4
  import { runCommand } from '../scripts';
4
5
  import { MemoryStream } from '../MemoryStream';
5
6
 
7
+ // Helpers:
8
+
6
9
  function runNpmProcess(args: Array<string>, target: string, output?: NodeJS.WritableStream) {
7
10
  log('generalDebug_0003', 'Starting the npm process ...');
8
11
  const cwd = resolve(process.cwd(), target);
9
12
  return runCommand('npm', args, cwd, output);
10
13
  }
11
14
 
15
+ // Client interface functions:
16
+
12
17
  export async function installDependencies(target = '.', ...flags: Array<string>) {
13
18
  const ms = new MemoryStream();
14
19
  await runNpmProcess(['install', '--legacy-peer-deps', ...flags], target, ms);
@@ -16,26 +21,37 @@ export async function installDependencies(target = '.', ...flags: Array<string>)
16
21
  return ms.value;
17
22
  }
18
23
 
19
- export async function unpackPackage(packageRef: string, target = '.', ...flags: Array<string>) {
24
+ export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
20
25
  const ms = new MemoryStream();
21
- await runNpmProcess(['pack', packageRef, ...flags], target, ms);
22
- log('generalDebug_0003', `npm (un)pack result: ${ms.value}`);
26
+ await runNpmProcess(['install', packageRef, '--legacy-peer-deps', ...flags], target, ms);
27
+ log('generalDebug_0003', `npm install package result: ${ms.value}`);
23
28
  return ms.value;
24
29
  }
25
30
 
26
- export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
27
- try {
28
- const ms = new MemoryStream();
29
- await runNpmProcess(['install', packageRef, '--legacy-peer-deps', ...flags], target, ms);
30
- log('generalDebug_0003', `npm install package result: ${ms.value}`);
31
- return ms.value;
32
- } catch (ex) {
33
- log(
34
- 'generalError_0002',
35
- `Could not install the package "${packageRef}" using npm. Make sure npm is correctly installed and accessible: ${ex}`,
36
- );
37
- throw ex;
31
+ export async function detectClient(root: string) {
32
+ return !!(await findFile(root, 'package-lock.json'));
33
+ }
34
+
35
+ export async function isProject(root: string, packageRef: string) {
36
+ const details = await listPackage(packageRef, root);
37
+ const packageDetails = details?.dependencies?.[packageRef];
38
+
39
+ if (packageDetails && typeof packageDetails.resolved === 'string') {
40
+ return packageDetails.resolved.startsWith('file:');
38
41
  }
42
+
43
+ return false;
44
+ }
45
+
46
+ export async function initProject(projectName: string, target: string) {}
47
+
48
+ // Functions to exclusively use from npm client:
49
+
50
+ export async function unpackPackage(packageRef: string, target = '.', ...flags: Array<string>) {
51
+ const ms = new MemoryStream();
52
+ await runNpmProcess(['pack', packageRef, ...flags], target, ms);
53
+ log('generalDebug_0003', `npm (un)pack result: ${ms.value}`);
54
+ return ms.value;
39
55
  }
40
56
 
41
57
  export async function createPackage(target = '.', ...flags: Array<string>) {
@@ -73,6 +89,7 @@ export async function listPackage(packageRef: string, target = '.', ...flags: Ar
73
89
  await runNpmProcess(['ls', packageRef, '--json', '--depth', '0', ...flags], target, ms);
74
90
  } catch (e) {
75
91
  log('generalDebug_0003', `npm ls packageRef error: ${e}`);
92
+ return {};
76
93
  }
77
94
 
78
95
  log('generalDebug_0003', `npm ls packageRef result: ${ms.value}`);
@@ -1,8 +1,11 @@
1
1
  import { resolve } from 'path';
2
2
  import { log } from '../log';
3
+ import { findFile } from '../io';
3
4
  import { runCommand } from '../scripts';
4
5
  import { MemoryStream } from '../MemoryStream';
5
6
 
7
+ // Helpers:
8
+
6
9
  function runPnpmProcess(args: Array<string>, target: string, output?: NodeJS.WritableStream) {
7
10
  log('generalDebug_0003', 'Starting the Pnpm process ...');
8
11
  const cwd = resolve(process.cwd(), target);
@@ -21,6 +24,8 @@ function convert(flags: Array<string>) {
21
24
  });
22
25
  }
23
26
 
27
+ // Client interface functions:
28
+
24
29
  export async function installDependencies(target = '.', ...flags: Array<string>) {
25
30
  const ms = new MemoryStream();
26
31
  await runPnpmProcess(['install', ...convert(flags)], target, ms);
@@ -29,16 +34,40 @@ export async function installDependencies(target = '.', ...flags: Array<string>)
29
34
  }
30
35
 
31
36
  export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
37
+ const ms = new MemoryStream();
38
+ await runPnpmProcess(['add', packageRef, ...convert(flags)], target, ms);
39
+ log('generalDebug_0003', `Pnpm install package result: ${ms.value}`);
40
+ return ms.value;
41
+ }
42
+
43
+ export async function detectClient(root: string) {
44
+ return !!(await findFile(root, 'pnpm-lock.yaml'));
45
+ }
46
+
47
+ export async function initProject(projectName: string, target: string) {}
48
+
49
+ export async function isProject(root: string, packageRef: string) {
50
+ const projects = await listProjects(root);
51
+
52
+ if (Array.isArray(projects)) {
53
+ return projects?.some((p) => p.name === packageRef) ?? false;
54
+ }
55
+
56
+ return false;
57
+ }
58
+
59
+ // Functions to exclusively use from pnpm client:
60
+
61
+ export async function listProjects(target: string) {
62
+ const ms = new MemoryStream();
63
+
32
64
  try {
33
- const ms = new MemoryStream();
34
- await runPnpmProcess(['add', packageRef, ...convert(flags)], target, ms);
35
- log('generalDebug_0003', `Pnpm install package result: ${ms.value}`);
36
- return ms.value;
37
- } catch (ex) {
38
- log(
39
- 'generalError_0002',
40
- `Could not install the package "${packageRef}" using Pnpm. Make sure Pnpm is correctly installed and accessible: ${ex}`,
41
- );
42
- throw ex;
65
+ await runPnpmProcess(['list', '--json', '--recursive', '--depth', '0'], target, ms);
66
+ } catch (e) {
67
+ log('generalDebug_0003', `pnpm list error: ${e}`);
68
+ return [];
43
69
  }
70
+
71
+ log('generalDebug_0003', `pnpm list project result: ${ms.value}`);
72
+ return JSON.parse(ms.value);
44
73
  }
@@ -0,0 +1,111 @@
1
+ import { dirname, relative, resolve } from 'path';
2
+ import { log } from '../log';
3
+ import { findFile, readText, writeText } from '../io';
4
+ import { jju } from '../../external';
5
+ import { runCommand } from '../scripts';
6
+ import { MemoryStream } from '../MemoryStream';
7
+
8
+ // Helpers:
9
+ const rushJson = 'rush.json';
10
+
11
+ function runRushProcess(args: Array<string>, target: string, output?: NodeJS.WritableStream) {
12
+ log('generalDebug_0003', 'Starting the Rush process ...');
13
+ const cwd = resolve(process.cwd(), target);
14
+ return runCommand('rush', args, cwd, output);
15
+ }
16
+
17
+ function convert(flags: Array<string>) {
18
+ return flags.map((flag) => {
19
+ switch (flag) {
20
+ case '--save-exact':
21
+ // discard as this may lead to problems
22
+ return '';
23
+ case '--save-dev':
24
+ return '--dev';
25
+ case '--no-save':
26
+ // unfortunately no
27
+ return '';
28
+ default:
29
+ return flag;
30
+ }
31
+ });
32
+ }
33
+
34
+ // Client interface functions:
35
+
36
+ export async function installDependencies(target = '.', ...flags: Array<string>) {
37
+ const ms = new MemoryStream();
38
+ await runRushProcess(['update', ...convert(flags)], target, ms);
39
+ log('generalDebug_0003', `Rush install dependencies result: ${ms.value}`);
40
+ return ms.value;
41
+ }
42
+
43
+ export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
44
+ const ms = new MemoryStream();
45
+ await runRushProcess(['add', '--package', packageRef, ...convert(flags)], target, ms);
46
+ log('generalDebug_0003', `Rush install package result: ${ms.value}`);
47
+ return ms.value;
48
+ }
49
+
50
+ export async function detectClient(root: string) {
51
+ return !!(await findFile(root, rushJson));
52
+ }
53
+
54
+ export async function initProject(packageName: string, target: string) {
55
+ const rushPath = await findFile(target, rushJson);
56
+
57
+ if (!rushPath) {
58
+ throw new Error(
59
+ `Could not find the "${rushJson}" from "${target}". Sure you want to create the project in the right directory?`,
60
+ );
61
+ }
62
+
63
+ const rushDir = dirname(rushPath);
64
+ const rushContent = await readText(rushDir, rushJson);
65
+ const rushData = jju.parse(rushContent);
66
+ const projectFolder = relative(rushDir, target);
67
+
68
+ if (!Array.isArray(rushData.projects)) {
69
+ rushData.projects = [];
70
+ }
71
+
72
+ rushData.projects.push({
73
+ packageName,
74
+ projectFolder,
75
+ });
76
+
77
+ await writeText(
78
+ rushDir,
79
+ rushJson,
80
+ jju.update(rushContent, rushData, {
81
+ mode: 'cjson',
82
+ indent: 2,
83
+ }),
84
+ );
85
+ }
86
+
87
+ export async function isProject(root: string, packageRef: string) {
88
+ const details = await listProjects(root);
89
+
90
+ if (typeof details === 'object' && Array.isArray(details?.projects)) {
91
+ return details?.projects?.some((p) => p.name === packageRef) ?? false;
92
+ }
93
+
94
+ return false;
95
+ }
96
+
97
+ // Functions to exclusively use from rush client:
98
+
99
+ export async function listProjects(target: string) {
100
+ const ms = new MemoryStream();
101
+
102
+ try {
103
+ await runRushProcess(['list', '--json'], target, ms);
104
+ } catch (e) {
105
+ log('generalDebug_0003', `rush list error: ${e}`);
106
+ return {};
107
+ }
108
+
109
+ log('generalDebug_0003', `rush list project result: ${ms.value}`);
110
+ return JSON.parse(ms.value);
111
+ }
@@ -1,10 +1,13 @@
1
1
  import { resolve } from 'path';
2
2
  import { log } from '../log';
3
+ import { findFile } from '../io';
3
4
  import { runCommand } from '../scripts';
4
5
  import { MemoryStream } from '../MemoryStream';
5
6
 
7
+ // Helpers:
8
+
6
9
  function runYarnProcess(args: Array<string>, target: string, output?: NodeJS.WritableStream) {
7
- log('generalDebug_0003', 'Starting the Yarn process ...');
10
+ log('generalDebug_0003', 'Starting the Yarn@1 process ...');
8
11
  const cwd = resolve(process.cwd(), target);
9
12
  return runCommand('yarn', args, cwd, output);
10
13
  }
@@ -25,24 +28,50 @@ function convert(flags: Array<string>) {
25
28
  });
26
29
  }
27
30
 
31
+ // Client interface functions:
32
+
28
33
  export async function installDependencies(target = '.', ...flags: Array<string>) {
29
34
  const ms = new MemoryStream();
30
35
  await runYarnProcess(['install', ...convert(flags)], target, ms);
31
- log('generalDebug_0003', `Yarn install dependencies result: ${ms.value}`);
36
+ log('generalDebug_0003', `Yarn@1 install dependencies result: ${ms.value}`);
32
37
  return ms.value;
33
38
  }
34
39
 
35
40
  export async function installPackage(packageRef: string, target = '.', ...flags: Array<string>) {
41
+ const ms = new MemoryStream();
42
+ await runYarnProcess(['add', packageRef, ...convert(flags)], target, ms);
43
+ log('generalDebug_0003', `Yarn@1 install package result: ${ms.value}`);
44
+ return ms.value;
45
+ }
46
+
47
+ export async function detectClient(root: string) {
48
+ return !!(await findFile(root, 'yarn.lock'));
49
+ }
50
+
51
+ export async function initProject(projectName: string, target: string) {}
52
+
53
+ export async function isProject(root: string, packageRef: string) {
54
+ const details = await listProjects(root);
55
+
56
+ if (typeof details === 'object') {
57
+ return typeof details?.[packageRef]?.location === 'string';
58
+ }
59
+
60
+ return false;
61
+ }
62
+
63
+ // Functions to exclusively use from yarn client:
64
+
65
+ export async function listProjects(target: string) {
66
+ const ms = new MemoryStream();
67
+
36
68
  try {
37
- const ms = new MemoryStream();
38
- await runYarnProcess(['add', packageRef, ...convert(flags)], target, ms);
39
- log('generalDebug_0003', `Yarn install package result: ${ms.value}`);
40
- return ms.value;
41
- } catch (ex) {
42
- log(
43
- 'generalError_0002',
44
- `Could not install the package "${packageRef}" using Yarn. Make sure Yarn@1 is correctly installed and accessible: ${ex}`,
45
- );
46
- throw ex;
69
+ await runYarnProcess(['workspaces', 'info'], target, ms);
70
+ } catch (e) {
71
+ log('generalDebug_0003', `yarn workspaces error: ${e}`);
72
+ return {};
47
73
  }
74
+
75
+ log('generalDebug_0003', `yarn workspaces result: ${ms.value}`);
76
+ return JSON.parse(ms.value);
48
77
  }
@@ -16,10 +16,16 @@ function findPiralBaseApi(root: string) {
16
16
  const projectDir = dirname(packageJsonPath);
17
17
  // By default support for piral-base < 0.15
18
18
  const { piletApiTypings = 'lib/types.d.ts' } = project;
19
- return resolve(projectDir, piletApiTypings);
19
+
20
+ return [
21
+ {
22
+ file: resolve(projectDir, piletApiTypings),
23
+ name: 'PiletApi',
24
+ },
25
+ ];
20
26
  } catch (err) {
21
27
  log('generalError_0002', `Could not find the root API of "piral-base" from "${root}": ${err}`);
22
- return undefined;
28
+ return [];
23
29
  }
24
30
  }
25
31
 
@@ -142,15 +148,15 @@ export async function createPiralDeclaration(
142
148
  root,
143
149
  files,
144
150
  types: findDeclaredTypings(root),
145
- apis: [
146
- {
147
- file: findPiralBaseApi(root),
148
- name: 'PiletApi',
149
- },
150
- ],
151
+ apis: findPiralBaseApi(root),
151
152
  imports: externals,
152
153
  logLevel,
153
154
  logger: createLogger(),
154
155
  };
155
- return await createDeclarationFile(options, baseDir, target, forceOverwrite);
156
+
157
+ if (options.apis.length) {
158
+ return await createDeclarationFile(options, baseDir, target, forceOverwrite);
159
+ }
160
+
161
+ log('declarationCouldNotBeGenerated_0076', baseDir, 'The main Pilet API interface could not be found.');
156
162
  }