import * as formidable from 'formidable' import * as fs from 'fs' import * as path from 'path' import { NextApiRequest } from 'nextjs' import FormidableForm from 'formidable/Formidable' const formParse = (form: FormidableForm, req: NextApiRequest): Promise => new Promise((resolve, reject) => { form.parse(req, (err, _, files) => { if (err) return reject(err) resolve(files) }) }) export { formParse } const getJson = (req: NextApiRequest): Promise> => new Promise>((resolve) => { if (!req.body) { let buffer = '' req.on('data', (chunk) => { buffer += chunk }) req.on('end', () => { const str = Buffer.from(buffer).toString() if (str && str.indexOf('{') > -1) resolve(JSON.parse(str)) }) } }) export { getJson } const zip = (rows: any[][]): Array => rows[0].map((_: any, c: string | number) => rows.map((row) => row[c])) const exists = (s: fs.PathLike): Promise => fs.promises .access(s) .then(() => true) .catch(() => false) export { exists, zip } const readdirRecursive = (folder: string, files: string[] = []): string[] | void => { fs.readdirSync(folder).forEach((file) => { const pathAbsolute = path.join(folder, file) if (fs.statSync(pathAbsolute).isDirectory()) { readdirRecursive(pathAbsolute, files) } else { files.push(pathAbsolute) } }) return files } export { readdirRecursive } const isNextJs = path.parse(process.argv[1]).base === 'next' export { isNextJs }