@contentstack/cli-cm-export-query 1.0.0-beta.1 → 1.0.0-beta.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.
@@ -6,6 +6,5 @@ export default class ExportQueryCommand extends Command {
6
6
  static examples: string[];
7
7
  static usage: string;
8
8
  static flags: FlagInput;
9
- static aliases: string[];
10
9
  run(): Promise<void>;
11
10
  }
@@ -16,8 +16,16 @@ class ExportQueryCommand extends cli_command_1.Command {
16
16
  exportQueryConfig.developerHubBaseUrl = this.developerHubUrl;
17
17
  }
18
18
  this.exportDir = (0, cli_utilities_1.sanitizePath)(exportQueryConfig.exportDir);
19
- // Initialize and run query export
19
+ // Initialize management API client
20
20
  const managementAPIClient = await (0, cli_utilities_1.managementSDKClient)(exportQueryConfig);
21
+ // Setup and validate branch configuration
22
+ const stackAPIClient = managementAPIClient.stack({
23
+ api_key: exportQueryConfig.stackApiKey,
24
+ management_token: exportQueryConfig.managementToken,
25
+ });
26
+ // Setup branches (validate branch or set default to 'main')
27
+ await (0, utils_1.setupBranches)(exportQueryConfig, stackAPIClient);
28
+ // Initialize and run query export
21
29
  const queryExporter = new query_executor_1.QueryExporter(managementAPIClient, exportQueryConfig);
22
30
  await queryExporter.execute();
23
31
  (0, utils_1.log)(exportQueryConfig, 'Query-based export completed successfully!', 'success');
@@ -76,4 +84,3 @@ ExportQueryCommand.flags = {
76
84
  description: 'Skip confirmation prompts',
77
85
  }),
78
86
  };
