1+ /*
2+ * Windows Script Host Command Line Interface
3+ */
4+ /*global ActiveXObject, WScript, Enumerator, cli*/
5+ //TODO: This file needs major cleanup!!!
6+
7+ var wshapi = ( function ( ) {
8+ var fso = new ActiveXObject ( "Scripting.FileSystemObject" ) ;
9+ var shell = WScript . CreateObject ( "WScript.Shell" ) ;
10+ var finalArgs = [ ] , i , args = WScript . Arguments ;
11+
12+ if ( typeof Array . prototype . forEach !== "function" ) {
13+ Array . prototype . forEach = function ( f , m ) {
14+ for ( var i = 0 , L = this . length ; i < L ; ++ i ) {
15+ f ( this [ i ] , i , m ) ;
16+ }
17+ } ;
18+ }
19+
20+ if ( typeof Array . prototype . filter !== "function" ) {
21+ Array . prototype . filter = function ( fn /*, thisp*/ ) {
22+ if ( typeof fn != "function" ) {
23+ throw new Error ( "not a function" ) ;
24+ }
25+ var res = [ ] , val , thisp = finalArgs [ 1 ] ;
26+ for ( var i = 0 , L = this . length ; i < L ; i ++ ) {
27+ if ( i in this ) {
28+ val = this [ i ] ; // in case fun mutates this
29+ if ( fn . call ( thisp , val , i , this ) ) {
30+ res . push ( val ) ;
31+ }
32+ }
33+ }
34+
35+ return res ;
36+ } ;
37+ }
38+
39+ if ( ! Array . prototype . indexOf ) {
40+ Array . prototype . indexOf = function ( searchElement /*, fromIndex */ ) {
41+ "use strict" ;
42+ if ( this === void 0 || this === null ) {
43+ throw new Error ( "unknown instance" ) ;
44+ }
45+ var t = this ;
46+ var len = t . length >>> 0 ;
47+ if ( len === 0 ) {
48+ return - 1 ;
49+ }
50+ var n = 0 ;
51+ if ( finalArgs . length > 0 ) {
52+ n = Number ( finalArgs [ 1 ] ) ;
53+ if ( n !== n ) { // shortcut for verifying if it's NaN
54+ n = 0 ;
55+ } else if ( n !== 0 && n !== Infinity && n !== - Infinity ) {
56+ n = ( n > 0 || - 1 ) * Math . floor ( Math . abs ( n ) ) ;
57+ }
58+ }
59+ if ( n >= len ) {
60+ return - 1 ;
61+ }
62+ var k = n >= 0 ? n : Math . max ( len - Math . abs ( n ) , 0 ) ;
63+ for ( ; k < len ; k ++ ) {
64+ if ( k in t && t [ k ] === searchElement ) {
65+ return k ;
66+ }
67+ }
68+ return - 1 ;
69+ } ;
70+ }
71+
72+ function traverseDir ( files , path ) {
73+ var filename , folder = fso . GetFolder ( path ) ,
74+ subFlds , fc = new Enumerator ( folder . files ) ;
75+
76+ for ( ; ! fc . atEnd ( ) ; fc . moveNext ( ) ) {
77+ filename = fc . item ( ) ;
78+ if ( / \. c s s $ / . test ( filename ) ) {
79+ files . push ( filename ) ;
80+ }
81+ }
82+
83+ subFlds = new Enumerator ( folder . SubFolders ) ;
84+ for ( ; ! subFlds . atEnd ( ) ; subFlds . moveNext ( ) ) {
85+ traverseDir ( files , subFlds . item ( ) ) ;
86+ }
87+ }
88+
89+ // turn the WScript.Arguments thing into a regular array
90+ if ( args . Length > 0 ) {
91+ for ( i = 0 ; i < args . Length ; i ++ ) {
92+ finalArgs . push ( args ( i ) ) ;
93+ }
94+ }
95+
96+ return {
97+ args : finalArgs ,
98+ print : function ( s ) { WScript . Echo ( s ) ; } ,
99+ quit : function ( v ) { WScript . Quit ( v ) ; } ,
100+
101+ isDirectory : function ( name ) {
102+ return fso . FolderExists ( name ) ;
103+ } ,
104+
105+ getFiles : function ( dir ) {
106+ var files = [ ] ;
107+ traverseDir ( files , dir ) ;
108+ return files ;
109+ } ,
110+
111+ fixFilenames : function ( files ) {
112+ return files ;
113+ } ,
114+
115+ getWorkingDirectory : function ( ) {
116+ return shell . CurrentDirectory ;
117+ } ,
118+
119+ getFullPath : function ( filename ) {
120+ return fso . GetAbsolutePathName ( filename ) ;
121+ } ,
122+
123+ readFile : function ( path ) {
124+ var forReading = 1 ;
125+ var tf = fso . OpenTextFile ( path , forReading ) ;
126+ var allText = tf . ReadAll ( ) ;
127+ tf . Close ( ) ;
128+ return allText ;
129+ }
130+ } ;
131+
132+ } ( ) ) ;
133+
134+ cli ( wshapi ) ;
0 commit comments