Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 24f47abbd3224ba5b117eac210bc7e98cded6938 0 parents
@kmk1986 kmk1986 authored
Showing with 166 additions and 0 deletions.
  1. +5 −0 bin/uglifyjs-watcher
  2. +27 −0 package.json
  3. +134 −0 uglifyjs-watcher.js
5 bin/uglifyjs-watcher
@@ -0,0 +1,5 @@
+#!/usr/bin/env node
+// -*- js -*-
+
+require("uglifyjs-watcher").init();
+
27 package.json
@@ -0,0 +1,27 @@
+{
+ "author": {
+
+ "name" : "Moon",
+ "email" : "kmk1986@gmail.com"
+
+ },
+ "name": "uglifyjs-watcher",
+ "version": "0.0.0",
+ "repository": {
+ "url": ""
+ },
+ "engines": {
+ "node": "~0.6.10"
+ },
+ "dependencies": {
+ "uglify-js": "1.2.6"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "main": "./uglifyjs-watcher.js",
+ "bin" : {
+ "uglifyjs-watcher" : "./bin/uglifyjs-watcher"
+ }
+
+
+}
134 uglifyjs-watcher.js
@@ -0,0 +1,134 @@
+/*
+* Required Modules
+**/
+var fs = require("fs");
+var exec = require("child_process").exec;
+
+var uglifyjsWatcher = (function(process, fs, exec){
+
+ var userDir = process.cwd();
+ var list;
+
+ var isWatching = false;
+ var cmdString = "cat";
+ var minifiedIndex = 1;
+
+ var loadSettingFile = function()
+ {
+
+ if( process.argv[2] == undefined )
+ {
+ console.log("missing argument: filename");
+ process.exit();
+ }
+ else
+ {
+
+ try
+ {
+
+ list = require( userDir + "/" + process.argv[2] );
+
+ // see if we have scripts to minify
+
+ if( list.scripts == undefined )
+ {
+ console.log("can't find scripts property in the json file");
+ process.exit();
+ }
+
+ if( list.scripts.length == 0 )
+ {
+ console.log("scripts property has length of 0");
+ process.exit();
+ }
+
+ if( list.minifiedFilename == undefined )
+ {
+ console.log("please specify minifiedFilename property");
+ process.exit();
+ }
+
+ if( list['uglify-js-arguments'] == undefined )
+ {
+ console.log("please specify uglify-js arguments");
+ process.exit();
+ }
+
+ }
+ catch(e)
+ {
+ console.log("can't find a json file: " + process.argv[2] );
+ }
+
+ }
+
+ }
+
+ var generateUglifyJSCommand = function()
+ {
+ list.scripts.forEach(function(obj,index){
+
+ cmdString += " " + obj;
+
+ });
+
+ cmdString += " | uglifyjs " + list['uglify-js-arguments'] + " " + list.minifiedFilename;
+
+ }
+
+ var watchFiles = function()
+ {
+
+ list.scripts.forEach(function(file,index){
+
+ fs.watchFile( userDir + "/" + file , { persistent: true, interval: 2000}, function(curr,prev){
+
+ if( curr.mtime - prev.mtime)
+ {
+ minify();
+ }
+
+
+ } );
+
+ });
+
+
+ }
+
+ var minify = function()
+ {
+ exec(cmdString, function(error, stdout, stderr){
+
+ if( error == null && stderr == "" )
+ {
+ console.log( minifiedIndex + ". minified: " + list.minifiedFilename );
+ minifiedIndex++;
+
+ if( !isWatching )
+ {
+ watchFiles();
+ isWatching = true;
+ }
+ }
+
+ });
+ }
+
+
+ return {
+
+ init: function()
+ {
+ loadSettingFile();
+ generateUglifyJSCommand();
+ minify();
+
+ }
+
+ }
+
+}( process, fs, exec));
+
+module.exports = uglifyjsWatcher;
Please sign in to comment.
Something went wrong with that request. Please try again.