Skip to content

Commit

Permalink
feat: use global config on templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetinate committed May 17, 2024
1 parent 9ba6b5e commit 65c8087
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 92 deletions.
18 changes: 18 additions & 0 deletions .clingon/presets/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"framework": "react",
"cssFramework": "css_modules",
"testFramework": "jest",
"version": null,
"name": "KebabCase",
"resourcePath": "src/components",
"testPath": "src/components",
"storyPath": "src/components",
"testPostfix": "spec",
"storyPostfix": "stories",
"type": "component",
"typescript": true,
"withStory": true,
"withTest": true,
"withTestingLibrary": true,
"folderWrapper": false
}
6 changes: 0 additions & 6 deletions clingon.config.json

This file was deleted.

14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
},
"dependencies": {
"@inquirer/prompts": "^4.3.2",
"commander": "^12.0.0",
"rxjs": "^7.8.1"
"commander": "^12.0.0"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
Expand Down
15 changes: 3 additions & 12 deletions src/actions/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@ import {
createFileIfNotExists,
createPresetFolderIfNotExists,
getConfigContent,
getConfigFilePath,
updateGlobalStore
getConfigFilePath
} from '../utils/init-action.js'

export async function initAction() {
/*
* Global Config
*/

compose(
getConfigFilePath,
createFileIfNotExists,
getConfigContent,
updateGlobalStore
)
compose(getConfigFilePath, createFileIfNotExists, getConfigContent)

/*
* Preset Folder
*/

compose(
checkIfPresetFolderAlreadyExists,
createPresetFolderIfNotExists
)
compose(checkIfPresetFolderAlreadyExists, createPresetFolderIfNotExists)
}
29 changes: 29 additions & 0 deletions src/flows/coldStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join } from 'node:path'
import { readFileContent } from '../utils/file.js'
import { getConfigContent } from '../utils/init-action.js'

export async function coldStart() {
const configPath = join(process.cwd(), 'clingon.config.json')

/**
* App data
*/
const data = {
/**
* @type {import('../types').GlobalConfig | undefined}
*/
globalConfig: undefined
}

try {
const config = getConfigContent(configPath)

if (config) {
data.globalConfig = JSON.parse(config)
}
} catch (error) {
console.error(error)
}

return data
}
52 changes: 43 additions & 9 deletions src/generators/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path, { join } from 'node:path'

import { CssFrameworkEnum, FrameworkEnum } from '../enums/frameworks.js'

import { localDirname } from '../main.js'
import { globalConfig, localDirname } from '../main.js'

import { frameworkTemplates } from '../constants/templates.js'

import { compose } from '../utils/compose.js'
Expand All @@ -16,6 +17,10 @@ import { checkDirectoriesTree, createDir } from '../utils/directory.js'
* @typedef {Record<2 | 3, "options" | "setup">} VueApi - Vue API variant options or setup (composition)
*/

/*
* Data Variables
*/

