Skip to content

Commit

Permalink
fix: improve config logic, and attach config properties to options ex…
Browse files Browse the repository at this point in the history
…plicitly
  • Loading branch information
stepan662 committed Apr 23, 2024
1 parent 34c819d commit 2eb8f7a
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 107 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"ansi-colors": "^4.1.3",
"base32-decode": "^1.0.0",
"commander": "^11.0.0",
"commander": "^12.0.0",
"cosmiconfig": "^8.2.0",
"form-data": "^4.0.0",
"glob": "^10.3.3",
Expand Down
87 changes: 64 additions & 23 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,85 @@
"type": "string"
},
"patterns": {
"description": "File glob patterns to include",
"description": "File glob patterns to your source code, used for keys extraction.",
"type": "array",
"items": {
"type": "string"
}
},
"path": {
"description": "Path to folder with localization files for push/pull commands.",
"type": "string"
},
"format": {
"description": "Format of the exported files.",
"type": "string"
"description": "Format for push and pull operations.",
"enum": [
"JSON",
"JSON_TOLGEE",
"XLIFF",
"PO",
"APPLE_STRINGS_STRINGSDICT",
"APPLE_XLIFF",
"ANDROID_XML",
"FLUTTER_ARB",
"PROPERTIES",
"YAML_RUBY",
"YAML"
]
},
"languages": {
"description": "List of languages to pull. Leave unspecified to export them all.",
"type": "array",
"items": {
"type": "string"
"push": {
"type": "object",
"properties": {
"path": {
"description": "File glob specifying which files to include.",
"type": "string"
},
"records": { "type": "array", "items": { "$ref": "#/$defs/record" } }
}
},
"states": {
"description": "List of translation states to include. Defaults all except untranslated.",
"type": "array",
"items": {
"enum": ["UNTRANSLATED", "TRANSLATED", "REVIEWED"]
"pull": {
"type": "object",
"properties": {
"path": {
"description": "File ",
"type": "string"
},
"languages": {
"description": "List of languages to pull. Leave unspecified to export them all.",
"type": "array",
"items": {
"type": "string"
}
},
"states": {
"description": "List of translation states to include. Defaults all except untranslated.",
"type": "array",
"items": {
"enum": ["UNTRANSLATED", "TRANSLATED", "REVIEWED"]
}
},
"namespaces": {
"description": "List of namespaces to pull. Defaults to all namespaces.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"delimiter": {
"description": "Structure delimiter to use. By default, Tolgee interprets `.` as a nested structure. You can change the delimiter, or disable structure formatting by not specifying any value to the option.",
"type": ["string", "null"]
},
"namespaces": {
"description": "List of namespaces to pull. Defaults to all namespaces.",
"type": "array",
"items": {
"type": "string"
}
},
"$defs": {
"record": {
"type": "object",
"properties": {
"path": { "$ref": "#/$defs/path" },
"language": { "type": "string" },
"namespace": { "type": "string" }
}
},
"path": {
"description": "File glob specifying which files to include.",
"type": "string"
}
}
}
2 changes: 1 addition & 1 deletion src/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export type BaseExtractOptions = {
export default (config: Schema) =>
new Command('extract')
.description('Extracts strings from your projects')
.addOption(EXTRACTOR)
.addOption(EXTRACTOR.default(config.extractor))
.addCommand(extractPrint(config))
.addCommand(extractCheck(config));
2 changes: 1 addition & 1 deletion src/commands/extract/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ export default (config: Schema) =>
.description(
'Checks if the keys can be extracted automatically, and reports problems if any'
)
.addArgument(FILE_PATTERNS)
.addArgument(FILE_PATTERNS.default(config.patterns))
.action(lintHandler(config));
2 changes: 1 addition & 1 deletion src/commands/extract/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ const printHandler = (config: Schema) =>
export default (config: Schema) =>
new Command('print')
.description('Prints extracted data to the console')
.addArgument(FILE_PATTERNS)
.addArgument(FILE_PATTERNS.default(config.patterns))
.action(printHandler(config));
17 changes: 9 additions & 8 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ async function fetchZipBlob(opts: PullOptions): Promise<Blob> {
});
}

const pullHandler = (config: Schema) =>
async function (this: Command, argPath: string | undefined) {
const pullHandler = () =>
async function (this: Command, path: string | undefined) {
const opts: PullOptions = this.optsWithGlobals();
const path = argPath ?? config.path;

if (!path) {
throw new Error('Missing or argument <path>');
Expand Down Expand Up @@ -63,12 +62,13 @@ export default (config: Schema) =>
.description('Pulls translations to Tolgee')
.argument(
'[path]',
'Destination path where translation files will be stored in'
'Destination path where translation files will be stored in',
config.pull?.path
)
.addOption(
new Option('-f, --format <format>', 'Format of the exported files')
.choices(['JSON', 'XLIFF'])
.default('JSON')
.default(config.format ?? 'JSON')
.argParser((v) => v.toUpperCase())
)
.option(
Expand All @@ -80,6 +80,7 @@ export default (config: Schema) =>
'-s, --states <states...>',
'List of translation states to include. Defaults all except untranslated'
)
.default(config.pull?.languages)
.choices(['UNTRANSLATED', 'TRANSLATED', 'REVIEWED'])
.argParser((v, a: string[]) => [v.toUpperCase(), ...(a || [])])
)
Expand All @@ -88,17 +89,17 @@ export default (config: Schema) =>
'-d, --delimiter',
'Structure delimiter to use. By default, Tolgee interprets `.` as a nested structure. You can change the delimiter, or disable structure formatting by not specifying any value to the option'
)
.default('.')
.default(config.delimiter ?? '.')
.argParser((v) => v || '')
)
.addOption(
new Option(
'-n, --namespaces <namespaces...>',
'List of namespaces to pull. Defaults to all namespaces'
)
).default(config.pull?.namespaces)
)
.option(
'-o, --overwrite',
'Whether to automatically overwrite existing files. BE CAREFUL, THIS WILL WIPE *ALL* THE CONTENTS OF THE TARGET FOLDER. If unspecified, the user will be prompted interactively, or the command will fail when in non-interactive'
)
.action(pullHandler(config));
.action(pullHandler());
13 changes: 8 additions & 5 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ async function applyImport(client: Client) {
}
}

const pushHandler = (config: Schema) =>
async function (this: Command, pathArg: string) {
const pushHandler = () =>
async function (this: Command, path: string) {
const opts: PushOptions = this.optsWithGlobals();
const path = pathArg ?? config.path;

if (!path) {
throw new Error('Missing or argument <path>');
Expand Down Expand Up @@ -174,7 +173,11 @@ export default (config: Schema) =>
new Command()
.name('push')
.description('Pushes translations to Tolgee')
.argument('[path]', 'Path to the files to push to Tolgee')
.argument(
'[path]',
'Path to the files to push to Tolgee',
config.push?.path
)
.addOption(
new Option(
'-f, --force-mode <mode>',
Expand All @@ -183,4 +186,4 @@ export default (config: Schema) =>
.choices(['OVERRIDE', 'KEEP', 'NO'])
.argParser((v) => v.toUpperCase())
)
.action(pushHandler(config));
.action(pushHandler());
12 changes: 5 additions & 7 deletions src/commands/sync/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ type Options = BaseOptions & {
extractor: string;
};

const asyncHandler = (config: Schema) =>
async function (this: Command, filesPatterns: string[]) {
const asyncHandler = () =>
async function (this: Command, patterns: string[]) {
const opts: Options = this.optsWithGlobals();

const patterns = filesPatterns.length ? filesPatterns : config.patterns;

if (!patterns?.length) {
error('Missing argument <patterns>');
process.exit(1);
Expand Down Expand Up @@ -81,6 +79,6 @@ export default (config: Schema) =>
.description(
'Compares the keys in your code project and in the Tolgee project.'
)
.addArgument(FILE_PATTERNS)
.addOption(EXTRACTOR)
.action(asyncHandler(config));
.addArgument(FILE_PATTERNS.default(config.patterns))
.addOption(EXTRACTOR.default(config.extractor))
.action(asyncHandler());
4 changes: 2 additions & 2 deletions src/commands/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export default (config: Schema) =>
.description(
'Synchronizes the keys in your code project and in the Tolgee project, by creating missing keys and optionally deleting unused ones. For a dry-run, use `tolgee compare`.'
)
.addArgument(FILE_PATTERNS)
.addOption(EXTRACTOR)
.addArgument(FILE_PATTERNS.default(config.patterns))
.addOption(EXTRACTOR.default(config.extractor))
.option(
'-B, --backup <path>',
'Path where a backup should be downloaded before performing the sync. If something goes wrong, the backup can be used to restore the project to its previous state.'
Expand Down
19 changes: 13 additions & 6 deletions src/config/tolgeerc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ function parseConfig(input: Schema, configDir: string): Schema {
}
}

if (rc.path !== undefined) {
rc.path = resolve(configDir, rc.path);
if (rc.delimiter !== undefined) {
rc.delimiter = rc.delimiter || '';
}

// convert relative paths in config to absolute
// so it's always relative to config location

if (rc.push?.path !== undefined) {
rc.push.path = resolve(configDir, rc.push.path);
}

if (rc.pull?.path !== undefined) {
rc.pull.path = resolve(configDir, rc.pull.path);
}

if (rc.patterns !== undefined) {
Expand All @@ -54,10 +65,6 @@ function parseConfig(input: Schema, configDir: string): Schema {
);
}

if (rc.delimiter !== undefined) {
rc.delimiter = rc.delimiter || '';
}

return rc;
}

Expand Down
Loading

0 comments on commit 2eb8f7a

Please sign in to comment.