forked from Modernizr/Modernizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodernizr
executable file
·121 lines (109 loc) · 2.92 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
#!/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: 'version',
describe: 'Print the version and exit'
})
.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 configPath;
var config;
function log() {
if (!argv.q) {
console.log.apply(console, arguments);
}
}
function stringify(obj, minified) {
var args = minified ? [0,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.c) {
try {
configPath = fs.realpathSync(argv.c);
} catch (e) {
console.error(argv.c + ' does not exist.');
process.exit(1);
}
} else {
configPath = path.resolve(__dirname, '../lib/config-all.json');
}
try {
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);
});