mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Co-authored-by: Sebastian Malton <sebastian@malton.name> Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
|
import type { EnsureOptions, WriteOptions } from "fs-extra";
|
|
import path from "path";
|
|
import type { JsonValue } from "type-fest";
|
|
import fsInjectable from "./fs.injectable";
|
|
|
|
interface Dependencies {
|
|
writeJson: (file: string, object: any, options?: WriteOptions | BufferEncoding | string) => Promise<void>;
|
|
ensureDir: (dir: string, options?: EnsureOptions | number) => Promise<void>;
|
|
}
|
|
|
|
const writeJsonFile = ({ writeJson, ensureDir }: Dependencies) => async (filePath: string, content: JsonValue) => {
|
|
await ensureDir(path.dirname(filePath), { mode: 0o755 });
|
|
|
|
await writeJson(filePath, content, {
|
|
encoding: "utf-8",
|
|
spaces: 2,
|
|
});
|
|
};
|
|
|
|
const writeJsonFileInjectable = getInjectable({
|
|
instantiate: (di) => {
|
|
const { writeJson, ensureDir } = di.inject(fsInjectable);
|
|
|
|
return writeJsonFile({
|
|
writeJson,
|
|
ensureDir,
|
|
});
|
|
},
|
|
|
|
lifecycle: lifecycleEnum.singleton,
|
|
});
|
|
|
|
export default writeJsonFileInjectable;
|