-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighcharts-builder.js
130 lines (109 loc) · 3.65 KB
/
highcharts-builder.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
120
121
122
123
124
125
126
127
128
129
130
var command,
path = Npm.require('path'),
fs = Npm.require('fs'),
mkdirp = Npm.require('mkdirp'),
echo = Npm.require('node-echo'),
packageDir = path.resolve('./packages/highcharts-container'),
prefixPath = path.resolve('./packages/highcharts-container/meteor-prefix.js'),
postFixPath = path.resolve('./packages/highcharts-container/meteor-postfix.js'),
packageJs = path.resolve('./packages/highcharts-container/package.js'),
configFile = path.resolve('./client/config.highcharts.json'),
clientDir = path.resolve('./client');
// Don't run on tests and publish:
if (process.argv.length > 2) {
command = process.argv[2];
if (command == 'publish' || command == 'test-packages') {
return; // do nothing
}
}
// Generate files and paths:
if (!fs.existsSync(clientDir)) {
mkdirp.sync(clientDir);
}
if (!fs.existsSync(configFile)) {
fs.writeFileSync(configFile, '{\n \n}');
}
if (!fs.existsSync(packageDir)) {
mkdirp.sync(packageDir);
}
if (!fs.existsSync(packageJs)) {
fs.writeFileSync(packageJs, getFunctionBody(packageJS));
fs.writeFileSync(prefixPath, getFunctionBody(prefix));
fs.writeFileSync(postFixPath, getFunctionBody(postfix));
// Add Highcharts package:
echo.sync("\nhighcharts-container", ">>", ".meteor/packages");
console.log("Highcharts initialized, please restart Meteor.");
process.exit(0);
}
// getContent inside a function
function getFunctionBody (func) {
var lines = func.toString().split('\n'),
body = lines.slice(1, lines.length - 1); // remove declaration and closing bracket
return body.join('\n');
}
function postfix () {
var undefined;
Highcharts = module.exports;
module = undefined;
}
function prefix () {
module = {};
module.exports = {};
}
function packageJS () {
var minVersion = '4.2.1',
fs = Npm.require('fs'),
path = Npm.require('path'),
npmPath = ['.npm', 'package', 'node_modules', 'highcharts'],
configFile = path.resolve('./client/config.highcharts.json'),
where = 'client',
config = false;
// Get user options:
try {
config = JSON.parse(fs.readFileSync(configFile));
} catch (err) {
console.log('Config file: config.highcharts.json doesn\'t exist or is not a proper JSON. Proceeding with the default options.\n', err);
}
// Build up options:
var adapter = config && config.adapter ? config.adapter : 'default',
base = config && config.base ? config.base : 'highcharts.js',
modules = config && config.modules ? config.modules : [],
releaseVersion = config && config.version ? config.version : minVersion,
modulesLength = modules.length,
dependency = {},
files = [],
i;
// Hook to highcharts NPM package:
Npm.depends({
highcharts: releaseVersion
});
// Add modules:
for (i = 0; i < modulesLength; i++) {
files.push(npmPath.concat([modules[i]]).join(path.sep));
}
Package.describe({
name: 'highcharts-container',
version: '1.0.0',
summary: 'Container for Highcharts build.'
});
Package.onUse(function (api) {
api.versionsFrom('1.2.1');
api.export('Highcharts');
// Add config file to force Meteor watch for changes:
api.addFiles(configFile, where, {isAsset: true});
// jQuery dependency or Standalone Adapter:
if (adapter === 'jquery') {
api.use(adapter);
} else if (adapter !== 'default') {
api.addFiles(npmPath.concat(['adapters', adapter]).join(path.sep), where);
}
// Add core:
api.addFiles([
'meteor-prefix.js',
npmPath.concat([base]).join(path.sep),
'meteor-postfix.js'
], where);
// Add modules:
api.addFiles(files, where);
});
}