Skip to content

Commit

Permalink
[update] basic support for ts route files
Browse files Browse the repository at this point in the history
  • Loading branch information
calvintwr committed Jun 8, 2020
1 parent ab6b2bc commit 6b95a52
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Express Route-Magic v2.0.1
* Express Route-Magic v2.0.2
* (c) 2020 Calvin Tan
* Released under the MIT License.
*/
Expand Down Expand Up @@ -138,10 +138,13 @@ Magic.scan = function(directory) {
}

// js files
return (file.indexOf('.js') === file.length - '.js'.length)
return (
(file.indexOf('.js') === file.length - '.js'.length) ||
(file.indexOf('.ts') === file.length - '.ts'.length)
)

}).forEach(file => {
if (file === 'index.js') {
if (['index.js', 'index.ts'].indexOf(file) > -1) {
_files.unshift(file)
} else {
this.push(_files, file)
Expand All @@ -164,7 +167,7 @@ Magic.push = function(array, payload, isDirectory) {
}

Magic.toIgnore = function(payload, isDirectory) {
if (!isDirectory) payload = payload.replace('.js', '')
if (!isDirectory) payload = payload.substring(0, payload.length - 3) // remove the extension
let toIgnore = false
if (this.ignoreSuffix) {
this.ignoreSuffix.forEach(suffix => {
Expand All @@ -180,7 +183,7 @@ Magic.toIgnore = function(payload, isDirectory) {
Magic.checkConflict = function(files, folders, directory) {
if (this.allowSameName) return false
files.forEach(file => {
if (folders.indexOf(file.replace('.js', '')) !== -1) throw new Error(`Folder and file with conflict name: \`${file.replace('.js', '')}\` in directory: \`${directory}\`.`)
if (folders.indexOf(file.substring(0, file.length - 3)) !== -1) throw new Error(`Folder and file with conflict name: \`${file.substring(0, file.length - 3)}\` in directory: \`${directory}\`.`)
})
}

Expand All @@ -201,7 +204,7 @@ Magic.apiPath = function(file, apiDir) {
// TODO: To support passing array to
// have to check whether the apiDir have any commas. if yes can indicate a ['/route1', '/route2'] kind.
// also need to check if file have any commans. if yes can indicate a ['/route1/filename1', '/route2/filename1', '/route1/filename2', '/route2/filename2'] kind of situation.
return (file === 'index.js') ? apiDir : path.join(apiDir, file.replace('.js', ''))
return (['index.js', 'index.ts'].indexOf(file) > -1) ? apiDir : path.join(apiDir, file.substring(0, file.length - 3))
}
Magic.absolutePathToRoutesFolder = function() {
return path.join(this.invokerPath, this.routesFolder)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-routemagic",
"version": "2.0.1",
"version": "2.0.2",
"description": "A simple and fast, fire-and-forget module that all Nodejs+Express app should have, to automatically require all your express routes without bloating your code with `app.use('i/will/repeat/this', require('./i/will/repeat/this')`. 把 Express 路由图给自动化。",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('toIgnore', () => {
magic_toIgnore.ignoreSuffix = ['ignore', 'dev']
it('should be false when payload matches', () => {
magic_toIgnore.toIgnore('ignore', true).should.be.true
magic_toIgnore.toIgnore('dev').should.be.true
magic_toIgnore.toIgnore('dev.ts').should.be.true
magic_toIgnore.toIgnore('dev.js').should.be.true
})
it('should be true when payload don\'t match', () => {
Expand Down Expand Up @@ -118,8 +118,14 @@ describe('apiDirectory', () => {
})

describe('apiPath', () => {
it('should out correct apiPath', () => {
it('should out correct apiPath for non-index files', () => {
magic.apiPath('bar.js', '/api').should.equal('/api/bar')
magic.apiPath('bar.ts', '/api').should.equal('/api/bar')
magic.apiPath('index.ts', '/api').should.equal('/api')
})
it('should out correct apiPath for index files', () => {
magic.apiPath('index.ts', '/api').should.equal('/api')
magic.apiPath('index.js', '/api').should.equal('/api')
})
})

Expand Down

0 comments on commit 6b95a52

Please sign in to comment.