Skip to content

Commit

Permalink
fix: generate any kind of file
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetinate committed May 22, 2024
1 parent 9b43fbb commit 5b5756f
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .clingon/templates/docs/ReactHookDoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ResourceName

> My hok doc
- Test generate with MD file
7 changes: 7 additions & 0 deletions .clingon/templates/functions/AsyncFunction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('ResourceName', () => {
it("work's properly", async () => {
const result = await ResourceName()

expect(result).toBeTruthy()
})
})
5 changes: 5 additions & 0 deletions .clingon/templates/functions/AsyncFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Props = {}

export async function ResourceName(props: Props) {
return new Promise<boolean>((resolve) => resolve(true))
}
9 changes: 6 additions & 3 deletions .clingon/templates/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- identifier: component
folderWrapper: true
case: 'PascalCase'
resource:
path: 'src/components'
template: './ReactComponent/Component.tsx'
Expand All @@ -11,17 +12,19 @@
template: './ReactComponent/Component.stories.tsx'

- identifier: async-function
case: 'kebab-case'
resource:
path: src/utils
template: './Functions/AsyncFunction.ts'
template: './functions/AsyncFunction.ts'
test:
path: src/utils
template: './Functions/AsyncFunction.spec.ts'
template: './functions/AsyncFunction.spec.ts'

- identifier: markdown
case: 'UPPERCASE'
resource:
path: src/docs
template: './Docs/ReactHookDoc.md'
template: './docs/ReactHookDoc.md'

- identifier: nvmrc
resource:
Expand Down
76 changes: 42 additions & 34 deletions src/generators/scaffold-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,33 @@ function checkPaths(name, template) {
story: false
}

const resourcePath = template.folderWrapper
? join(template.resource.path, name)
: template.resource.path
const storyPath = template.folderWrapper
? join(template.story.path, name)
: template.story.path
const testPath = template.folderWrapper
? join(template.test.path, name)
: template.test.path
const resourcePath = template?.folderWrapper
? join(template?.resource?.path, name)
: template?.resource?.path

const storyPath = template?.folderWrapper
? join(template?.story?.path, name)
: template?.story?.path

const testPath = template?.folderWrapper
? join(template?.test?.path, name)
: template?.test?.path

targets.resource = checkDirectoriesTree(splitPathString(resourcePath))

