@@ -22,6 +22,7 @@ import {
2222import {
2323 COLLECTION_VARIABLES_FILE ,
2424 CONFIG_FILE ,
25+ DESCRIPTION_FILE ,
2526 DS_STORE_FILE ,
2627 ENVIRONMENT_VARIABLES_FOLDER ,
2728 GLOBAL_CONFIG_FILE_NAME ,
@@ -36,6 +37,7 @@ import {
3637 Variables ,
3738 EnvironmentVariableType ,
3839 GlobalConfig ,
40+ Description ,
3941} from "./schemas" ;
4042import { Stats } from "node:fs" ;
4143
@@ -198,28 +200,35 @@ export async function copyRecursive<T extends FsResource>(
198200 }
199201}
200202
201- export function serializeContentForWriting ( content : Record < any , any > ) {
203+ export function serializeContentForWriting ( content : string | Record < any , any > ) {
204+ if ( typeof content === "string" ) {
205+ return content ;
206+ }
202207 return JSON . stringify ( content , null , 2 ) ;
203208}
204209
205210export async function writeContent < T extends TSchema > (
206211 resource : FileResource ,
207- content : Record < any , any > ,
208- validator : T
212+ content : string | Record < any , any > ,
213+ validator : T ,
214+ parseAsJson = true
209215) : Promise < FileSystemResult < { resource : FileResource } > > {
210216 try {
211217 const serializedContent = serializeContentForWriting ( content ) ;
212- const parsedContentResult = parseContent ( serializedContent , validator ) ;
213- if ( parsedContentResult . type === "error" ) {
214- return {
215- type : "error" ,
216- error : {
217- message : parsedContentResult . error . message ,
218- path : resource . path ,
219- } ,
220- } ;
218+ if ( parseAsJson ) {
219+ const parsedContentResult = parseContent ( serializedContent , validator ) ;
220+ if ( parsedContentResult . type === "error" ) {
221+ return {
222+ type : "error" ,
223+ error : {
224+ message : parsedContentResult . error . message ,
225+ path : resource . path ,
226+ } ,
227+ } ;
228+ }
221229 }
222- console . log ( 'writing at' , resource . path ) ;
230+
231+ console . log ( "writing at" , resource . path ) ;
223232 await fsp . writeFile ( resource . path , serializedContent ) ;
224233 return {
225234 type : "success" ,
@@ -231,7 +240,7 @@ export async function writeContent<T extends TSchema>(
231240 return {
232241 type : "error" ,
233242 error : {
234- message : e . message || "An unexpected error has occured !" ,
243+ message : e . message || "An unexpected error has occurred !" ,
235244 path : resource . path ,
236245 } ,
237246 } ;
@@ -241,10 +250,17 @@ export async function writeContent<T extends TSchema>(
241250export async function parseFile < T extends TSchema > ( params : {
242251 resource : FileResource ;
243252 validator : T ;
253+ parseAsJson ?: boolean ;
244254} ) : Promise < FileSystemResult < Static < T > > > {
245- const { resource, validator } = params ;
255+ const { resource, validator, parseAsJson = true } = params ;
246256 try {
247257 const content = ( await fsp . readFile ( resource . path ) ) . toString ( ) ;
258+ if ( ! parseAsJson ) {
259+ return {
260+ type : "success" ,
261+ content,
262+ } as FileSystemResult < Static < T > > ;
263+ }
248264 const parsedContentResult = parseContent ( content , validator ) ;
249265 if ( parsedContentResult . type === "error" ) {
250266 return {
@@ -440,46 +456,88 @@ function getCollectionId(rootPath: string, fsResource: FsResource) {
440456 return getIdFromPath ( parentPath ) ;
441457}
442458
443- export async function parseFolderToCollection (
459+ async function getCollectionVariables (
444460 rootPath : string ,
445461 folder : FolderResource
446- ) : Promise < FileSystemResult < Collection > > {
462+ ) : Promise < FileSystemResult < Static < typeof Variables > > > {
447463 const varsPath = appendPath ( folder . path , COLLECTION_VARIABLES_FILE ) ;
448464 const collectionVariablesExist = await fsp
449465 . lstat ( varsPath )
450466 . then ( ( stats ) => stats . isFile ( ) )
451467 . catch ( ( ) => false ) ;
452468
453- const collectionVariablesResult = await ( async ( ) => {
454- if ( collectionVariablesExist ) {
455- return parseFile ( {
456- resource : createFsResource ( {
457- rootPath,
458- path : varsPath ,
459- type : "file" ,
460- } ) ,
461- validator : Variables ,
462- } ) ;
463- }
469+ if ( collectionVariablesExist ) {
470+ return parseFile ( {
471+ resource : createFsResource ( {
472+ rootPath,
473+ path : varsPath ,
474+ type : "file" ,
475+ } ) ,
476+ validator : Variables ,
477+ } ) ;
478+ }
464479
465- return {
466- type : "success" ,
467- content : { } ,
468- } as FileSystemResult < Static < typeof Variables > > ;
469- } ) ( ) ;
480+ return {
481+ type : "success" ,
482+ content : { } ,
483+ } as FileSystemResult < Static < typeof Variables > > ;
484+ }
485+
486+ async function getDescription (
487+ rootPath : string ,
488+ folder : FolderResource
489+ ) : Promise < FileSystemResult < string > > {
490+ const descriptionPath = appendPath ( folder . path , DESCRIPTION_FILE ) ;
491+ const descriptionFileExists = await fsp
492+ . lstat ( descriptionPath )
493+ . then ( ( stats ) => stats . isFile ( ) )
494+ . catch ( ( ) => false ) ;
495+
496+ if ( descriptionFileExists ) {
497+ return parseFile ( {
498+ resource : createFsResource ( {
499+ rootPath,
500+ path : descriptionPath ,
501+ type : "file" ,
502+ } ) ,
503+ validator : Description ,
504+ parseAsJson : false ,
505+ } ) ;
506+ }
470507
508+ return {
509+ type : "success" ,
510+ content : "" ,
511+ } ;
512+ }
513+
514+ export async function parseFolderToCollection (
515+ rootPath : string ,
516+ folder : FolderResource
517+ ) : Promise < FileSystemResult < Collection > > {
518+ const collectionVariablesResult = await getCollectionVariables (
519+ rootPath ,
520+ folder
521+ ) ;
471522 if ( collectionVariablesResult . type === "error" ) {
472523 return collectionVariablesResult ;
473524 }
474525
526+ const descriptionFileResult = await getDescription ( rootPath , folder ) ;
527+ if ( descriptionFileResult . type === "error" ) {
528+ return descriptionFileResult ;
529+ }
530+
475531 const collectionVariables = collectionVariablesResult . content ;
532+ const collectionDescription = descriptionFileResult . content ;
476533
477534 const collection : Collection = {
478535 type : "collection" ,
479536 id : getIdFromPath ( folder . path ) ,
480537 name : getNameOfResource ( folder ) ,
481538 collectionId : getCollectionId ( rootPath , folder ) ,
482539 variables : collectionVariables ,
540+ description : collectionDescription ,
483541 } ;
484542
485543 const result : FileSystemResult < Collection > = {
@@ -540,6 +598,7 @@ export function sanitizeFsResourceList(
540598 const checks : ( ( resource : FsResource ) => boolean ) [ ] = [
541599 ( resource ) => resource . path !== appendPath ( rootPath , CONFIG_FILE ) ,
542600 ( resource ) => ! resource . path . endsWith ( COLLECTION_VARIABLES_FILE ) ,
601+ ( resource ) => ! resource . path . endsWith ( DESCRIPTION_FILE ) ,
543602 ( resource ) => ! resource . path . includes ( DS_STORE_FILE ) ,
544603 ] ;
545604 if ( type === "api" ) {
0 commit comments