Accent CLI
The `accent-cli` package provides a robust command-line interface for seamless interaction with an Accent localization instance. It empowers developers to automate critical localization workflows, including synchronizing translation files, managing project configurations, and executing custom hooks directly from the terminal or within CI/CD pipelines. The current stable version is 0.17.0, and it follows a semantic versioning approach, with updates released to introduce new features, improve stability, and address bug fixes in alignment with the Accent platform's development. A key differentiator is its deep integration with the Accent platform, supporting various localization file formats such as JSON and `.strings`. It offers highly flexible configuration via a dedicated `accent.json` file, which supports powerful glob-pattern matching for file paths, and environment variables for sensitive data like API keys and URLs, providing secure and adaptable deployment options.
Common errors
-
Error: Missing Accent API Key. Please set ACCENT_API_KEY environment variable or 'apiKey' in accent.json
cause The Accent API key required for authentication is not provided, either as an environment variable or in the configuration file.fixEnsure that the `ACCENT_API_KEY` environment variable is set to your valid Accent API key, or add an `"apiKey": "YOUR_API_KEY"` entry to your `accent.json` file. -
Error: Command 'xyz' not found.
cause The command executed (e.g., `accent xyz`) is not a recognized or supported subcommand of `accent-cli` for the installed version.fixVerify the command name is spelled correctly. Use `accent --help` to list all available commands and their usage. If the command should exist, ensure you have the latest stable version of `accent-cli` or a version where the command is supported. -
Error: Invalid accent.json: Unexpected token [token] in JSON at position [position]
cause The `accent.json` configuration file contains a syntax error (e.g., missing comma, unclosed quote, invalid character) that prevents it from being parsed as valid JSON.fixOpen your `accent.json` file and meticulously review its JSON syntax. Pay close attention to commas between properties, correctly balanced brackets and braces, and properly quoted string values. Utilize a JSON linter or an IDE with JSON validation to quickly identify and correct the syntax error.
Warnings
- gotcha Environment variables (e.g., `ACCENT_API_KEY`, `ACCENT_API_URL`, `ACCENT_PROJECT`) override corresponding values defined in the `accent.json` configuration file. This precedence can lead to unexpected behavior if not understood.
- breaking As an active CLI tool, the syntax, arguments, or flags for specific commands can undergo changes between minor or even patch versions. Relying on undocumented or experimental flags may lead to breakage upon upgrade.
- gotcha Incorrectly formatted glob patterns in the `source` or `target` properties within `accent.json` can cause files to be missed during synchronization or incorrectly written to unintended locations. `accent-cli` uses the `node-glob` library, which has specific syntax rules.
- gotcha The `hooks` feature allows executing arbitrary shell commands, which can introduce security vulnerabilities if the commands are not trusted or if they process untrusted input. Malicious commands could lead to data exposure or system compromise.
Install
-
npm install accent-cli -
yarn add accent-cli -
pnpm add accent-cli
Imports
- AccentCommand
import { AccentCommand } from 'accent-cli'This package is primarily a CLI tool and not intended for direct programmatic import of its internal commands or modules. Interaction is via the 'accent' binary.
- AccentConfiguration
import { AccentConfiguration } from 'accent-cli'Configuration for accent-cli is managed externally through `accent.json` files or environment variables.
- run
import { run } from 'accent-cli'To interact with accent-cli programmatically from Node.js, execute the 'accent' command using Node's `child_process` module.
Quickstart
import { exec } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
// Define paths for dummy files
const configPath = path.join(process.cwd(), 'accent.json');
const dummyLocaleDir = path.join(process.cwd(), 'localization_test');
const dummyLocaleFilePath = path.join(dummyLocaleDir, 'en.json');
// Create a dummy accent.json for demonstration
const dummyConfig = {
"apiUrl": process.env.ACCENT_API_URL ?? "http://localhost:3000",
"apiKey": process.env.ACCENT_API_KEY ?? "sk_exampleapikey123", // Replace with a real key or env var in production
"project": process.env.ACCENT_PROJECT ?? "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"files": [
{
"format": "json",
"source": "localization_test/en.json",
"target": "localization_test/%slug%/%document_path%.json"
}
]
};
fs.writeFileSync(configPath, JSON.stringify(dummyConfig, null, 2));
console.log(`Created dummy accent.json at ${configPath}`);
// Create a dummy localization file
if (!fs.existsSync(dummyLocaleDir)) {
fs.mkdirSync(dummyLocaleDir);
}
fs.writeFileSync(dummyLocaleFilePath, JSON.stringify({ "hello": "Hello from accent-cli!" }, null, 2));
console.log(`Created dummy localization/en.json at ${dummyLocaleFilePath}`);
console.log('Running accent-cli programmatically with `accent sync`...');
exec('accent sync', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing accent sync: ${error.message}`);
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
if (stderr) {
console.warn(`stderr (warnings/info): ${stderr}`);
}
console.log('accent-cli sync command executed successfully.');
// Clean up dummy files and directory
try {
fs.unlinkSync(configPath);
fs.unlinkSync(dummyLocaleFilePath);
fs.rmdirSync(dummyLocaleDir, { recursive: true });
console.log('Cleaned up dummy files and directory.');
} catch (cleanupError) {
console.error(`Error during cleanup: ${cleanupError}`);
}
});