generated from cubetiq/ts-project
Task: Add write file from string and tests
This commit is contained in:
parent
27f9320bda
commit
8453af2efa
2
.gitignore
vendored
2
.gitignore
vendored
@ -118,3 +118,5 @@ out
|
|||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
# package-lock.json
|
# package-lock.json
|
||||||
|
|
||||||
|
ignore/
|
@ -12,3 +12,17 @@ export const readFileToStringDefaultEncoding = (
|
|||||||
export const readFileToJson = (relativeFilePath: string): any => {
|
export const readFileToJson = (relativeFilePath: string): any => {
|
||||||
return JSON.parse(readFileToStringDefaultEncoding(relativeFilePath))
|
return JSON.parse(readFileToStringDefaultEncoding(relativeFilePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const writeFileFromString = (
|
||||||
|
relativeFilePath: string,
|
||||||
|
content: string
|
||||||
|
): void => {
|
||||||
|
const baseDir = path.dirname(relativeFilePath)
|
||||||
|
const absolutePath = path.resolve(relativeFilePath)
|
||||||
|
|
||||||
|
if (!fs.existsSync(baseDir)) {
|
||||||
|
fs.mkdirSync(baseDir, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFileSync(absolutePath, content)
|
||||||
|
}
|
||||||
|
11
test/file.test.ts
Normal file
11
test/file.test.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { randomUUID } from "crypto"
|
||||||
|
import { writeFileFromString } from "../src/util/file.util"
|
||||||
|
|
||||||
|
test("Write File from String", () => {
|
||||||
|
const filePath = "./ignore/test.json"
|
||||||
|
const fileContent = {
|
||||||
|
key: randomUUID(),
|
||||||
|
iv: randomUUID(),
|
||||||
|
}
|
||||||
|
writeFileFromString(filePath, JSON.stringify(fileContent))
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user