From 40d46b22bdda5e527b5e7819b4b1bb1d4bec8d1d Mon Sep 17 00:00:00 2001 From: Sean Meininger Date: Sun, 9 Oct 2022 21:58:27 -0700 Subject: [PATCH] fix async for appendFile --- src/lib/fileOperations.ts | 9 ++++++++- src/main.ts | 8 +++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/fileOperations.ts b/src/lib/fileOperations.ts index ffa19a2..0c48d76 100644 --- a/src/lib/fileOperations.ts +++ b/src/lib/fileOperations.ts @@ -1,4 +1,5 @@ const fs = require("fs"); +import { generateHereDoc } from "./configHeredoc"; import { exit } from "process"; // Check for the presence of sentinel.hcl, or else one gets created @@ -9,4 +10,10 @@ export const ensureFileExists = async (filename: string) => { console.log(`${filename} not present. Are you running this in the right directory?`); exit(); } -} \ No newline at end of file +} + +export const appendHeredocToFile = async (filename:string, textToAppend:string) => { + fs.promises.appendFile(filename, textToAppend); +}; + +appendHeredocToFile('ok', generateHereDoc('arg')); \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 0f9bc8a..96febca 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,9 @@ #!/usr/bin/env node // Most of the Typescript essentials are from: https://www.section.io/engineering-education/how-to-use-typescript-with-nodejs/ -const fs = require("fs"); +import fs from "fs"; import {generateHereDoc, functionUrls} from "./lib/configHeredoc"; import {ripGrep} from "./lib/ripGrep"; -import { ensureFileExists } from "lib/fileOperations"; +import { ensureFileExists, appendHeredocToFile } from "lib/fileOperations"; const checkForCommonFunctions = async (functionsArray: Array) => { // iterate over all common functions by name @@ -14,9 +14,7 @@ const checkForCommonFunctions = async (functionsArray: Array) => { if(exitCode === 0) { console.log(`found import ${i}, appending to sentinel.hcl`); // Add heredoc to sentinel.hcl file if a match is found - await fs.appendFile("sentinel.hcl", generateHereDoc(i), (err:any) => { - if(err) throw err; - }); + await appendHeredocToFile("sentinel.hcl", generateHereDoc(i)); } } }