forked from Modernizr/Modernizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modernizr
executable file
·176 lines (156 loc) · 4.1 KB
/
modernizr
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var mkdirp = require('mkdirp');
var Modernizr = require(path.resolve(__dirname + '/../lib/cli.js'));
var yargs = require('yargs')
.options('h', {
alias: 'help',
describe: 'Print Help'
})
.options('V', {
alias: ['v', 'version'],
describe: 'Print the version and exit'
})
.options('f', {
alias: 'features',
describe: 'comma separated list of feature detects'
})
.options('o', {
alias: 'options',
describe: 'comma separated list of extensibility options'
})
.options('c', {
alias: 'config',
describe: 'Path to a JSON file containing Modernizr configuration. See lib/config-all.json for an example'
})
.options('d', {
alias: 'dest',
describe: 'Path to write the Modernizr build file to. Defaults to ./modernizr.js'
})
.options('m', {
alias: 'metadata',
describe: 'Path to where the Modernizr feature-detect metadata should be saved. Defaults to ./metadata.json'
})
.options('u', {
alias: 'uglify',
describe: 'uglify/minify the output'
})
.options('q', {
alias: 'quiet',
describe: 'Silence all output'
});
var argv = yargs.argv;
var cwd = process.cwd();
var dest = cwd + '/modernizr.js';
var inlineConfig;
var configPath;
var config;
function log() {
if (!argv.q) {
console.log.apply(console, arguments);
}
}
function stringify(obj, minified) {
var replacer = function(key, value) {
return value;
};
var args = minified ? [replacer,2] : [];
args.unshift(obj);
return JSON.stringify.apply(JSON, args);
}
if (argv.h) {
yargs.showHelp();
process.exit();
}
if (argv.V) {
var pkg = require('../package.json');
console.log('Modernizr v' + pkg.version);
process.exit();
}
if (argv.d) {
dest = path.normalize(argv.d);
var exists = fs.existsSync(dest);
var isDir = exists && fs.statSync(dest).isDirectory();
var fileRequested = _.endsWith(dest, '.js');
if ((exists && isDir) || (!exists && !fileRequested)) {
dest = path.join(dest, 'modernizr.js');
}
mkdirp.sync(path.dirname(dest));
}
if (argv.m) {
// path.normalize is used instead of normalize in order to support ~
// we get an absolute path on the fallback from cwd, and any user supplied
// argument will be relative to their current directory.
var metaDest = path.normalize(argv.m === true ? cwd + '/metadata.json' : argv.m);
Modernizr.metadata(function(metadata) {
mkdirp.sync(path.dirname(metaDest));
fs.writeFileSync(metaDest, stringify(metadata, !argv.u));
log('metadata saved to ' + metaDest);
});
if (!argv.d) {
// return early unless we explictly request Modernizr to be built
return;
}
}
if (argv.o || argv.f) {
var metadata = Modernizr.metadata();
var options = Modernizr.options();
var find = function(config, source) {
if (!config) {
return;
}
return config
.replace(/-/g, ',')
.split(',')
.map(function(prop) {
var obj = _.findWhere(source, {property: prop});
if (_.isUndefined(obj)) {
throw new Error('invalid key value name - ' + prop);
} else {
return obj.amdPath || obj.property;
}
});
};
config = {
'feature-detects': find(argv.f, metadata),
'options': find(argv.o, options)
};
inlineConfig = true;
}
if (argv.c) {
try {
configPath = fs.realpathSync(argv.c);
} catch (e) {
console.error(argv.c + ' does not exist.');
process.exit(1);
}
if (!configPath) {
configPath = path.resolve(__dirname, '../lib/config-all.json');
}
}
try {
if (!config && !configPath) {
console.error('config file, inline features, or options required.');
yargs.showHelp();
process.exit(1)
} else {
config = config || {};
if (configPath) {
config = _.extend(config, require(configPath));
}
}
} catch (e) {
console.error(configPath + ' is not valid JSON.');
console.error(e);
process.exit(1);
}
if (argv.u) {
config.minify = true;
}
Modernizr.build(config, function(output) {
fs.writeFileSync(dest, output);
log('Modernizr build saved to ' + dest);
});