Skip to content

Commit

Permalink
feat: isolated the types creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Apr 16, 2022
1 parent fd1a910 commit 09a655f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 73 deletions.
5 changes: 5 additions & 0 deletions lib/src/functions/api/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const writeFile = require('../../utils/writeFile')

/**
* @param {Object} args
* @param {Boolean|undefined} args.mongo
* @param {String} args.projectName
*/
module.exports = async ({ mongo = true, projectName }) => {
const createFoldersCommand = `mkdir ${projectName}/src/database \
${projectName}/src/database/mongo \
Expand Down
81 changes: 8 additions & 73 deletions lib/src/functions/api/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const os = require('os')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const writeFile = require('../../utils/writeFile')

const database = require('./database')
const schemas = require('./schemas')
const services = require('./services')
const types = require('./types')
const writeFile = require('../../utils/writeFile')

/*
* Express api:
Expand Down Expand Up @@ -116,29 +118,6 @@ module.exports = async ({
mongo = true
}) => {
const data = {
'@types': {
index: {
content: `/* eslint-disable no-var */
declare global {}
export {}
`,
file: `${projectName}/src/@types/index.d.ts`
}
},
'@types/models': {
user: {
content: `interface UserDBO {
id: string
name: string
lastName: string
createdAt: Date
updatedAt: Date
}
`,
file: `${projectName}/src/@types/models/user.d.ts`
}
},
network: {
index: {
content: `export * from './routes'
Expand Down Expand Up @@ -203,33 +182,6 @@ Server.start()
}
}
const expressData = {
'@types/custom': {
request: {
content: `type ExpressRequest = import('express').Request
interface CustomRequest extends ExpressRequest {
body: {
args?: import('schemas').UserDTO
}
// We can add custom headers via intersection, remember that for some reason
// headers must be in Snake-Pascal-Case
headers: import('http').IncomingHttpHeaders & {
'Custom-Header'?: string
}
}
`,
file: `${projectName}/src/@types/custom/request.d.ts`
},
response: {
content: `type ExpressResponse = import('express').Response
interface CustomResponse extends ExpressResponse {
newValue?: string
}
`,
file: `${projectName}/src/@types/custom/response.d.ts`
}
},
network: {
response: {
content: `interface ResponseProps {
Expand Down Expand Up @@ -1491,14 +1443,11 @@ export { validatorCompiler }
}

const expressFolders = `${projectName}/src/utils \
${projectName}/src/@types/custom \
${projectName}/src/network/routes/utils`

const fastifyFolders = `${projectName}/src/network/utils`

const createFoldersCommands = `mkdir ${projectName}/src \
${projectName}/src/@types \
${projectName}/src/@types/models \
${projectName}/src/network \
${projectName}/src/network/routes \
${fastify ? `${fastifyFolders}` : `${expressFolders}`}
Expand All @@ -1508,6 +1457,11 @@ ${fastify ? `${fastifyFolders}` : `${expressFolders}`}
await exec(createFoldersCommands.replaceAll('/', '\\'))
else await exec(createFoldersCommands)

// /@types
types({
express: !fastify,
projectName
})
// /database
database({
mongo,
Expand All @@ -1518,15 +1472,6 @@ ${fastify ? `${fastifyFolders}` : `${expressFolders}`}
// /services
services({ projectName })

// /@types
await writeFile(data['@types'].index.file, data['@types'].index.content)

// /@types/models
await writeFile(
data['@types/models'].user.file,
data['@types/models'].user.content
)

// /network
await writeFile(data.network.index.file, data.network.index.content)

Expand Down Expand Up @@ -1578,16 +1523,6 @@ ${fastify ? `${fastifyFolders}` : `${expressFolders}`}
fastifyData['network/utils'].index.content
)
} else {
// /@types/custom
await writeFile(
expressData['@types/custom'].request.file,
expressData['@types/custom'].request.content
)
await writeFile(
expressData['@types/custom'].response.file,
expressData['@types/custom'].response.content
)

// /network
await writeFile(
expressData.network.response.file,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/functions/api/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const writeFile = require('../../utils/writeFile')

/**
* @param {Object} args
* @param {String} args.projectName
*/
module.exports = async ({ projectName }) => {
const createFoldersCommand = `mkdir ${projectName}/src/schemas`

Expand Down
4 changes: 4 additions & 0 deletions lib/src/functions/api/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const writeFile = require('../../utils/writeFile')

/**
* @param {Object} args
* @param {String} args.projectName
*/
module.exports = async ({ projectName }) => {
const createFoldersCommand = `mkdir ${projectName}/src/services \
${projectName}/src/services/utils \
Expand Down
75 changes: 75 additions & 0 deletions lib/src/functions/api/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { platform } = require('os')
const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const writeFile = require('../../utils/writeFile')

module.exports = async ({ express, projectName }) => {
const createFoldersCommand = `mkdir ${projectName}/src/@types \
${projectName}/src/@types/models \
${express ? `${projectName}/src/@types/custom` : ''}`

if (platform() === 'win32')
await exec(createFoldersCommand.replaceAll('/', '\\'))
else await exec(createFoldersCommand)

const types = {
index: {
content: `/* eslint-disable no-var */
declare global {}
export {}
`,
file: `${projectName}/src/@types/index.d.ts`
},
models: {
user: {
content: `interface UserDBO {
id: string
name: string
lastName: string
createdAt: Date
updatedAt: Date
}
`,
file: `${projectName}/src/@types/models/user.d.ts`
}
},
...(express && {
custom: {
request: {
content: `type ExpressRequest = import('express').Request
interface CustomRequest extends ExpressRequest {
body: {
args?: import('schemas').UserDTO
}
// We can add custom headers via intersection, remember that for some reason
// headers must be in Snake-Pascal-Case
headers: import('http').IncomingHttpHeaders & {
'Custom-Header'?: string
}
}
`,
file: `${projectName}/src/@types/custom/request.d.ts`
},
response: {
content: `type ExpressResponse = import('express').Response
interface CustomResponse extends ExpressResponse {
newValue?: string
}
`,
file: `${projectName}/src/@types/custom/response.d.ts`
}
}
})
}

await writeFile(types.index.file, types.index.content)
await writeFile(types.models.user.file, types.models.user.content)

if (express) {
await writeFile(types.custom.request.file, types.custom.request.content)
await writeFile(types.custom.response.file, types.custom.response.content)
}
}

0 comments on commit 09a655f

Please sign in to comment.