Skip to content

Commit

Permalink
feat: add plugins starter
Browse files Browse the repository at this point in the history
  • Loading branch information
JPDovale committed Feb 5, 2024
1 parent f10019a commit d88c180
Show file tree
Hide file tree
Showing 28 changed files with 655 additions and 132 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@rubykgen/eslint-config/node"
]
}
6 changes: 6 additions & 0 deletions packages/eslint-config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rubykgen/eslint-config

## 0.0.4

### Patch Changes

- Add plugin starters

## 0.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@rubykgen/eslint-config",
"private": false,
"description": "A eslint-config for Rubyk",
"version": "0.0.3",
"version": "0.0.4",
"main": "index.js",
"license": "ISC",
"repository": {
Expand Down
2 changes: 0 additions & 2 deletions packages/rubyk-cli/.eslintignore

This file was deleted.

4 changes: 3 additions & 1 deletion packages/rubyk-cli/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": ["@rubykgen/eslint-config/node"]
"extends": [
"@rubykgen/eslint-config/node"
]
}
6 changes: 6 additions & 0 deletions packages/rubyk-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rubykgen/rubyk-cli

## 1.1.0

### Minor Changes

- Add plugin starters

## 1.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/rubyk-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rubykgen/rubyk-cli",
"version": "1.0.4",
"version": "1.1.0",
"description": "File generator CLI for typescript backend projects",
"types": "build/types/types.d.ts",
"bin": {
Expand Down
7 changes: 3 additions & 4 deletions packages/rubyk-cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { build } from 'gluegun'
import rubyk from './commands/Rubyk'
import rubyk from './commands/Rubyk'
import { Command } from 'gluegun/build/types/domain/command'

async function run(argv) {
Expand All @@ -14,13 +14,12 @@ async function run(argv) {
.version()
.create()


cli.defaultCommand = rubyk as Command
const toolbox = await cli.run(argv)

return toolbox
}

module.exports = {
run,
module.exports = {
run,
}
18 changes: 11 additions & 7 deletions packages/rubyk-cli/src/commands/Generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GluegunToolbox } from "gluegun";
import { Config } from "../modules/config/entities/Config";
import { GluegunToolbox } from 'gluegun'
import { Config } from '../modules/config/entities/Config'

module.exports = {
name: 'generate',
Expand All @@ -12,7 +12,7 @@ module.exports = {
readPluralModuleName,
generateFile,
parameters,
print: { error, success }
print: { error, success },
} = toolbox

if (!parameters.first) {
Expand All @@ -23,10 +23,14 @@ module.exports = {
const moduleName = await readModuleName()
const pluralModuleName = await readPluralModuleName()
const fileName = await readFileName()
const config = await makeConfig(moduleName, pluralModuleName, fileName) as Config
const config = (await makeConfig(
moduleName,
pluralModuleName,
fileName,
)) as Config

await generateFile(config, parameters.first)

success("Done! You ready ^^")
}
}
success('Done! You ready ^^')
},
}
33 changes: 20 additions & 13 deletions packages/rubyk-cli/src/commands/MultiGenerate.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { GluegunToolbox } from "gluegun";
import { Config } from "../modules/config/entities/Config";
import { GluegunToolbox } from 'gluegun'
import { Config } from '../modules/config/entities/Config'

module.exports = {
name: 'multi-generate',
alias: ['mgen', 'mg'],
run: async (toolbox: GluegunToolbox) => {
const {
makeConfig,
const {
makeConfig,
readModuleName,
readFileName,
readPluralModuleName,
generateFile,
parameters ,
print: { error, success }
parameters,
print: { error, success },
} = toolbox

const moduleName = await readModuleName()
const pluralModuleName = await readPluralModuleName()
const fileName = await readFileName()

const config = await makeConfig(moduleName, pluralModuleName, fileName) as Config
const config = (await makeConfig(
moduleName,
pluralModuleName,
fileName,
)) as Config

const _generatorsName = parameters.options['g'] ?? parameters.options['generators']
if(!_generatorsName) {
error('Generators name is missing! Please specify it with --g or --generators')
const _generatorsName =
parameters.options.g ?? parameters.options.generators
if (!_generatorsName) {
error(
'Generators name is missing! Please specify it with --g or --generators',
)
process.exit(1)
}

Expand All @@ -33,6 +40,6 @@ module.exports = {
await generateFile(config, gen)
}

success("Done! You ready ^^")
}
}
success('Done! You ready ^^')
},
}
69 changes: 36 additions & 33 deletions packages/rubyk-cli/src/commands/Rubyk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GluegunToolbox } from "gluegun";
import { Config, Module } from "../modules/config/entities/Config";
import { Config as ConfigType } from "src/types";

