@kyoji2/intercom-cli 0.1.0 → 0.1.6

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.
@@ -1,4 +1,5 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
+ import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
3
  import { homedir } from "node:os";
3
4
  import { join } from "node:path";
4
5
 
@@ -13,8 +14,11 @@ export class ConfigError extends Error {
13
14
  }
14
15
  }
15
16
 
16
- const CONFIG_DIR = join(homedir(), ".config", "intercom-cli");
17
- const CONFIG_FILE = join(CONFIG_DIR, "config.json");
17
+ export const DEFAULT_CONFIG_DIR = join(homedir(), ".config", "intercom-cli");
18
+
19
+ function getConfigFile(configDir: string): string {
20
+ return join(configDir, "config.json");
21
+ }
18
22
 
19
23
  function isValidConfig(data: unknown): data is Config {
20
24
  if (typeof data !== "object" || data === null) return false;
@@ -22,13 +26,16 @@ function isValidConfig(data: unknown): data is Config {
22
26
  return typeof obj.token === "string" && obj.token.length > 0;
23
27
  }
24
28
 
25
- export async function loadConfig(): Promise<Config | null> {
29
+ export async function loadConfig(configDir: string): Promise<Config | null> {
26
30
  try {
27
- const file = Bun.file(CONFIG_FILE);
28
- const exists = await file.exists();
29
- if (!exists) return null;
31
+ const configFile = getConfigFile(configDir);
32
+ try {
33
+ await access(configFile);
34
+ } catch {
35
+ return null;
36
+ }
30
37
 
31
- const text = await file.text();
38
+ const text = await readFile(configFile, "utf-8");
32
39
  if (!text.trim()) return null;
33
40
 
34
41
  let data: unknown;
@@ -36,13 +43,13 @@ export async function loadConfig(): Promise<Config | null> {
36
43
  data = JSON.parse(text);
37
44
  } catch {
38
45
  throw new ConfigError(
39
- `Invalid JSON in config file: ${CONFIG_FILE}. Delete the file and run 'intercom login' again.`,
46
+ `Invalid JSON in config file: ${configFile}. Delete the file and run 'intercom login' again.`,
40
47
  );
41
48
  }
42
49
 
43
50
  if (!isValidConfig(data)) {
44
51
  throw new ConfigError(
45
- `Invalid config format in ${CONFIG_FILE}. Expected { "token": "..." }. Delete the file and run 'intercom login' again.`,
52
+ `Invalid config format in ${configFile}. Expected { "token": "..." }. Delete the file and run 'intercom login' again.`,
46
53
  );
47
54
  }
48
55
 
@@ -53,11 +60,12 @@ export async function loadConfig(): Promise<Config | null> {
53
60
  }
54
61
  }
55
62
 
56
- export function loadConfigSync(): Config | null {
63
+ export function loadConfigSync(configDir: string): Config | null {
57
64
  try {
58
- if (!existsSync(CONFIG_FILE)) return null;
65
+ const configFile = getConfigFile(configDir);
66
+ if (!existsSync(configFile)) return null;
59
67
 
60
- const text = readFileSync(CONFIG_FILE, "utf-8");
68
+ const text = readFileSync(configFile, "utf-8");
61
69
  if (!text.trim()) return null;
62
70
 
63
71
  const data = JSON.parse(text);
@@ -69,25 +77,24 @@ export function loadConfigSync(): Config | null {
69
77
  }
70
78
  }
71
79
 
72
- export async function saveConfig(config: Config): Promise<void> {
80
+ export async function saveConfig(configDir: string, config: Config): Promise<void> {
73
81
  if (!config.token || typeof config.token !== "string") {
74
82
  throw new ConfigError("Invalid token: token must be a non-empty string");
75
83
  }
76
84
 
77
- await Bun.$`mkdir -p ${CONFIG_DIR}`;
78
- await Bun.write(CONFIG_FILE, JSON.stringify(config, null, 2));
85
+ const configFile = getConfigFile(configDir);
86
+ await mkdir(configDir, { recursive: true });
87
+ await writeFile(configFile, JSON.stringify(config, null, 2), "utf-8");
79
88
  }
80
89
 
81
- export async function deleteConfig(): Promise<void> {
90
+ export async function deleteConfig(configDir: string): Promise<void> {
82
91
  try {
83
- const file = Bun.file(CONFIG_FILE);
84
- if (await file.exists()) {
85
- await Bun.$`rm -f ${CONFIG_FILE}`;
86
- }
92
+ const configFile = getConfigFile(configDir);
93
+ await rm(configFile, { force: true });
87
94
  } catch {}
88
95
  }
89
96
 
90
- export function getToken(): string | null {
97
+ export function getToken(configDir: string): string | null {
91
98
  const envToken = process.env.INTERCOM_ACCESS_TOKEN;
92
99
  if (envToken) {
93
100
  if (envToken.trim().length === 0) {
@@ -96,11 +103,11 @@ export function getToken(): string | null {
96
103
  return envToken.trim();
97
104
  }
98
105
 
99
- const config = loadConfigSync();
106
+ const config = loadConfigSync(configDir);
100
107
  return config?.token || null;
101
108
  }
102
109
 
103
- export async function getTokenAsync(): Promise<string | null> {
110
+ export async function getTokenAsync(configDir: string): Promise<string | null> {
104
111
  const envToken = process.env.INTERCOM_ACCESS_TOKEN;
105
112
  if (envToken) {
106
113
  if (envToken.trim().length === 0) {
@@ -109,6 +116,6 @@ export async function getTokenAsync(): Promise<string | null> {
109
116
  return envToken.trim();
110
117
  }
111
118
 
112
- const config = await loadConfig();
119
+ const config = await loadConfig(configDir);
113
120
  return config?.token || null;
114
121
  }
@@ -1,6 +1,7 @@
1
1
  export type { Config } from "./config.ts";
2
2
  export {
3
3
  ConfigError,
4
+ DEFAULT_CONFIG_DIR,
4
5
  deleteConfig,
5
6
  getToken,
6
7
  getTokenAsync,
@@ -5,6 +5,7 @@ export type OutputFormat = "json" | "toon";
5
5
  export interface GlobalOptions {
6
6
  dryRun: boolean;
7
7
  format: OutputFormat;
8
+ configDir: string;
8
9
  }
9
10
 
10
11
  export { encodeToon };