index.d.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. declare namespace writeJsonFile {
  2. type Replacer = (this: unknown, key: string, value: any) => unknown;
  3. type SortKeys = (a: string, b: string) => number;
  4. interface Options {
  5. /**
  6. Indentation as a string or number of spaces. Pass in `undefined` for no formatting.
  7. @default '\t'
  8. */
  9. readonly indent?: string | number | undefined;
  10. /**
  11. Detect indentation automatically if the file exists.
  12. @default false
  13. */
  14. readonly detectIndent?: boolean;
  15. /**
  16. Sort the keys recursively. Optionally pass in a compare function.
  17. @default false
  18. */
  19. readonly sortKeys?: boolean | SortKeys;
  20. /**
  21. Passed into `JSON.stringify`.
  22. */
  23. readonly replacer?: Replacer | ReadonlyArray<number | string>;
  24. /**
  25. Mode used when writing the file.
  26. @default 0o666
  27. */
  28. readonly mode?: number;
  29. }
  30. }
  31. declare const writeJsonFile: {
  32. /**
  33. Stringify and write JSON to a file atomically.
  34. Creates directories for you as needed.
  35. @example
  36. ```
  37. import writeJsonFile = require('write-json-file');
  38. (async () => {
  39. await writeJsonFile('foo.json', {foo: true});
  40. })();
  41. ```
  42. */
  43. (
  44. filePath: string,
  45. data: unknown,
  46. options?: writeJsonFile.Options
  47. ): Promise<void>;
  48. /**
  49. Stringify and write JSON to a file atomically.
  50. Creates directories for you as needed.
  51. @example
  52. ```
  53. import writeJsonFile = require('write-json-file');
  54. writeJsonFile.sync('foo.json', {foo: true});
  55. ```
  56. */
  57. sync(
  58. filePath: string,
  59. data: unknown,
  60. options?: writeJsonFile.Options
  61. ): void;
  62. };
  63. export = writeJsonFile;