packages/content/lib/database.js
/**
   * Read a file and transform it to be insert / updated in collection
   * @param {string} path - The path of the file.
   */
Read a file and transform it to be insert / updated in collection
@param {string} path - The path of the file.
Read a file and transform it to be insert / updated in collection @param { string } path - The path of the file .
async parseFile (path) {
    const extension = extname(path)
    if (!EXTENSIONS.includes(extension) && !this.extendParserExtensions.includes(extension)) {
      return
    }
    const stats = await fs.stat(path)
    const file = {
      path,
      extension,
      data: await fs.readFile(path, 'utf-8')
    }
    await this.callHook('file:beforeParse', file)
    const parser = ({
      '.json': data => JSON.parse(data),
      '.json5': data => JSON5.parse(data),
      '.md': data => this.markdown.toJSON(data),
      '.csv': data => this.csv.toJSON(data),
      '.yaml': data => this.yaml.toJSON(data),
      '.yml': data => this.yaml.toJSON(data),
      '.xml': data => this.xml.toJSON(data),
      ...this.extendParser
    })[extension]
    let data = []
    try {
      data = await parser(file.data, { path: file.path })
      data = Array.isArray(data) ? data : [data]
    } catch (err) {
      logger.warn(`Could not parse ${path.replace(this.srcDir, '.')}:`, err.message)
      return null
    }
    const normalizedPath = this.normalizePath(path)
    const isValidDate = (date) => {
      return date instanceof Date && !isNaN(date)
    }
    return data.map((item) => {
      const paths = normalizedPath.split('/')
      if (data.length > 1 && item.slug) {
        paths.push(item.slug)
      }
      const dir = paths.slice(0, paths.length - 1).join('/') || '/'
      const slug = paths[paths.length - 1]
      const path = paths.join('/')
      const existingCreatedAt = item.createdAt && new Date(item.createdAt)
      const existingUpdatedAt = item.updatedAt && new Date(item.updatedAt)
      return {
        slug,
        ...item,
        dir,
        path,
        extension,
        createdAt: isValidDate(existingCreatedAt) ? existingCreatedAt : stats.birthtime,
        updatedAt: isValidDate(existingUpdatedAt) ? existingUpdatedAt : stats.mtime
      }
    })
  }
async parseFile ( path ) { const extension = extname ( path ) if ( ! EXTENSIONS . includes ( extension ) && ! this . extendParserExtensions . includes ( extension ) ) { return } const stats = await fs . stat ( path ) const file = { path , extension , data : await fs . readFile ( path , 'utf-8' ) } await this . callHook ( 'file:beforeParse' , file ) const parser = ( { '.json' : data => JSON . parse ( data ) , '.json5' : data => JSON5 . parse ( data ) , '.md' : data => this . markdown . toJSON ( data ) , '.csv' : data => this . csv . toJSON ( data ) , '.yaml' : data => this . yaml . toJSON ( data ) , '.yml' : data => this . yaml . toJSON ( data ) , '.xml' : data => this . xml . toJSON ( data ) , ... this . extendParser } ) [ extension ] let data = [ ] try { data = await parser ( file . data , { path : file . path } ) data = Array . isArray ( data ) ? data : [ data ] } catch ( err ) { logger . warn ( ` ${ path . replace ( this . srcDir , '.' ) } ` , err . message ) return null } const normalizedPath = this . normalizePath ( path ) const isValidDate = ( date ) => { return date instanceof Date && ! isNaN ( date ) } return data . map ( ( item ) => { const paths = normalizedPath . split ( '/' ) if ( data . length > 1 && item . slug ) { paths . push ( item . slug ) } const dir = paths . slice ( 0 , paths . length - 1 ) . join ( '/' ) || '/' const slug = paths [ paths . length - 1 ] const path = paths . join ( '/' ) const existingCreatedAt = item . createdAt && new Date ( item . createdAt ) const existingUpdatedAt = item . updatedAt && new Date ( item . updatedAt ) return { slug , ... item , dir , path , extension , createdAt : isValidDate ( existingCreatedAt ) ? existingCreatedAt : stats . birthtime , updatedAt : isValidDate ( existingUpdatedAt ) ? existingUpdatedAt : stats . mtime } } ) }
Read a file and transform it to be insert / updated in collection
 @param {string} path - The path of the file.
Read a file and transform it to be insert / updated in collection @param { string } path - The path of the file .
// If unknown extension, skip // Get parser depending on extension // Collect data from file // Force data to be an array // Normalize path without dir and ext // Validate the existing dates to avoid wrong date format or typo // `item.slug` is necessary with JSON arrays since `slug` comes from filename by default // Extract `dir` from paths // Extract `slug` from paths // Construct full path // Overrides createdAt & updatedAt if it exists in the document // Allow slug override