Skip to content

Commit

Permalink
Merge pull request #56 from jeffersondanielss/feat/keep-template-file…
Browse files Browse the repository at this point in the history
…-name

feat(Scaffold): keep template file name
  • Loading branch information
ipetinate authored Feb 14, 2025
2 parents 4ab3364 + 2b88562 commit 97c5190
Show file tree
Hide file tree
Showing 6 changed files with 2,298 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/actions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export async function createAction(resourceName, options) {

if (!preset && options.framework) {
if (!options.type || !options.framework || !options.path) {
throw new Error('You did not pass any of the mandatory parameters for Options Mode')
throw new Error(
'You did not pass any of the mandatory parameters for Options Mode'
)
}

let storyPath = options.path
Expand Down Expand Up @@ -68,6 +70,7 @@ export async function createAction(resourceName, options) {
withTestingLibrary: options.testingLibrary,
version: options.vueVersion,
folderWrapper: options.folderWrapper,
keepTemplateName: options.keepTemplateName,
storyPostfix: 'stories',
testPath,
storyPath,
Expand Down Expand Up @@ -130,6 +133,8 @@ export async function createAction(resourceName, options) {
if (answers) {
await mainGenerator(answers)
} else {
console.error('Error: an error has ocurred when retrieve answers, try again')
console.error(
'Error: an error has ocurred when retrieve answers, try again'
)
}
}
20 changes: 19 additions & 1 deletion src/generators/custom-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function buildCustomTemplate(name, template) {
const resourceData = {
identifier: template.identifier,
folderWrapper: template.folderWrapper,
keepTemplateName: template.keepTemplateName,
case: template.case,
resource
}
Expand Down Expand Up @@ -121,6 +122,19 @@ function handleTemplateReplacements({
return { name, template, templateContent, target }
}

/**
* Get template file name
*
* @param {Resource} template Template data from meta file
* @returns {string}
*/
function getTemplateFileName(template) {
const pathArray = template.resource.template.split('/')
const fileName = pathArray[pathArray.length - 1]

return fileName.split('.')[0]
}

/**
* Create files after processing
*
Expand All @@ -137,7 +151,11 @@ function createResources({ name, target, template, templateContent }) {
if (template.folderWrapper)
template.resource.path = join(template.resource?.path, name)

const fullPath = getFullPath(name, template)
const templateFileName = getTemplateFileName(template)

const fullPath = template.keepTemplateName
? getFullPath(templateFileName, template)
: getFullPath(name, template)
const resourceCreated = createFileWithContent(fullPath, templateContent)

if (resourceCreated) created = fullPath
Expand Down
25 changes: 25 additions & 0 deletions src/generators/custom-template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ describe('scaffoldTemplate', () => {
assert.deepEqual(result, ['controllers/test_controller/test_controller.rs'])
})

it('build a rust file and keep the original template name', async () => {
mockStatSync.mock.mockImplementation(() => ({
isDirectory: () => true
}))
mockExistsSync.mock.mockImplementation(() => true)
mockFsAccessSync.mock.mockImplementation(() => true)
mockFsReadFileSync.mock.mockImplementation(() => 'mock file content')
mockFsWriteFileSync.mock.mockImplementation(() => true)

const result = await buildCustomTemplate('TestController', {
identifier: 'component',
case: 'snake_case',
folderWrapper: false,
keepTemplateName: true,
resources: [
{
path: 'controllers',
template: './controllers/user_controller.rs'
}
]
})

assert.deepEqual(result, ['controllers/user_controller.rs'])
})

it('build a rust file without folder wrapper', async () => {
mockStatSync.mock.mockImplementation(() => ({
isDirectory: () => true
Expand Down
2 changes: 2 additions & 0 deletions src/templates/core/SCAFFOLD_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The table below describes the fields used in the resource definitions in the `me
| -------------------- | ------------ | -------- | --------------------------------------------------------------------------------------- |
| `identifier` | `string` | Yes | Unique identifier for the resource (used on `scaffold` flag `--template` as tag value). |
| `folderWrapper` | `boolean` | No | Indicates if the resource should be wrapped by a folder. |
| `keepTemplateName` | `boolean` | No | Use the template file name when generate files. |
| `case` | `Case` | No | Indicates if the resource should be wrapped by a folder. |
| `resources` | `Resource[]` | Yes | List of resources to be created. |
| `resources.path` | `string` | Yes | Path target directory where the resource will be generated. |
Expand All @@ -48,6 +49,7 @@ interface Resource {
interface Meta {
identifier: string
folderWrapper?: boolean
keepTemplateName?: boolean
case?: Case
resources: Resource[]
}
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import {
* identifier: string
* case?: TextCase
* folderWrapper?: boolean
* keepTemplateName?: boolean
* resources: TemplateResource[]
* }} CustomTemplate - Custom template from meta file
*
Expand Down
Loading

0 comments on commit 97c5190

Please sign in to comment.