eitri-cli 1.4.0-beta.1 → 1.4.0-beta.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eitri-cli",
3
- "version": "1.4.0-beta.1",
3
+ "version": "1.4.0-beta.3",
4
4
  "description": "Command Line Interface to make \"Eitri-App\" with code and fire.",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -8,9 +8,9 @@ const config = require('../service/ConfigService')
8
8
  const TargetService = require('../service/TargetService')
9
9
  const inquirer = require('inquirer')
10
10
  const TrackingEitriAnalytics = require('../service/TrackingEitriAnalytics')
11
- const VegvisirService = require('../modules/vegvisir/VegvisirService')
12
- const getWorkspace = require('../util/getWorkspace')
13
11
  const UserLocalCredential = require('../util/UserLocalCredential')
12
+ const VegvisirService = require('../modules/vegvisir/VegvisirService')
13
+ const {randomUUID} = require('crypto')
14
14
 
15
15
  const blindGuardian = workspace.blindGuardian
16
16
  const trackingService = new TrackingService(blindGuardian)
@@ -39,7 +39,6 @@ module.exports = async function pushVersion(cmdObj) {
39
39
  }
40
40
  workspace.publishing = true
41
41
  const miniConf = workspace.getMiniConf()
42
- await vegvisirService.check(miniConf.slug)
43
42
 
44
43
  let validateResult = checkErros(miniConf)
45
44
 
@@ -82,8 +81,12 @@ ${await targetService.getAppConfExampleSnippet()}
82
81
  blindGuardian.readConf()
83
82
  workspace.setServerUrl(config.get('workspace').url)
84
83
  console.log('Conectando ao Eitri...')
85
- await workspace.init()
86
- const userWorkspace = await getWorkspace()
84
+
85
+ const tempWorkspaceId = randomUUID()
86
+ vegvisirService.setWorkspaceId(tempWorkspaceId)
87
+ await workspace.initPushVersion(tempWorkspaceId)
88
+ const userWorkspace = await vegvisirService.getWorkspace()
89
+
87
90
  await miniLog.connect(userWorkspace.id)
88
91
  console.log(separator)
89
92
  console.log("Analisando versões...");
package/src/cmd/start.js CHANGED
@@ -10,7 +10,6 @@ const handleStartServer = require('../service/StarterService')
10
10
  const inquirer = require('inquirer')
11
11
  const TrackingEitriAnalytics = require('../service/TrackingEitriAnalytics')
12
12
  const VegvisirService = require('../modules/vegvisir/VegvisirService')
13
- const getWorkspace = require('../util/getWorkspace')
14
13
  const UserLocalCredential = require('../util/UserLocalCredential')
15
14
 
16
15
  const blindGuardian = workspace.blindGuardian
