From e9a9972209cf6dad350a89ad15553631b47a9a05 Mon Sep 17 00:00:00 2001 From: Andres Valencia Date: Mon, 1 May 2017 13:17:26 -0400 Subject: [PATCH] refactor print routes on start command --- commands/start.js | 38 ++++++++++++++++---------------------- test.js | 2 +- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/commands/start.js b/commands/start.js index b2f630e..139d2be 100644 --- a/commands/start.js +++ b/commands/start.js @@ -1,7 +1,5 @@ 'use strict'; -const fs = require('fs'); -const path = require('path'); const stubborn = require('stubborn-server'); const out = require('simple-output'); const globby = require('globby'); @@ -26,28 +24,24 @@ function startCmd(opts) { } } function printRoutes(srcPath, port) { - globby('**/', {cwd: srcPath}) + /** + * pattern to match valid file names + * @example HTTP method: post.json + * @example middleware: post.js + */ + const patterns = methods.map(m => `**/${m}.+(json|js)`); + + globby(patterns, {cwd: srcPath}) .then(paths => { - paths.forEach(pathName => { - if (methods.some(method => { - const fileLocation = path.join(srcPath, pathName, method); - return [ - `${fileLocation}.js`, - `${fileLocation}.json` - ].some(fileName => { - try { - fs.accessSync(fileName, fs.constants.R_OK); - return true; - } catch (err) { - return false; - } - }); - })) { - out.info(`http://localhost:${port}/${pathName}`); - } + const routes = paths.map(p => { + // remove filename from path + const directoryPath = p.substring(0, p.lastIndexOf('/')); + return `http://localhost:${port}/${directoryPath}`; }); - }) - .catch(out.error); + for (let route of new Set(routes)) { + out.info(route); + } + }); } module.exports = startCmd; diff --git a/test.js b/test.js index cc97436..9c6ce2e 100644 --- a/test.js +++ b/test.js @@ -318,7 +318,7 @@ describe('snapstub', function () { }); function validateRouteMsg() { child.stdout.once('data', d => { - assert.equal(d.toString(), 'ℹ http://localhost:8059/data/\n'); + assert.equal(d.toString(), 'ℹ http://localhost:8059/data\n'); }); } function validateSuccessMsg() {