Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
var inherits = require('util').inherits;
18
var path = require('path');
19
var ProjectConfiguration = require('../resource/ProjectConfiguration');
20
var ResourceLoader = require('./ResourceLoader');
21
22
/**
23
* @class Loads and parses package.json files
24
*
25
* @extends {ResourceLoader}
26
*/
27
function ProjectConfigurationLoader() {
28
ResourceLoader.call(this);
29
}
30
inherits(ProjectConfigurationLoader, ResourceLoader);
31
ProjectConfigurationLoader.prototype.path = __filename;
32
33
ProjectConfigurationLoader.prototype.isConfiguration = true;
34
35
36
ProjectConfigurationLoader.prototype.getResourceTypes = function() {
37
return [ProjectConfiguration];
38
};
39
40
ProjectConfigurationLoader.prototype.getExtensions = function() {
41
return ['.json'];
42
};
43
44
45
/**
46
* Initialize a resource with the source code and configuration
47
* Loader can parse, gzip, minify the source code to build the resulting
48
* Resource value object
49
*
50
* @protected
51
* @param {String} path resource being built
52
* @param {ProjectConfiguration} configuration configuration for the path
53
* @param {String} sourceCode
54
* @param {Function} callback
55
*/
56
ProjectConfigurationLoader.prototype.loadFromSource =
57
function(path, configuration, sourceCode, messages, callback) {
58
var config = new ProjectConfiguration(path);
59
config.id = path;
60
try {
61
config.data = sourceCode !== '' ? JSON.parse(sourceCode) : {};
62
} catch (e) {
63
console.error("Error parsing `" + path + "`!");
64
throw e;
65
}
66
callback(messages, config);
67
};
68
69
/**
70
* Only match package.json files
71
* @static
72
* @param {String} filePath
73
* @return {Boolean}
74
*/
75
ProjectConfigurationLoader.prototype.matchPath = function(filePath) {
76
return path.basename(filePath) === 'package.json';
77
};
78
79
80
module.exports = ProjectConfigurationLoader;
81
82