diff --git a/src/generators/custom-template.js b/src/generators/custom-template.js index 3c70e83..abf6e16 100644 --- a/src/generators/custom-template.js +++ b/src/generators/custom-template.js @@ -13,7 +13,6 @@ import { checkDirectoriesTree, createDir } from '../utils/directory.js' /** * @typedef {Record, boolean>} TargetPaths * - * @typedef {{name: string, template: import('../types.js').CustomTemplate, targets: TargetPaths}} CommomReturn * * @typedef {Omit & { resource: import('../types.js').TemplateResource }} Resource */ @@ -22,15 +21,23 @@ import { checkDirectoriesTree, createDir } from '../utils/directory.js' * Build resources from template * * @param {string} name Resource name from arguments - * @param {Resource} template Local template from meta file - * @returns {Promise} + * @param {import('../types.js').CustomTemplate} template Local template from meta file + * @returns {Promise} */ export async function buildCustomTemplate(name, template) { let results = [] - for (resource of template.resources) { + for (let resource of template.resources) { + /** @type {Resource} */ + const resourceData = { + identifier: template.identifier, + folderWrapper: template.folderWrapper, + case: template.case, + resource + } + const result = compose( - checkPaths(name), + checkPaths(name, resourceData), getTemplatesData, handleTemplateReplacements, createResources @@ -46,9 +53,9 @@ export async function buildCustomTemplate(name, template) { * Check templates path and ask if not exists * * @param {string} name Resource name from arguments - * @param {import('../types.js').CustomTemplate} template Template data from meta file + * @param {Resource} template Template data from meta file * - * @returns {CommomReturn} + * @returns {{ name: string, template: Resource, target: boolean }} */ function checkPaths(name, template) { return () => { @@ -57,16 +64,17 @@ function checkPaths(name, template) { : template?.resource?.path const resourceSplitedPath = splitPathString(resourcePath) + let target = '' if (resourceSplitedPath) { - targets.resource = checkDirectoriesTree(resourceSplitedPath) + target = checkDirectoriesTree(resourceSplitedPath) } - if (!targets?.resource) { - targets.resource = createDir(getTargetFullPath(resourcePath)) + if (!target?.resource) { + target = createDir(getTargetFullPath(resourcePath)) } - return { name, template, targets } + return { name, template, target } } } @@ -75,38 +83,17 @@ function checkPaths(name, template) { * * @param {ReturnType} param0 Template data from meta file */ -function getTemplatesData({ name, template, targets }) { - const templatesContent = { - resource: null, - story: null, - style: null, - test: null - } +function getTemplatesData({ name, template, target }) { + if (!target) return undefined + + let templateContent = '' try { - templatesContent.resource = readFileContent( + templateContent = readFileContent( getTemplateFullPath(template?.resource?.template) ) - if (template?.test) { - templatesContent.test = readFileContent( - getTemplateFullPath(template?.test?.template) - ) - } - - if (template?.story) { - templatesContent.story = readFileContent( - getTemplateFullPath(template?.story?.template) - ) - } - - if (template?.style) { - templatesContent.style = readFileContent( - getTemplateFullPath(template?.style?.template) - ) - } - - return { name, template, templatesContent, targets } + return { name, template, templateContent, target } } catch (error) { console.error(error) } @@ -120,48 +107,18 @@ function getTemplatesData({ name, template, targets }) { function handleTemplateReplacements({ name, template, - templatesContent, - targets + templateContent, + target }) { - if (!template.case) { - return { name, template, templatesContent, targets } - } - - name = convertCase(template.case, name) + name = convertCase(template.case ?? 'PascalCase', name) - templatesContent.resource = templatesContent.resource?.replace( - /ResourceName/g, - name + templateContent = replaceContentFromSideResource( + name, + templateContent, + template ) - if (templatesContent?.test) { - templatesContent.test = replaceContentFromSideResource( - name, - templatesContent.test, - 'test', - template - ) - - if (templatesContent?.story) { - templatesContent.story = replaceContentFromSideResource( - name, - templatesContent.story, - 'story', - template - ) - } - - if (templatesContent?.style) { - templatesContent.style = replaceContentFromSideResource( - name, - templatesContent.style, - 'style', - template - ) - } - } - - return { name, template, templatesContent, targets } + return { name, template, templateContent, target } } /** @@ -170,60 +127,21 @@ function handleTemplateReplacements({ * @param {ReturnType} param0 Template data from meta file * @returns {boolean} */ -function createResources({ name, targets, template, templatesContent }) { +function createResources({ name, target, template, templateContent }) { /** * @type {string[]} */ let created = [] - if (targets.resource) { + if (target) { if (template.folderWrapper) template.resource.path = join(template.resource?.path, name) - const fullPath = getFullPath(name, 'resource', template) - const resourceCreated = createFileWithContent( - fullPath, - templatesContent.resource - ) + const fullPath = getFullPath(name, template) + const resourceCreated = createFileWithContent(fullPath, templateContent) if (resourceCreated) created.push(fullPath) } - if (targets?.test) { - if (template.folderWrapper) - template.test.path = join(template?.test?.path, name) - - const fullPath = getFullPath(name, 'test', template) - const testCreated = createFileWithContent(fullPath, templatesContent.test) - - if (testCreated) created.push(fullPath) - } - - if (targets?.style) { - if (template.folderWrapper) - template.style.path = join(template?.style?.path, name) - - const fullPath = getFullPath(name, 'style', template) - const styleCreated = createFileWithContent( - fullPath, - templatesContent?.style - ) - - if (styleCreated) created.push(fullPath) - } - - if (targets?.story) { - if (template.folderWrapper) - template.story.path = join(template?.story?.path, name) - - const fullPath = getFullPath(name, 'story', template) - const storyCreated = createFileWithContent( - fullPath, - templatesContent?.story - ) - - if (storyCreated) created.push(fullPath) - } - return created } diff --git a/src/utils/scaffold-action.js b/src/utils/scaffold-action.js index cbe5af8..24ea49d 100644 --- a/src/utils/scaffold-action.js +++ b/src/utils/scaffold-action.js @@ -7,6 +7,10 @@ import { customTemplateTypeMap } from '../schemas/custom-template.js' import { checkFileExists, readFileContent } from '../utils/file.js' import { removePostfixAndExt } from './file-extension.js' +/** + * @typedef {Omit & { resource: import('../types.js').TemplateResource }} Template + */ + /** * Get template from meta file * @@ -109,14 +113,22 @@ export function getLastItem(text, pattern) { return pieces[pieces.length - 1] } -export function replaceContentFromSideResource(name, content, key, template) { +/** + * Replace text occurrences inside a string + * + * @param {string} name Resource name + * @param {string} content File content from template + * @param {Template} template Template config from meta file + * @returns {string} + */ +export function replaceContentFromSideResource(name, content, template) { const resourceNameReplaced = content.replace(/ResourceName/g, name) if (resourceNameReplaced) content = resourceNameReplaced - const { extension } = getFileNameFromMetadata(template[key].template) + const { extension } = getFileNameFromMetadata(template.resource.template) - const fullPath = join(template[key].path, `${name}.${extension}`) + const fullPath = join(template.resource.path, `${name}.${extension}`) content = replaceResourcePath(fullPath, content) @@ -144,20 +156,41 @@ export function replaceResourcePath(fullPath, fileContent) { return fileContent } +/** + * Get full path from root dir where command is called + * + * @param {string} path File path + * @returns {string} + */ export function getTemplateFullPath(path) { return join(process.cwd(), '.clingon', 'templates', path) } +/** + * Get target full path from root dir where command is called + * + * @param {string} path File path + * @returns {string} + */ export function getTargetFullPath(path) { return join(process.cwd(), path) } -export function getFullPath(name, key, template) { - const { extension } = getFileNameFromMetadata(template[key].template) +/** + * Get full path with file name and extension + * + * @param {string} name + * @param {Template} template + * @returns {string} + */ +export function getFullPath(name, template) { + const { extension } = getFileNameFromMetadata(template.resource.template) - const fileName = `${name}.${extension}` + const fileName = template.resource.template.includes('index') + ? `index.${extension}` + : `${name}.${extension}` - const fullPath = join(template[key].path, fileName) + const fullPath = join(template.resource.path, fileName) return fullPath }