-
Notifications
You must be signed in to change notification settings - Fork 77
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
Showing
7 changed files
with
203 additions
and
56 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { readFile, writeFile } from "node:fs/promises"; | ||
import { loadConfig } from "@/utils/config.ts"; | ||
import { configFile } from "@/utils/config.ts"; | ||
import { outro, spinner } from "@clack/prompts"; | ||
import chalk from "chalk"; | ||
import { z } from "zod"; | ||
|
||
const argsSchema = z.array(z.string()).transform((args) => { | ||
const [command, ...locales] = args; | ||
const localeList = locales | ||
.join("") | ||
.split(",") | ||
.map((l) => l.trim()); | ||
|
||
return { | ||
command, | ||
locales: localeList, | ||
}; | ||
}); | ||
|
||
export async function localeCommand(args: string[] = []) { | ||
const { command, locales } = argsSchema.parse(args); | ||
const s = spinner(); | ||
|
||
if (!command || !["add", "remove"].includes(command)) { | ||
throw new Error("Please specify a command: add or remove"); | ||
} | ||
|
||
if (locales.length === 0) { | ||
throw new Error("Please specify at least one locale code"); | ||
} | ||
|
||
try { | ||
// Load config file | ||
const config = await loadConfig(); | ||
const { path: configPath } = await configFile(); | ||
|
||
if (!config) { | ||
throw new Error( | ||
"Configuration file not found. Please run `languine init` to create one.", | ||
); | ||
} | ||
|
||
s.start(command === "add" ? "Adding locales..." : "Removing locales..."); | ||
|
||
// Read the current config file content | ||
const configContent = await readFile(configPath, "utf-8"); | ||
|
||
let updatedContent = configContent; | ||
const currentTargets = config.locale.targets; | ||
|
||
if (command === "add") { | ||
// Add new locales that don't already exist | ||
const newLocales = locales.filter( | ||
(locale) => !currentTargets.includes(locale), | ||
); | ||
|
||
if (newLocales.length === 0) { | ||
s.stop("No new locales to add"); | ||
outro("All specified locales are already in the configuration."); | ||
return; | ||
} | ||
|
||
const updatedTargets = [...currentTargets, ...newLocales]; | ||
updatedContent = updateTargetsInConfig(configContent, updatedTargets); | ||
|
||
s.stop("Locales added successfully"); | ||
outro(`Added locales: ${newLocales.join(", ")}`); | ||
} else { | ||
// Remove specified locales | ||
const localesToRemove = locales.filter((locale) => | ||
currentTargets.includes(locale), | ||
); | ||
|
||
if (localesToRemove.length === 0) { | ||
s.stop("No locales to remove"); | ||
outro("None of the specified locales exist in the configuration."); | ||
return; | ||
} | ||
|
||
const updatedTargets = currentTargets.filter( | ||
(locale) => !localesToRemove.includes(locale), | ||
); | ||
updatedContent = updateTargetsInConfig(configContent, updatedTargets); | ||
|
||
s.stop("Locales removed successfully"); | ||
outro(`Removed locales: ${localesToRemove.join(", ")}`); | ||
} | ||
|
||
// Write the updated config back to file | ||
await writeFile(configPath, updatedContent, "utf-8"); | ||
} catch (error) { | ||
const localeError = error as Error; | ||
console.error(chalk.red(`Locale command failed: ${localeError.message}`)); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function updateTargetsInConfig( | ||
configContent: string, | ||
targets: string[], | ||
): string { | ||
if (configContent.includes("targets:")) { | ||
// For TypeScript config | ||
if (configContent.includes("defineConfig")) { | ||
return configContent.replace( | ||
/targets:\s*\[(.*?)\]/s, | ||
`targets: ["${targets.join('", "')}"]`, | ||
); | ||
} | ||
// For JSON config | ||
return configContent.replace( | ||
/"targets"\s*:\s*\[(.*?)\]/s, | ||
`"targets": ["${targets.join('", "')}"]`, | ||
); | ||
} | ||
return configContent; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { existsSync } from "node:fs"; | ||
import { dirname, join } from "node:path"; | ||
|
||
/** | ||
* Check if the current directory is within a git repository | ||
* Traverses up the directory tree until it finds a .git directory or reaches the root | ||
* @returns {boolean} True if within a git repository | ||
*/ | ||
export function isGitRepo(startPath: string = process.cwd()): boolean { | ||
const gitDir = join(startPath, ".git"); | ||
|
||
if (existsSync(gitDir)) { | ||
return true; | ||
} | ||
|
||
const parentDir = dirname(startPath); | ||
// If we've reached the root directory | ||
if (parentDir === startPath) { | ||
return false; | ||
} | ||
|
||
return isGitRepo(parentDir); | ||
} |