@@ -10,33 +10,37 @@ const fs = require('fs');
10
10
const path = require ( 'path' ) ;
11
11
const chokidar = require ( 'chokidar' ) ;
12
12
13
- const cnsl = require ( `${ process . env . PWD } /node_modules/nycopportunity/pttrn/bin/util/console` ) ;
14
- const global = require ( `${ process . env . PWD } /node_modules/nycopportunity/pttrn/config/global` ) ;
15
- const alerts = require ( `${ process . env . PWD } /node_modules/nycopportunity/pttrn/config/alerts` ) ;
16
- const config = resolve ( `${ process . env . PWD } /config/sample` ) ;
13
+ /**
14
+ * Config file for this script
15
+ */
16
+
17
+ const config = require ( `${ process . env . PWD } /config/_sample` ) ;
17
18
18
19
/**
19
- * Constants
20
+ * @pttrn Dependencies
20
21
*/
21
22
22
- const SOURCE = path . join ( global . base , global . src ) ;
23
- const DIST = path . join ( global . base , global . dist ) ;
24
- const BASE_PATH = path . join ( SOURCE , global . entry . views ) ;
23
+ const pttrn = `${ process . env . PWD } /node_modules/@nycopportunity/patterns-framework` ;
24
+ const alerts = require ( `${ pttrn } /config/alerts` ) ;
25
+ const args = require ( `${ pttrn } /bin/util/args` ) . args ;
26
+ const cnsl = require ( `${ pttrn } /bin/util/console` ) ;
25
27
26
- const EXT = '.ext' ;
28
+ /**
29
+ * Constants
30
+ */
31
+
32
+ const SRC = config . src ;
33
+ const DIST = config . dist ;
34
+ const ENTRY = config . entry ;
35
+ const EXT = config . ext ;
27
36
const GLOBS = [
28
- `${ SOURCE } / **/*${ EXT } `
37
+ `${ config . src } / ${ config . dist } / **/*${ config . ext } `
29
38
] ;
30
39
31
- /** Process CLI args */
32
-
33
- const args = require ( `${ __dirname } /util/args` ) . args ;
34
- const cnsl = require ( `${ __dirname } /util/console` ) ;
35
-
36
40
/**
37
41
* Our Chokidar Watcher
38
42
*
39
- * @type { Source } https://github.com/paulmillr/chokidar
43
+ * @source https://github.com/paulmillr/chokidar
40
44
*/
41
45
const watcher = chokidar . watch ( GLOBS , {
42
46
usePolling : false ,
@@ -48,68 +52,113 @@ const watcher = chokidar.watch(GLOBS, {
48
52
/**
49
53
* Write file to the distribution folder
50
54
*
51
- * @param {String } file The file source
52
- * @param {Object } data The data to pass to the file
55
+ * @param {String } file The file source
56
+ * @param {Object } data The data to pass to the file
53
57
*
54
- * @return {undefined } The result of fs.writeFileSync()
58
+ * @return {Undefined } The result of fs.writeFileSync()
55
59
*/
56
60
const write = async ( file , data ) => {
57
61
try {
58
- let dist = file . replace ( BASE_PATH , path . join ( DIST , opts . svgs ) ) ;
59
- let local = dist . replace ( process . env . PWD , '.' ) ;
62
+ let dist = file . replace ( SRC , DIST ) ;
60
63
61
- if ( ! fs . existsSync ( path . dirname ( dist ) ) ) {
62
- fs . mkdirSync ( path . dirname ( dist ) ) ;
64
+ if ( ! fs . existsSync ( path . dirname ( dist ) ) ) {
65
+ fs . mkdirSync ( path . dirname ( dist ) , { recursive : true } ) ;
63
66
}
64
67
65
68
let written = fs . writeFileSync ( dist , data ) ;
66
69
67
- cnsl . describe ( `Sample written to ${ alerts . str . path ( local ) } ` ) ;
70
+ cnsl . describe ( `${ alerts . success } ${ alerts . str . path ( file ) } written to ${ alerts . str . path ( dist ) } ` ) ;
68
71
69
72
return written ;
70
73
} catch ( err ) {
71
- cnsl . error ( `Sample (write): ${ err . stack } ` ) ;
74
+ cnsl . error ( `Failed (write): ${ err . stack } ` ) ;
72
75
}
73
76
}
74
77
75
78
/**
76
- * Main script
79
+ * A sample file read and replace method
80
+ *
81
+ * @param {String } file Path to the file
82
+ *
83
+ * @return {String } File contents
84
+ */
85
+ const replace = async ( file ) => {
86
+ let data = await fs . readFileSync ( file , 'utf-8' ) ;
87
+
88
+ return data . replace ( '{{ source }}' , 'distribution' ) ;
89
+ } ;
90
+
91
+ /**
92
+ * The main task bus for transforming contents of a source file
93
+ *
94
+ * @param {String } file Path to source file
95
+ *
96
+ * @return {String } Transformed data
77
97
*/
78
98
const main = async ( file ) => {
79
99
try {
80
- // Main functionality. Call other async functions here.
81
- // let data = await ...(file)
100
+ let data = await replace ( file ) ; // Do something with the file data here
82
101
83
102
await write ( file , data ) ;
84
103
85
104
return data ;
86
105
} catch ( err ) {
87
- cnsl . error ( `Sample failed (main): ${ err . stack } ` ) ;
106
+ cnsl . error ( `Failed (main): ${ err . stack } ` ) ;
88
107
}
89
108
} ;
90
109
91
110
/**
92
- * Runner for the sample script
111
+ * Read a specific file, if it is a directory read all of the files in it,
112
+ * then, perform the main task on the file.
113
+ *
114
+ * @param {String } file A single file or directory to recursively walk
115
+ */
116
+ const walk = async ( file ) => {
117
+ if ( file . includes ( EXT ) ) {
118
+ await main ( file ) ;
119
+ } else {
120
+ try {
121
+ let files = fs . readdirSync ( file , 'utf-8' ) ;
122
+
123
+ for ( let i = files . length - 1 ; i >= 0 ; i -- ) {
124
+ await walk ( files [ i ] , file ) ;
125
+ }
126
+ } catch ( err ) {
127
+ cnsl . error ( `Failed (walk): ${ err . stack } ` ) ;
128
+ }
129
+ }
130
+ } ;
131
+
132
+ /**
133
+ * Runner for the sample script. If the -w or --watch flag is passed it will
134
+ * run the watcher method. If a single filename is passed it will run the
135
+ * main task on the file.
136
+ *
137
+ * @type {Function }
93
138
*/
94
139
const run = async ( ) => {
95
140
if ( args . watch ) {
96
141
try {
97
142
watcher . on ( 'change' , async changed => {
98
- let local = changed . replace ( process . env . PWD , '' ) ;
143
+ cnsl . watching ( `Detected change on ${ alerts . str . path ( changed ) } ` ) ;
99
144
100
145
await main ( changed ) ;
101
-
102
- cnsl . watching ( `Detected change on ${ alerts . str . path ( `.${ local } ` ) } ` ) ;
103
146
} ) ;
104
147
105
- cnsl . watching ( `Sample watching ${ alerts . str . ext ( GLOBS . map ( g => g . replace ( process . env . PWD , '' ) ) . join ( ', ' ) ) } ` ) ;
148
+ cnsl . watching ( `Sample watching ${ alerts . str . ext ( GLOBS . join ( ', ' ) ) } ` ) ;
106
149
} catch ( err ) {
107
- cnsl . error ( `Sample (run): ${ err . stack } ` ) ;
150
+ cnsl . error ( `Failed (run): ${ err . stack } ` ) ;
108
151
}
109
152
} else {
110
- await main ( ) ;
153
+ let file = ( process . argv [ 2 ] ) ? process . argv [ 2 ] : false ;
154
+
155
+ if ( file ) {
156
+ await main ( `${ SRC } /${ ENTRY } /${ file } ` ) ;
157
+ } else {
158
+ cnsl . error ( `Failed (run): A file needs to be passed as an argument or a directory with files needs to be present if not using the ${ alerts . str . string ( '--watch' ) } flag.` ) ;
159
+ }
111
160
112
- process . exit ( ) ;
161
+ process . exit ( ) ; // One-off commands must exit
113
162
}
114
163
} ;
115
164
@@ -119,7 +168,5 @@ const run = async () => {
119
168
* @type {Object }
120
169
*/
121
170
module . exports = {
122
- main : main ,
123
- run : run ,
124
- config : config
171
+ run : run
125
172
} ;
0 commit comments