Skip to content

Commit

Permalink
refactor: change read approach to avoid specific frontend filename ta…
Browse files Browse the repository at this point in the history
…rgets
  • Loading branch information
ipetinate committed Jun 21, 2024
1 parent 2592a04 commit 4d94fee
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 126 deletions.
156 changes: 37 additions & 119 deletions src/generators/custom-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { checkDirectoriesTree, createDir } from '../utils/directory.js'
/**
* @typedef {Record<keyof Omit<import('../types.js').CustomTemplate, "folderWrapper" | "identifier">, boolean>} TargetPaths
*
* @typedef {{name: string, template: import('../types.js').CustomTemplate, targets: TargetPaths}} CommomReturn
*
* @typedef {Omit<import('../types.js').CustomTemplate, "resources"> & { resource: import('../types.js').TemplateResource }} Resource
*/
Expand All @@ -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<boolean>}
* @param {import('../types.js').CustomTemplate} template Local template from meta file
* @returns {Promise<string[]>}
*/
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
Expand All @@ -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 () => {
Expand All @@ -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 }
}
}

Expand All @@ -75,38 +83,17 @@ function checkPaths(name, template) {
*
* @param {ReturnType<typeof checkPaths>} 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)
}
Expand All @@ -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 }
}

/**
Expand All @@ -170,60 +127,21 @@ function handleTemplateReplacements({
* @param {ReturnType<typeof checkPaths>} 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
}
47 changes: 40 additions & 7 deletions src/utils/scaffold-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('../types.js').CustomTemplate, 'resources'> & { resource: import('../types.js').TemplateResource }} Template
*/

/**
* Get template from meta file
*
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

0 comments on commit 4d94fee

Please sign in to comment.