if (!targets.resource) {
if (!targets?.resource) {
targets.resource = createDir(getTargetFullPath(resourcePath))
}

if (template.test) {
if (template?.test) {
targets.test = checkDirectoriesTree(splitPathString(testPath))

if (!targets.test) {
targets.resource = createDir(getTargetFullPath(testPath))
}
}

if (template.story) {
if (template?.story) {
targets.story = checkDirectoriesTree(splitPathString(storyPath))

if (!targets.story) {
Expand Down Expand Up @@ -105,18 +107,18 @@ function getTemplatesData({ name, template, targets }) {

try {
templatesContent.resource = readFileContent(
getTemplateFullPath(template.resource.template)
getTemplateFullPath(template?.resource?.template)
)

if (template.test) {
if (template?.test) {
templatesContent.test = readFileContent(
getTemplateFullPath(template.test.template)
getTemplateFullPath(template?.test?.template)
)
}

if (template.story) {
if (template?.story) {
templatesContent.story = readFileContent(
getTemplateFullPath(template.story.template)
getTemplateFullPath(template?.story?.template)
)
}

Expand All @@ -137,27 +139,33 @@ function handleTemplateReplacements({
templatesContent,
targets
}) {
name = convertCase('PascalCase', name)
if (!template.case) {
return { name, template, templatesContent, targets }
}

templatesContent.resource = templatesContent.resource.replace(
name = convertCase(template.case, name)

templatesContent.resource = templatesContent.resource?.replace(
/ResourceName/g,
name
)

if (templatesContent.story) {
templatesContent.story = replaceContentFromSideResource(
name,
templatesContent.story,
template
)
}

if (templatesContent.test) {
if (templatesContent?.test) {
templatesContent.test = replaceContentFromSideResource(
name,
templatesContent.test,
'test',
template
)

if (templatesContent?.story) {
templatesContent.story = replaceContentFromSideResource(
name,
templatesContent.story,
'story',
template
)
}
}

return { name, template, templatesContent, targets }
Expand All @@ -177,7 +185,7 @@ function createResources({ name, targets, template, templatesContent }) {

if (targets.resource) {
if (template.folderWrapper)
template.resource.path = join(template.resource.path, name)
template.resource.path = join(template.resource?.path, name)

const fullPath = getFullPath(name, 'resource', template)

Expand All @@ -187,22 +195,22 @@ function createResources({ name, targets, template, templatesContent }) {
)
}

if (targets.test) {
if (targets?.test) {
if (template.folderWrapper)
template.test.path = join(template.test.path, name)
template.test.path = join(template?.test?.path, name)

const fullPath = getFullPath(name, 'test', template)

created.test = createFileWithContent(fullPath, templatesContent.test)
}

if (targets.story) {
if (targets?.story) {
if (template.folderWrapper)
template.story.path = join(template.story.path, name)
template.story.path = join(template?.story?.path, name)

const fullPath = getFullPath(name, 'story', template)

created.story = createFileWithContent(fullPath, templatesContent.story)
created.story = createFileWithContent(fullPath, templatesContent?.story)
}

return {
Expand Down
12 changes: 2 additions & 10 deletions src/schemas/custom-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ export const templateResourceTypeMap = {
*/
export const customTemplateTypeMap = {
identifier: 'string',
folderWrapper: 'boolean',
resource: templateResourceTypeMap,
test: {
path: 'string',
template: 'string'
},
story: {
path: 'string',
template: 'string'
}
case: 'string',
resource: templateResourceTypeMap
}
5 changes: 4 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,17 @@ import {
*
* @typedef {"class" | "functional"} ReactComponentVariant - React component type variant (class or functional)
*
* @typedef {"camelCase" | "PascalCase" | "snake_case" | "kebab-case" | "CamelCase" | "UPPERCASE" | "lowercase"} TextCase - Case to convert strings
*
* @typedef {{
* path: string
* template: string
* }} TemplateResource - Template resource definition
*
* @typedef {{
* identifier: string
* folderWrapper: boolean
* case?: TextCase
* folderWrapper?: boolean
* resource: TemplateResource
* test?: TemplateResource
* story?: TemplateResource
Expand Down
8 changes: 4 additions & 4 deletions src/utils/scaffold-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function getMetaFilePath() {
* @returns {boolean} - Returns true if the template is valid, false otherwise.
*/
export function validateTemplate(template) {
return validateObject(template, customTemplateTypeMap)
return validateObject(template, customTemplateTypeMap, true)
}

/**
Expand Down Expand Up @@ -109,14 +109,14 @@ export function getLastItem(text, pattern) {
return pieces[pieces.length - 1]
}

export function replaceContentFromSideResource(name, content, template) {
export function replaceContentFromSideResource(name, content, key, template) {
const resourceNameReplaced = content.replace(/ResourceName/g, name)

if (resourceNameReplaced) content = resourceNameReplaced

const { extension } = getFileNameFromMetadata(template.story.template)
const { extension } = getFileNameFromMetadata(template[key].template)

const fullPath = join(template.story.path, `${name}.${extension}`)
const fullPath = join(template[key].path, `${name}.${extension}`)

content = replaceResourcePath(fullPath, content)

Expand Down
30 changes: 27 additions & 3 deletions src/utils/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ export function boolAsText(bool) {
/**
* Convert a specific case to target case, e.g "MyResource" to "my_resource"
*
* @param {"camelCase" | "snake_case" | "PascalCase" | "kebab-case"} targetPattern Case target
* @param {import("../types").TextCase} targetPattern Case target
* @param {string} inputString - String to be converted
* @returns {string}
*/
export function convertCase(targetPattern, inputString) {
switch (targetPattern) {
case 'PascalCase':
return convertToPascalCase(inputString)
case 'camelCase':
return convertToCamelCase(inputString)
case 'snake_case':
return convertToSnakeCase(inputString)
case 'PascalCase':
return convertToPascalCase(inputString)
case 'kebab-case':
return convertToKebabCase(inputString)
case 'UPPERCASE':
return convertToUpperCase(inputString)
case 'lowercase':
return convertToLowerCase(inputString)
default:
throw new Error('Invalid target pattern.')
}
Expand All @@ -64,6 +68,26 @@ export function convertToCamelCase(inputString) {
return words.join('')
}

/**
* Convert a string on any case to a UPPERCASE
*
* @param {string} inputString String to be converted
* @returns {string}
*/
export function convertToUpperCase(inputString) {
return inputString.toUpperCase()
}

/**
* Convert a string on any case to a lowercase
*
* @param {string} inputString String to be converted
* @returns {string}
*/
export function convertToLowerCase(inputString) {
return inputString.toLowerCase()
}

/**
* Convert a string on any case to a snake_case
*
Expand Down
45 changes: 41 additions & 4 deletions src/utils/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
convertToPascalCase,
convertToKebabCase,
convertToSnakeCase,
capitalizeLetter
capitalizeLetter,
convertToLowerCase,
convertToUpperCase
} from './string.js'

describe('String Utils', async () => {
Expand Down Expand Up @@ -81,6 +83,18 @@ describe('String Utils', async () => {

assert.strictEqual(result, 'PascalCaseString')
})

it('convert a string case: kebab-case -> lowercase', () => {
const result = convertCase('lowercase', 'PascalCaseString')

assert.strictEqual(result, 'pascalcasestring')
})

it('convert a string case: kebab-case -> UPPERCASE', () => {
const result = convertCase('UPPERCASE', 'PascalCaseString')

assert.strictEqual(result, 'PASCALCASESTRING')
})
})

describe('convert case individual utils', () => {
Expand All @@ -92,9 +106,18 @@ describe('String Utils', async () => {
})

it('convertToPascalCase', () => {
assert.strictEqual(convertToPascalCase('kebab-to-pascal'), 'KebabToPascal')
assert.strictEqual(convertToPascalCase('snake_to_pascal'), 'SnakeToPascal')
assert.strictEqual(convertToPascalCase('PascalToPascal'), 'PascalToPascal')
assert.strictEqual(
convertToPascalCase('kebab-to-pascal'),
'KebabToPascal'
)
assert.strictEqual(
convertToPascalCase('snake_to_pascal'),
'SnakeToPascal'
)
assert.strictEqual(
convertToPascalCase('PascalToPascal'),
'PascalToPascal'
)
assert.strictEqual(convertToPascalCase('camelToPascal'), 'CamelToPascal')
})

Expand All @@ -111,6 +134,20 @@ describe('String Utils', async () => {
assert.strictEqual(convertToSnakeCase('PascalToSnake'), 'pascal_to_snake')
assert.strictEqual(convertToSnakeCase('camelToSnake'), 'camel_to_snake')
})

it('convertToUpperCase', () => {
assert.strictEqual(convertToUpperCase('kebab-to-upper'), 'KEBAB-TO-UPPER')
assert.strictEqual(convertToUpperCase('snake_to_upper'), 'SNAKE_TO_UPPER')
assert.strictEqual(convertToUpperCase('PascalToUpper'), 'PASCALTOUPPER')
assert.strictEqual(convertToUpperCase('camelToUpper'), 'CAMELTOUPPER')
})

it('convertToLowerCase', () => {
assert.strictEqual(convertToLowerCase('kebab-to-lower'), 'kebab-to-lower')
assert.strictEqual(convertToLowerCase('snake_to_lower'), 'snake_to_lower')
assert.strictEqual(convertToLowerCase('PascalToLower'), 'pascaltolower')
assert.strictEqual(convertToLowerCase('camelToLower'), 'cameltolower')
})
})

describe('capitalizeLetter', () => {
Expand Down

0 comments on commit 5b5756f

Please sign in to comment.