@@ -71,7 +70,7 @@ module.exports = async function start(args) {
71
70
  workspace.setQrCodeUrl(qrCodeUrl)
72
71
  await workspace.init()
73
72
  const silentOnConnect = args.verbose ? false : true
74
- const userWorkspace = await getWorkspace()
73
+ const userWorkspace = await vegvisirService.getWorkspace()
75
74
  await miniLog.connect(userWorkspace.id, silentOnConnect)
76
75
 
77
76
  console.log("Construindo...");
@@ -18,6 +18,10 @@ module.exports = function VegvisirCommand() {
18
18
  "--local",
19
19
  "Seleciona um workspace para um diretório de Eitri-App específico"
20
20
  )
21
+ .option(
22
+ "--name <workspace-name>",
23
+ "Permite selecionar um workspace previamente criado pelo nome"
24
+ )
21
25
  .action(async (cmdObj) => {
22
26
  require("./cmd/use.js")(cmdObj);
23
27
  });
@@ -2,14 +2,26 @@ const { default: axios } = require("../../../node_modules/axios/index");
2
2
  const BlindGuardian = require("../../service/BlindGuardian");
3
3
  const Http = require("../../service/Http");
4
4
  const configService = require("../../service/ConfigService");
5
- const getWorkspace = require("../../util/getWorkspace");
6
5
  const writeGlobalWorkspaceConfig = require("./utils/writeGlobalWorkspaceConfig");
7
-
6
+ const { readFile } = require("fs/promises");
7
+ const os = require("os");
8
+ const {validateUUID} = require("../../util/GenericUtils");
9
+ const path = require("path");
10
+ const fs = require('fs')
8
11
  module.exports = class VegvisirService {
9
12
  constructor() {
10
13
  const blindGuardian = new BlindGuardian();
11
14
  this.http = new Http(blindGuardian);
12
15
  this.config = configService.get("vegvisir");
16
+ this.workspaceId = "";
17
+ }
18
+
19
+ getWorkspaceId() {
20
+ return this.workspaceId;
21
+ }
22
+
23
+ setWorkspaceId(id) {
24
+ this.workspaceId = id;
13
25
  }
14
26
 
15
27
  async listMyWorkspaces() {
@@ -44,7 +56,7 @@ module.exports = class VegvisirService {
44
56
 
45
57
  async check(slug) {
46
58
  try {
47
- let workspace = await getWorkspace()
59
+ let workspace = await this.getWorkspace()
48
60
  if(!workspace) {
49
61
  const workspaces = await this.listMyWorkspaces();
50
62
  const defaultWorkspace = await workspaces.find(w => w.name === 'DEFAULT')
@@ -78,4 +90,40 @@ module.exports = class VegvisirService {
78
90
  }
79
91
 
80
92
  }
93
+
94
+ async getWorkspace() {
95
+ try {
96
+ const workspaceEitriAppProjectPath = path.resolve(
97
+ process.cwd(),
98
+ ".workspaces",
99
+ "workspace"
100
+ );
101
+
102
+ if (fs.existsSync(workspaceEitriAppProjectPath)) {
103
+ const fileContent = await readFile(
104
+ workspaceEitriAppProjectPath,
105
+ "utf8"
106
+ );
107
+ const workspace = JSON.parse(fileContent);
108
+ return workspace;
109
+ }
110
+ const workspaceGlobalPath = path.resolve(
111
+ os.homedir(),
112
+ ".eitri",
113
+ "workspaces",
114
+ "workspace"
115
+ );
116
+ const fileContent = await readFile(workspaceGlobalPath, "utf8");
117
+ const workspace = JSON.parse(fileContent);
118
+ console.log("Consutrindo de ", workspace.id)
119
+ workspace.id = validateUUID(this.workspaceId) ? this.workspaceId : workspace.id;
120
+ return workspace;
121
+ } catch (error) {
122
+ if(error.code === "ENOENT") {
123
+ return;
124
+ }
125
+ console.error("Ocorreu um erro inesperado ao tentar ler o workspace atual.", error?.message)
126
+ return process.exit(1)
127
+ }
128
+ };
81
129
  };
@@ -1,8 +1,9 @@
1
- const getWorkspace = require("../../../util/getWorkspace");
1
+ const VegvisirService = require("../../vegvisir/VegvisirService");
2
+ const vegvisirService = new VegvisirService()
2
3
 
3
4
  module.exports = async function current(cmdObj) {
4
5
  try {
5
- const workspace = await getWorkspace()
6
+ const workspace = await vegvisirService.getWorkspace()
6
7
  if(!workspace) {
7
8
  console.log("Você não possui workspace selecionado para desenvolvimento.")
8
9
  return;
@@ -11,18 +11,28 @@ module.exports = async function use(cmdArgs) {
11
11
  const workspaces = await vegvisirService.listMyWorkspaces();
12
12
  if (!workspaces) return;
13
13
 
14
- const { workspaceName } = await inquirer.prompt([
15
- {
16
- name: "workspaceName",
17
- type: "rawlist",
18
- message: "Selecione qual workspace você deseja utilizar:",
19
- choices: workspaces,
20
- },
21
- ]);
14
+ let workspaceNameSelected = cmdArgs?.name
15
+
16
+ if(!cmdArgs?.name){
17
+ const { workspaceName } = await inquirer.prompt([
18
+ {
19
+ name: "workspaceName",
20
+ type: "rawlist",
21
+ message: "Selecione qual workspace você deseja utilizar:",
22
+ choices: workspaces,
23
+ },
24
+ ]);
25
+ workspaceNameSelected = workspaceName
26
+ }
22
27
 
23
28
  const selectedWorkspace = workspaces.find(
24
- (work) => work.name === workspaceName
29
+ (work) => work?.name === workspaceNameSelected
25
30
  );
31
+ if(!selectedWorkspace){
32
+ const workspacesNames = Array.isArray(workspaces) && workspaces?.length > 0 ? workspaces?.map(w => w.name) : [];
33
+ console.warn(`O workspace [${cmdArgs?.name}] não existe. Workspaces existentes: `, JSON.stringify(workspacesNames));
34
+ return;
35
+ };
26
36
 
27
37
  if (cmdArgs.local) {
28
38
  await writeLocalWorkspaceConfig(selectedWorkspace);
@@ -5,12 +5,11 @@ const CookieJar = require('tough-cookie').CookieJar
5
5
  const os = require('os')
6
6
  const path = require('path')
7
7
  const getCliVersion = require('../util/getCliVersion')
8
- const getWorkspace = require('../util/getWorkspace')
9
8
  const { randomUUID } = require('crypto')
9
+ const getWorkspace = require('../util/getWorkspace')
10
10
 
11
11
  const filePath = path.join(os.homedir(), './.eitri.cookie.json')
12
12
  const jar = new CookieJar(new FileCookieStore(filePath))
13
-
14
13
  const axios = require('axios').create({ withCredentials: true, jar })
15
14
 
16
15
  axiosCookieJarSupport(axios)
@@ -38,6 +37,7 @@ class Http {
38
37
  constructor(tokenFactory) {
39
38
  this.tokenFactory = tokenFactory
40
39
  this.newTokenAttempt = 0
40
+ this.workspaceId = ""
41
41
  }
42
42
 
43
43
  setTokenFactory(tokenFactory) {
@@ -48,6 +48,10 @@ class Http {
48
48
  this.token = token
49
49
  }
50
50
 
51
+ setWorkspaceId(id){
52
+ this.workspaceId = id
53
+ }
54
+
51
55
  getCookieSession(url) {
52
56
  return new Promise((resolve, reject) => {
53
57
  jar.getCookies(url, { allPaths: true }, function (err, cookies) {
@@ -87,7 +91,7 @@ class Http {
87
91
  'App-Tools-Version': getCliVersion()
88
92
  }
89
93
  if (url.includes("foundry") || url.includes("runes-foundry")) {
90
- headers["Workspace-Id"] = (await getWorkspace()).id;
94
+ headers["Workspace-Id"] = this.workspaceId ? this.workspaceId : (await getWorkspace()).id;
91
95
  }
92
96
 
93
97
  return headers
@@ -23,8 +23,9 @@ const Buffer = require("buffer");
23
23
  const DEFAULT_ENV = "dev";
24
24
  const cliProgress = require("cli-progress");
25
25
  const EitriAppManager = require("./EitriAppManager");
26
- const getWorkspace = require("../util/getWorkspace");
27
26
  const EitriAppService = require("./EitriAppService");
27
+ const VegvisirService = require("../modules/vegvisir/VegvisirService");
28
+ const vegvisirService = new VegvisirService()
28
29
 
29
30
  class Workspace {
30
31
 
@@ -96,6 +97,7 @@ class Workspace {
96
97
  return this.resourceFolders2watch;
97
98
  }
98
99
 
100
+
99
101
  async getTargetConfig(id, target) {
100
102
  try {
101
103
  const configs = await this.getAllTargetConfigs(target);
@@ -152,6 +154,11 @@ class Workspace {
152
154
  this.userEmail = token.email;
153
155
  }
154
156
 
157
+ async initPushVersion(workspaceId = "") {
158
+ this.http.setWorkspaceId(workspaceId);
159
+ this.init()
160
+ }
161
+
155
162
  getMiniConf() {
156
163
  if (!this._miniConf) {
157
164
  const eitriAppConfPath = path.resolve(
@@ -364,7 +371,7 @@ class Workspace {
364
371
  let miniAppUrl = `${this.getBootstrapURL(
365
372
  targetConfig.bootstrapBaseUrl
366
373
  )}/${this.config.basePath || "workspace"
367
- }/user/${(await getWorkspace()).id}/${viewHtml}?data=${encodedData}${paramOrderId}${paramShareId}&devmode=true`;
374
+ }/user/${(await vegvisirService.getWorkspace()).id}/${viewHtml}?data=${encodedData}${paramOrderId}${paramShareId}&devmode=true`;
368
375
 
369
376
  if (cid) {
370
377
  miniAppUrl = miniAppUrl + "&cid=" + cid;
@@ -1043,6 +1050,7 @@ class Workspace {
1043
1050
  toBase64(content) {
1044
1051
  return Buffer.from(content).toString("base64");
1045
1052
  }
1053
+
1046
1054
  }
1047
1055
 
1048
1056
  module.exports = {
@@ -14,6 +14,11 @@ class GenericUtils {
14
14
  return formattedDateTime;
15
15
  }
16
16
 
17
+ static validateUUID = (uuid = "") => {
18
+ const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi;
19
+ return uuid && typeof uuid === "string" && regexExp.test(uuid)
20
+ }
21
+
17
22
  }
18
23
 
19
24
  module.exports = GenericUtils
@@ -1,5 +1,5 @@
1
- const { existsSync } = require("fs");
2
- const { readFile } = require("fs/promises");
1
+ const {existsSync} = require("fs");
2
+ const {readFile} = require("fs/promises");
3
3
  const path = require("path");
4
4
  const os = require("os");
5
5
  module.exports = async function getWorkspace() {
@@ -9,7 +9,7 @@ module.exports = async function getWorkspace() {
9
9
  ".workspaces",
10
10
  "workspace"
11
11
  );
12
-
12
+
13
13
  if (existsSync(workspaceEitriAppProjectPath)) {
14
14
  const fileContent = await readFile(
15
15
  workspaceEitriAppProjectPath,
@@ -28,10 +28,10 @@ module.exports = async function getWorkspace() {
28
28
  const workspace = JSON.parse(fileContent);
29
29
  return workspace;
30
30
  } catch (error) {
31
- if(error.code === "ENOENT") {
31
+ if (error.code === "ENOENT") {
32
32
  return;
33
33
  }
34
34
  console.error("Houve um erro inesperado ao tentar ler o workspace atual.")
35
35
  return process.exit(1)
36
36
  }
37
- };
37
+ };
@@ -190,6 +190,27 @@ describe("eitri-cli", () => {
190
190
  2 * minutes
191
191
  );
192
192
 
193
+ it(
194
+ "should select workspace by name",
195
+ async () => {
196
+ try {
197
+ await execAsync(
198
+ `cd ${EITRI_WORK_DIR} && rm -rf ./eitri-test-*`,
199
+ { env: process.env }
200
+ );
201
+
202
+ const executor = new Executor({ env: devEnv });
203
+ await executor
204
+ .exec("eitri workspace use --name DEFAULT")
205
+ .waitFor(/Workspace configurado com sucesso!/);
206
+ } catch (e) {
207
+ console.error(e);
208
+ throw e;
209
+ }
210
+ },
211
+ 2 * minutes
212
+ );
213
+
193
214
  const getPublishedVersion = async (version, eitriAppId) => {
194
215
  const blindGuardian = new BlindGuardian();
195
216
  const http = new Http(blindGuardian);
@@ -1,5 +1,6 @@
1
- const getWorkspace = require("../../src/util/getWorkspace");
1
+ const VegvisirService = require("../../src/modules/vegvisir/VegvisirService");
2
2
  const fs = require('fs/promises')
3
+ const vegvisirService = new VegvisirService()
3
4
 
4
5
 
5
6
  jest.mock('fs', () => ({
@@ -9,7 +10,7 @@ jest.mock('fs', () => ({
9
10
  describe('Get WorkspaceID Tests', () => {
10
11
 
11
12
  it('should return error if workspace file not found', async () => {
12
- const promise = getWorkspace()
13
+ const promise = vegvisirService.getWorkspace()
13
14
  await expect(promise).rejects.toThrow("Você não tem nenhum workspace definido para desenvolvimento, execute o comando 'eitri workspace use' para definir um workspace.")
14
15
  });
15
16