1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/fs/write-json-file.injectable.ts
Sebastian Malton 156de6138a Fix test flakiness because of path side effects, propagate uses to as many places
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-10-04 17:55:54 -04:00

31 lines
956 B
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { JsonValue } from "type-fest";
import getDirnameOfPathInjectable from "../path/get-dirname.injectable";
import fsInjectable from "./fs.injectable";
export type WriteJson = (filePath: string, contents: JsonValue) => Promise<void>;
const writeJsonFileInjectable = getInjectable({
id: "write-json-file",
instantiate: (di): WriteJson => {
const { writeJson, ensureDir } = di.inject(fsInjectable);
const getDirnameOfPath = di.inject(getDirnameOfPathInjectable);
return async (filePath, content) => {
await ensureDir(getDirnameOfPath(filePath), { mode: 0o755 });
await writeJson(filePath, content, {
encoding: "utf-8",
spaces: 2,
});
};
},
});
export default writeJsonFileInjectable;