@@ -14,10 +14,12 @@ const gitignoreContent = '# .gitignore for Google Apps Script projects using htt
1414 * Unpack a remote google script file into seperate .js and .html files
1515 *
1616 * @param {string } rootFolder - relative path to the rootFolder of the project
17+ * @param {string } fileName - if specified, only this file will get unpacked
1718 * @returns {Promise } - A promise resolving no value
1819 */
19- function unpackRemote ( rootFolder ) {
20+ function unpackRemote ( rootFolder , fileName ) {
2021 return new Promise ( ( resolve , reject ) => {
22+ let foundSingleFile = false ;
2123 const local = path . join ( rootFolder , constants . META_DIR , constants . META_LOCAL ) ;
2224 const remote = path . join ( rootFolder , constants . META_DIR , constants . META_REMOTE ) ;
2325
@@ -39,14 +41,22 @@ function unpackRemote(rootFolder) {
3941
4042 const included = file . name . substring ( 0 , constants . INCLUDE_DIR . length + 1 ) === `${ constants . INCLUDE_DIR } /` ;
4143
44+ // What files do we need to create?
4245 if ( ! file . source . includes ( constants . IGNORE ) && ! included ) {
43- remoteFiles . push ( file ) ;
46+ if ( ! fileName ) {
47+ remoteFiles . push ( file ) ;
48+ } else if ( fileName === remoteFileName ) {
49+ remoteFiles . push ( file ) ;
50+ foundSingleFile = true ;
51+ }
52+
4453 }
4554 }
4655
47- // Synch create all necessary files
48- for ( const remoteFile of remoteFiles ) {
49- createFile ( remoteFile ) ;
56+ // Reject if we have not found our file
57+ if ( fileName && ! foundSingleFile ) {
58+ reject ( `Can't seem to find the file '${ fileName } ' in this project` ) ;
59+ return ;
5060 }
5161
5262 // Write local.json
@@ -55,28 +65,38 @@ function unpackRemote(rootFolder) {
5565 source : data ,
5666 } ) ;
5767
58- // Remove all .js and .html that were not in remote.json
59- const toDelete = [ ] ;
60- for ( const localFileName of localFiles ) {
61- const extension = path . parse ( localFileName ) . ext ;
62- if ( ( extension === '.html' || extension === '.js' ) && ! remoteNames . includes ( localFileName ) && localFileName !== constants . INCLUDE_FILE ) {
63- toDelete . push ( path . join ( rootFolder , localFileName ) ) ;
64- }
68+ // Synch create all necessary files
69+ for ( const remoteFile of remoteFiles ) {
70+ createFile ( remoteFile ) ;
6571 }
6672
67- for ( const fileToDelete of toDelete ) {
68- fs . removeSync ( fileToDelete ) ;
69- }
73+ // If there was no file specified to pull we will do a cleanup
74+ if ( ! fileName ) {
75+ // Remove all .js and .html that were not in remote.json
76+ const toDelete = [ ] ;
77+ for ( const localFileName of localFiles ) {
78+ const extension = path . parse ( localFileName ) . ext ;
79+ if ( ( extension === '.html' || extension === '.js' || extension === '.gs' ) && ! remoteNames . includes ( localFileName ) && localFileName !== constants . INCLUDE_FILE ) {
80+ toDelete . push ( path . join ( rootFolder , localFileName ) ) ;
81+ }
82+ }
83+
84+ for ( const fileToDelete of toDelete ) {
85+ if ( ! fileName ) {
86+ fs . removeSync ( fileToDelete ) ;
87+ }
88+ }
7089
71- // Remove all empty folders
72- const allFolders = getAllFolders ( rootFolder ) . sort ( ) . reverse ( ) ;
90+ // Remove all empty folders
91+ const allFolders = getAllFolders ( rootFolder ) . sort ( ) . reverse ( ) ;
7392
74- for ( const emptyFolder of allFolders ) {
75- const files = fs . readdirSync ( emptyFolder ) ;
76- if ( files . length === 0 ) {
77- fs . removeSync ( emptyFolder ) ;
78- } else if ( files . length === 1 && files [ 0 ] === '.DS_Store' ) {
79- fs . removeSync ( emptyFolder ) ;
93+ for ( const emptyFolder of allFolders ) {
94+ const files = fs . readdirSync ( emptyFolder ) ;
95+ if ( files . length === 0 ) {
96+ fs . removeSync ( emptyFolder ) ;
97+ } else if ( files . length === 1 && files [ 0 ] === '.DS_Store' ) {
98+ fs . removeSync ( emptyFolder ) ;
99+ }
80100 }
81101 }
82102
0 commit comments