-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP custom watcher should handle create events from directories and files.
- Loading branch information
1 parent
e74930a
commit cf502d1
Showing
4 changed files
with
241 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import path from 'node:path'; | ||
import { removeBasePath } from 'scripts/map-catalog-to-astro'; | ||
|
||
describe('MapCatalogToAstro', () => { | ||
describe('removeBasePath', () => { | ||
it('should remove the base path', () => { | ||
const fullPath = path.join('/home/user/ec/events/index.md'); | ||
const basePath = path.join('/home/user/ec/'); | ||
|
||
expect(removeBasePath(fullPath, basePath)).toEqual('events/index.md'); | ||
Check failure on line 11 in scripts/__tests__/map-catalog-to-astro.spec.ts GitHub Actions / Verify Build (windows-latest)scripts/__tests__/map-catalog-to-astro.spec.ts > MapCatalogToAstro > removeBasePath > should remove the base path
|
||
}); | ||
|
||
it('should remove the base path', () => { | ||
const fullPath = path.join('/events/index.md'); | ||
const basePath = path.join('/home/user/ec'); | ||
|
||
expect(removeBasePath(fullPath, basePath)).toEqual('/events/index.md'); | ||
Check failure on line 18 in scripts/__tests__/map-catalog-to-astro.spec.ts GitHub Actions / Verify Build (windows-latest)scripts/__tests__/map-catalog-to-astro.spec.ts > MapCatalogToAstro > removeBasePath > should remove the base path
|
||
}); | ||
|
||
it('should remove the base path', () => { | ||
const fullPath = path.join('/ec/events/index.md'); | ||
const basePath = path.join('/ec'); | ||
|
||
expect(removeBasePath(fullPath, basePath)).toEqual('events/index.md'); | ||
Check failure on line 25 in scripts/__tests__/map-catalog-to-astro.spec.ts GitHub Actions / Verify Build (windows-latest)scripts/__tests__/map-catalog-to-astro.spec.ts > MapCatalogToAstro > removeBasePath > should remove the base path
|
||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import path from 'node:path'; | ||
|
||
/** | ||
* @typedef {Object} MapCatalogToAstroParams | ||
* @prop {string} catalogFilePath The catalog file path | ||
* @prop {string} astroDir The astro directory | ||
* @prop {string} projectDir The user's project directory | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {MapCatalogToAstroParams} params | ||
* @returns {string[]|null} The astro file paths | ||
*/ | ||
export function mapCatalogToAstro({ catalogFilePath, astroDir, projectDir }) { | ||
const relativeCatalogFilePath = removeBasePath(catalogFilePath, projectDir); | ||
|
||
if (!isFileRelatedToCatalog(relativeCatalogFilePath)) { | ||
return null; | ||
} | ||
|
||
const baseTargetPaths = getBaseTargetPath(relativeCatalogFilePath); | ||
const relativeTargetPath = getRelativeTargetPath(relativeCatalogFilePath); | ||
|
||
return baseTargetPaths.map((base) => | ||
path.join(astroDir, base, relativeTargetPath.replace('index.md', 'index.mdx').replace('changelog.md', 'changelog.mdx')) | ||
); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} fullPath | ||
* @param {string} basePath | ||
* @returns {string} The fullPath without the basePath | ||
*/ | ||
export function removeBasePath(fullPath, basePath) { | ||
const relativePath = path.relative(basePath, fullPath); | ||
return relativePath.startsWith('..') ? fullPath : relativePath; | ||
} | ||
|
||
/** | ||
* Check if the key is an ASTRO COLLECTION KEY | ||
* @param {string} key | ||
* @returns {boolean} | ||
*/ | ||
function isCollectionKey(key) { | ||
const COLLECTION_KEYS = ['events', 'commands', 'services', 'users', 'teams', 'domains', 'flows', 'pages', 'changelogs']; | ||
return COLLECTION_KEYS.includes(key); | ||
} | ||
|
||
function seemsDirectory(filePath) { | ||
return path.parse(filePath).ext == ''; | ||
} | ||
|
||
/** | ||
* Checks whether the given file is a configuration file, styles file, public asset file or collection file. | ||
* @param {string} filePath - The file path without the projectDir prefix. | ||
* @returns {boolean} | ||
*/ | ||
function isFileRelatedToCatalog(filePath) { | ||
const filePathParsed = path.parse(filePath); | ||
|
||
// Check if the file is a configuration or style file | ||
if (filePathParsed.base == 'eventcatalog.config.js' || filePathParsed.base == 'eventcatalog.styles.css') { | ||
return true; | ||
} | ||
|
||
const firstDir = filePathParsed.dir.split(path.sep).filter(Boolean)[0]; | ||
|
||
if ( | ||
isCollectionKey(firstDir) || | ||
firstDir == 'public' || | ||
firstDir == 'components' || | ||
(seemsDirectory(filePath) && | ||
(isCollectionKey(filePathParsed.base) || filePathParsed.base == 'public' || filePathParsed.base == 'components')) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Generates the base target path accordingly to the file path. | ||
* @param {string} filePath The path to the file without PROJECT_DIR prefix. | ||
* @returns {Array.<'src/content'|'public/generated'|'src/catalog-files'|'/'>} The base target path. | ||
*/ | ||
function getBaseTargetPath(filePath) { | ||
const filePathParsed = path.parse(filePath); | ||
const fileDir = filePathParsed.dir.split(path.sep).filter(Boolean); | ||
const fileBase = filePathParsed.base; | ||
const fileExt = filePathParsed.ext; | ||
|
||
// Collection files (markdown or non-markdown) | ||
if (isCollectionKey(fileDir[0]) || /* handle directory */ isCollectionKey(fileBase)) { | ||
switch (true) { | ||
case fileBase == 'changelog.md': | ||
return [path.join('src', 'content', 'changelogs')]; | ||
case fileExt == '.md': | ||
case fileExt == '': // Maybe a directory | ||
return [path.join('src', 'content')]; | ||
default: | ||
return [path.join('public', 'generated'), path.join('src', 'catalog-files')]; | ||
} | ||
} | ||
|
||
// Custom components | ||
if (fileDir[0] == 'components' || /* handle directtory */ fileBase == 'components') { | ||
return [path.join('src', 'custom-defined-components')]; | ||
} | ||
|
||
// Public assets (public/*) | ||
if (fileDir[0] == 'public' || /* handle directory */ fileBase == 'public') { | ||
return [path.join('public')]; | ||
} | ||
|
||
/** | ||
* Config files: | ||
* - eventcatalog.config.js | ||
* - eventcatalog.styles.css | ||
*/ | ||
return [path.join('/')]; | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
function getRelativeTargetPath(filePath) { | ||
const filePathParsed = path.parse(filePath); | ||
const fileDir = filePathParsed.dir.split(path.sep).filter(Boolean); | ||
|
||
if (fileDir[0] == 'public' || fileDir[0] == 'components') { | ||
return path.join(...fileDir.slice(1), filePathParsed.base); | ||
} | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters