-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e01be7
commit 0484bfa
Showing
5 changed files
with
152 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os from "os"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { v4 } from "uuid"; | ||
import { z } from "zod"; | ||
|
||
export const globalConfigSchema = z.object({ | ||
userId: z.string(), | ||
}); | ||
|
||
function getConfigPath() { | ||
const appName = "napi"; | ||
const homeDir = os.homedir(); | ||
|
||
if (os.platform() === "win32") { | ||
// Windows: Use %APPDATA% | ||
const appData = | ||
process.env.APPDATA || path.join(homeDir, "AppData", "Roaming"); | ||
return path.join(appData, appName, "config.json"); | ||
} else if (os.platform() === "darwin") { | ||
// macOS: Use ~/Library/Application Support | ||
return path.join( | ||
homeDir, | ||
"Library", | ||
"Application Support", | ||
appName, | ||
"config.json", | ||
); | ||
} else { | ||
// Linux and others: Use ~/.config | ||
const configDir = | ||
process.env.XDG_CONFIG_HOME || path.join(homeDir, ".config"); | ||
return path.join(configDir, appName, "config.json"); | ||
} | ||
} | ||
|
||
async function createNewConfig(configPath: string) { | ||
const config = { | ||
userId: v4(), | ||
}; | ||
|
||
try { | ||
const configDir = path.dirname(configPath); | ||
if (!fs.existsSync(configDir)) { | ||
fs.mkdirSync(configDir, { recursive: true }); | ||
} | ||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); | ||
} catch (error) { | ||
console.error(`Failed to write config: ${error}`); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export async function getOrCreateGlobalConfig() { | ||
const configPath = getConfigPath(); | ||
|
||
let config: z.infer<typeof globalConfigSchema>; | ||
|
||
try { | ||
if (fs.existsSync(configPath)) { | ||
const content = fs.readFileSync(configPath, "utf-8"); | ||
|
||
try { | ||
config = globalConfigSchema.parse(JSON.parse(content)); | ||
} catch (error) { | ||
console.debug(`Failed to parse config: ${error}`); | ||
config = await createNewConfig(configPath); | ||
} | ||
} else { | ||
config = await createNewConfig(configPath); | ||
} | ||
} catch (error) { | ||
console.error(`Failed to read or create config: ${error}`); | ||
config = await createNewConfig(configPath); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export function updateConfig(newConfig: z.infer<typeof globalConfigSchema>) { | ||
const configPath = getConfigPath(); | ||
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import localPackageJson from "../../package.json"; | ||
|
||
export async function checkVersionMiddleware() { | ||
const currentVersion = localPackageJson.version; | ||
let latestVersion: string; | ||
|
||
try { | ||
const response = await fetch( | ||
`https://registry.npmjs.org/${localPackageJson.name}/latest`, | ||
); | ||
if (!response.ok) { | ||
console.warn( | ||
"Failed to fetch latest version from npm, ignoring version check", | ||
); | ||
} | ||
|
||
const latestPackageJson: { version: string } = await response.json(); | ||
latestVersion = latestPackageJson.version; | ||
} catch (err) { | ||
console.warn( | ||
`Failed to fetch latest version from npm, ignoring version check. Error: ${err}`, | ||
); | ||
return; | ||
} | ||
|
||
if (currentVersion !== latestVersion) { | ||
console.warn( | ||
`You are using version ${currentVersion} of ${localPackageJson.name}. ` + | ||
`The latest version is ${latestVersion}. Please update to the latest version.`, | ||
); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters