@joystick.js/cli-canary 0.0.0-canary.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.
Files changed (231) hide show
  1. package/.build/getFilesToBuild.js +38 -0
  2. package/.build/index.js +69 -0
  3. package/HMRNOTES.txt +23 -0
  4. package/README.md +7 -0
  5. package/_package.json +55 -0
  6. package/canary.js +12 -0
  7. package/dist/cli.js +69 -0
  8. package/dist/functions/build/getTarIgnoreList.js +25 -0
  9. package/dist/functions/build/index.js +54 -0
  10. package/dist/functions/create/buildPackageJSON.js +20 -0
  11. package/dist/functions/create/index.js +193 -0
  12. package/dist/functions/create/templates/api/index.js +4 -0
  13. package/dist/functions/create/templates/i18n/en-US.js +4 -0
  14. package/dist/functions/create/templates/index.client.js +7 -0
  15. package/dist/functions/create/templates/index.css +20 -0
  16. package/dist/functions/create/templates/index.html +23 -0
  17. package/dist/functions/create/templates/index.server.js +21 -0
  18. package/dist/functions/create/templates/public/apple-touch-icon-152x152.png +0 -0
  19. package/dist/functions/create/templates/public/favicon.ico +0 -0
  20. package/dist/functions/create/templates/public/manifest.json +25 -0
  21. package/dist/functions/create/templates/public/service-worker.js +32 -0
  22. package/dist/functions/create/templates/public/splash-screen-1024x1024.png +0 -0
  23. package/dist/functions/create/templates/settings.development.json +27 -0
  24. package/dist/functions/create/templates/ui/components/button/index.js +50 -0
  25. package/dist/functions/create/templates/ui/layouts/app/index.js +13 -0
  26. package/dist/functions/create/templates/ui/pages/error/index.js +31 -0
  27. package/dist/functions/create/templates/ui/pages/index/index.js +94 -0
  28. package/dist/functions/index.js +243 -0
  29. package/dist/functions/logout/index.js +20 -0
  30. package/dist/functions/push/checkIfProvisionAvailable.js +19 -0
  31. package/dist/functions/push/deploy/index.js +173 -0
  32. package/dist/functions/push/deploy/initial.js +100 -0
  33. package/dist/functions/push/deploy/version.js +100 -0
  34. package/dist/functions/push/domains.js +23 -0
  35. package/dist/functions/push/getAvailableCDN.js +23 -0
  36. package/dist/functions/push/getDeployment.js +41 -0
  37. package/dist/functions/push/getDeploymentSummary.js +38 -0
  38. package/dist/functions/push/getInstanceSizeRegions.js +37 -0
  39. package/dist/functions/push/getInstanceSizesForRegion.js +34 -0
  40. package/dist/functions/push/getProviderInstanceSizes.js +34 -0
  41. package/dist/functions/push/getProviderRegions.js +36 -0
  42. package/dist/functions/push/getProvidersWithConnectionStatus.js +20 -0
  43. package/dist/functions/push/getSessionToken.js +23 -0
  44. package/dist/functions/push/getUserFromSessionToken.js +37 -0
  45. package/dist/functions/push/index.js +210 -0
  46. package/dist/functions/push/loginToCheatCode.js +33 -0
  47. package/dist/functions/push/pingProvisionAPI.js +31 -0
  48. package/dist/functions/push/prompts.js +332 -0
  49. package/dist/functions/push/providerMap.js +11 -0
  50. package/dist/functions/push/providers.js +9 -0
  51. package/dist/functions/reset/index.js +0 -0
  52. package/dist/functions/reset/reset.js +127 -0
  53. package/dist/functions/start/bootstrapLayoutComponent.js +54 -0
  54. package/dist/functions/start/bootstrapPageComponent.js +51 -0
  55. package/dist/functions/start/buildFile.js +85 -0
  56. package/dist/functions/start/buildFiles.js +76 -0
  57. package/dist/functions/start/buildPlugins.js +183 -0
  58. package/dist/functions/start/checkIfPortAvailable.js +18 -0
  59. package/dist/functions/start/cleanup/index.js +18 -0
  60. package/dist/functions/start/databases/mongodb/availableQueryParameters.js +42 -0
  61. package/dist/functions/start/databases/mongodb/buildConnectionString.js +22 -0
  62. package/dist/functions/start/databases/mongodb/buildQueryParameters.js +14 -0
  63. package/dist/functions/start/databases/mongodb/checkConnection.js +38 -0
  64. package/dist/functions/start/databases/mongodb/connect.js +25 -0
  65. package/dist/functions/start/databases/mongodb/index.js +93 -0
  66. package/dist/functions/start/databases/postgresql/checkConnection.js +41 -0
  67. package/dist/functions/start/databases/postgresql/connect.js +27 -0
  68. package/dist/functions/start/databases/postgresql/index.js +106 -0
  69. package/dist/functions/start/databases/redis.js +0 -0
  70. package/dist/functions/start/databases/startProvider.js +16 -0
  71. package/dist/functions/start/fileDependencyMapper.js +22 -0
  72. package/dist/functions/start/filesToCopy.js +18 -0
  73. package/dist/functions/start/generateId.js +74 -0
  74. package/dist/functions/start/getCodependenciesForFile.js +64 -0
  75. package/dist/functions/start/getFilesToBuild.js +39 -0
  76. package/dist/functions/start/getProcessIdFromPort.js +60 -0
  77. package/dist/functions/start/hmrServer.js +66 -0
  78. package/dist/functions/start/index.js +535 -0
  79. package/dist/functions/start/isWindows.js +5 -0
  80. package/dist/functions/start/minifyFile.js +12 -0
  81. package/dist/functions/start/onWarn.js +63 -0
  82. package/dist/functions/start/readFileDependencyMap.js +16 -0
  83. package/dist/functions/start/removeDeletedDependenciesFromMap.js +18 -0
  84. package/dist/functions/start/setComponentId.js +46 -0
  85. package/dist/functions/start/updateFileMap.js +67 -0
  86. package/dist/functions/update/index.js +21 -0
  87. package/dist/functions/use/index.js +61 -0
  88. package/dist/index.js +8 -0
  89. package/dist/lib/CLILog.js +49 -0
  90. package/dist/lib/buildResolvePlugin.js +21 -0
  91. package/dist/lib/checkIfValidJSON.js +11 -0
  92. package/dist/lib/colorLog.js +7 -0
  93. package/dist/lib/getArgs.js +14 -0
  94. package/dist/lib/getCodeFrame.js +10 -0
  95. package/dist/lib/getOptions.js +15 -0
  96. package/dist/lib/getPlatformSafePath.js +7 -0
  97. package/dist/lib/getProvider.js +8 -0
  98. package/dist/lib/help.js +56 -0
  99. package/dist/lib/isObject.js +6 -0
  100. package/dist/lib/isValidJSONString.js +11 -0
  101. package/dist/lib/isWindows.js +5 -0
  102. package/dist/lib/killProcesses.js +0 -0
  103. package/dist/lib/loadSettings.js +33 -0
  104. package/dist/lib/loader.js +82 -0
  105. package/dist/lib/masterIgnoreList.js +16 -0
  106. package/dist/lib/nodeEnvironment.js +9 -0
  107. package/dist/lib/nodeEnvironments.js +8 -0
  108. package/dist/lib/parseCookiesFromLogin.js +18 -0
  109. package/dist/lib/rainbowRoad.js +7 -0
  110. package/dist/lib/readDirectorySync.js +14 -0
  111. package/dist/lib/regexes.js +12 -0
  112. package/dist/lib/replaceBackslashesWithForwardSlashes.js +8 -0
  113. package/dist/lib/replaceFileProtocol.js +8 -0
  114. package/dist/lib/serializeQueryParameters.js +8 -0
  115. package/dist/lib/settings.js +4 -0
  116. package/dist/lib/validateDatabasesFromSettings.js +34 -0
  117. package/dist/lib/wait.js +8 -0
  118. package/package.json +54 -0
  119. package/release.js +40 -0
  120. package/src/cli.js +88 -0
  121. package/src/functions/build/getTarIgnoreList.js +30 -0
  122. package/src/functions/build/index.js +63 -0
  123. package/src/functions/create/buildPackageJSON.js +18 -0
  124. package/src/functions/create/index.js +209 -0
  125. package/src/functions/create/templates/api/index.js +4 -0
  126. package/src/functions/create/templates/i18n/en-US.js +4 -0
  127. package/src/functions/create/templates/index.client.js +7 -0
  128. package/src/functions/create/templates/index.css +20 -0
  129. package/src/functions/create/templates/index.html +23 -0
  130. package/src/functions/create/templates/index.server.js +21 -0
  131. package/src/functions/create/templates/public/apple-touch-icon-152x152.png +0 -0
  132. package/src/functions/create/templates/public/favicon.ico +0 -0
  133. package/src/functions/create/templates/public/manifest.json +25 -0
  134. package/src/functions/create/templates/public/service-worker.js +32 -0
  135. package/src/functions/create/templates/public/splash-screen-1024x1024.png +0 -0
  136. package/src/functions/create/templates/settings.development.json +27 -0
  137. package/src/functions/create/templates/ui/components/button/index.js +50 -0
  138. package/src/functions/create/templates/ui/layouts/app/index.js +13 -0
  139. package/src/functions/create/templates/ui/pages/error/index.js +31 -0
  140. package/src/functions/create/templates/ui/pages/index/index.js +94 -0
  141. package/src/functions/index.js +243 -0
  142. package/src/functions/logout/index.js +20 -0
  143. package/src/functions/push/checkIfProvisionAvailable.js +20 -0
  144. package/src/functions/push/deploy/index.js +199 -0
  145. package/src/functions/push/deploy/initial.js +108 -0
  146. package/src/functions/push/deploy/version.js +108 -0
  147. package/src/functions/push/domains.js +23 -0
  148. package/src/functions/push/getAvailableCDN.js +27 -0
  149. package/src/functions/push/getDeployment.js +43 -0
  150. package/src/functions/push/getDeploymentSummary.js +39 -0
  151. package/src/functions/push/getInstanceSizeRegions.js +40 -0
  152. package/src/functions/push/getInstanceSizesForRegion.js +35 -0
  153. package/src/functions/push/getProviderInstanceSizes.js +35 -0
  154. package/src/functions/push/getProviderRegions.js +39 -0
  155. package/src/functions/push/getProvidersWithConnectionStatus.js +25 -0
  156. package/src/functions/push/getSessionToken.js +26 -0
  157. package/src/functions/push/getUserFromSessionToken.js +39 -0
  158. package/src/functions/push/index.js +245 -0
  159. package/src/functions/push/loginToCheatCode.js +34 -0
  160. package/src/functions/push/pingProvisionAPI.js +33 -0
  161. package/src/functions/push/prompts.js +276 -0
  162. package/src/functions/push/providerMap.js +8 -0
  163. package/src/functions/push/providers.js +6 -0
  164. package/src/functions/reset/index.js +0 -0
  165. package/src/functions/reset/reset.js +163 -0
  166. package/src/functions/start/bootstrapLayoutComponent.js +57 -0
  167. package/src/functions/start/bootstrapPageComponent.js +54 -0
  168. package/src/functions/start/buildFile.js +97 -0
  169. package/src/functions/start/buildFiles.js +101 -0
  170. package/src/functions/start/buildPlugins.js +215 -0
  171. package/src/functions/start/checkIfPortAvailable.js +21 -0
  172. package/src/functions/start/cleanup/index.js +23 -0
  173. package/src/functions/start/databases/mongodb/availableQueryParameters.js +39 -0
  174. package/src/functions/start/databases/mongodb/buildConnectionString.js +28 -0
  175. package/src/functions/start/databases/mongodb/buildQueryParameters.js +15 -0
  176. package/src/functions/start/databases/mongodb/checkConnection.js +40 -0
  177. package/src/functions/start/databases/mongodb/connect.js +25 -0
  178. package/src/functions/start/databases/mongodb/index.js +108 -0
  179. package/src/functions/start/databases/postgresql/checkConnection.js +43 -0
  180. package/src/functions/start/databases/postgresql/connect.js +27 -0
  181. package/src/functions/start/databases/postgresql/index.js +127 -0
  182. package/src/functions/start/databases/redis.js +0 -0
  183. package/src/functions/start/databases/startProvider.js +15 -0
  184. package/src/functions/start/fileDependencyMapper.js +24 -0
  185. package/src/functions/start/filesToCopy.js +15 -0
  186. package/src/functions/start/generateId.js +73 -0
  187. package/src/functions/start/getCodependenciesForFile.js +84 -0
  188. package/src/functions/start/getFilesToBuild.js +49 -0
  189. package/src/functions/start/getProcessIdFromPort.js +92 -0
  190. package/src/functions/start/hmrServer.js +78 -0
  191. package/src/functions/start/index.js +688 -0
  192. package/src/functions/start/isWindows.js +3 -0
  193. package/src/functions/start/minifyFile.js +10 -0
  194. package/src/functions/start/onWarn.js +68 -0
  195. package/src/functions/start/readFileDependencyMap.js +19 -0
  196. package/src/functions/start/removeDeletedDependenciesFromMap.js +19 -0
  197. package/src/functions/start/setComponentId.js +58 -0
  198. package/src/functions/start/updateFileMap.js +100 -0
  199. package/src/functions/update/index.js +20 -0
  200. package/src/functions/use/index.js +62 -0
  201. package/src/index.js +13 -0
  202. package/src/lib/CLILog.js +40 -0
  203. package/src/lib/buildResolvePlugin.js +32 -0
  204. package/src/lib/checkIfValidJSON.js +8 -0
  205. package/src/lib/colorLog.js +5 -0
  206. package/src/lib/getArgs.js +14 -0
  207. package/src/lib/getCodeFrame.js +8 -0
  208. package/src/lib/getOptions.js +12 -0
  209. package/src/lib/getPlatformSafePath.js +6 -0
  210. package/src/lib/getProvider.js +5 -0
  211. package/src/lib/help.js +56 -0
  212. package/src/lib/isObject.js +3 -0
  213. package/src/lib/isValidJSONString.js +8 -0
  214. package/src/lib/isWindows.js +3 -0
  215. package/src/lib/killProcesses.js +5 -0
  216. package/src/lib/loadSettings.js +40 -0
  217. package/src/lib/loader.js +94 -0
  218. package/src/lib/masterIgnoreList.js +13 -0
  219. package/src/lib/nodeEnvironment.js +6 -0
  220. package/src/lib/nodeEnvironments.js +8 -0
  221. package/src/lib/parseCookiesFromLogin.js +21 -0
  222. package/src/lib/rainbowRoad.js +5 -0
  223. package/src/lib/readDirectorySync.js +13 -0
  224. package/src/lib/regexes.js +5 -0
  225. package/src/lib/replaceBackslashesWithForwardSlashes.js +8 -0
  226. package/src/lib/replaceFileProtocol.js +8 -0
  227. package/src/lib/serializeQueryParameters.js +5 -0
  228. package/src/lib/settings.js +1 -0
  229. package/src/lib/validateDatabasesFromSettings.js +37 -0
  230. package/src/lib/wait.js +5 -0
  231. package/storage/.verdaccio-db.json +1 -0
