Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw on missing trailing slash #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"devDependencies": {
"assert-diff": "2.0.3",
"mocha": "5.2.0"
},
"dependencies": {
"mock-fs": "^4.7.0"
}
}
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var yesql = require('./yesql.js')
var assert = require('assert-diff')
var mockFs = require('mock-fs');

it('pg', function() {
assert.deepEqual(
Expand Down Expand Up @@ -52,3 +53,16 @@ it('raw from file', function() {
var sql = yesql('./')
assert.equal(sql.updatePokemon, '-- updatePokemon\nUPDATE pokemon SET price=:price;\n')
})

it('parses files from a directory without trailing slash', function() {
mockFs({
'./sqls': {
'one.sql': '-- updatePokemon\nUPDATE pokemon SET price=$1;\n',
'two.sql': '-- updateTransformer\nUPDATE transformer SET price=$1;\n'
}
})

var sql = yesql('./sqls')

mockFs.restore()
})
41 changes: 21 additions & 20 deletions yesql.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
var fs = require('fs')
var path = require('path')

module.exports = function readSqlFiles(dir, options) {
var opts = options ? options : {pg: false}
return fs.readdirSync(dir).filter(function(file) {
return file.endsWith('.sql')
}).map(function(file) {
return {
name: file,
content: fs
.readFileSync(dir + file, 'utf8')
.replace(/\r\n/g, '\n')
}
}).reduce(function(acc, value) {
acc[value.name] = value.content
value.content.split('\n\n').forEach(function(sql) {
if (sql.startsWith('-- ')) {
var sqlName = sql.split('\n')[0].substring(2).trim()
acc[sqlName] = opts.type ? module.exports[opts.type](sql) : sql
var opts = options ? options : {pg: false}
return fs.readdirSync(dir).filter(function(file) {
return file.endsWith('.sql')
}).map(function(file) {
return {
name: file,
content: fs
.readFileSync(path.resolve(dir, file), 'utf8')
.replace(/\r\n/g, '\n')
}
})
return acc
}, {})
}
}).reduce(function(acc, value) {
acc[value.name] = value.content
value.content.split('\n\n').forEach(function(sql) {
if (sql.startsWith('-- ')) {
var sqlName = sql.split('\n')[0].substring(2).trim()
acc[sqlName] = opts.type ? module.exports[opts.type](sql) : sql
}
})
return acc
}, {})
}
module.exports.pg = pg
module.exports.mysql = mysql

Expand Down