@capawesome/cli 4.6.0 → 4.7.0
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/CHANGELOG.md +24 -0
- package/dist/commands/apps/builds/create.js +145 -128
- package/dist/commands/apps/bundles/create.js +4 -2
- package/dist/commands/apps/bundles/delete.js +2 -3
- package/dist/commands/apps/bundles/update.js +2 -3
- package/dist/commands/apps/certificates/create.js +3 -19
- package/dist/commands/apps/certificates/delete.js +28 -5
- package/dist/commands/apps/certificates/get.js +28 -5
- package/dist/commands/apps/certificates/update.js +3 -1
- package/dist/commands/apps/deployments/create.js +5 -77
- package/dist/commands/apps/devices/forcechannel.js +9 -7
- package/dist/commands/apps/devices/unforcechannel.js +9 -7
- package/dist/commands/apps/link.js +34 -0
- package/dist/commands/apps/link.test.js +94 -0
- package/dist/commands/apps/liveupdates/bundle.js +12 -2
- package/dist/commands/apps/liveupdates/create.js +293 -0
- package/dist/commands/apps/liveupdates/create.test.js +300 -0
- package/dist/commands/apps/liveupdates/generate-manifest.js +17 -1
- package/dist/commands/apps/liveupdates/generate-manifest.test.js +21 -1
- package/dist/commands/apps/liveupdates/register.js +10 -15
- package/dist/commands/apps/liveupdates/upload.js +25 -16
- package/dist/commands/apps/transfer.js +47 -0
- package/dist/commands/apps/transfer.test.js +123 -0
- package/dist/commands/apps/unlink.js +35 -0
- package/dist/commands/apps/unlink.test.js +99 -0
- package/dist/commands/manifests/generate.js +1 -1
- package/dist/index.js +4 -0
- package/dist/services/app-build-sources.js +120 -0
- package/dist/services/app-certificates.js +0 -1
- package/dist/services/app-devices.js +8 -0
- package/dist/services/apps.js +25 -0
- package/dist/services/authorization-service.js +5 -1
- package/dist/services/jobs.js +13 -0
- package/dist/types/app-build-source.js +1 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/custom-properties.js +22 -0
- package/dist/utils/file.js +12 -1
- package/dist/utils/git.js +91 -0
- package/dist/utils/git.test.js +130 -0
- package/dist/utils/job.js +77 -0
- package/dist/utils/prompt.js +1 -1
- package/dist/utils/zip.js +19 -2
- package/package.json +2 -1
|
@@ -2,16 +2,15 @@ import { DEFAULT_CONSOLE_BASE_URL } from '../../../config/consts.js';
|
|
|
2
2
|
import appBuildsService from '../../../services/app-builds.js';
|
|
3
3
|
import appDeploymentsService from '../../../services/app-deployments.js';
|
|
4
4
|
import appDestinationsService from '../../../services/app-destinations.js';
|
|
5
|
-
import { unescapeAnsi } from '../../../utils/ansi.js';
|
|
6
5
|
import { withAuth } from '../../../utils/auth.js';
|
|
7
6
|
import { isInteractive } from '../../../utils/environment.js';
|
|
7
|
+
import { waitForJobCompletion } from '../../../utils/job.js';
|
|
8
8
|
import { prompt, promptAppSelection, promptOrganizationSelection } from '../../../utils/prompt.js';
|
|
9
|
-
import { wait } from '../../../utils/wait.js';
|
|
10
9
|
import { defineCommand, defineOptions } from '@robingenz/zli';
|
|
11
10
|
import consola from 'consola';
|
|
12
11
|
import { z } from 'zod';
|
|
13
12
|
export default defineCommand({
|
|
14
|
-
description: 'Create a new app deployment.',
|
|
13
|
+
description: 'Create a new app deployment on Capawesome Cloud.',
|
|
15
14
|
options: defineOptions(z.object({
|
|
16
15
|
appId: z
|
|
17
16
|
.uuid({
|
|
@@ -159,80 +158,9 @@ export default defineCommand({
|
|
|
159
158
|
// Wait for deployment job to complete by default, unless --detached flag is set
|
|
160
159
|
const shouldWait = !options.detached && build.platform !== 'web';
|
|
161
160
|
if (shouldWait) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
while (true) {
|
|
166
|
-
try {
|
|
167
|
-
const deployment = await appDeploymentsService.findOne({
|
|
168
|
-
appId,
|
|
169
|
-
appDeploymentId: response.id,
|
|
170
|
-
relations: 'job,job.jobLogs',
|
|
171
|
-
});
|
|
172
|
-
if (!deployment.job) {
|
|
173
|
-
await wait(3000);
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
const jobStatus = deployment.job.status;
|
|
177
|
-
// Show spinner while queued or pending
|
|
178
|
-
if (jobStatus === 'queued' || jobStatus === 'pending') {
|
|
179
|
-
if (isWaitingForStart) {
|
|
180
|
-
consola.start(`Waiting for deployment to start (status: ${jobStatus})...`);
|
|
181
|
-
}
|
|
182
|
-
await wait(3000);
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
// Stop spinner when job moves to in_progress
|
|
186
|
-
if (isWaitingForStart && jobStatus === 'in_progress') {
|
|
187
|
-
isWaitingForStart = false;
|
|
188
|
-
consola.success('Deployment started...');
|
|
189
|
-
}
|
|
190
|
-
// Print new logs
|
|
191
|
-
if (deployment.job.jobLogs && deployment.job.jobLogs.length > 0) {
|
|
192
|
-
const newLogs = deployment.job.jobLogs
|
|
193
|
-
.filter((log) => log.number > lastPrintedLogNumber)
|
|
194
|
-
.sort((a, b) => a.number - b.number);
|
|
195
|
-
for (const log of newLogs) {
|
|
196
|
-
console.log(unescapeAnsi(log.payload));
|
|
197
|
-
lastPrintedLogNumber = log.number;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
// Handle terminal states
|
|
201
|
-
if (jobStatus === 'succeeded' ||
|
|
202
|
-
jobStatus === 'failed' ||
|
|
203
|
-
jobStatus === 'canceled' ||
|
|
204
|
-
jobStatus === 'rejected' ||
|
|
205
|
-
jobStatus === 'timed_out') {
|
|
206
|
-
console.log(); // New line for better readability
|
|
207
|
-
if (jobStatus === 'succeeded') {
|
|
208
|
-
consola.success('Deployment completed successfully.');
|
|
209
|
-
process.exit(0);
|
|
210
|
-
}
|
|
211
|
-
else if (jobStatus === 'failed') {
|
|
212
|
-
consola.error('Deployment failed.');
|
|
213
|
-
process.exit(1);
|
|
214
|
-
}
|
|
215
|
-
else if (jobStatus === 'canceled') {
|
|
216
|
-
consola.warn('Deployment was canceled.');
|
|
217
|
-
process.exit(1);
|
|
218
|
-
}
|
|
219
|
-
else if (jobStatus === 'rejected') {
|
|
220
|
-
consola.error('Deployment was rejected.');
|
|
221
|
-
process.exit(1);
|
|
222
|
-
}
|
|
223
|
-
else if (jobStatus === 'timed_out') {
|
|
224
|
-
consola.error('Deployment timed out.');
|
|
225
|
-
process.exit(1);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
// Wait before next poll (3 seconds)
|
|
229
|
-
await wait(3000);
|
|
230
|
-
}
|
|
231
|
-
catch (error) {
|
|
232
|
-
consola.error('Error polling deployment status:', error);
|
|
233
|
-
process.exit(1);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
161
|
+
await waitForJobCompletion({ jobId: response.jobId });
|
|
162
|
+
consola.success('Deployment completed successfully.');
|
|
163
|
+
process.exit(0);
|
|
236
164
|
}
|
|
237
165
|
}),
|
|
238
166
|
});
|
|
@@ -10,11 +10,11 @@ export default defineCommand({
|
|
|
10
10
|
description: 'Force a device to use a specific channel.',
|
|
11
11
|
options: defineOptions(z.object({
|
|
12
12
|
appId: z.string().uuid({ message: 'App ID must be a UUID.' }).optional().describe('ID of the app.'),
|
|
13
|
-
deviceId: z.string().optional().describe('ID of the device.'),
|
|
13
|
+
deviceId: z.array(z.string()).optional().describe('ID of the device. Can be specified multiple times.'),
|
|
14
14
|
channel: z.string().optional().describe('Name of the channel to force.'),
|
|
15
15
|
})),
|
|
16
16
|
action: withAuth(async (options, args) => {
|
|
17
|
-
let { appId, deviceId, channel } = options;
|
|
17
|
+
let { appId, deviceId: deviceIds, channel } = options;
|
|
18
18
|
if (!appId) {
|
|
19
19
|
if (!isInteractive()) {
|
|
20
20
|
consola.error('You must provide an app ID when running in non-interactive environment.');
|
|
@@ -23,14 +23,15 @@ export default defineCommand({
|
|
|
23
23
|
const organizationId = await promptOrganizationSelection();
|
|
24
24
|
appId = await promptAppSelection(organizationId);
|
|
25
25
|
}
|
|
26
|
-
if (!
|
|
26
|
+
if (!deviceIds || deviceIds.length === 0) {
|
|
27
27
|
if (!isInteractive()) {
|
|
28
28
|
consola.error('You must provide the device ID when running in non-interactive environment.');
|
|
29
29
|
process.exit(1);
|
|
30
30
|
}
|
|
31
|
-
deviceId = await prompt('Enter the device ID:', {
|
|
31
|
+
const deviceId = await prompt('Enter the device ID:', {
|
|
32
32
|
type: 'text',
|
|
33
33
|
});
|
|
34
|
+
deviceIds = [deviceId];
|
|
34
35
|
}
|
|
35
36
|
if (!channel) {
|
|
36
37
|
if (!isInteractive()) {
|
|
@@ -56,11 +57,12 @@ export default defineCommand({
|
|
|
56
57
|
consola.error('Channel ID not found.');
|
|
57
58
|
process.exit(1);
|
|
58
59
|
}
|
|
59
|
-
await appDevicesService.
|
|
60
|
+
await appDevicesService.updateMany({
|
|
60
61
|
appId,
|
|
61
|
-
|
|
62
|
+
deviceIds,
|
|
62
63
|
forcedAppChannelId: channelId,
|
|
63
64
|
});
|
|
64
|
-
|
|
65
|
+
const deviceCount = deviceIds.length;
|
|
66
|
+
consola.success(`${deviceCount === 1 ? 'Device' : `${deviceCount} devices`} forced to channel successfully.`);
|
|
65
67
|
}),
|
|
66
68
|
});
|
|
@@ -9,10 +9,10 @@ export default defineCommand({
|
|
|
9
9
|
description: 'Remove the forced channel from a device.',
|
|
10
10
|
options: defineOptions(z.object({
|
|
11
11
|
appId: z.string().uuid({ message: 'App ID must be a UUID.' }).optional().describe('ID of the app.'),
|
|
12
|
-
deviceId: z.string().optional().describe('ID of the device.'),
|
|
12
|
+
deviceId: z.array(z.string()).optional().describe('ID of the device. Can be specified multiple times.'),
|
|
13
13
|
})),
|
|
14
14
|
action: withAuth(async (options, args) => {
|
|
15
|
-
let { appId, deviceId } = options;
|
|
15
|
+
let { appId, deviceId: deviceIds } = options;
|
|
16
16
|
if (!appId) {
|
|
17
17
|
if (!isInteractive()) {
|
|
18
18
|
consola.error('You must provide an app ID when running in non-interactive environment.');
|
|
@@ -21,20 +21,22 @@ export default defineCommand({
|
|
|
21
21
|
const organizationId = await promptOrganizationSelection();
|
|
22
22
|
appId = await promptAppSelection(organizationId);
|
|
23
23
|
}
|
|
24
|
-
if (!
|
|
24
|
+
if (!deviceIds || deviceIds.length === 0) {
|
|
25
25
|
if (!isInteractive()) {
|
|
26
26
|
consola.error('You must provide the device ID when running in non-interactive environment.');
|
|
27
27
|
process.exit(1);
|
|
28
28
|
}
|
|
29
|
-
deviceId = await prompt('Enter the device ID:', {
|
|
29
|
+
const deviceId = await prompt('Enter the device ID:', {
|
|
30
30
|
type: 'text',
|
|
31
31
|
});
|
|
32
|
+
deviceIds = [deviceId];
|
|
32
33
|
}
|
|
33
|
-
await appDevicesService.
|
|
34
|
+
await appDevicesService.updateMany({
|
|
34
35
|
appId,
|
|
35
|
-
|
|
36
|
+
deviceIds,
|
|
36
37
|
forcedAppChannelId: null,
|
|
37
38
|
});
|
|
38
|
-
|
|
39
|
+
const deviceCount = deviceIds.length;
|
|
40
|
+
consola.success(`Forced channel removed from ${deviceCount === 1 ? 'device' : `${deviceCount} devices`} successfully.`);
|
|
39
41
|
}),
|
|
40
42
|
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import appsService from '../../services/apps.js';
|
|
2
|
+
import { withAuth } from '../../utils/auth.js';
|
|
3
|
+
import { isInteractive } from '../../utils/environment.js';
|
|
4
|
+
import { getGitRemoteInfo } from '../../utils/git.js';
|
|
5
|
+
import { promptAppSelection, promptOrganizationSelection } from '../../utils/prompt.js';
|
|
6
|
+
import { defineCommand, defineOptions } from '@robingenz/zli';
|
|
7
|
+
import consola from 'consola';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
export default defineCommand({
|
|
10
|
+
description: 'Connect a git repository to an app.',
|
|
11
|
+
options: defineOptions(z.object({
|
|
12
|
+
appId: z.string().optional().describe('ID of the app.'),
|
|
13
|
+
})),
|
|
14
|
+
action: withAuth(async (options, args) => {
|
|
15
|
+
let { appId } = options;
|
|
16
|
+
if (!appId) {
|
|
17
|
+
if (!isInteractive()) {
|
|
18
|
+
consola.error('You must provide the app ID when running in non-interactive environment.');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
const organizationId = await promptOrganizationSelection();
|
|
22
|
+
appId = await promptAppSelection(organizationId);
|
|
23
|
+
}
|
|
24
|
+
const gitRemoteInfo = getGitRemoteInfo();
|
|
25
|
+
await appsService.linkRepository({
|
|
26
|
+
appId,
|
|
27
|
+
ownerSlug: gitRemoteInfo.ownerSlug,
|
|
28
|
+
provider: gitRemoteInfo.provider,
|
|
29
|
+
repositorySlug: gitRemoteInfo.repositorySlug,
|
|
30
|
+
projectSlug: gitRemoteInfo.projectSlug,
|
|
31
|
+
});
|
|
32
|
+
consola.success('Repository connected successfully.');
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { DEFAULT_API_BASE_URL } from '../../config/consts.js';
|
|
2
|
+
import authorizationService from '../../services/authorization-service.js';
|
|
3
|
+
import { promptAppSelection, promptOrganizationSelection } from '../../utils/prompt.js';
|
|
4
|
+
import userConfig from '../../utils/user-config.js';
|
|
5
|
+
import consola from 'consola';
|
|
6
|
+
import nock from 'nock';
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
8
|
+
import linkCommand from './link.js';
|
|
9
|
+
vi.mock('@/utils/user-config.js');
|
|
10
|
+
vi.mock('@/utils/prompt.js');
|
|
11
|
+
vi.mock('@/services/authorization-service.js');
|
|
12
|
+
vi.mock('consola');
|
|
13
|
+
vi.mock('@/utils/environment.js', () => ({
|
|
14
|
+
isInteractive: () => true,
|
|
15
|
+
}));
|
|
16
|
+
vi.mock('@/utils/git.js', () => ({
|
|
17
|
+
getGitRemoteInfo: () => ({
|
|
18
|
+
ownerSlug: 'capawesome-team',
|
|
19
|
+
provider: 'github',
|
|
20
|
+
repositorySlug: 'cli',
|
|
21
|
+
}),
|
|
22
|
+
}));
|
|
23
|
+
describe('apps-link', () => {
|
|
24
|
+
const mockUserConfig = vi.mocked(userConfig);
|
|
25
|
+
const mockPromptOrganizationSelection = vi.mocked(promptOrganizationSelection);
|
|
26
|
+
const mockPromptAppSelection = vi.mocked(promptAppSelection);
|
|
27
|
+
const mockConsola = vi.mocked(consola);
|
|
28
|
+
const mockAuthorizationService = vi.mocked(authorizationService);
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
vi.clearAllMocks();
|
|
31
|
+
mockUserConfig.read.mockReturnValue({ token: 'test-token' });
|
|
32
|
+
mockAuthorizationService.getCurrentAuthorizationToken.mockReturnValue('test-token');
|
|
33
|
+
mockAuthorizationService.hasAuthorizationToken.mockReturnValue(true);
|
|
34
|
+
vi.spyOn(process, 'exit').mockImplementation((code) => {
|
|
35
|
+
throw new Error(`Process exited with code ${code}`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
nock.cleanAll();
|
|
40
|
+
vi.restoreAllMocks();
|
|
41
|
+
});
|
|
42
|
+
it('should link repository with provided app ID', async () => {
|
|
43
|
+
const appId = 'app-123';
|
|
44
|
+
const testToken = 'test-token';
|
|
45
|
+
const options = { appId };
|
|
46
|
+
const scope = nock(DEFAULT_API_BASE_URL)
|
|
47
|
+
.put(`/v1/apps/${appId}/repository`, {
|
|
48
|
+
ownerSlug: 'capawesome-team',
|
|
49
|
+
provider: 'github',
|
|
50
|
+
repositorySlug: 'cli',
|
|
51
|
+
})
|
|
52
|
+
.matchHeader('Authorization', `Bearer ${testToken}`)
|
|
53
|
+
.reply(200, { id: appId, name: 'Test App' });
|
|
54
|
+
await linkCommand.action(options, undefined);
|
|
55
|
+
expect(scope.isDone()).toBe(true);
|
|
56
|
+
expect(mockConsola.success).toHaveBeenCalledWith('Repository connected successfully.');
|
|
57
|
+
});
|
|
58
|
+
it('should prompt for organization and app when app ID is not provided', async () => {
|
|
59
|
+
const appId = 'app-123';
|
|
60
|
+
const orgId = 'org-1';
|
|
61
|
+
const testToken = 'test-token';
|
|
62
|
+
const options = {};
|
|
63
|
+
mockPromptOrganizationSelection.mockResolvedValueOnce(orgId);
|
|
64
|
+
mockPromptAppSelection.mockResolvedValueOnce(appId);
|
|
65
|
+
const scope = nock(DEFAULT_API_BASE_URL)
|
|
66
|
+
.put(`/v1/apps/${appId}/repository`, {
|
|
67
|
+
ownerSlug: 'capawesome-team',
|
|
68
|
+
provider: 'github',
|
|
69
|
+
repositorySlug: 'cli',
|
|
70
|
+
})
|
|
71
|
+
.matchHeader('Authorization', `Bearer ${testToken}`)
|
|
72
|
+
.reply(200, { id: appId, name: 'Test App' });
|
|
73
|
+
await linkCommand.action(options, undefined);
|
|
74
|
+
expect(scope.isDone()).toBe(true);
|
|
75
|
+
expect(mockPromptOrganizationSelection).toHaveBeenCalled();
|
|
76
|
+
expect(mockPromptAppSelection).toHaveBeenCalledWith(orgId);
|
|
77
|
+
expect(mockConsola.success).toHaveBeenCalledWith('Repository connected successfully.');
|
|
78
|
+
});
|
|
79
|
+
it('should handle API error', async () => {
|
|
80
|
+
const appId = 'app-123';
|
|
81
|
+
const testToken = 'test-token';
|
|
82
|
+
const options = { appId };
|
|
83
|
+
const scope = nock(DEFAULT_API_BASE_URL)
|
|
84
|
+
.put(`/v1/apps/${appId}/repository`, {
|
|
85
|
+
ownerSlug: 'capawesome-team',
|
|
86
|
+
provider: 'github',
|
|
87
|
+
repositorySlug: 'cli',
|
|
88
|
+
})
|
|
89
|
+
.matchHeader('Authorization', `Bearer ${testToken}`)
|
|
90
|
+
.reply(400, { message: 'Git provider not connected' });
|
|
91
|
+
await expect(linkCommand.action(options, undefined)).rejects.toThrow();
|
|
92
|
+
expect(scope.isDone()).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isInteractive } from '../../../utils/environment.js';
|
|
2
|
-
import { fileExistsAtPath, isDirectory } from '../../../utils/file.js';
|
|
2
|
+
import { directoryContainsSourceMaps, directoryContainsSymlinks, fileExistsAtPath, isDirectory } from '../../../utils/file.js';
|
|
3
3
|
import { generateManifestJson } from '../../../utils/manifest.js';
|
|
4
4
|
import { prompt } from '../../../utils/prompt.js';
|
|
5
5
|
import zip from '../../../utils/zip.js';
|
|
@@ -9,7 +9,7 @@ import fs from 'fs';
|
|
|
9
9
|
import pathModule from 'path';
|
|
10
10
|
import { z } from 'zod';
|
|
11
11
|
export default defineCommand({
|
|
12
|
-
description: 'Generate manifest file and compress web assets into a bundle.zip file.',
|
|
12
|
+
description: 'Generate manifest file and compress locally built web assets into a bundle.zip file.',
|
|
13
13
|
options: defineOptions(z.object({
|
|
14
14
|
inputPath: z.string().optional().describe('Path to the web assets directory.'),
|
|
15
15
|
outputPath: z
|
|
@@ -62,6 +62,16 @@ export default defineCommand({
|
|
|
62
62
|
consola.error(`Directory must contain an index.html file: ${inputPath}`);
|
|
63
63
|
process.exit(1);
|
|
64
64
|
}
|
|
65
|
+
// Check for symlinks
|
|
66
|
+
const containsSymlinks = await directoryContainsSymlinks(inputPath);
|
|
67
|
+
if (containsSymlinks) {
|
|
68
|
+
consola.warn('Symbolic links were detected in the specified path. Symbolic links are skipped during bundling.');
|
|
69
|
+
}
|
|
70
|
+
// Check for source maps
|
|
71
|
+
const containsSourceMaps = await directoryContainsSourceMaps(inputPath);
|
|
72
|
+
if (containsSourceMaps) {
|
|
73
|
+
consola.warn('Source map files were detected in the specified path. Source maps should not be distributed to end users as they expose your original source code and increase the download size. Consider excluding source map files from your build output.');
|
|
74
|
+
}
|
|
65
75
|
// 2. Output path resolution
|
|
66
76
|
if (!outputPath) {
|
|
67
77
|
outputPath = './bundle.zip';
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { DEFAULT_CONSOLE_BASE_URL } from '../../../config/consts.js';
|
|
2
|
+
import appBuildSourcesService from '../../../services/app-build-sources.js';
|
|
3
|
+
import appBuildsService from '../../../services/app-builds.js';
|
|
4
|
+
import appCertificatesService from '../../../services/app-certificates.js';
|
|
5
|
+
import appDeploymentsService from '../../../services/app-deployments.js';
|
|
6
|
+
import appEnvironmentsService from '../../../services/app-environments.js';
|
|
7
|
+
import { parseKeyValuePairs } from '../../../utils/app-environments.js';
|
|
8
|
+
import { withAuth } from '../../../utils/auth.js';
|
|
9
|
+
import { parseCustomProperties } from '../../../utils/custom-properties.js';
|
|
10
|
+
import { isInteractive } from '../../../utils/environment.js';
|
|
11
|
+
import { waitForJobCompletion } from '../../../utils/job.js';
|
|
12
|
+
import { prompt, promptAppSelection, promptOrganizationSelection } from '../../../utils/prompt.js';
|
|
13
|
+
import zip from '../../../utils/zip.js';
|
|
14
|
+
import { defineCommand, defineOptions } from '@robingenz/zli';
|
|
15
|
+
import consola from 'consola';
|
|
16
|
+
import fs from 'fs/promises';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
export default defineCommand({
|
|
20
|
+
description: 'Create a new live update by building and deploying web assets using Capawesome Cloud Runners.',
|
|
21
|
+
options: defineOptions(z.object({
|
|
22
|
+
androidEq: z.string().optional().describe('The exact Android versionCode for the live update.'),
|
|
23
|
+
androidMax: z.string().optional().describe('The maximum Android versionCode for the live update.'),
|
|
24
|
+
androidMin: z.string().optional().describe('The minimum Android versionCode for the live update.'),
|
|
25
|
+
appId: z
|
|
26
|
+
.uuid({
|
|
27
|
+
message: 'App ID must be a UUID.',
|
|
28
|
+
})
|
|
29
|
+
.optional()
|
|
30
|
+
.describe('App ID to create the live update for.'),
|
|
31
|
+
certificate: z.string().optional().describe('The name of the certificate to use for the build.'),
|
|
32
|
+
channel: z
|
|
33
|
+
.array(z.string())
|
|
34
|
+
.optional()
|
|
35
|
+
.describe('The name of the channel to deploy to. Can be specified multiple times.'),
|
|
36
|
+
customProperty: z
|
|
37
|
+
.array(z.string().min(1).max(100))
|
|
38
|
+
.max(10)
|
|
39
|
+
.optional()
|
|
40
|
+
.describe('A custom property to assign to the build. Must be in the format `key=value`. Can be specified multiple times.'),
|
|
41
|
+
environment: z.string().optional().describe('The name of the environment to use for the build.'),
|
|
42
|
+
gitRef: z.string().optional().describe('The Git reference (branch, tag, or commit SHA) to build.'),
|
|
43
|
+
iosEq: z.string().optional().describe('The exact iOS CFBundleVersion for the live update.'),
|
|
44
|
+
iosMax: z.string().optional().describe('The maximum iOS CFBundleVersion for the live update.'),
|
|
45
|
+
iosMin: z.string().optional().describe('The minimum iOS CFBundleVersion for the live update.'),
|
|
46
|
+
json: z.boolean().optional().describe('Output in JSON format.'),
|
|
47
|
+
path: z.string().optional().describe('Path to local source files to upload.'),
|
|
48
|
+
rolloutPercentage: z.coerce
|
|
49
|
+
.number()
|
|
50
|
+
.int()
|
|
51
|
+
.min(0)
|
|
52
|
+
.max(100)
|
|
53
|
+
.optional()
|
|
54
|
+
.describe('The rollout percentage for the deployment (0-100). Default: 100.'),
|
|
55
|
+
stack: z
|
|
56
|
+
.enum(['macos-sequoia', 'macos-tahoe'], {
|
|
57
|
+
message: 'Build stack must be either `macos-sequoia` or `macos-tahoe`.',
|
|
58
|
+
})
|
|
59
|
+
.optional()
|
|
60
|
+
.describe('The build stack to use for the build process.'),
|
|
61
|
+
url: z.string().optional().describe('URL to a zip file to use as build source.'),
|
|
62
|
+
variable: z
|
|
63
|
+
.array(z.string())
|
|
64
|
+
.optional()
|
|
65
|
+
.describe('Ad hoc environment variable in key=value format. Can be specified multiple times.'),
|
|
66
|
+
variableFile: z
|
|
67
|
+
.string()
|
|
68
|
+
.optional()
|
|
69
|
+
.describe('Path to a file containing ad hoc environment variables in .env format.'),
|
|
70
|
+
yes: z.boolean().optional().describe('Skip confirmation prompts.'),
|
|
71
|
+
}), { y: 'yes' }),
|
|
72
|
+
action: withAuth(async (options) => {
|
|
73
|
+
let { appId, certificate, channel, gitRef, environment, json, stack, path: sourcePath, url } = options;
|
|
74
|
+
// Validate that path, url, and gitRef cannot be used together
|
|
75
|
+
if (sourcePath && gitRef) {
|
|
76
|
+
consola.error('The --path and --git-ref flags cannot be used together.');
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
if (url && gitRef) {
|
|
80
|
+
consola.error('The --url and --git-ref flags cannot be used together.');
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
if (url && sourcePath) {
|
|
84
|
+
consola.error('The --url and --path flags cannot be used together.');
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
// Validate url if provided
|
|
88
|
+
if (url) {
|
|
89
|
+
consola.warn('The --url option is experimental and may change in the future.');
|
|
90
|
+
}
|
|
91
|
+
// Validate path if provided
|
|
92
|
+
if (sourcePath) {
|
|
93
|
+
consola.warn('The --path option is experimental and may change in the future.');
|
|
94
|
+
const resolvedPath = path.resolve(sourcePath);
|
|
95
|
+
const stat = await fs.stat(resolvedPath).catch(() => null);
|
|
96
|
+
if (!stat || !stat.isDirectory()) {
|
|
97
|
+
consola.error('The --path must point to an existing directory.');
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
const packageJsonPath = path.join(resolvedPath, 'package.json');
|
|
101
|
+
const packageJsonStat = await fs.stat(packageJsonPath).catch(() => null);
|
|
102
|
+
if (!packageJsonStat || !packageJsonStat.isFile()) {
|
|
103
|
+
consola.error('The directory specified by --path must contain a package.json file.');
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Prompt for app ID if not provided
|
|
108
|
+
if (!appId) {
|
|
109
|
+
if (!isInteractive()) {
|
|
110
|
+
consola.error('You must provide an app ID when running in non-interactive environment.');
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
const organizationId = await promptOrganizationSelection({ allowCreate: true });
|
|
114
|
+
appId = await promptAppSelection(organizationId, { allowCreate: true });
|
|
115
|
+
}
|
|
116
|
+
// Prompt for git ref if not provided and no path or url specified
|
|
117
|
+
if (!sourcePath && !url && !gitRef) {
|
|
118
|
+
if (!isInteractive()) {
|
|
119
|
+
consola.error('You must provide a git ref, path, or url when running in non-interactive environment.');
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
gitRef = await prompt('Enter the Git reference (branch, tag, or commit SHA):', {
|
|
123
|
+
type: 'text',
|
|
124
|
+
});
|
|
125
|
+
if (!gitRef) {
|
|
126
|
+
consola.error('You must provide a git ref.');
|
|
127
|
+
process.exit(1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Prompt for channel if not provided
|
|
131
|
+
if (!channel || channel.length === 0) {
|
|
132
|
+
if (!isInteractive()) {
|
|
133
|
+
consola.error('You must provide at least one channel when running in non-interactive environment.');
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
const channelName = await prompt('Enter the channel name to deploy to:', {
|
|
137
|
+
type: 'text',
|
|
138
|
+
});
|
|
139
|
+
if (!channelName) {
|
|
140
|
+
consola.error('You must provide a channel.');
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
143
|
+
channel = [channelName];
|
|
144
|
+
}
|
|
145
|
+
// Prompt for environment if not provided
|
|
146
|
+
if (!environment && !options.yes && isInteractive()) {
|
|
147
|
+
// @ts-ignore wait till https://github.com/unjs/consola/pull/280 is merged
|
|
148
|
+
const selectEnvironment = await prompt('Do you want to select an environment?', {
|
|
149
|
+
type: 'confirm',
|
|
150
|
+
initial: false,
|
|
151
|
+
});
|
|
152
|
+
if (selectEnvironment) {
|
|
153
|
+
const environments = await appEnvironmentsService.findAll({ appId });
|
|
154
|
+
if (environments.length === 0) {
|
|
155
|
+
consola.warn('No environments found for this app.');
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
// @ts-ignore wait till https://github.com/unjs/consola/pull/280 is merged
|
|
159
|
+
environment = await prompt('Select the environment for the build:', {
|
|
160
|
+
type: 'select',
|
|
161
|
+
options: environments.map((env) => ({ label: env.name, value: env.name })),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Prompt for certificate if not provided
|
|
167
|
+
if (!certificate && !options.yes && isInteractive()) {
|
|
168
|
+
// @ts-ignore wait till https://github.com/unjs/consola/pull/280 is merged
|
|
169
|
+
const selectCertificate = await prompt('Do you want to select a certificate?', {
|
|
170
|
+
type: 'confirm',
|
|
171
|
+
initial: false,
|
|
172
|
+
});
|
|
173
|
+
if (selectCertificate) {
|
|
174
|
+
const certificates = await appCertificatesService.findAll({ appId, platform: 'web' });
|
|
175
|
+
if (certificates.length === 0) {
|
|
176
|
+
consola.warn('No certificates found for this app.');
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// @ts-ignore wait till https://github.com/unjs/consola/pull/280 is merged
|
|
180
|
+
certificate = await prompt('Select the certificate for the build:', {
|
|
181
|
+
type: 'select',
|
|
182
|
+
options: certificates.map((cert) => ({ label: cert.name, value: cert.name })),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// Parse ad hoc environment variables from inline and file
|
|
188
|
+
const variablesMap = new Map();
|
|
189
|
+
if (options.variableFile) {
|
|
190
|
+
const fileContent = await fs.readFile(options.variableFile, 'utf-8');
|
|
191
|
+
const fileVariables = parseKeyValuePairs(fileContent);
|
|
192
|
+
fileVariables.forEach((v) => variablesMap.set(v.key, v.value));
|
|
193
|
+
}
|
|
194
|
+
if (options.variable) {
|
|
195
|
+
const inlineVariables = parseKeyValuePairs(options.variable.join('\n'));
|
|
196
|
+
inlineVariables.forEach((v) => variablesMap.set(v.key, v.value));
|
|
197
|
+
}
|
|
198
|
+
const adHocEnvironmentVariables = variablesMap.size > 0 ? Object.fromEntries(variablesMap) : undefined;
|
|
199
|
+
// Create build source from URL if provided
|
|
200
|
+
let appBuildSourceId;
|
|
201
|
+
if (url) {
|
|
202
|
+
consola.start('Creating build source from URL...');
|
|
203
|
+
const appBuildSource = await appBuildSourcesService.createFromUrl({ appId, fileUrl: url });
|
|
204
|
+
appBuildSourceId = appBuildSource.id;
|
|
205
|
+
consola.success('Build source created successfully.');
|
|
206
|
+
}
|
|
207
|
+
// Upload source files if path is provided
|
|
208
|
+
if (sourcePath) {
|
|
209
|
+
const resolvedPath = path.resolve(sourcePath);
|
|
210
|
+
consola.start('Zipping source files...');
|
|
211
|
+
const buffer = await zip.zipFolderWithGitignore(resolvedPath);
|
|
212
|
+
consola.start('Uploading source files...');
|
|
213
|
+
const appBuildSource = await appBuildSourcesService.createFromFile({
|
|
214
|
+
appId,
|
|
215
|
+
fileSizeInBytes: buffer.byteLength,
|
|
216
|
+
buffer,
|
|
217
|
+
name: 'source.zip',
|
|
218
|
+
}, (currentPart, totalParts) => {
|
|
219
|
+
consola.start(`Uploading source files (${currentPart}/${totalParts})...`);
|
|
220
|
+
});
|
|
221
|
+
appBuildSourceId = appBuildSource.id;
|
|
222
|
+
consola.success('Source files uploaded successfully.');
|
|
223
|
+
}
|
|
224
|
+
// Create the web build
|
|
225
|
+
consola.start('Creating build...');
|
|
226
|
+
const response = await appBuildsService.create({
|
|
227
|
+
adHocEnvironmentVariables,
|
|
228
|
+
appBuildSourceId,
|
|
229
|
+
appCertificateName: certificate,
|
|
230
|
+
appEnvironmentName: environment,
|
|
231
|
+
appId,
|
|
232
|
+
stack,
|
|
233
|
+
gitRef,
|
|
234
|
+
platform: 'web',
|
|
235
|
+
});
|
|
236
|
+
consola.info(`Build ID: ${response.id}`);
|
|
237
|
+
consola.info(`Build Number: ${response.numberAsString}`);
|
|
238
|
+
consola.info(`Build URL: ${DEFAULT_CONSOLE_BASE_URL}/apps/${appId}/builds/${response.id}`);
|
|
239
|
+
consola.success('Build created successfully.');
|
|
240
|
+
// Wait for build to complete
|
|
241
|
+
await waitForJobCompletion({ jobId: response.jobId });
|
|
242
|
+
consola.success('Build completed successfully.');
|
|
243
|
+
console.log();
|
|
244
|
+
// Update build with custom properties and version constraints if any are provided
|
|
245
|
+
const customProperties = parseCustomProperties(options.customProperty);
|
|
246
|
+
const hasUpdateFields = customProperties ||
|
|
247
|
+
options.androidMin ||
|
|
248
|
+
options.androidMax ||
|
|
249
|
+
options.androidEq ||
|
|
250
|
+
options.iosMin ||
|
|
251
|
+
options.iosMax ||
|
|
252
|
+
options.iosEq;
|
|
253
|
+
if (hasUpdateFields) {
|
|
254
|
+
consola.start('Updating build...');
|
|
255
|
+
await appBuildsService.update({
|
|
256
|
+
appId,
|
|
257
|
+
appBuildId: response.id,
|
|
258
|
+
customProperties,
|
|
259
|
+
minAndroidAppVersionCode: options.androidMin,
|
|
260
|
+
maxAndroidAppVersionCode: options.androidMax,
|
|
261
|
+
eqAndroidAppVersionCode: options.androidEq,
|
|
262
|
+
minIosAppVersionCode: options.iosMin,
|
|
263
|
+
maxIosAppVersionCode: options.iosMax,
|
|
264
|
+
eqIosAppVersionCode: options.iosEq,
|
|
265
|
+
});
|
|
266
|
+
consola.success('Build updated successfully.');
|
|
267
|
+
}
|
|
268
|
+
// Deploy to channels
|
|
269
|
+
const rolloutPercentage = (options.rolloutPercentage ?? 100) / 100;
|
|
270
|
+
const deploymentIds = [];
|
|
271
|
+
for (const channelName of channel) {
|
|
272
|
+
consola.start(`Creating deployment for channel "${channelName}"...`);
|
|
273
|
+
const deployment = await appDeploymentsService.create({
|
|
274
|
+
appId,
|
|
275
|
+
appBuildId: response.id,
|
|
276
|
+
appChannelName: channelName,
|
|
277
|
+
rolloutPercentage,
|
|
278
|
+
});
|
|
279
|
+
deploymentIds.push(deployment.id);
|
|
280
|
+
consola.info(`Deployment ID: ${deployment.id}`);
|
|
281
|
+
consola.info(`Deployment URL: ${DEFAULT_CONSOLE_BASE_URL}/apps/${appId}/deployments/${deployment.id}`);
|
|
282
|
+
consola.success('Deployment created successfully.');
|
|
283
|
+
}
|
|
284
|
+
// Output JSON if json flag is set
|
|
285
|
+
if (json) {
|
|
286
|
+
console.log(JSON.stringify({
|
|
287
|
+
buildId: response.id,
|
|
288
|
+
buildNumberAsString: response.numberAsString,
|
|
289
|
+
deploymentIds,
|
|
290
|
+
}, null, 2));
|
|
291
|
+
}
|
|
292
|
+
}),
|
|
293
|
+
});
|