@ima/cli
Version:
IMA.js CLI tool to build, develop and work with IMA.js applications.
123 lines (122 loc) • 4.78 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serverControllerProcessor = void 0;
const traverse_1 = __importDefault(require("@babel/traverse"));
const b = __importStar(require("@babel/types"));
/**
* Checks if a class is a controller class. We really look at the
* existence of `controller` substring in the parent class name,
* since users can create their own abstract controllers and we can't
* know if they are extending the `AbstractController` class.
*/
function isControllerClass(node) {
return !!(node.superClass &&
b.isIdentifier(node.superClass) &&
node.superClass.name.toLowerCase().includes('controller'));
}
function createThrowingLoadMethod() {
return b.classMethod('method', b.identifier('load'), [], b.blockStatement([
b.throwStatement(b.newExpression(b.identifier('GenericError'), [
b.stringLiteral('Route not found.'),
b.objectExpression([
b.objectProperty(b.identifier('status'), b.numericLiteral(404)),
]),
])),
]));
}
function hasGenericErrorImport(importPath) {
if (importPath.node.source.value === '@ima/core' &&
importPath.node.specifiers.some(spec => b.isImportSpecifier(spec) &&
b.isIdentifier(spec.imported) &&
spec.imported.name === 'GenericError')) {
return true;
}
return false;
}
function addOrReplaceLoadMethod(classNode) {
let loadMethodExists = false;
classNode.body.body = classNode.body.body.map(member => {
if (b.isClassMethod(member) &&
b.isIdentifier(member.key) &&
member.key.name === 'load') {
loadMethodExists = true;
return createThrowingLoadMethod();
}
return member;
});
if (!loadMethodExists) {
classNode.body.body.push(createThrowingLoadMethod());
}
}
/**
* Special processor for server-only controllers. We're looking for
* controller classes and replacing the `load` method with a throwing
* method, to make sure the controller is not called on the client and
* if so, it throws a `GenericError` with a 404 status code.
*/
const serverControllerProcessor = ast => {
const imports = new Set();
let needsGenericErrorImport = false;
let alreadyHasGenericErrorImport = false;
(0, traverse_1.default)(ast, {
ClassDeclaration(path) {
if (isControllerClass(path.node)) {
needsGenericErrorImport = true;
addOrReplaceLoadMethod(path.node);
}
},
ClassExpression(path) {
if (isControllerClass(path.node)) {
needsGenericErrorImport = true;
addOrReplaceLoadMethod(path.node);
}
},
ImportDeclaration(path) {
imports.add(path.node);
alreadyHasGenericErrorImport = hasGenericErrorImport(path);
},
});
if (needsGenericErrorImport && alreadyHasGenericErrorImport) {
ast.program.body.unshift(b.importDeclaration([
b.importSpecifier(b.identifier('GenericError'), b.identifier('GenericError')),
], b.stringLiteral('@ima/core')));
}
return ast;
};
exports.serverControllerProcessor = serverControllerProcessor;