import { GluegunToolbox } from 'gluegun'
import { Config, Module } from '../modules/config/entities/Config'
import { Config as ConfigType } from 'src/types'

const rubyk = {
name: 'default',
Expand All @@ -11,95 +10,99 @@ const rubyk = {
readConfigFile,
makeConfig,
generateFile,
prompt: {
ask,
},
print: { error, spin, info }
} = toolbox
prompt: { ask },
print: { error, spin, info },
} = toolbox

const configPath = await readConfigFile()
const ConfigFile = await import(configPath) as ConfigType
const ConfigFile = (await import(configPath)) as ConfigType

const modules: Module[] = ConfigFile.modules.map((module) => {
if(module instanceof Array) {
if (module instanceof Array) {
return {
name: module[0],
plural: module[1]
plural: module[1],
}
}

return {
name: module,
plural: `${module}s`
plural: `${module}s`,
}
})

const config = await makeConfig('user', 'users', undefined, configPath) as Config
const config = (await makeConfig(
'user',
'users',
undefined,
configPath,
)) as Config

const res = await ask([
{
message: 'Please select multiple modules for which you wish to generate files. Choose as needed and proceed.',
message:
'Please select multiple modules for which you wish to generate files. Choose as needed and proceed.',
name: 'modules',
type: 'multiselect',
align: 'left',
choices: modules.map(m => m.name),
choices: modules.map((m) => m.name),
},
{
message: 'Please select the generators you wish to use. Choose as needed and continue with the process.',
message:
'Please select the generators you wish to use. Choose as needed and continue with the process.',
name: 'generators',
type: 'multiselect',
choices: config.generators.map(m => ({
choices: config.generators.map((m) => ({
name: m.type,
value: m.alias ?? m.type,
})),
},
{
message: 'Set you file name',
name: 'file',
type: 'input'
}
type: 'input',
},
]).catch(() => {
info('Cancelled')
info('Cancelled')
process.exit(1)
})
if(!res.modules || res.modules.length === 0) {

if (!res.modules || res.modules.length === 0) {
error('Modules are required')
process.exit(1)
}


if(!res.generators || res.generators.length === 0) {
if (!res.generators || res.generators.length === 0) {
error('Generators are required')
process.exit(1)
}
if(!res.file) {

if (!res.file) {
error('File name is required')
process.exit(1)
}

const sp = spin('Generating...')

for (const m of res.modules) {
const module = modules.find(mod => mod.name === m)
const module = modules.find((mod) => mod.name === m)

for (const gen of res.generators) {
const generator = config.findGenerator(gen)

if(!generator) {
if (!generator) {
error(`Generator not found ${gen}`)
}

const conf = await makeConfig(m, module.plural, res.file, configPath)
const conf = await makeConfig(m, module.plural, res.file, configPath)

await generateFile(conf, gen)
}
}

sp.succeed('Done! You ready ^^')
process.exit(0)
}
},
}

export default rubyk
export default rubyk
35 changes: 35 additions & 0 deletions packages/rubyk-cli/src/commands/StartPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GluegunToolbox } from 'gluegun'
import { Config } from '../modules/config/entities/Config'

module.exports = {
name: 'startPlugins',
alias: ['srtp', 'sp'],
run: async (toolbox: GluegunToolbox) => {
const {
makeConfig,
createFile,
print: { error, spin },
} = toolbox

const config = (await makeConfig('any', 'anys')) as Config

const starters = config.options?.starters ?? []

if (starters.length <= 0) {
error('No one starter provide by anyone plugin.')
process.exit(1)
}

const sp = spin('Coping starter files...')

for (const starter of starters) {
const folder = starter.path.split('/').slice(0, -1).join('/')
const fileName = starter.path.split('/').slice(-1)[0]

await createFile(folder, fileName, starter.template)
}

sp.succeed('Starter files copied!')
process.exit(0)
},
}
17 changes: 11 additions & 6 deletions packages/rubyk-cli/src/extensions/CreateFile.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { GluegunToolbox } from "gluegun";
import { GluegunToolbox } from 'gluegun'

module.exports = (toolbox: GluegunToolbox) => {
const { template, filesystem } = toolbox

async function createFile(folder: string, fileName: string, content: string, contentProps?: {[x: string]: any}) {
async function createFile(
folder: string,
fileName: string,
content: string,
contentProps?: { [x: string]: unknown },
) {
await template.generate({
directory: filesystem.path(
filesystem.cwd(),
'node_modules/@rubykgen/rubyk-cli/build/templates/src/templates',
filesystem.cwd(),
'node_modules/@rubykgen/rubyk-cli/build/templates/src/templates',
),
template: content,
target: `${folder}/${fileName}`,
props: contentProps || {}
props: contentProps || {},
})
}

toolbox.createFile = createFile
}
}
Loading

0 comments on commit d88c180

Please sign in to comment.