11'use strict' ;
22
33const BbPromise = require ( 'bluebird' ) ;
4+ const colors = require ( 'colors/safe' ) ;
5+ const fse = require ( 'fs-extra' ) ;
6+ const path = require ( 'path' ) ;
47const glob = require ( 'glob' ) ;
8+ const watchFiles = require ( './utils/watch' ) ;
59const relative = require ( './utils/relative-path' ) ;
610
7- module . exports = ( args ) => BbPromise . resolve ( args ) . then ( args => {
8- const colors = require ( 'colors/safe' ) ;
9- const fse = require ( 'fs-extra' ) ;
10- const path = require ( 'path' ) ;
11+ const copyWatcher = ( fromDir , toDir ) => ( watcher ) => watcher . on ( 'changed' , ( filePath ) => {
12+ const toPath = path . join ( toDir , path . relative ( fromDir , filePath ) ) ;
13+ fse . copySync ( filePath , toPath ) ;
14+ console . log ( ` Copying ${ colors . gray ( filePath ) } to ${ colors . gray ( toPath ) } ` ) ;
15+ } ) ;
1116
17+ module . exports = ( args ) => BbPromise . resolve ( args ) . then ( args => {
1218 console . log ( colors . yellow ( 'Copy...' ) ) ;
1319
1420 const options = Object . assign (
1521 {
1622 package : './package.json' ,
1723 outDir : './dist' ,
24+ watch : false ,
1825 } ,
1926 args || { }
2027 ) ;
@@ -26,6 +33,7 @@ module.exports = (args) => BbPromise.resolve(args).then(args => {
2633 console . log ( ' Copy package json in dist directory as well as static files.' ) ;
2734 console . log ( ` ${ colors . gray ( 'package' ) } : The npm package.json (${ options . package } )` ) ;
2835 console . log ( ` ${ colors . gray ( 'outDir' ) } : The output directory (${ options . outDir } )` ) ;
36+ console . log ( ` ${ colors . gray ( 'watch' ) } : Copy again on files changes (${ options . watch } )` ) ;
2937 return args ;
3038 }
3139
@@ -49,6 +57,9 @@ module.exports = (args) => BbPromise.resolve(args).then(args => {
4957 const toPath = relative ( copyGlobs ) ;
5058 console . log ( ` Copying ${ colors . gray ( fromPath ) } to ${ colors . gray ( toPath ) } ` ) ;
5159 fse . copySync ( fromPath , toPath ) ;
60+ if ( options . watch ) {
61+ watchFiles ( path . join ( fromPath , '*' ) ) . then ( copyWatcher ( fromPath , toPath ) ) ;
62+ }
5263 } else {
5364 // copyGlobs has the form { 'glob': 'dest folder' }
5465 for ( let sourceGlob in copyGlobs ) {
@@ -59,6 +70,9 @@ module.exports = (args) => BbPromise.resolve(args).then(args => {
5970 . map ( ( src ) => ( [ src , path . join ( toPath , path . relative ( fromPath , src ) ) ] ) )
6071 . forEach ( ( srcDst ) => fse . copySync . apply ( null , srcDst ) ) ;
6172 console . log ( ` Copying ${ colors . gray ( fromGlob ) } to ${ colors . gray ( toPath ) } ` ) ;
73+ if ( options . watch ) {
74+ watchFiles ( fromGlob ) . then ( copyWatcher ( fromPath , toPath ) ) ;
75+ }
6276 }
6377 }
6478 }
0 commit comments