This repository was archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (124 loc) · 3.79 KB
/
index.js
File metadata and controls
140 lines (124 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
// Requires
var async = require('async')
var json2mongo = require('json2mongo')
var MongoClient = require('mongodb').MongoClient
var ObjectID = require('mongodb-core').BSON.ObjectID
var path = require('path')
var PluginError = require('plugin-error')
var through = require('through2')
module.exports = function (opts) {
return through.obj(function (file, enc, cb) {
// File is null - pass along
if (file.isNull()) return cb(null, file)
// File is stream - not supported
if (file.isStream()) {
return cb(
new PluginError('gulp-mongodb-data', 'Streaming is not supported')
)
}
// Set default options
opts = setDefaultOptions(opts)
var content = String(file.contents)
var json
if (!content) {
return cb(
new PluginError(
'gulp-mongodb-data', 'File cannot be empty', {
fileName: file.path,
showStack: true
}))
}
if (content[0] === '[') {
// Assume file contains properly formatted JSON array
try {
json = json2mongo(JSON.parse(content))
} catch (e) {
return cb(
new PluginError(
'gulp-mongodb-data', 'Problem parsing JSON file', {
fileName: file.path,
showStack: true
}))
}
} else if (content[0] === '{') {
// Assume file contains mongoexport dump or single item
json = []
var dumpedObjects = content.split('\n')
for (var i = 0; i < dumpedObjects.length; i++) {
if (!dumpedObjects[i]) continue
try {
json.push(json2mongo(JSON.parse(dumpedObjects[i])))
} catch (e) {
return cb(
new PluginError(
'gulp-mongodb-data', 'Problem parsing JSON file', {
fileName: file.path,
showStack: true
}))
}
}
} else {
// We have no idea what the user sent us
return cb(
new PluginError(
'gulp-mongodb-data', 'File is not valid', {
fileName: file.path,
showStack: true
}))
}
if (opts.idAsObjectID) {
json = json.map(function (obj) {
if (obj._id && typeof obj._id === 'string') {
obj._id = ObjectID(obj._id)
}
return obj
})
}
MongoClient.connect(opts.mongoUri, function (err, client) {
if (err) {
return cb(
new PluginError('gulp-mongodb-data', err, {showStack: true}))
}
var db = client.db(opts.databaseName)
var collectionName = opts.collectionName ||
path.basename(file.path, path.extname(file.path))
var coll = db.collection(collectionName)
// Run methods synchronous
async.series([
// Drop collection if option is set and collection exists
function (cb) {
if (opts.dropCollection) {
db.listCollections({name: collectionName})
.toArray(function (err, items) {
if (err) return cb(err)
if (items.length) return coll.drop(cb)
cb()
})
} else cb()
},
// Insert dato into collection
function (cb) {
coll.insertMany(json, cb)
}
], function (err) {
client.close()
if (err) {
return cb(
new PluginError('gulp-mongodb-data', err, {showStack: true}))
}
// Pass on
cb(null, file)
})
})
})
}
function setDefaultOptions (opts) {
opts = opts || {}
opts.mongoUri = opts.mongoUri || process.env.GULP_MONGODB_DATA_DEFAULT_CONNECTIONSTRING || 'mongodb://localhost'
opts.databaseName = opts.databaseName || 'nope'
opts.idAsObjectID =
typeof opts.idAsObjectID !== 'undefined' &&
opts.idAsObjectID !== null ? opts.idAsObjectID : true
return opts
}