piral-cli 1.7.2 → 1.7.3-beta.7581

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 (53) hide show
  1. package/lib/apps/add-piral-instance-pilet.d.ts +8 -0
  2. package/lib/apps/add-piral-instance-pilet.js +6 -2
  3. package/lib/apps/add-piral-instance-pilet.js.map +1 -1
  4. package/lib/apps/new-pilet.d.ts +8 -0
  5. package/lib/apps/new-pilet.js +6 -2
  6. package/lib/apps/new-pilet.js.map +1 -1
  7. package/lib/apps/publish-pilet.d.ts +4 -0
  8. package/lib/apps/publish-pilet.js +9 -7
  9. package/lib/apps/publish-pilet.js.map +1 -1
  10. package/lib/apps/publish-piral.d.ts +4 -0
  11. package/lib/apps/publish-piral.js +6 -4
  12. package/lib/apps/publish-piral.js.map +1 -1
  13. package/lib/apps/run-emulator-piral.d.ts +8 -0
  14. package/lib/apps/run-emulator-piral.js +7 -5
  15. package/lib/apps/run-emulator-piral.js.map +1 -1
  16. package/lib/apps/upgrade-pilet.d.ts +8 -0
  17. package/lib/apps/upgrade-pilet.js +7 -3
  18. package/lib/apps/upgrade-pilet.js.map +1 -1
  19. package/lib/commands.js +40 -0
  20. package/lib/commands.js.map +1 -1
  21. package/lib/common/config.d.ts +4 -0
  22. package/lib/common/config.js +1 -0
  23. package/lib/common/config.js.map +1 -1
  24. package/lib/common/http.d.ts +9 -3
  25. package/lib/common/http.js +24 -8
  26. package/lib/common/http.js.map +1 -1
  27. package/lib/common/package.d.ts +5 -3
  28. package/lib/common/package.js +7 -7
  29. package/lib/common/package.js.map +1 -1
  30. package/lib/common/release.d.ts +2 -1
  31. package/lib/common/release.js +2 -2
  32. package/lib/common/release.js.map +1 -1
  33. package/lib/common/shell.d.ts +3 -1
  34. package/lib/common/shell.js +2 -2
  35. package/lib/common/shell.js.map +1 -1
  36. package/lib/common/website.d.ts +4 -2
  37. package/lib/common/website.js +6 -11
  38. package/lib/common/website.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/apps/add-piral-instance-pilet.ts +20 -1
  41. package/src/apps/new-pilet.ts +19 -1
  42. package/src/apps/publish-pilet.ts +17 -7
  43. package/src/apps/publish-piral.ts +13 -4
  44. package/src/apps/run-emulator-piral.ts +22 -6
  45. package/src/apps/upgrade-pilet.ts +21 -2
  46. package/src/commands.ts +41 -1
  47. package/src/common/config.ts +5 -0
  48. package/src/common/http.test.ts +5 -4
  49. package/src/common/http.ts +26 -7
  50. package/src/common/package.ts +10 -11
  51. package/src/common/release.ts +3 -2
  52. package/src/common/shell.ts +3 -1
  53. package/src/common/website.ts +7 -11
@@ -73,6 +73,10 @@ export interface PiralCliConfig {
73
73
  * Npm registry.
74
74
  */
75
75
  registry?: string;
76
+ /**
77
+ * Allow self-signed certificates.
78
+ */
79
+ allowSelfSigned?: boolean;
76
80
  }
77
81
 
78
82
  export const config: PiralCliConfig = rc(
@@ -89,6 +93,7 @@ export const config: PiralCliConfig = rc(
89
93
  validators: {},
90
94
  schemaVersion: 'v2' as const,
91
95
  openBrowser: false,
96
+ allowSelfSigned: false,
92
97
  port: 1234,
93
98
  strictPort: false,
94
99
  language: 'ts' as const,
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect, vitest } from 'vitest';
2
- import { postFile, downloadFile } from './http';
2
+ import { postFile, downloadFile, getAgent } from './http';
3
3
 
4
4
  const apiUrl = 'http://sample.fooo.com/api/v1/pilet';
5
5
 
@@ -80,7 +80,7 @@ vitest.mock('../external', async () => {
80
80
  },
81
81
  },
82
82
  },
83
- FormData: (await vitest.importActual('form-data') as any).default,
83
+ FormData: ((await vitest.importActual('form-data')) as any).default,
84
84
  };
