Ensure the directory exists before saving file

This commit is contained in:
Andreas Eriksson 2021-02-27 17:20:37 +01:00
parent d61ffa0c07
commit 9701d0c4c2
1 changed files with 12 additions and 0 deletions

12
run.js
View File

@ -3,6 +3,8 @@ const nodeFetch = require('node-fetch')
const { CookieJar } = require('tough-cookie')
const fetchCookie = require('fetch-cookie/node-fetch')
const { writeFile } = require('fs/promises')
const path = require('path')
const fs = require('fs')
const init = require('./dist').default
@ -13,8 +15,18 @@ if (!personalNumber) {
process.exit(1)
}
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
const record = async (info, data) => {
const filename = `./record/${info.name}.json`
ensureDirectoryExistence(filename)
const content = {
url: info.url,
headers: info.headers,