-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteNewOptions.ts
63 lines (52 loc) · 2.25 KB
/
WriteNewOptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
Options for writing new files. Currently just lets you set the output directory, with more options possibly, maybe, potentially coming in *el futuro* (e.g. a custom unique-filename-generator function).
@property outputDirectory Where to write the files (defaults to current working directory)
*/
export type WriteNewOptions = {
outputDirectory: string;
// mode?: string, // perhaps one day
};
/**
Private class that manages the default options. You shouldn't need to (or be able to, if coding normally) use this directly - use `WriteNewOptions.default` instead.
*/
class WriteNewOptionsDefaults
{
private constructor()
{
// 🌹 Hi, mom!!
}
private static _defaultOptions?: WriteNewOptions;
static get defaultOptions(): WriteNewOptions
{
return this._defaultOptions ?? {
outputDirectory: Deno.cwd(),
};
}
static set defaultOptions(options: WriteNewOptions | null)
{
this._defaultOptions = options ?? undefined;
}
}
/**
THIS DID NOT WORK OUT 😅 (it did show up in some docs)
This type isn't supposed to show up anywhere. This is my latest workaround for JSR's slow-types checking. I actually like the slow-types checking, and this workaround isn't as onerous as the previous ones I came up with.
But, not well tested either so I hope "SlowTypesDefeater" doesn't show up in in-editor help popups, etc. (The problem at hand is that slow-types checking doesn't allow this old TypeScript saw of exporting a type and a const with the same name to get class-like convenience without class warfare.)
*/
// type SlowTypesDefeater = WriteNewOptions & { default: WriteNewOptions };
/**
Configuration object for `writeNewFile()`. Access default options via `WriteNewOptions.default`. You can also set the default options for your own app so that they needn't be passed to each call to `writeNewFile()`. (Useful if your app only writes to a single directory, for example.)
*/
export const WriteNewOptions: WriteNewOptions & { default: WriteNewOptions } = {
get default(): WriteNewOptions
{
return WriteNewOptionsDefaults.defaultOptions;
},
set default(options: WriteNewOptions | null)
{
WriteNewOptionsDefaults.defaultOptions = options;
},
get outputDirectory(): string
{
return this.default.outputDirectory;
},
};