85
85
  });
86
86
 
@@ -155,10 +155,11 @@ describe('HTTP Module', () => {
155
155
 
156
156
  it('downloadFile calls results in error', async () => {
157
157
  errorOther = true;
158
- let result = await downloadFile('http://sample.com/', Buffer.from('example'));
158
+ const agent = getAgent({ ca: Buffer.from('example') });
159
+ let result = await downloadFile('http://sample.com/', agent);
159
160
  expect(result.length).toBe(0);
160
161
  errorOther = false;
161
- result = await downloadFile('http://sample.com/', Buffer.from('example'));
162
+ result = await downloadFile('http://sample.com/', agent);
162
163
  expect(result.length).toBe(0);
163
164
  });
164
165
  });
@@ -100,8 +100,28 @@ export function getAuthorizationHeaders(scheme: PublishScheme, key: string) {
100
100
  return {};
101
101
  }
102
102
 
103
- export function downloadFile(target: string, ca?: Buffer): Promise<Array<string>> {
104
- const httpsAgent = ca ? new Agent({ ca }) : undefined;
103
+ export interface AgentOptions {
104
+ ca?: Buffer;
105
+ allowSelfSigned?: boolean;
106
+ }
107
+
108
+ export async function getDefaultAgent() {
109
+ const ca = await getCertificate();
110
+ const allowSelfSigned = config.allowSelfSigned;
111
+ return getAgent({ ca, allowSelfSigned });
112
+ }
113
+
114
+ export function getAgent({ allowSelfSigned, ca }: AgentOptions) {
115
+ if (ca) {
116
+ return new Agent({ ca });
117
+ } else if (allowSelfSigned) {
118
+ return new Agent({ rejectUnauthorized: false });
119
+ } else {
120
+ return undefined;
121
+ }
122
+ }
123
+
124
+ export function downloadFile(target: string, httpsAgent: Agent): Promise<Array<string>> {
105
125
  return axios.default
106
126
  .get<Stream>(target, {
107
127
  responseType: 'stream',
@@ -206,10 +226,9 @@ export async function postForm(
206
226
  key: string,
207
227
  formData: FormDataObj,
208
228
  customHeaders: Record<string, string> = {},
209
- ca?: Buffer,
229
+ httpsAgent?: Agent,
210
230
  interactive = false,
211
231
  ): Promise<PostFormResult> {
212
- const httpsAgent = ca ? new Agent({ ca }) : undefined;
213
232
  const form = createAxiosForm(formData);
214
233
 
215
234
  const headers: Record<string, string> = {
@@ -237,7 +256,7 @@ export async function postForm(
237
256
  error,
238
257
  interactive,
239
258
  httpsAgent,
240
- (mode, token) => postForm(target, mode, token, formData, customHeaders, ca, false),
259
+ (mode, token) => postForm(target, mode, token, formData, customHeaders, httpsAgent, false),
241
260
  (status, statusText, response) => {
242
261
  if (status === 500) {
243
262
  log('failedHttpPost_0065', response);
@@ -266,9 +285,9 @@ export function postFile(
266
285
  file: Buffer,
267
286
  customFields: Record<string, string> = {},
268
287
  customHeaders: Record<string, string> = {},
269
- ca?: Buffer,
288
+ agent?: Agent,
270
289
  interactive = false,
271
290
  ): Promise<PostFormResult> {
272
291
  const data: FormDataObj = { ...customFields, file: [file, 'pilet.tgz'] };
273
- return postForm(target, scheme, key, data, customHeaders, ca, interactive);
292
+ return postForm(target, scheme, key, data, customHeaders, agent, interactive);
274
293
  }
@@ -1,3 +1,4 @@
1
+ import { Agent } from 'https';
1
2
  import { resolve, join, extname, basename, dirname, relative } from 'path';
2
3
  import { log, fail } from './log';
3
4
  import { cliVersion } from './info';
@@ -161,7 +162,8 @@ async function loadPiralInstance(root: string, details?: PiralInstanceDetails):
161
162
  export async function findPiralInstance(
162
163
  proposedApp: string,
163
164
  rootDir: string,
164
- details?: PiralInstanceDetails,
165
+ details: PiralInstanceDetails,
166
+ agent: Agent,
165
167
  interactive = false,
166
168
  ) {
167
169
  const path = findPackageRoot(proposedApp, rootDir);
@@ -172,13 +174,13 @@ export async function findPiralInstance(
172
174
 
173
175
  if (url) {
174
176
  log('generalDebug_0003', `Updating the emulator from remote "${url}" ...`);
175
- await updateFromEmulatorWebsite(root, url, interactive);
177
+ await updateFromEmulatorWebsite(root, url, agent, interactive);
176
178
  }
177
179
 
178
180
  return await loadPiralInstance(root, details);
179
181
  } else if (url) {
180
182
  log('generalDebug_0003', `Piral instance not installed yet - trying from remote "${url}" ...`);
181
- const emulator = await scaffoldFromEmulatorWebsite(rootDir, url);
183
+ const emulator = await scaffoldFromEmulatorWebsite(rootDir, url, agent);
182
184
  return await loadPiralInstance(emulator.path, details);
183
185
  }
184
186
 
@@ -190,6 +192,7 @@ export async function findPiralInstances(
190
192
  piletPackage: PiletPackageData,
191
193
  piletDefinition: undefined | PiletDefinition,
192
194
  rootDir: string,
195
+ agent: Agent,
193
196
  interactive?: boolean,
194
197
  ) {
195
198
  if (proposedApps) {
@@ -208,7 +211,7 @@ export async function findPiralInstances(
208
211
  if (proposedApps.length > 0) {
209
212
  return Promise.all(
210
213
  proposedApps.map((proposedApp) =>
211
- findPiralInstance(proposedApp, rootDir, piletDefinition?.piralInstances?.[proposedApp], interactive),
214
+ findPiralInstance(proposedApp, rootDir, piletDefinition?.piralInstances?.[proposedApp], agent, interactive),
212
215
  ),
213
216
  );
214
217
  }
@@ -280,11 +283,7 @@ export async function getPiralPackage(app: string, data: PiralInstanceData, vers
280
283
  };
281
284
  }
282
285
 
283
- async function getAvailableFiles(
284
- root: string,
285
- name: string,
286
- dirName: string,
287
- ): Promise<Array<FileDescriptor>> {
286
+ async function getAvailableFiles(root: string, name: string, dirName: string): Promise<Array<FileDescriptor>> {
288
287
  const source = getPiralPath(root, name);
289
288
  const tgz = `${dirName}.tar`;
290
289
  log('generalDebug_0003', `Checking if "${tgz}" exists in "${source}" ...`);
@@ -780,13 +779,13 @@ export async function findPiletRoot(proposedRoot: string) {
780
779
  return dirname(packageJsonPath);
781
780
  }
782
781
 
783
- export async function retrievePiletData(target: string, app?: string, interactive?: boolean) {
782
+ export async function retrievePiletData(target: string, app?: string, agent?: Agent, interactive?: boolean) {
784
783
  const piletJsonPath = await findFile(target, piletJson);
785
784
  const proposedRoot = piletJsonPath ? dirname(piletJsonPath) : target;
786
785
  const root = await findPiletRoot(proposedRoot);
787
786
  const piletPackage = await readJson(root, packageJson);
788
787
  const piletDefinition: PiletDefinition = piletJsonPath && (await readJson(proposedRoot, piletJson));
789
- const appPackages = await findPiralInstances(app && [app], piletPackage, piletDefinition, root, interactive);
788
+ const appPackages = await findPiralInstances(app && [app], piletPackage, piletDefinition, root, agent, interactive);
790
789
  const apps: Array<AppDefinition> = [];
791
790
 
792
791
  for (const appPackage of appPackages) {
@@ -1,3 +1,4 @@
1
+ import { Agent } from 'https';
1
2
  import { basename, dirname, relative } from 'path';
2
3
  import { readBinary } from './io';
3
4
  import { publishNpmPackage } from './npm';
@@ -39,7 +40,7 @@ export async function publishWebsiteEmulator(
39
40
  files: Array<string>,
40
41
  interactive: boolean,
41
42
  headers?: Record<string, string>,
42
- ca?: Buffer,
43
+ agent?: Agent,
43
44
  ) {
44
45
  const data: FormDataObj = {
45
46
  version,
@@ -53,5 +54,5 @@ export async function publishWebsiteEmulator(
53
54
  data[relPath] = [content, fileName];
54
55
  }
55
56
 
56
- return await postForm(url, mode, apiKey, data, headers, ca, interactive);
57
+ return await postForm(url, mode, apiKey, data, headers, agent, interactive);
57
58
  }
@@ -1,3 +1,4 @@
1
+ import { Agent } from 'https';
1
2
  import { progress } from './log';
2
3
  import { packageJson, piletJson } from './constants';
3
4
  import { readJson, updateExistingJson, writeJson } from './io';
@@ -55,12 +56,13 @@ export async function installPiralInstance(
55
56
  baseDir: string,
56
57
  rootDir: string,
57
58
  npmClient: NpmClientType,
59
+ agent: Agent,
58
60
  selected?: boolean,
59
61
  ): Promise<string> {
60
62
  const [sourceName, sourceVersion, hadVersion, type] = await dissectPackageName(baseDir, usedSource);
61
63
 
62
64
  if (type === 'remote') {
63
- const emulator = await scaffoldFromEmulatorWebsite(rootDir, sourceName);
65
+ const emulator = await scaffoldFromEmulatorWebsite(rootDir, sourceName, agent);
64
66
  const packageName = emulator.name;
65
67
  await updatePiletJson(rootDir, packageName, {
66
68
  selected,
@@ -1,7 +1,7 @@
1
1
  import { Agent } from 'https';
2
2
  import { posix, relative, resolve } from 'path';
3
3
  import { createPiralStubIndexIfNotExists } from './template';
4
- import { getAuthorizationHeaders, getAxiosOptions, getCertificate, handleAxiosError } from './http';
4
+ import { getAuthorizationHeaders, getAxiosOptions, handleAxiosError } from './http';
5
5
  import { packageJson } from './constants';
6
6
  import { updateConfig } from './config';
7
7
  import { ForceOverwrite } from './enums';
@@ -11,7 +11,7 @@ import { progress, log } from './log';
11
11
  import { axios, isInteractive } from '../external';
12
12
  import { EmulatorWebsiteManifestFiles, EmulatorWebsiteManifest } from '../types';
13
13
 
14
- async function requestManifest(url: string, interactive: boolean, httpsAgent?: Agent) {
14
+ async function requestManifest(url: string, httpsAgent: Agent, interactive: boolean) {
15
15
  const opts = getAxiosOptions(url);
16
16
 
17
17
  try {
@@ -26,7 +26,7 @@ async function requestManifest(url: string, interactive: boolean, httpsAgent?: A
26
26
  value: headers.authorization,
27
27
  },
28
28
  });
29
- return await requestManifest(url, false, httpsAgent);
29
+ return await requestManifest(url, httpsAgent, false);
30
30
  });
31
31
  }
32
32
  }
@@ -105,13 +105,11 @@ async function createEmulatorFiles(
105
105
  await downloadEmulatorFiles(manifestUrl, targetDir, appDir, emulatorJson.files, httpsAgent);
106
106
  }
107
107
 
108
- export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl: string, interactive: boolean) {
108
+ export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl: string, httpsAgent: Agent, interactive: boolean) {
109
109
  progress(`Updating emulator from %s ...`, manifestUrl);
110
- const ca = await getCertificate();
111
- const httpsAgent = ca ? new Agent({ ca }) : undefined;
112
110
 
113
111
  try {
114
- const response = await requestManifest(manifestUrl, interactive, httpsAgent);
112
+ const response = await requestManifest(manifestUrl, httpsAgent, interactive);
115
113
  const nextEmulator: EmulatorWebsiteManifest = response.data;
116
114
  const currentEmulator = await readJson(targetDir, packageJson);
117
115
 
@@ -130,12 +128,10 @@ export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl:
130
128
  }
131
129
  }
132
130
 
133
- export async function scaffoldFromEmulatorWebsite(rootDir: string, manifestUrl: string) {
131
+ export async function scaffoldFromEmulatorWebsite(rootDir: string, manifestUrl: string, httpsAgent: Agent) {
134
132
  progress(`Downloading emulator from %s ...`, manifestUrl);
135
- const ca = await getCertificate();
136
- const httpsAgent = ca ? new Agent({ ca }) : undefined;
137
133
  const interactive = isInteractive();
138
- const response = await requestManifest(manifestUrl, interactive, httpsAgent);
134
+ const response = await requestManifest(manifestUrl, httpsAgent, interactive);
139
135
  const emulatorJson: EmulatorWebsiteManifest = response.data;
140
136
  const targetDir = resolve(rootDir, 'node_modules', emulatorJson.name);
141
137
  const appDir = resolve(targetDir, 'app');