From 034be33108b91924c379803c12b6641ffadf41b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Poullain?= Date: Wed, 22 Jan 2020 11:57:52 +0100 Subject: [PATCH] [@foal/cli] Fix --register failure when no imports --- .../controller/create-controller.spec.ts | 11 ++++ .../controller/register-controller.ts | 50 ++++++++++--------- .../controller/app.controller.no-import.ts | 1 + .../app.controller.controller-import.ts | 1 + .../app.controller.empty-property.ts | 1 + .../app.controller.empty-spaced-property.ts | 1 + .../app.controller.no-controller-import.ts | 1 + .../controller/app.controller.no-import.ts | 4 ++ .../specs/controller/app.controller.rest.ts | 1 + .../app.controller.controller-import.ts | 1 + .../rest-api/app.controller.empty-property.ts | 1 + .../app.controller.empty-spaced-property.ts | 1 + .../app.controller.no-controller-import.ts | 1 + 13 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 packages/cli/src/generate/mocks/controller/app.controller.no-import.ts create mode 100644 packages/cli/src/generate/specs/controller/app.controller.no-import.ts diff --git a/packages/cli/src/generate/generators/controller/create-controller.spec.ts b/packages/cli/src/generate/generators/controller/create-controller.spec.ts index 458d04b553..220e7fc3b2 100644 --- a/packages/cli/src/generate/generators/controller/create-controller.spec.ts +++ b/packages/cli/src/generate/generators/controller/create-controller.spec.ts @@ -84,6 +84,17 @@ describe('createController', () => { testEnv.copyFileFromMocks('index.ts'); }); + // TODO: refactor these tests and their mock and spec files. + + it('should add all the imports if none exists.', () => { + testEnv.copyFileFromMocks('app.controller.no-import.ts', '../app.controller.ts'); + + createController({ name: 'test-fooBar', type: 'Empty', register: true }); + + testEnv + .validateSpec('app.controller.no-import.ts', '../app.controller.ts'); + }); + it('should update the "subControllers" import in src/app/app.controller.ts if it exists.', () => { testEnv.copyFileFromMocks('app.controller.controller-import.ts', '../app.controller.ts'); diff --git a/packages/cli/src/generate/generators/controller/register-controller.ts b/packages/cli/src/generate/generators/controller/register-controller.ts index 3a53703dfa..49bb1d9a6d 100644 --- a/packages/cli/src/generate/generators/controller/register-controller.ts +++ b/packages/cli/src/generate/generators/controller/register-controller.ts @@ -4,50 +4,53 @@ function createNamedImport(specifiers: string[], path: string): string { return `import { ${specifiers.join(', ')} } from '${path}';`; } -function addImport(source: string, importDeclaration: string): string { +function addImport(fileContent: string, importDeclaration: string): string { const regex = new RegExp('import (.*) from (.*);', 'g'); let lastOccurence: RegExpExecArray|undefined; let lastExec: RegExpExecArray|null; - while ((lastExec = regex.exec(source)) !== null) { + while ((lastExec = regex.exec(fileContent)) !== null) { lastOccurence = lastExec; } - // TODO: remove the "as RegExpExecArray". - const endPos = (lastOccurence as RegExpExecArray).index + (lastOccurence as RegExpExecArray)[0].length; - return source.substr(0, endPos) + '\n' + importDeclaration + source.substr(endPos); + if (lastOccurence === undefined) { + return `${importDeclaration}\n\n${fileContent}`; + } + const endPos = lastOccurence.index + lastOccurence[0].length; + return fileContent.substr(0, endPos) + '\n' + importDeclaration + fileContent.substr(endPos); } -function addSpecifierToNamedImport(source: string, path: string, specifier: string): string { - let namedImportFound = false; +function extendImport(fileContent: string, path: string, specifier: string): string { const pathRegex = path.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); - const result = source - .replace(new RegExp(`import {(.*)} from \'${pathRegex}\';`), (str, content: string) => { - namedImportFound = true; + const importRegex = new RegExp(`import {(.*)} from \'${pathRegex}\';`); + if (!importRegex.test(fileContent)) { + throw new ImportNotFound(); + } + + return fileContent + .replace(importRegex, (str, content: string) => { const importSpecifiers = content.split(',').map(imp => imp.trim()); - if (importSpecifiers.includes('controller')) { + if (importSpecifiers.includes(specifier)) { return str; } importSpecifiers.push(specifier); importSpecifiers.sort((a, b) => a.localeCompare(b)); return createNamedImport(importSpecifiers, path); }); - if (!namedImportFound) { - throw new ImportNotFound(); - } - return result; } -export function registerController(parentControllerContent: string, controllerName: string, path: string): string { +function addOrExtendImport(fileContent: string, specifier: string, path: string): string { try { - parentControllerContent = addSpecifierToNamedImport(parentControllerContent, './controllers', controllerName); + return extendImport(fileContent, path, specifier); } catch (err) { - const namedImport = createNamedImport([ controllerName ], './controllers'); - parentControllerContent = addImport(parentControllerContent, namedImport); + const namedImport = createNamedImport([ specifier ], path); + return addImport(fileContent, namedImport); } - try { - parentControllerContent = addSpecifierToNamedImport(parentControllerContent, '@foal/core', 'controller'); - } catch (err) {} - parentControllerContent = parentControllerContent +} + +export function registerController(fileContent: string, controllerName: string, path: string): string { + fileContent = addOrExtendImport(fileContent, controllerName, './controllers'); + fileContent = addOrExtendImport(fileContent, 'controller', '@foal/core'); + return fileContent .replace(/( *)subControllers = \[((.|\n)*)\];/, (str, spaces, content: string) => { const regex = /controller\((.*)\)/g; const controllerCalls = content.match(regex) || []; @@ -55,5 +58,4 @@ export function registerController(parentControllerContent: string, controllerNa const formattedCalls = controllerCalls.join(`,\n${spaces} `); return `${spaces}subControllers = [\n${spaces} ${formattedCalls}\n${spaces}];`; }); - return parentControllerContent; } diff --git a/packages/cli/src/generate/mocks/controller/app.controller.no-import.ts b/packages/cli/src/generate/mocks/controller/app.controller.no-import.ts new file mode 100644 index 0000000000..273000c0fb --- /dev/null +++ b/packages/cli/src/generate/mocks/controller/app.controller.no-import.ts @@ -0,0 +1 @@ +export class MyController {} diff --git a/packages/cli/src/generate/specs/controller/app.controller.controller-import.ts b/packages/cli/src/generate/specs/controller/app.controller.controller-import.ts index 787d11e225..1216d179ca 100644 --- a/packages/cli/src/generate/specs/controller/app.controller.controller-import.ts +++ b/packages/cli/src/generate/specs/controller/app.controller.controller-import.ts @@ -1,4 +1,5 @@ // App import { TestFooBarController, ViewController } from './controllers'; +import { controller } from '@foal/core'; export class MyController {} diff --git a/packages/cli/src/generate/specs/controller/app.controller.empty-property.ts b/packages/cli/src/generate/specs/controller/app.controller.empty-property.ts index 3ba4f3856d..5f560d0043 100644 --- a/packages/cli/src/generate/specs/controller/app.controller.empty-property.ts +++ b/packages/cli/src/generate/specs/controller/app.controller.empty-property.ts @@ -1,6 +1,7 @@ // 3p import {} from 'somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController { subControllers = [ diff --git a/packages/cli/src/generate/specs/controller/app.controller.empty-spaced-property.ts b/packages/cli/src/generate/specs/controller/app.controller.empty-spaced-property.ts index 3ba4f3856d..5f560d0043 100644 --- a/packages/cli/src/generate/specs/controller/app.controller.empty-spaced-property.ts +++ b/packages/cli/src/generate/specs/controller/app.controller.empty-spaced-property.ts @@ -1,6 +1,7 @@ // 3p import {} from 'somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController { subControllers = [ diff --git a/packages/cli/src/generate/specs/controller/app.controller.no-controller-import.ts b/packages/cli/src/generate/specs/controller/app.controller.no-controller-import.ts index 3c78bea925..1b2bea7108 100644 --- a/packages/cli/src/generate/specs/controller/app.controller.no-controller-import.ts +++ b/packages/cli/src/generate/specs/controller/app.controller.no-controller-import.ts @@ -1,5 +1,6 @@ // 3p import { Something } from '@somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController {} diff --git a/packages/cli/src/generate/specs/controller/app.controller.no-import.ts b/packages/cli/src/generate/specs/controller/app.controller.no-import.ts new file mode 100644 index 0000000000..88ee2e4427 --- /dev/null +++ b/packages/cli/src/generate/specs/controller/app.controller.no-import.ts @@ -0,0 +1,4 @@ +import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; + +export class MyController {} diff --git a/packages/cli/src/generate/specs/controller/app.controller.rest.ts b/packages/cli/src/generate/specs/controller/app.controller.rest.ts index cc278989f5..4c250d8151 100644 --- a/packages/cli/src/generate/specs/controller/app.controller.rest.ts +++ b/packages/cli/src/generate/specs/controller/app.controller.rest.ts @@ -1,6 +1,7 @@ // 3p import {} from 'somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController { subControllers = [ diff --git a/packages/cli/src/generate/specs/rest-api/app.controller.controller-import.ts b/packages/cli/src/generate/specs/rest-api/app.controller.controller-import.ts index 787d11e225..1216d179ca 100644 --- a/packages/cli/src/generate/specs/rest-api/app.controller.controller-import.ts +++ b/packages/cli/src/generate/specs/rest-api/app.controller.controller-import.ts @@ -1,4 +1,5 @@ // App import { TestFooBarController, ViewController } from './controllers'; +import { controller } from '@foal/core'; export class MyController {} diff --git a/packages/cli/src/generate/specs/rest-api/app.controller.empty-property.ts b/packages/cli/src/generate/specs/rest-api/app.controller.empty-property.ts index cc278989f5..4c250d8151 100644 --- a/packages/cli/src/generate/specs/rest-api/app.controller.empty-property.ts +++ b/packages/cli/src/generate/specs/rest-api/app.controller.empty-property.ts @@ -1,6 +1,7 @@ // 3p import {} from 'somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController { subControllers = [ diff --git a/packages/cli/src/generate/specs/rest-api/app.controller.empty-spaced-property.ts b/packages/cli/src/generate/specs/rest-api/app.controller.empty-spaced-property.ts index cc278989f5..4c250d8151 100644 --- a/packages/cli/src/generate/specs/rest-api/app.controller.empty-spaced-property.ts +++ b/packages/cli/src/generate/specs/rest-api/app.controller.empty-spaced-property.ts @@ -1,6 +1,7 @@ // 3p import {} from 'somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController { subControllers = [ diff --git a/packages/cli/src/generate/specs/rest-api/app.controller.no-controller-import.ts b/packages/cli/src/generate/specs/rest-api/app.controller.no-controller-import.ts index 3c78bea925..1b2bea7108 100644 --- a/packages/cli/src/generate/specs/rest-api/app.controller.no-controller-import.ts +++ b/packages/cli/src/generate/specs/rest-api/app.controller.no-controller-import.ts @@ -1,5 +1,6 @@ // 3p import { Something } from '@somewhere'; import { TestFooBarController } from './controllers'; +import { controller } from '@foal/core'; export class MyController {}