campaign-cli 0.7.2

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/src/main.js ADDED
@@ -0,0 +1,205 @@
1
+ // packages
2
+ import { program, Command } from "commander";
3
+ import sdk from "@adobe/acc-js-sdk";
4
+ import Configstore from "configstore";
5
+ import fs from "fs-extra";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+ // Campaign
9
+ import CampaignError from "./CampaignError.js";
10
+ import CampaignAuth from "./CampaignAuth.js";
11
+ import CampaignInstance from "./CampaignInstance.js";
12
+
13
+ const dirMain = path.dirname(fileURLToPath(import.meta.url));
14
+ const dirPackage = path.resolve(dirMain, "..");
15
+ const packageJsonPath = path.join(dirPackage, "package.json");
16
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
17
+
18
+ const authFile = new Configstore("campaign-cli.auth");
19
+ const auth = new CampaignAuth(sdk, authFile);
20
+ const defaultDistRoot = path.join(process.cwd());
21
+ const defaultConfigPath = path.join(process.cwd(), "acc.config.json"); // default config path in current working directory, if not specified
22
+
23
+ const vAcc = packageJson.version;
24
+ const vSdk = sdk.getSDKVersion().version;
25
+ const pathConfig = "config.path";
26
+ console.log(
27
+ `🏠 acc ${vAcc} initialized with Adobe acc-js-sdk ${vSdk} and authentication from ${pathConfig}`,
28
+ );
29
+
30
+ program
31
+ .name("acc")
32
+ .description(
33
+ `${packageJson.description}. Documentation on ${packageJson.homepage}`,
34
+ )
35
+ .version(vAcc);
36
+
37
+ // AUTH
38
+ program
39
+ .command("auth")
40
+ // INIT
41
+ .addCommand(
42
+ new Command()
43
+ .name("init")
44
+ .requiredOption(
45
+ "--alias <alias>",
46
+ "Local alias for this instance, e.g. prod, staging, local",
47
+ )
48
+ .requiredOption(
49
+ "--host <url>",
50
+ "URL of Adobe Campaign root, e.g. http://localhost:8080",
51
+ )
52
+ .requiredOption("--user <user>", "Operator username")
53
+ .requiredOption("--password <pwd>", "Operator password")
54
+ .action(async (options) => {
55
+ try {
56
+ await auth.init(options);
57
+ } catch (err) {
58
+ handleCampaignError(err);
59
+ }
60
+ }),
61
+ )
62
+ // LOGIN
63
+ .addCommand(
64
+ new Command()
65
+ .name("login")
66
+ .requiredOption(
67
+ "--alias <alias>",
68
+ "Local alias for this instance, e.g. prod, staging, local",
69
+ )
70
+ .action(async (options) => {
71
+ try {
72
+ await auth.login(options);
73
+ } catch (err) {
74
+ handleCampaignError(err);
75
+ }
76
+ }),
77
+ )
78
+ // LIST
79
+ .addCommand(
80
+ new Command().name("list").action(() => {
81
+ try {
82
+ auth.list();
83
+ } catch (err) {
84
+ handleCampaignError(err);
85
+ }
86
+ }),
87
+ )
88
+ // IP
89
+ .addCommand(
90
+ new Command().name("ip").action(async () => {
91
+ try {
92
+ await auth.ip();
93
+ } catch (err) {
94
+ handleCampaignError(err);
95
+ }
96
+ }),
97
+ );
98
+
99
+ // INSTANCE
100
+ program
101
+ .command("instance")
102
+ // CHECK
103
+ .addCommand(
104
+ new Command()
105
+ .name("check")
106
+ .requiredOption(
107
+ "--alias <alias>",
108
+ "Local alias for this instance, e.g. prod, staging, local",
109
+ )
110
+ .option(
111
+ "--path <path>",
112
+ "Path where the command should run. Defaults to current working directory.",
113
+ defaultDistRoot,
114
+ )
115
+ .option(
116
+ "--config <path>",
117
+ "Path to the configuration file. Defaults ./config/acc.config.json.",
118
+ defaultConfigPath,
119
+ )
120
+ .option(
121
+ "--verbose",
122
+ "Verbose output with details on each configuration item. Defaults to false.",
123
+ false,
124
+ )
125
+ .action(async (options) => {
126
+ try {
127
+ await pull(options, true);
128
+ } catch (err) {
129
+ handleCampaignError(err);
130
+ }
131
+ }),
132
+ )
133
+ // PULL
134
+ .addCommand(
135
+ new Command()
136
+ .name("pull")
137
+ .requiredOption(
138
+ "--alias <alias>",
139
+ "Local alias for this instance, e.g. prod, staging, local",
140
+ )
141
+ .option(
142
+ "--path <path>",
143
+ "Path where the command should run. Defaults to current working directory.",
144
+ defaultDistRoot,
145
+ )
146
+ .option(
147
+ "--config <path>",
148
+ "Path to the configuration file. Defaults ./config/acc.config.json.",
149
+ defaultConfigPath,
150
+ )
151
+ .option(
152
+ "--verbose",
153
+ "Verbose output with details on each configuration item. Defaults to false.",
154
+ false,
155
+ )
156
+ .action(async (options) => {
157
+ try {
158
+ await pull(options, false);
159
+ } catch (err) {
160
+ handleCampaignError(err);
161
+ }
162
+ }),
163
+ );
164
+
165
+ program.parse(process.argv);
166
+
167
+ async function pull(options, isPreview) {
168
+ // if the config file doesn't exist at the default location, copy the example config there
169
+ if (options.config == defaultConfigPath && !fs.existsSync(options.config)) {
170
+ console.log(`🛠️ Config not found, initalializing ${options.config}`);
171
+ fs.copySync(
172
+ path.join(dirPackage, "config", "acc.config.json"),
173
+ options.config,
174
+ );
175
+ } else {
176
+ console.log(`🛠️ Using config ${options.config}`);
177
+ }
178
+ const campaignConfig = JSON.parse(fs.readFileSync(options.config));
179
+ const client = await auth.login({ alias: options.alias });
180
+ const instance = new CampaignInstance(client, campaignConfig, options);
181
+ await instance.pull(isPreview);
182
+ }
183
+
184
+ /**
185
+ * Handles errors from Campaign CLI operations.
186
+ * Distinguishes between CampaignError and other errors for appropriate handling.
187
+ *
188
+ * @param {Error} err - The error to handle
189
+ * @returns {void}
190
+ *
191
+ * @example
192
+ * try {
193
+ * await auth.login({ alias: 'prod' });
194
+ * } catch (err) {
195
+ * handleCampaignError(err);
196
+ * }
197
+ */
198
+ function handleCampaignError(err) {
199
+ if (err instanceof CampaignError) {
200
+ console.error(`⚠️ Campaign warning: ${err.message}`);
201
+ } else {
202
+ throw err;
203
+ }
204
+ process.exit(1);
205
+ }