Skip to content

Commit

Permalink
enable typescript services and sdl
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Reidy committed May 5, 2020
1 parent 529f72a commit 9bc6b0d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
14 changes: 8 additions & 6 deletions packages/cli/src/commands/generate/sdl/sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const sdlFromSchemaModel = async (name) => {
}
}

export const files = async ({ name, crud }) => {
export const files = async ({ name, crud, typescript }) => {
const {
query,
createInput,
Expand All @@ -115,8 +115,9 @@ export const files = async ({ name, crud }) => {
enums,
} = await sdlFromSchemaModel(pascalcase(pluralize.singular(name)))

const extension = typescript ? 'ts' : 'js'
const template = generateTemplate(
path.join('sdl', 'templates', 'sdl.js.template'),
path.join('sdl', 'templates', `sdl.${extension}.template`),
{
name,
crud,
Expand All @@ -130,11 +131,11 @@ export const files = async ({ name, crud }) => {

const outputPath = path.join(
getPaths().api.graphql,
`${camelcase(pluralize(name))}.sdl.js`
`${camelcase(pluralize(name))}.sdl.${extension}`
)
return {
[outputPath]: template,
...(await serviceFiles({ name, crud, relations })),
...(await serviceFiles({ name, crud, relations, typescript })),
}
}

Expand All @@ -144,15 +145,16 @@ export const builder = {
services: { type: 'boolean', default: true },
crud: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
typescript: { type: 'boolean', default: false },
}
// TODO: Add --dry-run command
export const handler = async ({ model, crud, force }) => {
export const handler = async ({ model, crud, force, typescript }) => {
const tasks = new Listr(
[
{
title: 'Generating SDL files...',
task: async () => {
const f = await files({ name: model, crud })
const f = await files({ name: model, crud, typescript })
return writeFilesTask(f, { overwriteExisting: force })
},
},
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/commands/generate/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import {

export const files = async ({ name, relations, ...rest }) => {
const componentName = camelcase(pluralize(name))
const extension = rest.typescript ? 'ts' : 'js'
const serviceFile = templateForComponentFile({
name,
componentName: componentName,
extension: `.${extension}`,
apiPathSection: 'services',
generator: 'service',
templatePath: 'service.js.template',
templatePath: `service.${extension}.template`,
templateVars: { relations: relations || [], ...rest },
})
const testFile = templateForComponentFile({
name,
componentName: componentName,
extension: '.test.js',
extension: `.test.${extension}`,
apiPathSection: 'services',
generator: 'service',
templatePath: 'test.js.template',
templatePath: `test.${extension}.template`,
templateVars: { relations: relations || [], ...rest },
})

Expand All @@ -42,6 +44,7 @@ export const files = async ({ name, relations, ...rest }) => {
export const builder = {
crud: { type: 'boolean', default: false, desc: 'Create CRUD functions' },
force: { type: 'boolean', default: false },
typescript: { type: 'boolean', default: false },
}

export const { command, desc, handler } = createYargsForComponentGeneration({
Expand Down
6 changes: 5 additions & 1 deletion packages/dev-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const purgeRequireCache = (): void => {

const requireLambdaFunctions = (path: string): { [path: string]: any } => {
// @ts-ignore ; requireDir is outdated.
return requireDir(path, { recurse: false, extensions: ['.js', '.ts'] })
return requireDir(path, {
recurse: false,
extensions: ['.js', '.ts'],
filter: (path: string) => path.match(/\.test\.[jt]s/),
})
}

const app = express()
Expand Down

0 comments on commit 9bc6b0d

Please sign in to comment.