8000 Make build.js work as a npm executable. · lodash/lodash@5ca2da7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ca2da7

Browse files
committed
Make build.js work as a npm executable.
Former-commit-id: fff327957854aa85a5abfe80994b59f3d0d24370
1 parent 2ac887f commit 5ca2da7

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

build.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
vm = require('vm'),
99
minify = require(path.join(__dirname, 'build', 'minify'));
1010

11+
/** The current working directory */
12+
var cwd = process.cwd();
13+
1114
/** Flag used to specify a backbone build */
1215
var isBackbone = process.argv.indexOf('backbone') > -1;
1316

@@ -871,14 +874,14 @@
871874

872875
// begin the minification process
873876
if (filterType || isBackbone || isLegacy || isMobile) {
874-
fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source);
877+
fs.writeFileSync(path.join(cwd, 'lodash.custom.js'), source);
875878
minify(source, 'lodash.custom.min', function(result) {
876-
fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result);
879+
fs.writeFileSync(path.join(cwd, 'lodash.custom.min.js'), result);
877880
});
878881
}
879882
else {
880883
minify(source, 'lodash.min', function(result) {
881-
fs.writeFileSync(path.join(__dirname, 'lodash.min.js'), result);
884+
fs.writeFileSync(path.join(cwd, 'lodash.min.js'), result);
882885
});
883886
}
884887
}());

build/minify.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
/*--------------------------------------------------------------------------*/
3636

3737
/**
38-
* The exposed `minify` function minifies a given `source` and invokes the
39-
* `onComplete` callback when finished.
38+
* The exposed `minify` function minifies a given Lo-Dash `source` and invokes
39+
* the `onComplete` callback when finished.
4040
*
4141
* @param {String} source The source to minify.
4242
* @param {String} workingName The name to give temporary files creates during the minification process.
@@ -58,7 +58,10 @@
5858
function Minify(source, workingName, onComplete) {
5959
// create the destination directory if it doesn't exist
6060
if (!fs.existsSync(distPath)) {
61-
fs.mkdirSync(distPath);
61+
// avoid errors when called as a npm executable
62+
try {
63+
fs.mkdirSync(distPath);
64+
} catch(e) { }
6265
}
6366

6467
this.compiled = {};
@@ -291,17 +294,20 @@
291294
name = this.workingName,
292295
uglified = this.uglified;
293296

294-
// save the Closure Compiled version to disk
295-
fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source);
296-
fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip);
297+
// avoid errors when called as a npm executable
298+
try {
299+
// save the Closure Compiled version to disk
300+
fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source);
301+
fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip);
297302

298-
// save the Uglified version to disk
299-
fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source);
300-
fs.writeFileSync(path.join(distPath, name + < 8000 span class="pl-s">'.uglify.js.gz'), uglified.gzip);
303+
// save the Uglified version to disk
304+
fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source);
305+
fs.writeFileSync(path.join(distPath, name + '.uglify.js.gz'), uglified.gzip);
301306

302-
// save the hybrid minified version to disk
303-
fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source);
304-
fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip);
307+
// save the hybrid minified version to disk
308+
fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source);
309+
fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip);
310+
} catch(e) { }
305311

306312
// select the smallest gzipped file and use its minified counterpart as the
307313
// official minified release (ties go to Closure Compiler)
@@ -324,7 +330,7 @@
324330
module.exports = minify;
325331
}
326332
else {
327-
// read the JavaScript source file from the first argument if the script
333+
// read the Lo-Dash source file from the first argument if the script
328334
// was invoked directly (e.g. `node minify.js source.js`) and write to
329335
// the same file
330336
(function() {

build/post-compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
if (module != require.main) {
5757
module.exports = postprocess;
5858
} else {
59-
// read the JavaScript source file from the first argument if the script
59+
// read the Lo-Dash source file from the first argument if the script
6060
// was invoked directly (e.g. `node post-compile.js source.js`) and write to
6161
// the same file
6262
(function() {

build/pre-compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
/*--------------------------------------------------------------------------*/
197197

198198
/**
199-
* Pre-process a given Lo-Dash source, preparing it for minification.
199+
* Pre-process a given Lo-Dash `source`, preparing it for minification.
200200
*
201201
* @param {String} source The source to process.
202202
* @returns {String} Returns the processed source.
@@ -361,7 +361,7 @@
361361
module.exports = preprocess;
362362
}
363363
else {
364-
// read the JavaScript source file from the first argument if the script
364+
// read the Lo-Dash source file from the first argument if the script
365365
// was invoked directly (e.g. `node pre-compile.js source.js`) and write to
366366
// the same file
367367
(function() {

0 commit comments

Comments
 (0)
0