1+ /*********************************************************************************************************************
2+ * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. *
3+ * *
4+ * Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance *
5+ * with the License. A copy of the License is located at *
6+ * *
7+ * http://aws.amazon.com/asl/ *
8+ * *
9+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES *
10+ * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions *
11+ * and limitations under the License. *
12+ *********************************************************************************************************************/
13+
14+ /**
15+ * @author Solution Builders
16+ */
17+
18+ 'use strict' ;
19+
20+ const fs = require ( 'fs' ) ;
21+ const path = require ( 'path' ) ;
22+ const args = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
23+
24+ let getFileList = function ( path ) {
25+ let fileInfo ;
26+ let filesFound ;
27+ let fileList = [ ] ;
28+
29+ filesFound = fs . readdirSync ( path ) ;
30+ for ( let i = 0 ; i < filesFound . length ; i ++ ) {
31+ fileInfo = fs . lstatSync ( [ path , filesFound [ i ] ] . join ( '/' ) ) ;
32+ if ( fileInfo . isFile ( ) ) {
33+ fileList . push ( filesFound [ i ] ) ;
34+ }
35+
36+ if ( fileInfo . isDirectory ( ) ) {
37+ console . log ( [ path , filesFound [ i ] ] . join ( '/' ) ) ;
38+ }
39+ }
40+
41+ return fileList ;
42+ } ;
43+
44+ // List all files in a directory in Node.js recursively in a synchronous fashion
45+ let walkSync = function ( dir , filelist ) {
46+ // let filelist = []; //getFileList('./temp/site');
47+ let files = fs . readdirSync ( dir ) ;
48+ filelist = filelist || [ ] ;
49+ files . forEach ( function ( file ) {
50+ if ( fs . statSync ( path . join ( dir , file ) ) . isDirectory ( ) ) {
51+ filelist = walkSync ( path . join ( dir , file ) , filelist ) ;
52+ } else {
53+ filelist . push ( path . join ( dir , file ) ) ;
54+ }
55+ } ) ;
56+
57+ return filelist ;
58+ } ;
59+
60+ let _filelist = [ ] ;
61+ let _manifest = {
62+ files : [ ]
63+ } ;
64+
65+ if ( ! args . hasOwnProperty ( 'target' ) ) {
66+ console . log ( '--target parameter missing. This should be the target directory containing content for the manifest.' ) ;
67+ process . exit ( 1 ) ;
68+ }
69+
70+ if ( ! args . hasOwnProperty ( 'output' ) ) {
71+ console . log ( '--ouput parameter missing. This should be the out directory where the manifest file will be generated.' ) ;
72+ process . exit ( 1 ) ;
73+ }
74+
75+ console . log ( `Generating a manifest file ${ args . output } for directory ${ args . target } ` ) ;
76+
77+ walkSync ( args . target , _filelist ) ;
78+
79+ for ( let i = 0 ; i < _filelist . length ; i ++ ) {
80+ _manifest . files . push ( _filelist [ i ] . replace ( `${ args . target } /` , '' ) ) ;
81+ } ;
82+
83+ fs . writeFileSync ( args . output , JSON . stringify ( _manifest , null , 4 ) ) ;
84+ console . log ( `Manifest file ${ args . output } generated.` ) ;
0 commit comments