@audienti/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE +19 -0
- package/README.md +62 -0
- package/bin/audienti.js +6 -0
- package/package.json +42 -0
- package/src/api-client.js +295 -0
- package/src/cli.js +3055 -0
- package/src/config.js +69 -0
package/src/config.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { chmod, mkdir, readFile, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
|
|
5
|
+
const CONFIG_FILENAME = "config.json";
|
|
6
|
+
|
|
7
|
+
export class ConfigError extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "ConfigError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function configDirectory(env = process.env) {
|
|
15
|
+
return env.AUDIENTI_CONFIG_HOME || join(homedir(), ".config", "audienti");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function configPath(env = process.env) {
|
|
19
|
+
return join(configDirectory(env), CONFIG_FILENAME);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function readConfig({ env = process.env } = {}) {
|
|
23
|
+
const filePath = configPath(env);
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(await readFile(filePath, "utf8"));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error.code === "ENOENT") return {};
|
|
29
|
+
if (error instanceof SyntaxError) {
|
|
30
|
+
throw new ConfigError(`Config file is invalid JSON: ${filePath}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function writeConfig(config, { env = process.env } = {}) {
|
|
38
|
+
const filePath = configPath(env);
|
|
39
|
+
const dir = dirname(filePath);
|
|
40
|
+
const tempPath = join(dir, `.config-${process.pid}-${Date.now()}.tmp`);
|
|
41
|
+
|
|
42
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
43
|
+
await chmod(dir, 0o700);
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
await writeFile(tempPath, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
47
|
+
await chmod(tempPath, 0o600);
|
|
48
|
+
await rename(tempPath, filePath);
|
|
49
|
+
await chmod(filePath, 0o600);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
await rm(tempPath, { force: true }).catch(() => {});
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function deleteConfig({ env = process.env } = {}) {
|
|
57
|
+
try {
|
|
58
|
+
await unlink(configPath(env));
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error.code !== "ENOENT") throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function maskToken(token) {
|
|
65
|
+
const value = String(token || "");
|
|
66
|
+
if (value.length <= 8) return "********";
|
|
67
|
+
|
|
68
|
+
return `${value.slice(0, 4)}...${value.slice(-4)}`;
|
|
69
|
+
}
|