@fugood/bricks-project 2.21.0-beta.14.test5 → 2.21.0-beta.14.test7

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/api/instance.ts CHANGED
@@ -1,19 +1,19 @@
1
- const stage = process.env.BRICKS_STAGE || 'production'
2
-
3
1
  const apiUrlMap = {
4
2
  production: 'https://display.bricks.tools/api/graphql-workspace',
5
3
  beta: 'https://display-beta.bricks.tools/api/graphql-workspace',
6
- dev: 'http://localhost:3001/api/graphql-workspace',
4
+ development: 'http://localhost:3001/api/graphql-workspace',
7
5
  }
8
6
 
9
- const apiURL = apiUrlMap[stage]
10
- if (!apiURL) throw new Error(`Invalid BRICKS_STAGE: ${stage}`)
11
-
12
7
  const workspaceToken = process.env.BRICKS_WORKSPACE_TOKEN
13
8
 
14
- const doGQL = async (query: string, variables: Record<string, any>) => {
9
+ type Stage = 'production' | 'beta' | 'dev'
10
+
11
+ const doGQL = async (stage: Stage, query: string, variables: Record<string, any>) => {
15
12
  if (!workspaceToken) throw new Error('env BRICKS_WORKSPACE_TOKEN is not set')
16
13
 
14
+ const apiURL = apiUrlMap[stage]
15
+ if (!apiURL) throw new Error(`Invalid stage: ${stage}`)
16
+
17
17
  const data = await fetch(apiURL, {
18
18
  method: 'POST',
19
19
  headers: {
@@ -22,12 +22,57 @@ const doGQL = async (query: string, variables: Record<string, any>) => {
22
22
  },
23
23
  body: JSON.stringify({ query, variables }),
24
24
  }).then((res) => res.json())
25
+
26
+ if (data.error && data.error.message) throw new Error(data.error.message)
25
27
  return data
26
28
  }
27
29
 
28
- export const deployApp = async (appId: string, config: {}, lastCommitId: string) => {
30
+ export const pullApp = async (stage: Stage, appId: string) => {
31
+ const { data, errors } = await doGQL(
32
+ stage,
33
+ `query BRICKS_PROJECT_application($id: ID!) {
34
+ application(id: $id) {
35
+ _id
36
+ name
37
+ description
38
+ config
39
+ lock {
40
+ enabled
41
+ }
42
+ dev_ref {
43
+ is_dev
44
+ }
45
+ }
46
+ }`,
47
+ { id: appId },
48
+ )
49
+ if (errors) throw new Error(errors[0].message)
50
+
51
+ const app = data.application
52
+ if (!app) throw new Error('App not found')
53
+ if (app.lock.enabled) throw new Error('App is locked')
54
+ if (!app.dev_ref.is_dev)
55
+ throw new Error(
56
+ 'Currently BRICKS Project is experimental, please use the fork version of the app',
57
+ )
58
+ return app
59
+ }
60
+
61
+ type Config = { title?: string }
62
+
63
+ export const deployApp = async (
64
+ stage: Stage,
65
+ appId: string,
66
+ config: Config,
67
+ lastCommitId: string,
68
+ ) => {
69
+ const app = await pullApp(stage, appId)
70
+ if (app.config?.bricks_project_last_commit_id === lastCommitId)
71
+ throw new Error('No changes to deploy')
72
+
29
73
  const { errors } = await doGQL(
30
- `mutation BRICKS_PROJECT_updateApplication($id: ID!, $config: JSON) {
74
+ stage,
75
+ `mutation BRICKS_PROJECT_updateApplication($id: ID!, $config: String) {
31
76
  updateApplication(
32
77
  id: $id,
33
78
  config: $config
@@ -38,19 +83,67 @@ export const deployApp = async (appId: string, config: {}, lastCommitId: string)
38
83
  }`,
39
84
  {
40
85
  id: appId,
41
- config: {
86
+ config: JSON.stringify({
42
87
  ...config,
88
+ title: `${config.title || app.name || 'Untitled'} (${Date.now()})`,
43
89
  bricks_project_last_commit_id: lastCommitId,
44
- },
90
+ }),
45
91
  },
46
92
  )
47
93
  if (errors) throw new Error(errors[0].message)
48
94
  return true
49
95
  }
50
96
 
51
- export const deployModule = async (modId: string, config: {}, lastCommitId: string) => {
97
+ export const pullApplicationProject = async (stage: Stage, appId: string) => {
98
+ const { data, errors } = await doGQL(
99
+ stage,
100
+ `query BRICKS_PROJECT_applicationProject($id: ID!) {
101
+ UNSTABLE_applicationProject(id: $id, buildApplicationOnly: true)
102
+ }`,
103
+ { id: appId },
104
+ )
105
+ if (errors) throw new Error(errors[0].message)
106
+ if (!data.UNSTABLE_applicationProject) throw new Error('Application not found')
107
+ return data.UNSTABLE_applicationProject
108
+ }
109
+
110
+ export const pullModule = async (stage: Stage, modId: string) => {
111
+ const { data, errors } = await doGQL(
112
+ stage,
113
+ `query BRICKS_PROJECT_module($id: ID!) {
114
+ module(id: $id) {
115
+ _id
116
+ name
117
+ description
118
+ is_public
119
+ config
120
+ }
121
+ }`,
122
+ { id: modId },
123
+ )
124
+ if (errors) throw new Error(errors[0].message)
125
+ const mod = data.module
126
+ if (!mod) throw new Error('Module not found')
127
+ if (mod.is_public)
128
+ throw new Error(
129
+ 'Currently BRICKS Project is experimental, public module deployment is temporary not recommended',
130
+ )
131
+ return mod
132
+ }
133
+
134
+ export const deployModule = async (
135
+ stage: Stage,
136
+ modId: string,
137
+ config: Config,
138
+ lastCommitId: string,
139
+ ) => {
140
+ const mod = await pullModule(stage, modId)
141
+ if (mod.config?.bricks_project_last_commit_id === lastCommitId)
142
+ throw new Error('No changes to deploy')
143
+
52
144
  const { errors } = await doGQL(
53
- `mutation BRICKS_PROJECT_updateModule($id: ID!, $config: JSON) {
145
+ stage,
146
+ `mutation BRICKS_PROJECT_updateModule($id: ID!, $config: String) {
54
147
  updateModule(
55
148
  id: $id
56
149
  config: $config
@@ -62,44 +155,26 @@ export const deployModule = async (modId: string, config: {}, lastCommitId: stri
62
155
  }`,
63
156
  {
64
157
  id: modId,
65
- config: {
158
+ config: JSON.stringify({
66
159
  ...config,
160
+ title: `${config.title || mod.name || 'Untitled'} (${Date.now()})`,
67
161
  bricks_project_last_commit_id: lastCommitId,
68
- },
162
+ }),
69
163
  },
70
164
  )
71
165
  if (errors) throw new Error(errors[0].message)
72
166
  return true
73
167
  }
74
168
 
75
- export const pullApp = async (appId: string) => {
76
- const { data, errors } = await doGQL(
77
- `query BRICKS_PROJECT_application($id: ID!) {
78
- application(id: $id) {
79
- _id
80
- name
81
- description
82
- config
83
- }
84
- }`,
85
- { id: appId },
86
- )
87
- if (errors) throw new Error(errors[0].message)
88
- return data.application
89
- }
90
-
91
- export const pullModule = async (modId: string) => {
169
+ export const pullModuleProject = async (stage: Stage, modId: string) => {
92
170
  const { data, errors } = await doGQL(
93
- `query BRICKS_PROJECT_module($id: ID!) {
94
- module(id: $id) {
95
- _id
96
- name
97
- description
98
- config
99
- }
171
+ stage,
172
+ `query BRICKS_PROJECT_moduleProject($id: ID!) {
173
+ UNSTABLE_moduleProject(id: $id, buildApplicationOnly: true)
100
174
  }`,
101
175
  { id: modId },
102
176
  )
103
177
  if (errors) throw new Error(errors[0].message)
104
- return data.module
178
+ if (!data.UNSTABLE_moduleProject) throw new Error('Module not found')
179
+ return data.UNSTABLE_moduleProject
105
180
  }