Skip to content

Commit

Permalink
feat: remove add/remove route methods for now
Browse files Browse the repository at this point in the history
Since typescript is likely to be a popular option for Denali
apps, and the jscodeshift parser doesn support typescript,
we will remove these methods for now to ensure a consistent
experience across languages
  • Loading branch information
davewasmer committed Feb 5, 2018
1 parent ccc6c5d commit 2b3ca95
Showing 1 changed file with 0 additions and 78 deletions.
78 changes: 0 additions & 78 deletions lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,84 +318,6 @@ export default class Blueprint extends Command {
return path.join((<typeof Blueprint>this.constructor).dir, 'files');
}

/**
* Adds a route to this package's router.
*/
public addRoute(method: string, urlPattern: string, actionPath?: string, ...args: any[]): void {
let routesFilepath = path.join(process.cwd(), 'config', 'routes.js');
let routesSource;
try {
routesSource = fs.readFileSync(routesFilepath, 'utf-8');
} catch (e) {
ui.warn(`Attempted to add "${ method.toUpperCase() } ${ urlPattern } -> ${ actionPath }" route, but config/routes.js does not exist. Skipping ...`);
return;
}
let j = require('jscodeshift');
let ast = j(routesSource);
let drawRoutesFunction = ast.find(j.ExportDefaultDeclaration).get().value.declaration;
let routerArgName = drawRoutesFunction.params[0].name;
let drawRoutesFunctionBody = j(drawRoutesFunction.body);
let duplicate = drawRoutesFunctionBody.find(j.ExpressionStatement, {
expression: {
callee: {
object: { name: routerArgName },
property: { name: method }
},
arguments: [ urlPattern, actionPath ].concat(args).map((arg) => {
return { value: arg };
})
}
});
if (duplicate.length > 0) {
return;
}
let routerInvocations = drawRoutesFunctionBody.find(j.ExpressionStatement, {
expression: {
callee: {
object: { name: routerArgName }
}
}
});
let lastRouterInvocation = routerInvocations.at(routerInvocations.length - 1);
let routerMethodExpression = j.memberExpression(j.identifier(routerArgName), j.identifier(method));
let routerArguments = args.map((arg) => j.stringLiteral(arg));
let routerMethodInvocation = j.callExpression(routerMethodExpression, routerArguments);
let newRoute = j.expressionStatement(routerMethodInvocation);
lastRouterInvocation.insertAfter(newRoute);
fs.writeFileSync(routesFilepath, ast.toSource({ quote: 'single' }));
}

/**
* Removes a route from this package's router
*/
public removeRoute(method: string, urlPattern: string, actionPath?: string, ...args: any[]): void {
let routesFilepath = path.join(process.cwd(), 'config', 'routes.js');
let routesSource;
try {
routesSource = fs.readFileSync(routesFilepath, 'utf-8');
} catch (e) {
ui.warn(`Attempted to remove "${ method.toUpperCase() } ${ urlPattern } -> ${ actionPath }" route, but config/routes.js does not exist. Skipping ...`);
return;
}
let j = require('codeshift');
let ast = j(routesSource);
let drawRoutesFunction = ast.find(j.ExportDefaultDeclaration).get().value.declaration;
let routerArgName = drawRoutesFunction.params[0].name;
let drawRoutesFunctionBody = j(drawRoutesFunction.body);
drawRoutesFunctionBody.find(j.ExpressionStatement, {
expression: {
callee: {
object: { name: routerArgName },
property: { name: method }
},
arguments: [ urlPattern, actionPath ].concat(args).map((arg) => {
return { value: arg };
})
}
}).remove();
fs.writeFileSync(routesFilepath, ast.toSource());
}

/**
* Add a package to this project, using yarn or npm as appropriate.
*/
Expand Down

0 comments on commit 2b3ca95

Please sign in to comment.