summaryrefslogtreecommitdiff
path: root/docs/src/components/Examples/Functions/get-examples-data.js
blob: e206bab10926857312078b5ae4400ad5ccce4b7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import glob from 'tiny-glob'
import fs from 'fs'


/**
 * @returns list of templates's package.json from the examples folder
 */
async function getPkgJSON(){
    let data = []
    const paths = await glob('../examples/*/package.json',{filesOnly:true})
    paths.map((files)=>{
        let readFile =  fs.readFileSync(files)
        let json = JSON.parse(readFile)
        return data.push({...json})
    })
    return data
}

/**
 * @returns list of templates readme from the examples folder
 */
async function getExamplesREADME(){
    let data = []
    const paths = await glob('../examples/*/README.md',{filesOnly:true})
    paths.map( (files)=>{
        const buffer = fs.readFileSync(files)
        let text = buffer.toString()  
        let fileName = files.split('/')[2]
        data.push({fileName,text})
    })
    return data
}

/**
 * @returns list of template data
 */
async function getTemplateData(){ 
    let data = []
    const pkgJSONS =  await getPkgJSON()
    pkgJSONS.map((pkg)=>{
        let {name} = pkg
        name = name.replace('@example/','')
        let obj = {
            name,
            pkgJSON: pkg,
            readme:undefined,
        }
        data.push(obj)
    })
    return data
}


/**
 * 
 * @returns Array of Template objects, 
 */
async function templateData() {
    let readmeData = await getExamplesREADME()
    let templateData = await getTemplateData()
    let arr = templateData.map((obj,i)=>{
        let {name} = obj
        readmeData.map((file)=>{
            let {fileName,text} = file
            if(name === fileName)
            obj.readme = text
        })

        return obj
    })
    return arr
}

export default templateData