This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (95 loc) · 2.85 KB
/
index.js
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
/* jshint node: true */
'use strict';
var attachmate = require('attachmate');
var fstream = require('fstream');
var debug = require('debug')('steelmesh-publish');
var path = require('path');
var semver = require('semver');
var DEFAULT_IGNORES = [
/^\.git/i
];
/**
# steelmesh-publish
This module is responsible for uploading a local folder to a steelmesh CouchDB
instance under the following conditions:
- the selected CouchDB database is writable
- the app version is a greater semver than what has been published to the db
already
## Example Usage
<<< examples/publish.js
**/
module.exports = function(db, opts) {
var baseUrl = db.config.url + '/' + db.config.db;
var srcPath = (opts || {}).srcPath || process.cwd();
var ignorePaths = DEFAULT_IGNORES.concat((opts || {}).ignore || []).map(makeRegex);
function checkPublishOK(pkg, callback) {
db.get(pkg.name, function(err, body) {
// if we received an error and it was a missing error then proceed
if (err && err.message === 'missing') {
return callback();
}
else if (err) {
return callback(err);
}
// do a comparison on the uploaded version vs the local version
if (! semver.gt(pkg.version, body.version)) {
return callback(new Error('Need a new version to update app'));
}
return callback();
});
}
function filterItem(item) {
var relPath = item.path.slice(srcPath.length + 1);
var include = true;
// ensure the item does not match the ignore paths
ignorePaths.forEach(function(regex) {
include = include && (! regex.test(relPath));
});
return include;
}
function makeRegex(input) {
if (input instanceof RegExp) {
return input;
}
return new RegExp('^' + input + '$');
}
function upload(pkg, callback) {
// initialise the reader
var reader = fstream.Reader({
type: 'Directory',
path: srcPath,
filter: filterItem
});
// create the new attachmate writer
var writer = new attachmate.Writer({
path: baseUrl + '/' + pkg.name,
docData: pkg,
preserveExisting: false
});
reader.pipe(writer).on('end', callback).on('error', callback);
}
return function(callback) {
var pkg;
var reader;
try {
debug('reading package.json from ' + srcPath);
pkg = require(path.join(srcPath, 'package.json'));
}
catch (e) {
return callback(e);
}
// ensure we have a package name and version
if (! pkg.name) {
return callback(new Error('no name defined in package.json - cannot publish'));
}
if (! semver.valid(pkg.version)) {
return callback(new Error('invalid or missing version in package.json - cannot publish'));
}
checkPublishOK(pkg, function(err) {
if (err) {
return callback(err);
}
upload(pkg, callback);
});
};
};