UNPKG

4.42 kBTypeScriptView Raw
1import type { MemFsEditor } from 'mem-fs-editor';
2import type { StorageRecord, StorageValue } from '../types.js';
3export type StorageOptions = {
4 name?: string;
5 /**
6 * Set true to treat name as a lodash path.
7 */
8 lodashPath?: boolean;
9 /**
10 * Set true to disable json object cache.
11 */
12 disableCache?: boolean;
13 /**
14 * Set true to cleanup cache for every fs change.
15 */
16 disableCacheByFile?: boolean;
17 /**
18 * Set true to write sorted json.
19 */
20 sorted?: boolean;
21};
22/**
23 * Storage instances handle a json file where Generator authors can store data.
24 *
25 * The `Generator` class instantiate the storage named `config` by default.
26 *
27 * @constructor
28 * @param name The name of the new storage (this is a namespace)
29 * @param fs A mem-fs editor instance
30 * @param configPath The filepath used as a storage.
31 *
32 * @example
33 * class extend Generator {
34 * writing: function() {
35 * this.config.set('coffeescript', false);
36 * }
37 * }
38 */
39declare class Storage {
40 path: string;
41 name?: string;
42 fs: MemFsEditor;
43 indent: number;
44 lodashPath: boolean;
45 disableCache: boolean;
46 disableCacheByFile: boolean;
47 sorted: boolean;
48 existed: boolean;
49 _cachedStore?: StorageRecord;
50 constructor(name: string | undefined, fs: MemFsEditor, configPath: string, options?: StorageOptions);
51 constructor(fs: MemFsEditor, configPath: string, options?: StorageOptions);
52 /**
53 * @protected
54 * @return the store content
55 */
56 readContent(): StorageRecord;
57 /**
58 * @protected
59 * @return the store content
60 */
61 writeContent(fullStore: StorageValue): string;
62 /**
63 * Return the current store as JSON object
64 * @return the store content
65 * @private
66 */
67 get _store(): StorageRecord;
68 /**
69 * Persist a configuration to disk
70 * @param val - current configuration values
71 * @private
72 */
73 _persist(value: StorageRecord): void;
74 /**
75 * Save a new object of values
76 */
77 save(): void;
78 /**
79 * Get a stored value
80 * @param key The key under which the value is stored.
81 * @return The stored value. Any JSON valid type could be returned
82 */
83 get<T extends StorageValue = StorageValue>(key: string): T;
84 /**
85 * Get a stored value from a lodash path
86 * @param path The path under which the value is stored.
87 * @return The stored value. Any JSON valid type could be returned
88 */
89 getPath<T extends StorageValue = StorageValue>(path: string): T;
90 /**
91 * Get all the stored values
92 * @return key-value object
93 */
94 getAll(): StorageRecord;
95 /**
96 * Assign a key to a value and schedule a save.
97 * @param key The key under which the value is stored
98 * @param val Any valid JSON type value (String, Number, Array, Object).
99 * @return val Whatever was passed in as val.
100 */
101 set<V = StorageValue>(value: V): V;
102 set<V = StorageValue>(key: string | number, value?: V): V | undefined;
103 /**
104 * Assign a lodash path to a value and schedule a save.
105 * @param path The key under which the value is stored
106 * @param val Any valid JSON type value (String, Number, Array, Object).
107 * @return val Whatever was passed in as val.
108 */
109 setPath(path: string | number, value: StorageValue): import("json-schema").JSONSchema7Type;
110 /**
111 * Delete a key from the store and schedule a save.
112 * @param key The key under which the value is stored.
113 */
114 delete(key: string): void;
115 /**
116 * Setup the store with defaults value and schedule a save.
117 * If keys already exist, the initial value is kept.
118 * @param defaults Key-value object to store.
119 * @return val Returns the merged options.
120 */
121 defaults(defaults: StorageRecord): StorageRecord;
122 /**
123 * @param defaults Key-value object to store.
124 * @return val Returns the merged object.
125 */
126 merge(source: StorageRecord): StorageRecord;
127 /**
128 * Create a child storage.
129 * @param path - relative path of the key to create a new storage.
130 * Some paths need to be escaped. Eg: ["dotted.path"]
131 * @return Returns a new Storage.
132 */
133 createStorage(path: string): Storage;
134 /**
135 * Creates a proxy object.
136 * @return proxy.
137 */
138 createProxy(): StorageRecord;
139}
140export default Storage;