/**
* Component generator
*
Expand All @@ -32,8 +37,7 @@ export function generateComponent(answers) {
)

if (success) {
console.info('\n')
console.info('Component created successfully: ' + path)
console.info('\nComponent created successfully: ' + path)
} else {
console.error('Error on create component, try again')
}
Expand Down Expand Up @@ -77,12 +81,18 @@ export function defineComponentTemplate(data) {

switch (data.framework) {
case FrameworkEnum.react: {
templatePath = frameworkTemplates.react[variant].component.functional[data.cssFramework]
templatePath =
frameworkTemplates.react[variant].component.functional[
data.cssFramework
]

break
}
case FrameworkEnum.vue: {
templatePath = frameworkTemplates.vue[vueVersion][variant].component[api][data.cssFramework]
templatePath =
frameworkTemplates.vue[vueVersion][variant].component[api][
data.cssFramework
]

break
}
Expand Down Expand Up @@ -133,7 +143,9 @@ export function makeFolderWrapperOrBypass(data) {
data.name = convertCase('PascalCase', data.name)

const folderWrapperPath = join(data.resourcePath, data.name)
const folderWrapperExists = checkDirectoriesTree(splitPathString(folderWrapperPath))
const folderWrapperExists = checkDirectoriesTree(
splitPathString(folderWrapperPath)
)

if (data.folderWrapper && !folderWrapperExists) {
const created = createDir(folderWrapperPath)
Expand Down Expand Up @@ -197,6 +209,17 @@ export function makePathWithExtension(data) {
export function replaceAllComponentTextOccurrences(data) {
switch (data.framework) {
case FrameworkEnum.react: {
if (!data.folderWrapper) {
if (globalConfig?.exportDefault) {
console.log({ globalConfig })

data.fileContent = data.fileContent.replace(
/export function/g,
'export default function'
)
}
}

data.fileContent = data.fileContent.replace(/ResourceName/g, data.name)

return data
Expand Down Expand Up @@ -239,9 +262,20 @@ export function generateComponentFile(data) {
const success = createFileWithContent(data.pathWithFileName, data.fileContent)

if (data.folderWrapper) {
const filePath = join(data.resourcePath, data.name, 'index.' + data.extension)

createFileWithContent(filePath, `export * from './${data.name}'`)
const filePath = join(
data.resourcePath,
data.name,
'index.' + data.extension
)

if (globalConfig?.exportDefault) {
createFileWithContent(
filePath,
`import { ${data.name} } from './${data.name}'\n\nexport * from './${data.name}'\nexport default ${data.name}\n`
)
} else {
createFileWithContent(filePath, `export * from './${data.name}'`)
}
}

return { success, path }
Expand Down
11 changes: 10 additions & 1 deletion src/generators/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'

import { localDirname } from '../main.js'
import { globalConfig, localDirname } from '../main.js'
import { functionTemplates } from '../constants/templates.js'

import { compose } from '../utils/compose.js'
Expand Down Expand Up @@ -83,6 +83,15 @@ export function getTemplateContent(data) {
* @returns {() => data}
*/
export function replaceAllFunctionTextOccurrences(data) {
if (!data.folderWrapper) {
if (globalConfig?.exportDefault) {
data.fileContent = data.fileContent.replace(
/export function/g,
'export default function'
)
}
}

data.name = convertCase('camelCase', data.name)

data.fileContent = data.fileContent.replace('FunctionName', data.name)
Expand Down
20 changes: 18 additions & 2 deletions src/generators/storybook-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import path from 'node:path'

import { FrameworkEnum } from '../enums/frameworks.js'

import { localDirname } from '../main.js'
import { globalConfig, localDirname } from '../main.js'
import { storiesTemplates } from '../constants/templates.js'

import { compose } from '../utils/compose.js'
import { convertCase } from '../utils/string.js'
import { getFileExtension, removePostfixAndExt } from '../utils/file-extension.js'
import {
getFileExtension,
removePostfixAndExt
} from '../utils/file-extension.js'
import { createFileWithContent, readFileContent } from '../utils/file.js'

/**
Expand Down Expand Up @@ -157,6 +160,15 @@ export function makePathWithExtension(data) {
* }}
*/
export function replaceAllTestTextOccurrences(data) {
if (globalConfig?.exportDefault) {
const regex = new RegExp(`import { ResourceName } from`)

data.fileContent = data.fileContent.replace(
regex,
`import ResourceName from`
)
}

data.fileContent = data.fileContent.replace(/ResourceName/g, data.name)

if (data.framework === FrameworkEnum.vue) {
Expand All @@ -171,6 +183,10 @@ export function replaceAllTestTextOccurrences(data) {
data.fileContent = data.fileContent.replace(/resourcePath/g, resourcePath)
}

if (globalConfig?.alias?.src) {
data.fileContent = data.fileContent.replace(/src/g, globalConfig.alias.src)
}

return data
}

Expand Down
27 changes: 23 additions & 4 deletions src/generators/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import path from 'node:path'

import { FrameworkEnum } from '../enums/frameworks.js'

import { localDirname } from '../main.js'
import { globalConfig, localDirname } from '../main.js'
import { unitTestTemplates } from '../constants/templates.js'

import { compose } from '../utils/compose.js'
import { getFileExtension, removePostfixAndExt } from '../utils/file-extension.js'
import {
getFileExtension,
removePostfixAndExt
} from '../utils/file-extension.js'
import { capitalizeLetter, convertCase } from '../utils/string.js'
import { createFileWithContent, readFileContent } from '../utils/file.js'

Expand All @@ -29,7 +32,9 @@ export function generateTests(answers) {
)

if (success) {
console.info(capitalizeLetter(answers.testPostfix) + ' created successfully: ' + path)
console.info(
capitalizeLetter(answers.testPostfix) + ' created successfully: ' + path
)
} else {
console.error(`Error on create ${answers.testPostfix}, try again`)
}
Expand Down Expand Up @@ -70,7 +75,8 @@ export function defineTestTemplate(data) {

if (data.testFramework === 'vitest') {
if (data.withTestingLibrary) {
templatePath = unitTestTemplates.react[variant].vitestTestingLibrary
templatePath =
unitTestTemplates.react[variant].vitestTestingLibrary
} else {
templatePath = unitTestTemplates.react[variant].vitest
}
Expand Down Expand Up @@ -197,6 +203,15 @@ export function makePathWithExtension(data) {
* }}
*/
export function replaceAllTestTextOccurrences(data) {
if (globalConfig?.exportDefault) {
const regex = new RegExp(`import { ResourceName } from`)

data.fileContent = data.fileContent.replace(
regex,
`import ResourceName from`
)
}

if (['function'].includes(data.type)) {
data.name = convertCase('camelCase', data.name)
}
Expand Down Expand Up @@ -231,6 +246,10 @@ export function replaceAllTestTextOccurrences(data) {
}
}

if (globalConfig?.alias?.src) {
data.fileContent = data.fileContent.replace(/src/g, globalConfig.alias.src)
}

return data
}

Expand Down
Loading

0 comments on commit 65c8087

Please sign in to comment.