Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@contentstack/cli-utilities';

import { trace } from '../../utils/log';
import { askEncryptionKey, getAppName } from '../../utils/interactive';
import { askEncryptionKey, getLocationName } from '../../utils/interactive';
import { ModuleClassParams, MarketplaceAppsConfig, ImportConfig, Installation, Manifest } from '../../types';
import {
log,
Expand Down Expand Up @@ -51,6 +51,7 @@ export default class ImportMarketplaceApps {
public developerHubBaseUrl: string;
public nodeCrypto: NodeCrypto;
public appSdk: ContentstackMarketplaceClient;
public existingNames: Set<string>;

constructor({ importConfig }: ModuleClassParams) {
this.importConfig = importConfig;
Expand All @@ -63,6 +64,7 @@ export default class ImportMarketplaceApps {
this.appOriginalName = undefined;
this.installedApps = [];
this.installationUidMapping = {};
this.existingNames = new Set<string>();
}

/**
Expand Down Expand Up @@ -346,15 +348,15 @@ export default class ImportMarketplaceApps {
if (location.meta) {
location.meta = map(location.meta, (meta) => {
if (meta.name && this.appOriginalName == meta.name) {
const name = getAppName(first(split(meta.name, '◈')), appSuffix);
const name = getLocationName(first(split(meta.name, '◈')), appSuffix, this.existingNames);

if (!this.appNameMapping[this.appOriginalName]) {
this.appNameMapping[this.appOriginalName] = name;
}

meta.name = name;
} else if (meta.name) {
meta.name = getAppName(first(split(meta.name, '◈')), appSuffix + (+index + 1));
meta.name = getLocationName(first(split(meta.name, '◈')), appSuffix + (+index + 1), this.existingNames);
}

return meta;
Expand Down
23 changes: 23 additions & 0 deletions packages/contentstack-import/src/utils/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ export const getAppName= (name: string, appSuffix = 1) => {
return name;
}

export const getLocationName= (name: string, appSuffix = 1, existingNames: Set<string>) => {
const maxLength = 50;
const suffixLength = appSuffix.toString().length + 1; // +1 for the '◈' character

let truncatedName = name;
if (name.length + suffixLength > maxLength) {
truncatedName = name.slice(0, maxLength - suffixLength);
}

let newName = `${first(split(truncatedName, '◈'))}◈${appSuffix}`;

// Ensure uniqueness
while (existingNames.has(newName)) {
appSuffix++;
newName = `${first(split(truncatedName, '◈'))}◈${appSuffix}`;
}

// Add the new name to the set of existing names
existingNames.add(newName);

return newName;
}

const validateAppName =(name: string ) =>{
if (name.length < 3 || name.length > 20) {
return 'The app name should be within 3-20 characters long.';
Expand Down