55var fs = require ( 'fs' ) ;
66var ChildProcess = require ( 'child_process' ) ;
77var path = require ( 'path' ) ;
8- var request , unzip , gyp , wrench ; //these are dynamically required later on
8+ var request , unzip , gyp , fsextra ; //these are dynamically required later on
99
1010var originalPath = process . cwd ( ) ;
1111var tempPath = path . resolve ( require ( 'os' ) . tmpdir ( ) + '/ozwinstall-' + Math . random ( ) . toString ( 36 ) . substring ( 7 ) ) ;
@@ -97,8 +97,8 @@ function init() {
9797 console . log ( 'Temp Path:' , tempPath ) ;
9898 console . log ( 'Install Path:' , installPath ) ;
9999
100- wrench . rmdirSyncRecursive ( installPath , true ) ;
101- wrench . mkdirSyncRecursive ( installPath ) ;
100+ fsextra . removeSync ( installPath ) ;
101+ fsextra . mkdirsSync ( installPath ) ;
102102}
103103
104104function download ( url , dest , cb ) {
@@ -141,15 +141,40 @@ function build(gypArgs, cb) {
141141}
142142
143143function copyFiles ( ) {
144- wrench . copyDirSyncRecursive ( tempPath + '/ozw/open-zwave-master/config' , installPath + '/config' ) ;
145- wrench . copyDirSyncRecursive ( tempPath + '/ozw/open-zwave-master/cpp/src' , installPath + '/include' , { filter : / ^ .* \. (? ! h ) [ ^ . ] + $ / } ) ;
146- wrench . copyDirSyncRecursive ( tempPath + '/ozw/open-zwave-master/build/Release' , installPath + '/bin' ) ;
144+ fsextra . copySync ( tempPath + '/ozw/open-zwave-master/config' , installPath + '/config' ) ;
145+ fsextra . copySync ( tempPath + '/ozw/open-zwave-master/cpp/src' , installPath + '/include' , { filter : function ( src , dest ) {
146+ var stat = fs . statSync ( src ) ;
147+ return ( stat && stat . isDirectory ( ) ) || ! src . match ( / ^ .* \. (? ! h ) [ ^ . ] + $ / ) ;
148+ } } ) ;
149+ fsextra . copySync ( tempPath + '/ozw/open-zwave-master/build/Release' , installPath + '/bin' ) ;
147150}
148151
149152function handleError ( err ) {
150153 throw err ;
151154}
152155
156+ function readdirSyncRecursive ( dir ) {
157+ var results = [ ] ;
158+ var list = fs . readdirSync ( dir ) ;
159+ list . forEach ( function ( file ) {
160+ file = dir + '/' + file ;
161+ var stat = fs . statSync ( file ) ;
162+ if ( stat && stat . isDirectory ( ) ) {
163+ /* Recurse into a subdirectory */
164+ results = results . concat ( readdirSyncRecursive ( file ) ) ;
165+ } else {
166+ /* Is a file */
167+ results . push ( file ) ;
168+ }
169+ } ) ;
170+
171+
172+ return results ;
173+ }
174+
175+
176+
177+
153178module . exports = function ( opts ) {
154179 if ( / ^ w i n / . test ( process . platform ) ) {
155180 if ( process . env . OZW_HOME ) {
@@ -161,15 +186,27 @@ module.exports = function(opts) {
161186 }
162187
163188 fs . mkdirSync ( tempPath ) ;
164-
189+ fs . writeFileSync ( tempPath + '/package.json' ,
190+ '{ \r\n' +
191+ ' "name": "", \r\n' +
192+ ' "version": "", \r\n' +
193+ ' "description": "", \r\n' +
194+ ' "main": "", \r\n' +
195+ ' "dependencies": {}, \r\n' +
196+ ' "devDependencies": {}, \r\n' +
197+ ' "scripts": {}, \r\n' +
198+ ' "author": "", \r\n' +
199+ ' "license": "" \r\n' +
200+ '}'
201+ ) ;
202+
165203 process . chdir ( tempPath ) ;
166204 console . log ( 'Installing dependencies to ' + tempPath ) ;
167- ChildProcess . execSync ( 'npm install request unzip node-gyp wrench ' ) ;
205+ ChildProcess . execSync ( 'npm install request unzipper node-gyp fs-extra ' ) ;
168206 request = require ( tempPath + '/node_modules/request' ) ;
169- unzip = require ( tempPath + '/node_modules/unzip ' ) ;
207+ unzip = require ( tempPath + '/node_modules/unzipper ' ) ;
170208 gyp = require ( tempPath + '/node_modules/node-gyp' ) ( ) ;
171- wrench = require ( tempPath + '/node_modules/wrench' ) ;
172-
209+ fsextra = require ( tempPath + '/node_modules/fs-extra' ) ;
173210
174211
175212 gypOptions = opts . gyp || gypOptions ;
@@ -183,7 +220,9 @@ module.exports = function(opts) {
183220 fs . createReadStream ( tempPath + "/ozw.zip" )
184221 . pipe ( unzip . Extract ( { path : tempPath + '/ozw' } ) )
185222 . on ( 'close' , function ( ) {
186- var sources = wrench . readdirSyncRecursive ( tempPath + '/ozw/open-zwave-master/cpp' ) ;
223+ var sources = readdirSyncRecursive ( tempPath + '/ozw/open-zwave-master/cpp' ) . map ( function ( f ) { return f . replace ( tempPath + '/ozw/open-zwave-master/cpp/' , '' ) ; } ) ;
224+
225+
187226 sources = sources . filter ( function ( f ) {
188227 return f . match ( / ^ ( s r c | t i n y x m l ) / ) && f . match ( / \. ( c | c p p ) $ / ) ;
189228 } ) . map ( function ( f ) {
0 commit comments