@agilecustoms/envctl 1.2.0 → 1.3.1

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.
@@ -1,4 +1,10 @@
1
1
  import { spawn } from 'child_process';
2
+ import { createWriteStream } from 'node:fs';
3
+ import { mkdir } from 'node:fs/promises';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { Readable } from 'node:stream';
7
+ import { pipeline } from 'node:stream/promises';
2
8
  import path from 'path';
3
9
  import * as readline from 'readline';
4
10
  import inquirer from 'inquirer';
@@ -95,4 +101,11 @@ export class Cli {
95
101
  getKind(cwd) {
96
102
  return getDirName(cwd);
97
103
  }
104
+ async writeInTmpFile(stream, fileName) {
105
+ const outDir = join(tmpdir(), 'envctl');
106
+ await mkdir(outDir, { recursive: true });
107
+ const outPath = join(outDir, fileName);
108
+ await pipeline(Readable.fromWeb(stream), createWriteStream(outPath, { flags: 'w' }));
109
+ return outPath;
110
+ }
98
111
  }
@@ -1,11 +1,14 @@
1
1
  import { BusinessException, KnownException, NotFoundException } from '../exceptions.js';
2
2
  import { EnvStatus } from '../model/index.js';
3
- import { HttpClient } from './HttpClient.js';
3
+ import { HttpClient, toUrl } from './HttpClient.js';
4
4
  export class EnvApiClient {
5
5
  httpClient;
6
6
  constructor(httpClient) {
7
7
  this.httpClient = httpClient;
8
8
  }
9
+ async fetch(url) {
10
+ return await fetch(url);
11
+ }
9
12
  async get(key) {
10
13
  try {
11
14
  return await this.httpClient.fetch(`/ci/env/${key}`);
@@ -28,7 +31,7 @@ export class EnvApiClient {
28
31
  }
29
32
  throw error;
30
33
  }
31
- return result.token;
34
+ return toUrl(`/public/env/${env.key}/extend?token=${result.token}`);
32
35
  }
33
36
  async create(env) {
34
37
  let result;
@@ -1,5 +1,8 @@
1
1
  import { BusinessException, HttpException, NotFoundException } from '../exceptions.js';
2
2
  const HOST = 'cli.maintenance.agilecustoms.com';
3
+ export function toUrl(path) {
4
+ return `https://${HOST}/env-api${path}`;
5
+ }
3
6
  export class HttpClient {
4
7
  constructor() { }
5
8
  async post(path, body, options = {}) {
@@ -15,7 +18,7 @@ export class HttpClient {
15
18
  return await this.fetch(path, options);
16
19
  }
17
20
  async fetch(path, options = {}) {
18
- const url = `https://${HOST}/env-api${path}`;
21
+ const url = toUrl(path);
19
22
  let response;
20
23
  try {
21
24
  response = await fetch(url, options);
@@ -1,9 +1,4 @@
1
- import { createWriteStream } from 'node:fs';
2
- import { mkdir } from 'node:fs/promises';
3
- import { tmpdir } from 'node:os';
4
- import { basename, join } from 'node:path';
5
- import { Readable } from 'node:stream';
6
- import { pipeline } from 'node:stream/promises';
1
+ import { basename } from 'node:path';
7
2
  import { TerraformAdapter } from '../client/index.js';
8
3
  import { KnownException } from '../exceptions.js';
9
4
  import { BaseService } from './BaseService.js';
@@ -32,15 +27,12 @@ export class LogService extends BaseService {
32
27
  }).formatToParts(timestamp);
33
28
  const get = (type) => parts.find(p => p.type === type)?.value ?? '';
34
29
  const outName = `${get('year')}-${get('month')}-${get('day')}_${get('hour')}.${get('minute')}.${get('second')}.log`;
35
- const outDir = join(tmpdir(), 'envctl');
36
- await mkdir(outDir, { recursive: true });
37
- const outPath = join(outDir, outName);
38
- const res = await fetch(url);
30
+ const res = await this.envApi.fetch(url);
39
31
  if (!res.ok) {
40
32
  const msg = await res.text();
41
33
  throw new KnownException(`Failed to download logs: ${res.status} ${res.statusText}${msg ? ` - ${msg}` : ''}`);
42
34
  }
43
- await pipeline(Readable.fromWeb(res.body), createWriteStream(outPath, { flags: 'w' }));
35
+ const outPath = await this.cli.writeInTmpFile(res.body, outName);
44
36
  console.log(`Logs saved to: ${outPath}`);
45
37
  await this.cli.run('open', ['-R', outPath]);
46
38
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agilecustoms/envctl",
3
3
  "description": "node.js CLI client for manage environments",
4
- "version": "1.2.0",
4
+ "version": "1.3.1",
5
5
  "author": "Alex Chekulaev",
6
6
  "type": "module",
7
7
  "engines": {