Tuesday 15 May 2018

Async & Recursive Function

Hey Folks!I guess i need your help now.Im writing a Script which goes through a order-file to determine a Folder structure to convert md files to a single pdf fileThese Files are in seperated FoldersFor Example/Guidelines/Guidelines/Javascript/Guidelines/C#etc.in all these Folders is a order File and markdown files which needs to converted into a single pdf file (In the Right Order of course...)My Problem right now is, the recursive function is written but it looks like - i cant get the right order because readline seems to put an whole array to my array and not line by linewhich results in a wrong order because folders get added first and in the end the subfolders...not /Folder and then /Folder/Subfolder but Folder, Folder, Folder... and then /Folder/Subfolder etc(Hope everybody understands what im trying to tell :D English isnt my native language)If someone could give me any hint, or want to see any code - feel free to ask :DEdit:CodePen: https://ift.tt/2rHLIIV CreatePDFFiles { constructor() { this.files = []; this.createFileArray(this); } async createFileArray(self) { await self._recursiveReadOrder(self); setTimeout(() => { self._createPDFFile(self.files); }, 5000); } async _recursiveReadOrder(self, orderFile = '.order', folder = '.') { let orderList = self._getOrderList(orderFile); await self._readOrderList(self, orderList, async (self, line) => { const newFolder = `${folder}/${line}`; const markdown = `${newFolder}.md`; self.files.push(markdown); if (self._isFolder(newFolder)) { orderFile = `${newFolder}/.order`; await self._recursiveReadOrder(self, orderFile, newFolder); } }); } _getOrderList(orderPath) { return readline.createInterface({ input: fs.createReadStream(orderPath) }); } async _readOrderList(self, list, callback) { await list.on('line', (line) => { callback(self, line); }); } _isFolder(folder) { return fs.existsSync(folder); } _removeDuplicates(fileArray) { return fileArray.filter((elem, pos) => { return fileArray.indexOf(elem) == pos; }); } _createPDFFile(files) { setTimeout(() => { const pdfname = files[0].replace('.md', '.pdf').replace('./', ''); markdownpdf().concat.from(files).to(`pdf/${pdfname}`, () => { console.log(pdfname, 'created'); }); }, 5000); } }

Submitted May 15, 2018 at 10:15AM by ToxicalToast

No comments:

Post a Comment