Skip to content

Commit

Permalink
Merge pull request #619 from FoalTS/fix-register-bug
Browse files Browse the repository at this point in the history
[@foal/cli] Fix --register failure when no imports
  • Loading branch information
LoicPoullain committed Jan 22, 2020
2 parents 2b67fef + 034be33 commit bff7f96
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,58 @@ 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) || [];
controllerCalls.push(`controller('${path}', ${controllerName})`);
const formattedCalls = controllerCalls.join(`,\n${spaces} `);
return `${spaces}subControllers = [\n${spaces} ${formattedCalls}\n${spaces}];`;
});
return parentControllerContent;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class MyController {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// App
import { TestFooBarController, ViewController } from './controllers';
import { controller } from '@foal/core';

export class MyController {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 3p
import {} from 'somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {
subControllers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 3p
import {} from 'somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {
subControllers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 3p
import { Something } from '@somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 3p
import {} from 'somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {
subControllers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// App
import { TestFooBarController, ViewController } from './controllers';
import { controller } from '@foal/core';

export class MyController {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 3p
import {} from 'somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {
subControllers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 3p
import {} from 'somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {
subControllers = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 3p
import { Something } from '@somewhere';
import { TestFooBarController } from './controllers';
import { controller } from '@foal/core';

export class MyController {}

0 comments on commit bff7f96

Please sign in to comment.