-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwatch.ts
More file actions
81 lines (70 loc) · 2.94 KB
/
watch.ts
File metadata and controls
81 lines (70 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {watchCartridges} from '@salesforce/b2c-tooling-sdk/operations/code';
import {CartridgeCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {t} from '../../i18n/index.js';
export default class CodeWatch extends CartridgeCommand<typeof CodeWatch> {
static args = {
...CartridgeCommand.baseArgs,
};
static description = t('commands.code.watch.description', 'Watch cartridges and upload changes to an instance');
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> ./my-cartridges',
'<%= config.bin %> <%= command.id %> --server my-sandbox.demandware.net --code-version v1',
'<%= config.bin %> <%= command.id %> -c app_storefront_base',
'<%= config.bin %> <%= command.id %> -x test_cartridge',
];
static flags = {
...CartridgeCommand.baseFlags,
...CartridgeCommand.cartridgeFlags,
};
async run(): Promise<void> {
this.requireWebDavCredentials();
this.requireOAuthCredentials();
const hostname = this.resolvedConfig.values.hostname!;
const version = this.resolvedConfig.values.codeVersion;
this.log(t('commands.code.watch.starting', 'Starting watcher for {{path}}', {path: this.cartridgePath}));
this.log(t('commands.code.watch.target', 'Target: {{hostname}}', {hostname}));
if (version) {
this.log(t('commands.code.watch.codeVersion', 'Code Version: {{version}}', {version}));
}
try {
const result = await watchCartridges(this.instance, this.cartridgePath, {
...this.cartridgeOptions,
onUpload: (files) => {
this.log(t('commands.code.watch.uploaded', '[UPLOAD] {{count}} file(s)', {count: files.length}));
},
onDelete: (files) => {
this.log(t('commands.code.watch.deleted', '[DELETE] {{count}} file(s)', {count: files.length}));
},
onError: (error) => {
this.warn(t('commands.code.watch.error', 'Error: {{message}}', {message: error.message}));
},
});
this.log(
t('commands.code.watch.watching', 'Watching {{count}} cartridge(s)...', {count: result.cartridges.length}),
);
this.log(t('commands.code.watch.pressCtrlC', 'Press Ctrl+C to stop'));
// Keep the process running until interrupted
await new Promise<void>((resolve) => {
const cleanup = () => {
this.log(t('commands.code.watch.stopping', '\nStopping watcher...'));
result.stop().then(() => {
resolve();
});
};
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
});
} catch (error) {
if (error instanceof Error) {
this.error(t('commands.code.watch.failed', 'Watch failed: {{message}}', {message: error.message}));
}
throw error;
}
}
}