Skip to content

Commit

Permalink
Merge pull request #52 from ipetinate/feat/advanced-command
Browse files Browse the repository at this point in the history
feat: Advanced command
  • Loading branch information
ipetinate authored Jun 24, 2024
2 parents d19124c + e6917fe commit b4b2983
Show file tree
Hide file tree
Showing 41 changed files with 1,697 additions and 130 deletions.
7 changes: 7 additions & 0 deletions doc/SCAFFOLD_COMMAND.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Scaffold Command

> Generate resources with local templates
- [Flow and process steps](https://excalidraw.com/#json=Ucl3J2Z61I3fx9JPVEkVV,4Ble4pYYHnbcr1nkK3IOHw)

![Scaffold Code Flow](./img/scaffold-flow.svg)
21 changes: 21 additions & 0 deletions doc/img/scaffold-flow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MD046: fenced
24 changes: 18 additions & 6 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"dependencies": {
"@inquirer/prompts": "^4.3.2",
"commander": "^12.0.0"
"commander": "^12.0.0",
"yaml": "^2.4.2"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-templates-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ function generateDocumentation() {

generateDocumentation()

console.log('Documentação gerada com sucesso!')
console.info('📚 Documentação gerada com sucesso!')
30 changes: 26 additions & 4 deletions src/actions/init.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import { compose } from '../utils/compose.js'
import {
checkIfPresetFolderAlreadyExists,
checkIfTemplateFolderAlreadyExists,
createFileIfNotExists,
createPresetFolderIfNotExists,
getConfigContent,
createPresetsFolderAssets,
createTemplateFolderAssets,
createTemplateFolderIfNotExists,
getConfigFilePath
} from '../utils/init-action.js'

export async function initAction() {
/**
* Init clingon assets, generate necessary files and folders.
*
* @param {Record<"examples", boolean>} options Command options with flags, like `--e`
*/
export async function initAction(options = { examples: false }) {
/*
* Global Config
*/

compose(getConfigFilePath, createFileIfNotExists, getConfigContent)
compose(getConfigFilePath(options?.examples), createFileIfNotExists)

/*
* Preset Folder
*/

compose(checkIfPresetFolderAlreadyExists, createPresetFolderIfNotExists)
compose(
checkIfPresetFolderAlreadyExists(options?.examples),
createPresetFolderIfNotExists,
createPresetsFolderAssets
)

/*
* Templates Folder
*/

compose(
checkIfTemplateFolderAlreadyExists(options?.examples),
createTemplateFolderIfNotExists,
createTemplateFolderAssets
)
}
65 changes: 65 additions & 0 deletions src/actions/scaffold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { join } from 'node:path'

import { buildCustomTemplate } from '../generators/custom-template.js'

import {
getTemplateFromMetaFile,
validateTemplate
} from '../utils/scaffold-action.js'

/**
* Build resources from local custom templates
*
* @typedef {"template"} Options
*
* @param {string} name
* @param {Record<Options, string>} options
*/

export async function scaffoldAction(name, options) {
/**
* Templates folder path
*/
const basePath = join(process.cwd(), '.clingon', 'templates')

/**
* Templates from meta file
*/
const template = getTemplateFromMetaFile(options.template)

/**
* Template already be validated and flow can continue
*/
const validationErrors = validateTemplate(template)

if (validationErrors.length > 0) {
console.error(
`\n⎡ Template has many errors, review your meta file at: \n⎪\n⎣ → ${basePath}`
)

console.error(`\n⎡ Validation errors: \n⎪`)

const last = validationErrors.length - 1

validationErrors.forEach((error, index) =>
console.error(`${last === index ? '⎣' : '⎪'}${error}`)
)

return
}

/**
* Resources already be created
*
* @type {Record<"resource" | "test" | "story" | "style", string>}
*/
const paths = await buildCustomTemplate(name, template)

if (paths) showCreatedResources(paths)
}

export function showCreatedResources(paths) {
console.info('⎧ 💿 Files successfully created at:\n⎪')

paths.forEach((path) => console.info('⎪⎯→ ' + path))
}
15 changes: 0 additions & 15 deletions src/constants/config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
export const defaultConfig = {
/**
* Alias for text ocurrences replacement
*
* 🚨 Be careful, do not replace "resourcePath" or "ResourceName", to avoid generating strange behavior in the templates,
* causing auto-completion to be unconfigured
*/
alias: {
/**
* Will replace all `src` occurrences on templates to `@`, Example: `src/components/...` become `@/components/...`
*/
src: '@'
},
/**
* If `true` will default export functions, components, pages, etc. Example:
*/
exportDefault: false
}
44 changes: 44 additions & 0 deletions src/constants/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const templateCoreFiles = [
{
folder: 'templates/core/functions',
target: 'functions',
files: ['AsyncFunction.ts', 'AsyncFunction.spec.ts']
},
{
folder: 'templates/core/markdown',
target: 'docs',
files: ['HookDoc.md']
},
{
folder: 'templates/core/react-component',
target: 'components/react-component',
files: [
'index.tsx',
'Component.tsx',
'Component.test.tsx',
'Component.styles.css',
'Component.stories.tsx'
]
},
{
folder: 'templates/core',
target: '',
files: ['meta.yaml', 'SCAFFOLD_GUIDE.md']
}
]

export const presetsCoreFiles = [
{
folder: 'templates/core',
target: '',
files: ['PRESETS_GUIDE.md', 'function-preset.json']
}
]

export const globalCoreFiles = [
{
folder: 'templates/core',
target: '',
files: ['clingon.config.json']
}
]
12 changes: 9 additions & 3 deletions src/flows/coldStart.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from 'node:path'
import { readFileContent } from '../utils/file.js'
import { getConfigContent } from '../utils/init-action.js'
import { checkFileExists, readFileContent } from '../utils/file.js'

export async function coldStart() {
const configPath = join(process.cwd(), 'clingon.config.json')
Expand All @@ -16,7 +15,14 @@ export async function coldStart() {
}

try {
data.globalConfig = getConfigContent(configPath)
const exists = checkFileExists(configPath)

if (!exists) return { globalConfig: null }

const fileContent = readFileContent(configPath)
const fileContentParsed = JSON.parse(fileContent)

data.globalConfig = fileContentParsed
} catch (error) {
console.error(error)
}
Expand Down
2 changes: 0 additions & 2 deletions src/generators/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ export function replaceAllComponentTextOccurrences(data) {
case FrameworkEnum.react: {
if (!data.folderWrapper) {
if (globalConfig?.exportDefault) {
console.log({ globalConfig })

data.fileContent = data.fileContent.replace(
/export function/g,
'export default function'
Expand Down
Loading

0 comments on commit b4b2983

Please sign in to comment.