This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
settings.js
47 lines (44 loc) · 1.46 KB
/
settings.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
var utils = require('./utils'),
path = require('path'),
semver = require('semver'),
async = require('async');
exports.load = async.memoize(function (dir, callback) {
var settings_file = path.resolve(dir, 'package.json');
utils.readJSON(settings_file, function (err, settings) {
if (err) {
callback(err);
}
try {
exports.validate(settings, settings_file);
}
catch (e) {
return callback(e);
}
callback(null, settings);
});
});
exports.validate = function (settings, filename) {
if (!settings.name) {
throw new Error('Missing name property in ' + filename);
}
if (!/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/.test(settings.name)) {
throw new Error(
'Invalid name property in ' + filename + ', ' +
'package names can only contain numbers, upper or lowercase ' +
'letters and "_", "-" or ".", and must start with a letter'
);
}
if (!settings.version) {
throw new Error('Missing version property in ' + filename);
}
if (!settings.description) {
throw new Error('Missing description property in ' + filename);
}
if (!semver.valid(settings.version)) {
throw new Error(
'Invalid version number in ' + filename + '\n' +
'Version numbers should follow the format described at ' +
'http://semver.org (eg, 1.2.3 or 4.5.6-jam.1)'
);
}
};