@vuesion/addon-contentful
Version:
vuesion contentful integration
84 lines (83 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts_morph_1 = require("ts-morph");
const utils_1 = require("@vuesion/utils");
const models_1 = require("@vuesion/models");
class Isomorphic extends models_1.ASTModel {
constructor() {
super(utils_1.runtimeRoot('src/server/isomorphic.ts'));
}
getExportFunction() {
return this.sourceFile
.getFirstChildByKind(ts_morph_1.SyntaxKind.ExportAssignment)
.getFirstChildByKind(ts_morph_1.SyntaxKind.ArrowFunction)
.getFirstChildByKind(ts_morph_1.SyntaxKind.Block)
.getFirstChildByKind(ts_morph_1.SyntaxKind.ReturnStatement)
.getFirstChildByKind(ts_morph_1.SyntaxKind.NewExpression)
.getFirstChildByKind(ts_morph_1.SyntaxKind.ArrowFunction)
.getFirstChildByKind(ts_morph_1.SyntaxKind.Block);
}
getRouterReady(exportReturnFunction) {
return exportReturnFunction
.getChildrenOfKind(ts_morph_1.SyntaxKind.ExpressionStatement)
.find((expression) => {
const propertyAcccess = expression
.getFirstChildByKind(ts_morph_1.SyntaxKind.CallExpression)
.getFirstChildByKind(ts_morph_1.SyntaxKind.PropertyAccessExpression);
if (propertyAcccess) {
const identifiers = propertyAcccess.getChildrenOfKind(ts_morph_1.SyntaxKind.Identifier);
return identifiers[0].getText() === 'router' && identifiers[1].getText() === 'onReady';
}
})
.getFirstChildByKind(ts_morph_1.SyntaxKind.CallExpression)
.getFirstChildByKind(ts_morph_1.SyntaxKind.ArrowFunction)
.getFirstChildByKind(ts_morph_1.SyntaxKind.Block);
}
removeRouteMatchesCatchAll(routerReady) {
const routeMatchesCatchAll = routerReady.getChildrenOfKind(ts_morph_1.SyntaxKind.VariableStatement).find((varStatement) => varStatement
.getFirstChildByKind(ts_morph_1.SyntaxKind.VariableDeclarationList)
.getFirstChildByKind(ts_morph_1.SyntaxKind.VariableDeclaration)
.getFirstChildByKind(ts_morph_1.SyntaxKind.Identifier)
.getText() === 'routeMatchesCatchAll');
if (routeMatchesCatchAll) {
routerReady.removeText(routeMatchesCatchAll.getPos(), routeMatchesCatchAll.getEnd());
}
}
removeRouteMatchesCatchAllIfStatement(routerReady) {
const routeMatchesCatchAllIfStatement = routerReady
.getChildrenOfKind(ts_morph_1.SyntaxKind.IfStatement)
.find((varStatement) => {
const identifier = varStatement.getFirstChildByKind(ts_morph_1.SyntaxKind.Identifier);
if (identifier) {
return identifier.getText() === 'routeMatchesCatchAll';
}
});
if (routeMatchesCatchAllIfStatement) {
routerReady.removeText(routeMatchesCatchAllIfStatement.getPos(), routeMatchesCatchAllIfStatement.getEnd());
}
}
addErrorHandling(routerReady) {
const tryStatement = routerReady.getFirstChildByKind(ts_morph_1.SyntaxKind.TryStatement);
if (tryStatement) {
const catchClause = tryStatement
.getFirstChildByKind(ts_morph_1.SyntaxKind.CatchClause)
.getFirstChildByKind(ts_morph_1.SyntaxKind.Block);
catchClause.insertText(catchClause.getStart() + 1, `if (e.response.status === 404) {
reject({ code: 404 });
} else {
reject(e);
}
`);
}
}
transform() {
this.load();
const exportReturnFunction = this.getExportFunction();
const routerReady = this.getRouterReady(exportReturnFunction);
this.removeRouteMatchesCatchAll(routerReady);
this.removeRouteMatchesCatchAllIfStatement(routerReady);
this.addErrorHandling(routerReady);
this.save();
}
}
exports.Isomorphic = Isomorphic;