Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): splitting target path while coping content files #749

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions scripts/catalog-to-astro-content-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const scriptsDir = path.dirname(__filename);

const getTargetPath = (source, target, type, file) => {
const relativePath = path.relative(source, file);
const cleanedRelativePath = relativePath.split(type);
const cleanedRelativePath = relativePath.split(`${type}/`);
Copy link
Contributor

@carlosallexandre carlosallexandre Sep 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The / was removed for compatibility with win32 plataform.
Maybe path.sep could works 🤷🏼‍♂️.

When I worked at a refactor proposal for the catalog-to-astro-content-directory.js I got the following for this snippet:

/**
 * Generates the path until the ASTRO_COLLECTION_KEY or the PROJECT_DIR root.
 * @param {string} filePath The path to the file.
 * @returns {string} The path until the COLLECTION_KEY or PROJECT_DIR root.
 */
const getRelativeTargetPath = (filePath) => {
  const filePathParsed = path.parse(filePath);
  const fileDir = filePathParsed.dir.split(path.sep).filter(Boolean);

  const relativePath = [];
  for (let i = fileDir.length - 1; i >= 0; i--) {
    relativePath.unshift(fileDir[i]);
    if (isCollectionKey(fileDir[i])) break;
  }

  return path.join(...relativePath, filePathParsed.base);
};

/**
 * Check if the key is an ASTRO COLLECTION KEY
 * @param {string} key
 * @returns {boolean}
 */
const isCollectionKey = (key) => {
  const COLLECTION_KEYS = ['commands', 'domains', 'events', 'pages', 'services', 'teams', 'users'];
  /** 
   *  Using includes won't works for your use case either
   *  The solution will be a comparison
   */
  return COLLECTION_KEYS.includes(key);
};

You can check the implementation here.
I hope this can help you find a suitable solution.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @carlosallexandre for review. I committed new changes, can you check?

const targetForEvents = path.join(type, cleanedRelativePath[1]);
return path.join(target, targetForEvents);
};
Expand Down Expand Up @@ -64,7 +64,7 @@ const copyFiles = async ({ source, target, catalogFilesDir, pathToMarkdownFiles,
}

const relativePath = path.relative(source, file);
const cleanedRelativePath = relativePath.split(type);
const cleanedRelativePath = relativePath.split(`${type}/`);
if (!cleanedRelativePath[1]) continue;
const targetForEvents = path.join(type, cleanedRelativePath[1]);

Expand Down