Verified Commit 1970fa24 authored by Aral Balkan's avatar Aral Balkan
Browse files

Skip node_modules and hidden files, add license and changelog

Bump version
parent c67b36d8
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- Nothing yet.
## [1.1.0] - 2019-05-14
### Added
- Ignore `node_modules` folder.
- Ignore hidden folders (folders with names that start with a dot).
## [1.0.0] - 2019-05-14
Initial release.
This diff is collapsed.
......@@ -5,7 +5,8 @@ const app = express()
const routes = getRoutes('.routes')
routes.forEach(route => {
console.log(` • Adding route: ${route.path}`)
app.get(route.path, require(route.callback))
})
app.listen(8080, () => console.log('Server running on http://localhost:8080'))
app.listen(8080, () => console.log('\nServer running on http://localhost:8080'))
......@@ -3,11 +3,24 @@ const path = require('path')
function routes (directory) {
let _routes = []
const files = fs.readdirSync(directory, {withFileTypes: true})
files.forEach(file => {
if (file.isDirectory()) {
//
// Directory.
//
if (file.name === 'node_modules' || file.name.startsWith('.')) {
// Skip.
return
}
// Recurse.
_routes = _routes.concat(routes(path.join(directory, file.name)))
} else if (file.isFile() && file.name.endsWith('.js')) {
//
// File.
//
let routeCallbackFilePath = path.resolve(path.join(directory, file.name))
let routeUrlPath = path.join(directory.replace(/.*\.routes/, ''), file.name.replace('.js', ''))
routeUrlPath = routeUrlPath.replace(/index$/, '')
......@@ -16,6 +29,7 @@ function routes (directory) {
_routes.push({path: routeUrlPath, callback: routeCallbackFilePath})
}
})
return _routes
}
......
{
"name": "@ind.ie/web-routes-from-files",
"version": "1.0.0",
"version": "1.1.0",
"description": "Recursively traverses a given directory structure and uses convention to create a list of web route objects that map url paths to JavaScript callback files.",
"main": "index.js",
"directories": {
......
......@@ -11,7 +11,7 @@ npm i @ind.ie/web-routes-from-files
## Use
```js
const getRoutes = require ('recursive-directory-structure-to-web-route-map')
const getRoutes = require ('web-routes-from-files')
const routes = getRoutes('.')
......@@ -36,7 +36,7 @@ Given the following directory structure:
And the following invocation:
```js
const getRoutes = require ('recursive-directory-structure-to-web-route-map')
const getRoutes = require ('web-routes-from-files')
const routes = getRoutes('.routes')
```
......@@ -56,7 +56,7 @@ Which, for example, you could pass to an [Express](https://expressjs.com/) app:
```js
const express = require('express')
const getRoutes = require ('recursive-directory-structure-to-web-route-map')
const getRoutes = require ('web-routes-from-files')
const app = express()
const routes = getRoutes('.routes')
......@@ -76,3 +76,7 @@ function route (request, response, next) {
}
module.exports = route
```
__Note:__ The module will ignore `node_modules` folders and any folder within the root folder being traversed that begins with a dot (i.e., any hidden folder).
__Copyright:__ ⓒ 2019 [Aral Balkan](https://ar.al). Licensed under AGPLv3 or later.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment