From 9bc6b0d5126a508ea18ea3ea9d42f74879b5dd3c Mon Sep 17 00:00:00 2001 From: Justin Reidy Date: Tue, 5 May 2020 14:04:49 -0400 Subject: [PATCH] enable typescript services and sdl --- packages/cli/src/commands/generate/sdl/sdl.js | 14 ++++++++------ .../cli/src/commands/generate/service/service.js | 9 ++++++--- packages/dev-server/src/main.ts | 6 +++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/commands/generate/sdl/sdl.js b/packages/cli/src/commands/generate/sdl/sdl.js index 466eb7b8dc9b..f36479c32bac 100644 --- a/packages/cli/src/commands/generate/sdl/sdl.js +++ b/packages/cli/src/commands/generate/sdl/sdl.js @@ -105,7 +105,7 @@ const sdlFromSchemaModel = async (name) => { } } -export const files = async ({ name, crud }) => { +export const files = async ({ name, crud, typescript }) => { const { query, createInput, @@ -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, @@ -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 })), } } @@ -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 }) }, }, diff --git a/packages/cli/src/commands/generate/service/service.js b/packages/cli/src/commands/generate/service/service.js index 945ace05df2b..e3d11a173ee0 100644 --- a/packages/cli/src/commands/generate/service/service.js +++ b/packages/cli/src/commands/generate/service/service.js @@ -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 }, }) @@ -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({ diff --git a/packages/dev-server/src/main.ts b/packages/dev-server/src/main.ts index be80a412845f..6a2694010c6e 100755 --- a/packages/dev-server/src/main.ts +++ b/packages/dev-server/src/main.ts @@ -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()