11import fetch , { Response } from 'node-fetch' ;
22import * as unzipper from 'unzipper' ;
3+ import { zip } from 'zip-a-folder' ;
34import {
45 Uri ,
56 ProgressOptions ,
@@ -184,9 +185,9 @@ async function databaseArchiveFetcher(
184185 progressCallback ?: ProgressCallback
185186) : Promise < DatabaseItem > {
186187 progressCallback ?.( {
187- maxStep : 3 ,
188188 message : 'Getting database' ,
189189 step : 1 ,
190+ maxStep : 4 ,
190191 } ) ;
191192 if ( ! storagePath ) {
192193 throw new Error ( 'No storage path specified.' ) ;
@@ -201,9 +202,9 @@ async function databaseArchiveFetcher(
201202 }
202203
203204 progressCallback ?.( {
204- maxStep : 3 ,
205205 message : 'Opening database' ,
206206 step : 3 ,
207+ maxStep : 4 ,
207208 } ) ;
208209
209210 // find the path to the database. The actual database might be in a sub-folder
@@ -213,6 +214,13 @@ async function databaseArchiveFetcher(
213214 'codeql-database.yml'
214215 ) ;
215216 if ( dbPath ) {
217+ progressCallback ?.( {
218+ message : 'Validating and fixing source location' ,
219+ step : 4 ,
220+ maxStep : 4 ,
221+ } ) ;
222+ await ensureZippedSourceLocation ( dbPath ) ;
223+
216224 const item = await databasesManager . openDatabase ( Uri . file ( dbPath ) ) ;
217225 databasesManager . setCurrentDatabaseItem ( item ) ;
218226 return item ;
@@ -436,3 +444,24 @@ async function promptForLanguage(
436444 }
437445 ) ;
438446}
447+
448+ /**
449+ * Databases created by the old odasa tool will not have a zipped
450+ * source location. However, this extension works better if sources
451+ * are zipped.
452+ *
453+ * This function ensures that the source location is zipped. If the
454+ * `src` folder exists and the `src.zip` file does not, the `src`
455+ * folder will be zipped and then deleted.
456+ *
457+ * @param databasePath The full path to the unzipped database
458+ */
459+ async function ensureZippedSourceLocation ( databasePath : string ) : Promise < void > {
460+ const srcFolderPath = path . join ( databasePath , 'src' ) ;
461+ const srcZipPath = srcFolderPath + '.zip' ;
462+
463+ if ( ( await fs . pathExists ( srcFolderPath ) ) && ! ( await fs . pathExists ( srcZipPath ) ) ) {
464+ await zip ( srcFolderPath , srcZipPath ) ;
465+ await fs . remove ( srcFolderPath ) ;
466+ }
467+ }
0 commit comments