@@ -0,0 +1,199 @@
1
+ /* eslint-disable consistent-return */
2
+
3
+ import fetch from 'node-fetch';
4
+ import chalk from 'chalk';
5
+ import _ from 'lodash';
6
+ import fs from 'fs';
7
+ import FormData from 'form-data';
8
+ import Loader from '../../lib/loader.js';
9
+ import domains from './domains.js';
10
+ import checkIfValidJSON from '../../lib/checkIfValidJSON.js';
11
+ import CLILog from '../../lib/CLILog.js';
12
+ import build from '../build/index.js';
13
+ import getAvailableCDN from "../getAvailableCDN.js";
14
+ import initialDeployment from './initial.js';
15
+ import versionDeployment from './version.js';
16
+
17
+ const getAppSettings = (environment = '') => {
18
+ try {
19
+ if (fs.existsSync(`settings.${environment}.json`)) {
20
+ const file = fs.readFileSync(`settings.${environment}.json`, 'utf-8');
21
+ return file;
22
+ }
23
+
24
+ return '{}';
25
+ } catch (exception) {
26
+ throw new Error(`[initDeployment.getAppSettings] ${exception.message}`);
27
+ }
28
+ };
29
+
30
+ const startDeployment = async ({
31
+ isInitialDeployment = false,
32
+ loginSessionToken = '',
33
+ deployment = {},
34
+ version = '',
35
+ appSettings = '',
36
+ environment = 'production',
37
+ server = 'production',
38
+ }) => {
39
+ try {
40
+ if (isInitialDeployment) {
41
+ return initialDeployment({
42
+ version,
43
+ deployment,
44
+ settings: appSettings,
45
+ environment,
46
+ loginSessionToken,
47
+ server,
48
+ });
49
+ }
50
+
51
+ return versionDeployment({
52
+ version,
53
+ deployment,
54
+ settings: appSettings,
55
+ environment,
56
+ loginSessionToken,
57
+ server,
58
+ });
59
+ } catch (exception) {
60
+ throw new Error(`[initDeployment.startDeployment] ${exception.message}`);
61
+ }
62
+ };
63
+
64
+ const uploadBuildToCDN = ({
65
+ mirror = '',
66
+ domain = '',
67
+ loginSessionToken = '',
68
+ deploymentId = '',
69
+ version = '',
70
+ }) => {
71
+ try {
72
+ const formData = new FormData();
73
+ const timestamp = new Date().toISOString();
74
+
75
+ formData.append(
76
+ "version_tar",
77
+ fs.readFileSync(`.build/build.tar.xz`),
78
+ `${timestamp}.tar.xz`
79
+ );
80
+
81
+ formData.append('deploymentId', deploymentId);
82
+ formData.append('version', version);
83
+
84
+ fetch(`${mirror}/api/uploads/receive`, {
85
+ method: "POST",
86
+ headers: {
87
+ ...formData.getHeaders(),
88
+ "x-deployment-domain": domain,
89
+ "x-login-session-token": loginSessionToken,
90
+ },
91
+ body: formData,
92
+ }).then(async (response) => {
93
+ const data = await response.json();
94
+ return data?.data;
95
+ });
96
+ } catch (exception) {
97
+ throw new Error(`[initDeployment.uploadBuildToCDN] ${exception.message}`);
98
+ }
99
+ };
100
+
101
+ const uploadVersionToCDN = async ({
102
+ loader,
103
+ deployment,
104
+ loginSessionToken,
105
+ version,
106
+ }) => {
107
+ try {
108
+ const availableCDN = await getAvailableCDN();
109
+
110
+ if (!availableCDN) {
111
+ loader.stop();
112
+ console.log(chalk.redBright(`\nUnable to upload version. Check status.cheatcode.co for availability.\n`));
113
+ process.exit();
114
+ }
115
+
116
+ // TODO: This doesn't work. The versions server needs access to a database, so we need to give
117
+ // it access to one and make sure it has user data similar to localhost. May be worth writing a
118
+ // localhost sync script to the database so we can still do local deploys.
119
+
120
+ await uploadBuildToCDN({
121
+ mirror: availableCDN,
122
+ domain: deployment?.domain,
123
+ loginSessionToken,
124
+ version,
125
+ deploymentId: deployment?._id,
126
+ });
127
+ } catch (exception) {
128
+ throw new Error(`[actionName.uploadVersionToCDN] ${exception.message}`);
129
+ }
130
+ };
131
+
132
+ const buildApp = (environment = 'production') => {
133
+ try {
134
+ return build({}, { isDeploy: true, type: 'tar', environment });
135
+ } catch (exception) {
136
+ throw new Error(`[initDeployment.buildApp] ${exception.message}`);
137
+ }
138
+ };
139
+
140
+ const validateOptions = (options) => {
141
+ try {
142
+ if (!options) throw new Error('options object is required.');
143
+ if (!options.loginSessionToken) throw new Error('options.loginSessionToken is required.');
144
+ } catch (exception) {
145
+ throw new Error(`[initDeployment.validateOptions] ${exception.message}`);
146
+ }
147
+ };
148
+
149
+ const initDeployment = async (options, { resolve, reject }) => {
150
+ try {
151
+ validateOptions(options);
152
+
153
+ console.log("");
154
+ const loader = new Loader({ padding: ' ', defaultMessage: "Deploying app..." });
155
+ loader.text("Deploying app...");
156
+
157
+ const deploymentTimestamp = new Date().toISOString();
158
+
159
+ // NOTE: If we're just doing a version update and not an initial deployment, we want to respect the
160
+ // existing environment for the deployment. Otherwise, fallback to the --environment flag.
161
+ await buildApp(options?.deployment?.environment || options?.environment);
162
+
163
+ const appSettings = getAppSettings(options?.environment);
164
+
165
+ loader.text("Uploading deployment...");
166
+
167
+ await uploadVersionToCDN({
168
+ loader,
169
+ loginSessionToken: options?.loginSessionToken,
170
+ deployment: options?.deployment,
171
+ version: deploymentTimestamp,
172
+ });
173
+
174
+ loader.text("Starting deployment...");
175
+
176
+ await startDeployment({
177
+ isInitialDeployment: options?.isInitialDeployment,
178
+ loginSessionToken: options?.loginSessionToken,
179
+ deployment: options.deployment,
180
+ version: deploymentTimestamp,
181
+ appSettings,
182
+ environment: options?.environment,
183
+ server: options?.server,
184
+ });
185
+
186
+ loader.stop();
187
+ console.log(chalk.greenBright(`Your app is deploying! To monitor progress, head to ${domains?.push}/deployments/${options?.deployment?.domain}\n`));
188
+
189
+ resolve();
190
+ } catch (exception) {
191
+ console.warn(exception);
192
+ reject(`[initDeployment] ${exception.message}`);
193
+ }
194
+ };
195
+
196
+ export default (options) =>
197
+ new Promise((resolve, reject) => {
198
+ initDeployment(options, { resolve, reject });
199
+ });
@@ -0,0 +1,108 @@
1
+ /* eslint-disable consistent-return */
2
+
3
+ import fs from 'fs';
4
+ import fetch from 'node-fetch';
5
+ import domains from '../domains.js';
6
+ import checkIfValidJSON from "../../../lib/checkIfValidJSON.js";
7
+ import CLILog from "../../../lib/CLILog.js";
8
+
9
+ const pushDeploymentToProvision = ({
10
+ deploymentPayload = null,
11
+ loginSessionToken,
12
+ server = 'production',
13
+ }) => {
14
+ try {
15
+ return fetch(`${domains?.provision[server][0]}/api/deployments/initial`, {
16
+ method: 'POST',
17
+ headers: {
18
+ ...deploymentPayload.getHeaders(),
19
+ 'x-login-session-token': loginSessionToken,
20
+ },
21
+ body: formData
22
+ }).then(async (response) => {
23
+ const text = await response.text();
24
+ const data = checkIfValidJSON(text);
25
+ const isPayloadSizeError = text?.includes('payload');
26
+
27
+ if (data?.error || isPayloadSizeError) {
28
+ CLILog(
29
+ data.error?.message || text,
30
+ {
31
+ level: 'danger',
32
+ docs: isPayloadSizeError ? 'https://cheatcode.co/docs/push/considerations/payload-size' : 'https://cheatcode.co/docs/push'
33
+ }
34
+ );
35
+
36
+ process.exit(0);
37
+ }
38
+
39
+ return data?.data;
40
+ });
41
+ } catch (exception) {
42
+ throw new Error(`[initialDeployment.pushDeploymentToProvision] ${exception.message}`);
43
+ }
44
+ };
45
+
46
+ const getDeploymentPayload = ({
47
+ version,
48
+ deployment,
49
+ settings,
50
+ environment,
51
+ }) => {
52
+ try {
53
+ const formData = new FormData();
54
+
55
+ formData.append('deployment', JSON.stringify({
56
+ ...(deployment || {}),
57
+ version,
58
+ settings,
59
+ environment,
60
+ }));
61
+
62
+ return formData;
63
+ } catch (exception) {
64
+ throw new Error(`[initialDeployment.getDeploymentPayload] ${exception.message}`);
65
+ }
66
+ };
67
+
68
+ const validateOptions = (options) => {
69
+ try {
70
+ if (!options) throw new Error('options object is required.');
71
+ if (!options.version) throw new Error('options.version is required.');
72
+ if (!options.deployment) throw new Error('options.deployment is required.');
73
+ if (!options.settings) throw new Error('options.settings is required.');
74
+ if (!options.environment) throw new Error('options.environment is required.');
75
+ if (!options.loginSessionToken) throw new Error('options.loginSessionToken is required.');
76
+ } catch (exception) {
77
+ throw new Error(`[initialDeployment.validateOptions] ${exception.message}`);
78
+ }
79
+ };
80
+
81
+ const initialDeployment = async (options, { resolve, reject }) => {
82
+ try {
83
+ validateOptions(options);
84
+
85
+ const deploymentPayload = getDeploymentPayload({
86
+ version: options?.version,
87
+ deployment: options?.deployment,
88
+ settings: options?.settings,
89
+ environment: options?.environment,
90
+ });
91
+
92
+ await pushDeploymentToProvision({
93
+ deploymentPayload,
94
+ loginSessionToken: options?.loginSessionToken,
95
+ server: options?.server,
96
+ });
97
+
98
+ resolve();
99
+ } catch (exception) {
100
+ reject(`[initialDeployment] ${exception.message}`);
101
+ }
102
+ };
103
+
104
+ export default (options) =>
105
+ new Promise((resolve, reject) => {
106
+ initialDeployment(options, { resolve, reject });
107
+ });
108
+
@@ -0,0 +1,108 @@
1
+ /* eslint-disable consistent-return */
2
+
3
+ import fs from 'fs';
4
+ import fetch from 'node-fetch';
5
+ import domains from './domains.js';
6
+ import checkIfValidJSON from "../../../lib/checkIfValidJSON.js";
7
+ import CLILog from "../../../lib/CLILog.js";
8
+
9
+ const pushDeploymentToProvision = ({
10
+ deploymentPayload = null,
11
+ loginSessionToken,
12
+ server = 'production',
13
+ }) => {
14
+ try {
15
+ return fetch(`${domains?.provision[server][0]}/api/deployments/version`, {
16
+ method: 'POST',
17
+ headers: {
18
+ ...deploymentPayload.getHeaders(),
19
+ 'x-login-session-token': loginSessionToken,
20
+ },
21
+ body: formData
22
+ }).then(async (response) => {
23
+ const text = await response.text();
24
+ const data = checkIfValidJSON(text);
25
+ const isPayloadSizeError = text?.includes('payload');
26
+
27
+ if (data?.error || isPayloadSizeError) {
28
+ CLILog(
29
+ data.error?.message || text,
30
+ {
31
+ level: 'danger',
32
+ docs: isPayloadSizeError ? 'https://cheatcode.co/docs/push/considerations/payload-size' : 'https://cheatcode.co/docs/push'
33
+ }
34
+ );
35
+
36
+ process.exit(0);
37
+ }
38
+
39
+ return data?.data;
40
+ });
41
+ } catch (exception) {
42
+ throw new Error(`[versionDeployment.pushDeploymentToProvision] ${exception.message}`);
43
+ }
44
+ };
45
+
46
+ const getDeploymentPayload = ({
47
+ version,
48
+ deployment,
49
+ settings,
50
+ environment,
51
+ }) => {
52
+ try {
53
+ const formData = new FormData();
54
+
55
+ formData.append('deployment', JSON.stringify({
56
+ ...(deployment || {}),
57
+ version,
58
+ settings,
59
+ environment,
60
+ }));
61
+
62
+ return formData;
63
+ } catch (exception) {
64
+ throw new Error(`[versionDeployment.getDeploymentPayload] ${exception.message}`);
65
+ }
66
+ };
67
+
68
+ const validateOptions = (options) => {
69
+ try {
70
+ if (!options) throw new Error('options object is required.');
71
+ if (!options.version) throw new Error('options.version is required.');
72
+ if (!options.deployment) throw new Error('options.deployment is required.');
73
+ if (!options.settings) throw new Error('options.settings is required.');
74
+ if (!options.environment) throw new Error('options.environment is required.');
75
+ if (!options.loginSessionToken) throw new Error('options.loginSessionToken is required.');
76
+ } catch (exception) {
77
+ throw new Error(`[versionDeployment.validateOptions] ${exception.message}`);
78
+ }
79
+ };
80
+
81
+ const versionDeployment = async (options, { resolve, reject }) => {
82
+ try {
83
+ validateOptions(options);
84
+
85
+ const deploymentPayload = getDeploymentPayload({
86
+ version: options?.version,
87
+ deployment: options?.deployment,
88
+ settings: options?.settings,
89
+ environment: options?.environment,
90
+ });
91
+
92
+ await pushDeploymentToProvision({
93
+ deploymentPayload,
94
+ loginSessionToken: options?.loginSessionToken,
95
+ server: options?.server,
96
+ });
97
+
98
+ resolve();
99
+ } catch (exception) {
100
+ reject(`[versionDeployment] ${exception.message}`);
101
+ }
102
+ };
103
+
104
+ export default (options) =>
105
+ new Promise((resolve, reject) => {
106
+ versionDeployment(options, { resolve, reject });
107
+ });
108
+
@@ -0,0 +1,23 @@
1
+ const push = {
2
+ local: ['http://localhost:2600'],
3
+ staging: [],
4
+ production: ['https://push.cheatcode.co'],
5
+ };
6
+
7
+ const provision = {
8
+ local: ['http://localhost:2602'],
9
+ staging: [],
10
+ production: ['https://provision.cheatcode.co'],
11
+ };
12
+
13
+ const versions = {
14
+ local: ['http://localhost:2603', 'http://localhost:2604', 'http://localhost:2605'],
15
+ staging: [],
16
+ production: ['https://push-versions-1.cheatcode.co', 'https://push-versions-2.cheatcode.co', 'https://push-versions-3.cheatcode.co'],
17
+ };
18
+
19
+ export default {
20
+ push,
21
+ provision,
22
+ versions,
23
+ };
@@ -0,0 +1,27 @@
1
+ import fetch from "node-fetch";
2
+ import _ from 'lodash';
3
+ import domains from './domains.js';
4
+
5
+ export default async () => {
6
+ // NOTE: Even though we can pass a separate server, we always want to use production as that
7
+ // is where the instance-cli will pull versions from.
8
+ const results = (await Promise.allSettled(domains?.versions?.production?.map(async (mirror) => {
9
+ const controller = new AbortController();
10
+ setTimeout(() => controller.abort(), 2000);
11
+
12
+ const result = await fetch(`${mirror}/api/ping`, { signal: controller.signal }).catch((error) => {
13
+ return { status: 503 };
14
+ });
15
+
16
+ return {
17
+ mirror,
18
+ status: result.status,
19
+ };
20
+ })))?.map((result) => {
21
+ return result.value;
22
+ });
23
+
24
+ const firstAvailable = _.orderBy(results, ['status', 'mirror'])[0];
25
+
26
+ return firstAvailable?.status === 200 ? firstAvailable?.mirror : null;
27
+ };
@@ -0,0 +1,43 @@
1
+ import fetch from "node-fetch";
2
+ import chalk from "chalk";
3
+ import { URL, URLSearchParams } from 'url';
4
+ import domains from "./domains.js";
5
+ import checkIfValidJSON from "./checkIfValidJSON.js";
6
+ import CLILog from "./CLILog.js";
7
+
8
+ export default ({
9
+ domain = '',
10
+ environment = 'production',
11
+ loginSessionToken = '',
12
+ }) => {
13
+ const url = new URL(`${domains?.provision}/api/deployments`);
14
+ url.search = new URLSearchParams({
15
+ domain,
16
+ environment,
17
+ });
18
+
19
+ return fetch(url, {
20
+ method: 'GET',
21
+ headers: {
22
+ 'x-login-session-token': loginSessionToken,
23
+ 'content-type': 'application/json',
24
+ },
25
+ }).then(async (response) => {
26
+ const text = await response.text();
27
+ const data = checkIfValidJSON(text);
28
+
29
+ if (data?.error) {
30
+ CLILog(
31
+ data?.error?.message,
32
+ {
33
+ level: 'danger',
34
+ docs: 'https://cheatcode.co/docs/push'
35
+ }
36
+ );
37
+
38
+ process.exit(0);
39
+ }
40
+
41
+ return data?.data?.deployment;
42
+ });
43
+ };
@@ -0,0 +1,39 @@
1
+ import fetch from "node-fetch";
2
+ import domains from "./domains.js";
3
+ import checkIfValidJSON from "../../lib/checkIfValidJSON.js";
4
+ import CLILog from "../../lib/CLILog.js";
5
+
6
+ export default (answers = {}, loginSessionToken = '', deploymentDomain = '') => {
7
+ return fetch(`${domains.provision}/api/deployments/summary`, {
8
+ method: 'POST',
9
+ headers: {
10
+ 'x-login-session-token': loginSessionToken,
11
+ 'x-deployment-domain': deploymentDomain,
12
+ 'content-type': 'application/json',
13
+ },
14
+ body: JSON.stringify({
15
+ provider: answers?.provider,
16
+ sizes: [
17
+ { type: 'loadBalancers', quantity: answers?.loadBalancerInstances, id: answers?.loadBalancer_size },
18
+ { type: 'instances', quantity: answers?.appInstances, id: answers?.app_size }
19
+ ],
20
+ })
21
+ }).then(async (response) => {
22
+ const text = await response.text();
23
+ const data = checkIfValidJSON(text);
24
+
25
+ if (data?.error) {
26
+ CLILog(
27
+ data.error?.message,
28
+ {
29
+ level: 'danger',
30
+ docs: 'https://cheatcode.co/docs/push'
31
+ }
32
+ );
33
+
34
+ process.exit(0);
35
+ }
36
+
37
+ return data?.data?.summary;
38
+ });
39
+ };
@@ -0,0 +1,40 @@
1
+ import fetch from 'node-fetch';
2
+ import chalk from 'chalk';
3
+ import domains from "./domains.js";
4
+ import CLILog from "../../lib/CLILog.js";
5
+ import checkIfValidJSON from '../../lib/checkIfValidJSON.js';
6
+
7
+ export default (target = '', answers = {}, loginSessionToken = '') => {
8
+ const url = new URL(`${domains.provision}/api/providers/${answers?.provider}/regions`);
9
+ const params = new URLSearchParams({
10
+ size: answers[target],
11
+ loadBalancerRegion: answers?.loadBalancer_region,
12
+ });
13
+
14
+ url.search = params.toString();
15
+
16
+ return fetch(url, {
17
+ method: 'GET',
18
+ headers: {
19
+ 'x-login-session-token': loginSessionToken,
20
+ 'content-type': 'application/json',
21
+ },
22
+ }).then(async (response) => {
23
+ const text = await response.text();
24
+ const data = checkIfValidJSON(text);
25
+
26
+ if (data?.error) {
27
+ CLILog(
28
+ data.error?.message,
29
+ {
30
+ level: 'danger',
31
+ docs: 'https://cheatcode.co/docs/push/deployment-tokens'
32
+ }
33
+ );
34
+
35
+ process.exit(0);
36
+ }
37
+
38
+ return data?.data;
39
+ });
40
+ };
@@ -0,0 +1,35 @@
1
+ import fetch from 'node-fetch';
2
+ import chalk from 'chalk';
3
+ import domains from "./domains.js";
4
+ import CLILog from "../../lib/CLILog.js";
5
+ import checkIfValidJSON from '../../lib/checkIfValidJSON.js';
6
+
7
+ export default (provider = '', region = '', loginSessionToken = '') => {
8
+ return fetch(
9
+ `${domains.provision}/api/providers/${provider}/sizes?region=${region}`,
10
+ {
11
+ method: 'GET',
12
+ headers: {
13
+ 'x-login-session-token': loginSessionToken,
14
+ 'Content-Type': 'application/json',
15
+ },
16
+ }
17
+ ).then(async (response) => {
18
+ const text = await response.text();
19
+ const data = checkIfValidJSON(text);
20
+
21
+ if (data?.error) {
22
+ CLILog(
23
+ data.error?.message,
24
+ {
25
+ level: 'danger',
26
+ docs: 'https://cheatcode.co/docs/push/deployment-tokens'
27
+ }
28
+ );
29
+
30
+ process.exit(0);
31
+ }
32
+
33
+ return data?.data?.sizes;
34
+ });
35
+ };
@@ -0,0 +1,35 @@
1
+ import fetch from 'node-fetch';
2
+ import chalk from 'chalk';
3
+ import domains from "./domains.js";
4
+ import CLILog from "../../lib/CLILog.js";
5
+ import checkIfValidJSON from '../../lib/checkIfValidJSON.js';
6
+
7
+ export default (answers = {}, loginSessionToken = '') => {
8
+ return fetch(
9
+ `${domains.provision}/api/providers/${answers?.provider}/sizes`,
10
+ {
11
+ method: 'GET',
12
+ headers: {
13
+ 'x-login-session-token': loginSessionToken,
14
+ 'Content-Type': 'application/json',
15
+ },
16
+ }
17
+ ).then(async (response) => {
18
+ const text = await response.text();
19
+ const data = checkIfValidJSON(text);
20
+
21
+ if (data?.error) {
22
+ CLILog(
23
+ data.error?.message,
24
+ {
25
+ level: 'danger',
26
+ docs: 'https://cheatcode.co/docs/push/deployment-tokens'
27
+ }
28
+ );
29
+
30
+ process.exit(0);
31
+ }
32
+
33
+ return data?.data;
34
+ });
35
+ };
@@ -0,0 +1,39 @@
1
+ import fetch from 'node-fetch';
2
+ import chalk from 'chalk';
3
+ import domains from "./domains.js";
4
+ import CLILog from "../../lib/CLILog.js";
5
+ import checkIfValidJSON from '../../lib/checkIfValidJSON.js';
6
+
7
+ export default (provider = '', loginSessionToken = '') => {
8
+ const url = new URL(`${domains.provision}/api/providers/${provider}/regions`);
9
+ const params = new URLSearchParams({
10
+ provider,
11
+ });
12
+
13
+ url.search = params.toString();
14
+
15
+ return fetch(url, {
16
+ method: 'GET',
17
+ headers: {
18
+ 'x-login-session-token': loginSessionToken,
19
+ 'content-type': 'application/json',
20
+ },
21
+ }).then(async (response) => {
22
+ const text = await response.text();
23
+ const data = checkIfValidJSON(text);
24
+
25
+ if (data?.error) {
26
+ CLILog(
27
+ data.error?.message,
28
+ {
29
+ level: 'danger',
30
+ docs: 'https://cheatcode.co/docs/push/deployment-tokens'
31
+ }
32
+ );
33
+
34
+ process.exit(0);
35
+ }
36
+
37
+ return data?.data?.regions;
38
+ });
39
+ };