79
- ExportQueryCommand.aliases = ['cm:export-query'];
@@ -15,7 +15,6 @@ class ModuleExporter {
15
15
  (0, logger_1.log)(this.exportQueryConfig, `Exporting module: ${moduleName}`, 'info');
16
16
  // Build command arguments
17
17
  const cmd = this.buildExportCommand(moduleName, options);
18
- (0, logger_1.log)(this.exportQueryConfig, `Running export command: ${cmd.join(' ')}`, 'debug');
19
18
  // Configurable delay
20
19
  const delay = this.exportQueryConfig.exportDelayMs || 2000;
21
20
  await new Promise((resolve) => setTimeout(resolve, delay));
@@ -0,0 +1,10 @@
1
+ import { QueryExportConfig } from '../types';
2
+ /**
3
+ * Validates and sets up branch configuration for the stack
4
+ *
5
+ * @param config The export configuration
6
+ * @param stackAPIClient The stack API client
7
+ * @returns Promise that resolves when branch setup is complete
8
+ */
9
+ export declare const setupBranches: (config: QueryExportConfig, stackAPIClient: any) => Promise<void>;
10
+ export default setupBranches;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupBranches = void 0;
4
+ const logger_1 = require("./logger");
5
+ /**
6
+ * Validates and sets up branch configuration for the stack
7
+ *
8
+ * @param config The export configuration
9
+ * @param stackAPIClient The stack API client
10
+ * @returns Promise that resolves when branch setup is complete
11
+ */
12
+ const setupBranches = async (config, stackAPIClient) => {
13
+ if (typeof config !== 'object') {
14
+ throw new Error('Invalid config to setup the branch');
15
+ }
16
+ try {
17
+ if (config.branchName) {
18
+ // Check if the specified branch exists
19
+ (0, logger_1.log)(config, `Validating branch: ${config.branchName}`, 'info');
20
+ const result = await stackAPIClient
21
+ .branch(config.branchName)
22
+ .fetch()
23
+ .catch((err) => {
24
+ (0, logger_1.log)(config, `Error fetching branch: ${err.message}`, 'error');
25
+ return null;
26
+ });
27
+ if (result && typeof result === 'object') {
28
+ (0, logger_1.log)(config, `Branch '${config.branchName}' found`, 'success');
29
+ }
30
+ else {
31
+ throw new Error(`No branch found with the name '${config.branchName}'`);
32
+ }
33
+ }
34
+ else {
35
+ // If no branch name provided, check if the stack has branches
36
+ (0, logger_1.log)(config, 'No branch specified, checking if stack has branches', 'info');
37
+ const result = await stackAPIClient
38
+ .branch()
39
+ .query()
40
+ .find()
41
+ .catch(() => {
42
+ (0, logger_1.log)(config, 'Stack does not have branches', 'info');
43
+ return null;
44
+ });
45
+ if (result && result.items && Array.isArray(result.items) && result.items.length > 0) {
46
+ // Set default branch to 'main' if it exists
47
+ config.branchName = 'main';
48
+ }
49
+ else {
50
+ // Stack doesn't have branches
51
+ (0, logger_1.log)(config, 'Stack does not have branches', 'info');
52
+ return;
53
+ }
54
+ }
55
+ config.branchEnabled = true;
56
+ }
57
+ catch (error) {
58
+ (0, logger_1.log)(config, `Error setting up branches: ${error.message}`, 'error');
59
+ throw error;
60
+ }
61
+ };
62
+ exports.setupBranches = setupBranches;
63
+ exports.default = exports.setupBranches;
@@ -15,19 +15,19 @@ async function setupQueryExportConfig(flags) {
15
15
  // Handle authentication
16
16
  if (flags.alias) {
17
17
  const { token, apiKey } = cli_utilities_1.configHandler.get(`tokens.${flags.alias}`) || {};
18
- config_1.default.managementToken = token;
19
- config_1.default.stackApiKey = apiKey;
20
- if (!config_1.default.managementToken) {
18
+ exportQueryConfig.managementToken = token;
19
+ exportQueryConfig.stackApiKey = apiKey;
20
+ if (!exportQueryConfig.managementToken) {
21
21
  throw new Error(`No management token found on given alias ${flags.alias}`);
22
22
  }
23
23
  }
24
- if (!config_1.default.managementToken) {
24
+ if (!exportQueryConfig.managementToken) {
25
25
  if (!(0, cli_utilities_1.isAuthenticated)()) {
26
26
  throw new Error('Please login or provide an alias for the management token');
27
27
  }
28
28
  else {
29
- config_1.default.stackApiKey = flags['stack-api-key'] || (await (0, common_helper_1.askAPIKey)());
30
- if (typeof config_1.default.stackApiKey !== 'string') {
29
+ exportQueryConfig.stackApiKey = flags['stack-api-key'] || (await (0, common_helper_1.askAPIKey)());
30
+ if (typeof exportQueryConfig.stackApiKey !== 'string') {
31
31
  throw new Error('Invalid API key received');
32
32
  }
33
33
  }
@@ -6,3 +6,4 @@ export * from './config-handler';
6
6
  export * from './content-type-helper';
7
7
  export * from './dependency-resolver';
8
8
  export * from './referenced-asset-handler';
9
+ export { setupBranches } from './branch-helper';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unlinkFileLogger = exports.log = exports.fsUtil = exports.fileHelper = void 0;
3
+ exports.setupBranches = exports.unlinkFileLogger = exports.log = exports.fsUtil = exports.fileHelper = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  exports.fileHelper = tslib_1.__importStar(require("./file-helper"));
6
6
  var file_helper_1 = require("./file-helper");
@@ -13,3 +13,5 @@ tslib_1.__exportStar(require("./config-handler"), exports);
13
13
  tslib_1.__exportStar(require("./content-type-helper"), exports);
14
14
  tslib_1.__exportStar(require("./dependency-resolver"), exports);
15
15
  tslib_1.__exportStar(require("./referenced-asset-handler"), exports);
16
+ var branch_helper_1 = require("./branch-helper");
17
+ Object.defineProperty(exports, "setupBranches", { enumerable: true, get: function () { return branch_helper_1.setupBranches; } });
@@ -83,10 +83,21 @@ class AssetReferenceHandler {
83
83
  assetUIDs.add(match[1]);
84
84
  }
85
85
  }
86
+ let assetUrlRegex = '';
87
+ let assetUIDMatchIndex;
88
+ if (process.env.ENVIRONMENT === 'NON_PROD') {
89
+ assetUrlRegex = '(https://.*?/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))';
90
+ assetUIDMatchIndex = 3;
91
+ }
92
+ else {
93
+ assetUrlRegex =
94
+ '(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))';
95
+ assetUIDMatchIndex = 6;
96
+ }
86
97
  // Pattern 2: Contentstack asset URLs
87
- const urlRegex = new RegExp('(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))', 'g');
98
+ const urlRegex = new RegExp(assetUrlRegex, 'g');
88
99
  while ((match = urlRegex.exec(content)) !== null) {
89
- const assetUID = match[6]; // The asset UID is in the 6th capture group
100
+ const assetUID = match[assetUIDMatchIndex]; // The asset UID is in the 6th capture group
90
101
  if (assetUID) {
91
102
  assetUIDs.add(assetUID);
92
103
  }
@@ -1,9 +1,7 @@
1
1
  {
2
2
  "commands": {
3
3
  "cm:stacks:export-query": {
4
- "aliases": [
5
- "cm:export-query"
6
- ],
4
+ "aliases": [],
7
5
  "args": {},
8
6
  "description": "Export content from a stack using query-based filtering",
9
7
  "examples": [
@@ -104,5 +102,5 @@
104
102
  ]
105
103
  }
106
104
  },
107
- "version": "1.0.0-beta.1"
105
+ "version": "1.0.0-beta.2"
108
106
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-export-query",
3
3
  "description": "Contentstack CLI plugin to export content from stack",
4
- "version": "1.0.0-beta.1",
4
+ "version": "1.0.0-beta.2",
5
5
  "author": "Contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "dependencies": {