Skip to content

Commit

Permalink
Merge pull request #15 from TheSoftwareHouse/feature/ZN-514-copy-to-n…
Browse files Browse the repository at this point in the history
…ew-folder-when-default-one-exist

feat: create new folder during component copy
  • Loading branch information
przemyslaw-bak authored Nov 20, 2023
2 parents e3e58d1 + 7215ce4 commit 4147252
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
15 changes: 14 additions & 1 deletion packages/cli/commands/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
jsonFileExtension,
storiesFileExtension,
packageVersion,
MAXIMUM_COMPONENTS_DIR_POSTFIX,
} from '../shared/constants';
import { generatePath } from '../utils/generatePath';
import { installDependencies } from '../utils/installDependencies';
Expand Down Expand Up @@ -94,11 +95,23 @@ copy
}

for (const resultSrcPath of results.srcPath) {
const destinationDirectory = `${outputPath}/${path.basename(resultSrcPath)}`;
let destinationDirectory = `${outputPath}/${path.basename(resultSrcPath)}`;
const componentDirName = path.basename(resultSrcPath).replace(packageVersion + '/', '');

const excludedExtensions = !results.shouldIncludeStories
? [jsonFileExtension, storiesFileExtension]
: [jsonFileExtension];

if (fs.existsSync(destinationDirectory)) {
for (let index = 1; index < MAXIMUM_COMPONENTS_DIR_POSTFIX; index++) {
const componentDirWithPostfix = `${componentDirName}_${index}`;
if (!fs.existsSync(destinationDirectory.replace(componentDirName, componentDirWithPostfix))) {
destinationDirectory = `${outputPath}/${componentDirWithPostfix}`;
break;
}
}
}

try {
await copyAwsFolderWithExclusion({
Contents,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const packageManagers = ['npm', 'yarn'];
/* eslint-disable @typescript-eslint/no-var-requires */
export const { version: packageVersion } = require('../../../package.json');

export const COMPONENT_ITEMS_BUCKET = process.env.S3_BUCKET_COMPONENT_ITEMS || '';
export const COMPONENT_ITEMS_BUCKET = process.env.S3_BUCKET_COMPONENT_ITEMS || 'tsh-frontend-components-items-catalog';

export const EXCLUDED_BASE_S3_PATHS = [`${packageVersion}/.`, `${packageVersion}/`, `.`];

export const MAXIMUM_COMPONENTS_DIR_POSTFIX = 10;
10 changes: 4 additions & 6 deletions packages/cli/utils/copyAwsFolderWithExclusion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import fs from 'fs';
import { ListObjectsV2Output } from '@aws-sdk/client-s3';

import { COMPONENT_ITEMS_BUCKET, packageVersion } from '../shared/constants';
import { COMPONENT_ITEMS_BUCKET } from '../shared/constants';

import { getS3Object } from './getS3Object';
import { logger } from './logger';
Expand Down Expand Up @@ -38,12 +38,10 @@ export const copyAwsFolderWithExclusion = async ({
}

const getObjectItem = await getS3Object(COMPONENT_ITEMS_BUCKET, object.Key as string);
const destinationDirName = `${path.dirname(destinationDirectory)}/`;
const filePath = `${destinationDirName}${object.Key?.replace(packageVersion, '')}`;
const dirName = `${destinationDirName}${path.dirname(object.Key || '').replace(packageVersion, '')}`;
const filePath = `${destinationDirectory}/${path.basename(object.Key || '')}`;

if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, { recursive: true });
if (!fs.existsSync(destinationDirectory)) {
fs.mkdirSync(destinationDirectory, { recursive: true });
}

if (!fs.existsSync(filePath) && getObjectItem) {
Expand Down

0 comments on commit 4147252

Please sign in to comment.