{"version":3,"sources":["../../../packages/tools/gulp-doc-generator/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["import fs from 'fs';\r\nimport Path from 'path';\r\nimport through2 from 'through2';\r\nimport Vinyl from 'vinyl';\r\n\r\nfunction generateSmeDocumentation(_) {\r\n    const smeDocTag = '@smeDoc';\r\n    const labelTag = '@label';\r\n    const idTag = '@id';\r\n    const overviewTag = '@overview';\r\n    const exampleTag = '@example';\r\n    const fileTag = '@file ';\r\n    const filenameTag = '@filename';\r\n    const filepathTag = '@filepath';\r\n    const commentEnd = '*/';\r\n    const internalTag = '@internal';\r\n    const parentIdTag = '@parentId';\r\n\r\n    function isDocumentationRequired(content: string): boolean {\r\n        return content.indexOf(smeDocTag) !== -1;\r\n    }\r\n\r\n    function documentControl(template: string, path: any): any {\r\n        const blocks = template.split(' *\\r\\n');\r\n        let smeDocId, smeDocLabel, overviewFileContent, parentId, isInternal = false;\r\n        const examplesMap = {};\r\n        for (let i = 0; i < blocks.length; i++) {\r\n            const lines = blocks[i].split('\\n');\r\n            if (lines[0].indexOf(smeDocTag) !== -1) {\r\n                let smeDocDetails = lines[0].split(smeDocTag)[1].split('{')[1].split('}')[0];\r\n                if (smeDocDetails.indexOf(internalTag) !== -1) {\r\n                    smeDocDetails = smeDocDetails.split(internalTag)[0].trim();\r\n                    isInternal = true;\r\n                }\r\n\r\n                if (smeDocDetails.indexOf(parentIdTag) !== -1) {\r\n                    const smeDocTree = smeDocDetails.split(parentIdTag);\r\n                    parentId = smeDocTree[1].trim();\r\n                    smeDocDetails = smeDocTree[0].trim();\r\n                }\r\n\r\n                if (smeDocDetails.indexOf(idTag) === -1 || smeDocDetails.indexOf(labelTag) === -1) {\r\n                    throw new Error('@label and/or @id are missing from @smeDoc in ' + path.name);\r\n                }\r\n                const smeDocHeader = smeDocDetails.split(idTag);\r\n                smeDocId = smeDocHeader[1].trim();\r\n                smeDocLabel = smeDocHeader[0].split(labelTag)[1].trim();\r\n            } else if (lines[0].indexOf(overviewTag) !== -1) {\r\n                if (lines[1].indexOf(fileTag) === -1 || lines[1].indexOf(filepathTag) === -1) {\r\n                    throw new Error('@file and/or @filepath are missing under @overview in ' + path.name);\r\n                }\r\n                const overviewFileDetails = lines[1].split(fileTag)[1].split('{')[1].split('}')[0];\r\n                const overviewFilePath = overviewFileDetails.split(filepathTag)[1].trim();\r\n                overviewFileContent = readFileContent(overviewFilePath, path);\r\n            } else {\r\n                if (lines[0].indexOf(exampleTag) !== -1) {\r\n                    const exampleDetails = lines[0].split(exampleTag)[1].split('{')[1].split('}')[0];\r\n\r\n                    if (exampleDetails.indexOf(idTag) === -1 || exampleDetails.indexOf(labelTag) === -1) {\r\n                        throw new Error('@label and/or @id are missing from @example in ' + path.name);\r\n                    }\r\n                    const exampleDetailsId = exampleDetails.split(idTag)[1].trim();\r\n                    const exampleDetailsLabel = exampleDetails.split(idTag)[0].split(labelTag)[1].trim();\r\n\r\n                    const exampleFiles = [];\r\n                    for (let j = 1; j < lines.length - 1; j++) {\r\n\r\n                        if (lines[j].indexOf(fileTag) === -1 || lines[j].indexOf(filepathTag) === -1\r\n                            || lines[j].indexOf(filenameTag) === -1) {\r\n                            throw new Error('One or more of @file, @filepath and @filename are missing under @example in ' + path.name);\r\n                        }\r\n\r\n                        const exampleFileDetails = lines[j].split(fileTag)[1].split('{')[1].split('}')[0];\r\n                        const exampleFilePath = exampleFileDetails.split(filepathTag)[1].trim();\r\n                        const exampleFileName = exampleFileDetails.split(filepathTag)[0].split(filenameTag)[1].trim();\r\n                        const exampleFileContent = readFileContent(exampleFilePath, path);\r\n                        const fileNameDetails = exampleFileName.split('.');\r\n                        let fileLanguage = fileNameDetails[fileNameDetails.length - 1];\r\n\r\n                        if (fileLanguage === 'ts') {\r\n                            fileLanguage = 'typescript';\r\n                        }\r\n\r\n                        const exampleFile = {\r\n                            contents: exampleFileContent,\r\n                            language: fileLanguage,\r\n                            name: exampleFileName,\r\n                            sourcePath: exampleFilePath\r\n                        };\r\n\r\n                        exampleFiles.push(exampleFile);\r\n                    }\r\n                    if (examplesMap[exampleDetailsId]) {\r\n                        throw new Error('Duplicate example ID');\r\n                    }\r\n                    examplesMap[exampleDetailsId] = {\r\n                        id: exampleDetailsId,\r\n                        name: exampleDetailsLabel,\r\n                        files: exampleFiles\r\n                    };\r\n                }\r\n            }\r\n        }\r\n\r\n        const document = {\r\n            id: smeDocId,\r\n            name: smeDocLabel,\r\n            internal: isInternal,\r\n            parentId: parentId,\r\n            overview: overviewFileContent,\r\n            api: null,\r\n            examples: examplesMap\r\n        };\r\n\r\n        return document;\r\n    }\r\n\r\n    function retrieveTemplate(content: string): string {\r\n        let template = '';\r\n        let flag = false;\r\n        const lines = content.split('\\n');\r\n\r\n        for (let i = 0; i < lines.length; i++) {\r\n            const line = lines[i];\r\n\r\n            if (flag) {\r\n                if (line.indexOf(commentEnd) === -1) {\r\n                    template = template + line + '\\n';\r\n                } else {\r\n                    break;\r\n                }\r\n            } else {\r\n                if (line.indexOf(smeDocTag) !== -1) {\r\n                    flag = true;\r\n                    template = template + line + '\\n';\r\n                }\r\n            }\r\n        }\r\n\r\n        return template;\r\n    }\r\n\r\n    function readFileContent(filepath: string, path: any): string {\r\n        const absolutePath = path.dir + '/' + filepath.slice(2);\r\n        try {\r\n            if (fs.existsSync(absolutePath)) {\r\n                return fs.readFileSync(absolutePath, 'utf8');\r\n            }\r\n        } catch (err) {\r\n            // eslint-disable-next-line no-console\r\n            console.error(err);\r\n        }\r\n    }\r\n\r\n    const documents = {};\r\n\r\n    return through2.obj(\r\n        /**\r\n         * Transform\r\n         */\r\n        function (file, encoding, callback) {\r\n            const content = file.contents.toString('utf8');\r\n            const path = Path.parse(file.path);\r\n\r\n            if (isDocumentationRequired(content)) {\r\n                const template = retrieveTemplate(content);\r\n                const jsonContent = documentControl(template, path);\r\n                documents[jsonContent.id] = jsonContent;\r\n                return callback();\r\n            }\r\n\r\n            callback();\r\n        },\r\n        /**\r\n         * Flush\r\n         */\r\n        function (callback) {\r\n            const stringContent = JSON.stringify(documents);\r\n            const docFile = new Vinyl({\r\n                // cwd: '/',\r\n                path: './documentation.json',\r\n                contents: Buffer.from(stringContent, 'utf8')\r\n            });\r\n            this.push(docFile);\r\n            callback();\r\n        }\r\n    );\r\n}\r\nmodule.exports = generateSmeDocumentation;\r\n"]}