Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Advanced command #52

Merged
merged 33 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b9911b7
feat: implementing advanced command
ipetinate May 17, 2024
cb1e91d
feat: init implementing scaffold command logic
ipetinate May 20, 2024
c92e8e6
feat: implementing advanced command #wip
ipetinate May 21, 2024
b64a846
feat: generating files
ipetinate May 22, 2024
de4853f
feat: create wrapper folder
ipetinate May 22, 2024
689d7ee
fix: generate story and test
ipetinate May 22, 2024
9b43fbb
feat: add doc file
ipetinate May 22, 2024
5b5756f
fix: generate any kind of file
ipetinate May 22, 2024
05af645
feat: improve final result message
ipetinate May 22, 2024
124d2fc
feat: remove type and unecessary file
ipetinate May 22, 2024
6049a77
feat: some changes and add init templates folder
ipetinate May 22, 2024
e7df1d2
feat: add style to generated assets and update meta file template
ipetinate May 23, 2024
c8793d7
refactor: change meta DSL and generation flow
ipetinate Jun 8, 2024
91791ef
fix: interface on tests
ipetinate Jun 8, 2024
acef4fe
fix: change meta to new interface and add missing files
ipetinate Jun 21, 2024
dd57639
refactor: schema and validator to new interface
ipetinate Jun 21, 2024
7fec29c
refactor: change meta file to new interface
ipetinate Jun 21, 2024
2592a04
refactor: linting file
ipetinate Jun 21, 2024
4d94fee
refactor: change read approach to avoid specific frontend filename ta…
ipetinate Jun 21, 2024
f780e4e
feat: improve examples
ipetinate Jun 21, 2024
7b03e13
refactor: add new assets and init with examples
ipetinate Jun 21, 2024
d0f759d
feat: update guide doc
ipetinate Jun 21, 2024
e385fd2
fix: add default value for possible undefined object
ipetinate Jun 21, 2024
de1a1a0
fix: command option setup
ipetinate Jun 21, 2024
2f18e5a
fix: usage mode
ipetinate Jun 21, 2024
829f610
fix: examples path to match default meta yaml
ipetinate Jun 21, 2024
076cd5e
fix: generate new canary
ipetinate Jun 21, 2024
e6de0f9
test: fix tests
ipetinate Jun 22, 2024
4ec0e15
feat: update scaffold guide md file
ipetinate Jun 23, 2024
97f30f7
feat: update doc title
ipetinate Jun 23, 2024
1398969
fix: error message when generate examples with existent folders
ipetinate Jun 24, 2024
c6bda51
fix: wrong if wrapping logic
ipetinate Jun 24, 2024
e6917fe
fix: init command when folder already exists
ipetinate Jun 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading