Skip to content

Commit

Permalink
Fix namePattern parsing and add namePattern docs in README
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Aug 29, 2023
1 parent cf0662c commit d04e302
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 15 deletions.
48 changes: 48 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,56 @@ Each operation section `sync` and `addTranslations` can contain the following ob
- `format`: The format of the document
- `source`: The path of the document. This can contain glob pattern (See [the node glob library] used as a dependancy (https://github.com/isaacs/node-glob))
- `target`: Path of the target languages
- `namePattern`: Pattern to use to save the document name in Accent.
- `hooks`: List of hooks to be run

## Name pattern

`file` (default): Use the name of the file without the extension. In the example, the document name in Accent will be `Localizable`.

```
{
"files": [
{
"namePattern": "file",
"format": "strings",
"source": "Project/Resources/en.lproj/Localizable.strings",
"target": "Project/Resources/%slug%.lproj/%document_path%.strings"
}
]
}
```

`fileWithSlugSuffix`: Use the name of the file without the extension but also stripping the language slug in the file suffix. In the example, the document name in Accent will be `Localizable`.

```
{
"files": [
{
"namePattern": "fileWithSlugSuffix",
"format": "strings",
"source": "Project/Resources/Localizable.en.strings",
"target": "Project/Resources/%document_path%.%slug%.strings"
}
]
}
```

`parentDirectory`: Use the name of the directory instead of the file name. This is useful for framework which name the file with only the language. In the example, the document name in Accent will be `translations`.

```
{
"files": [
{
"namePattern": "parentDirectory",
"format": "json",
"source": "translations/en.json",
"target": "translations/%slug%.json"
}
]
}
```

## Hooks

Here is a list of available hooks. Those are self-explanatory
Expand Down
10 changes: 10 additions & 0 deletions cli/examples/directory-name-pattern/accent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"files": [
{
"format": "json",
"namePattern": "parentDirectory",
"source": "foo/translations.en.json",
"target": "foo/translations.%slug%.json"
}
]
}
3 changes: 3 additions & 0 deletions cli/examples/directory-name-pattern/foo/translations.en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "OK"
}
3 changes: 3 additions & 0 deletions cli/examples/directory-name-pattern/foo/translations.fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "ok-fr updated!"
}
10 changes: 10 additions & 0 deletions cli/examples/file-with-slug-suffix-name-pattern/accent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"files": [
{
"format": "json",
"namePattern": "fileWithSlugSuffix",
"source": "translate/test.en.json",
"target": "translate/test.%slug%.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "OK"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "OK fr"
}
2 changes: 1 addition & 1 deletion cli/src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Export extends Command {
for (const target of targets) {
const {path, language, documentPath} = target;
const localFile = document.fetchLocalFile(documentPath, path);
formatter.log(localFile, documentPath);
formatter.log(localFile, documentPath, language);

await document.export(localFile, language, documentPath, flags);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/jipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class Jipt extends Command {

for (const target of targets) {
const {path, documentPath} = target;
formatter.log(path, documentPath);
formatter.log(path, documentPath, 'jipt');

await document.exportJipt(path, documentPath);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class Sync extends Command {
for (const target of targets) {
const {path, language, documentPath} = target;
const localFile = document.fetchLocalFile(documentPath, path);
formatter.log(path, documentPath);
formatter.log(path, documentPath, language);

await document.export(localFile, language, documentPath, flags);
}
Expand Down
10 changes: 5 additions & 5 deletions cli/src/services/document-paths-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import * as path from 'path';
import {DocumentPath} from '../types/document-path';
import {Project} from '../types/project';
import Document from './document';
import {DocumentConfig} from '../types/document-config';
import {fetchFromRevisions} from './revision-slug-fetcher';

export default class DocumentPathsFetcher {
fetch(project: Project, document: Document): DocumentPath[] {
const languageSlugs = fetchFromRevisions(project.revisions);
const documentPaths: Set<string> = new Set();
project.documents.entries.forEach(({path}) => documentPaths.add(path));
document.paths.forEach((documentPath) =>
documentPaths.add(
path.basename(documentPath).replace(path.extname(documentPath), '')
)
);
document.paths.forEach((documentPath) => {
const name = document.parseDocumentName(documentPath, document.config);
documentPaths.add(name);
});

return languageSlugs.reduce((memo: DocumentPath[], slug) => {
documentPaths.forEach((path) => {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/services/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export default class Document {
const basename = path.basename(file).replace(path.extname(file), '');

if (config.namePattern === NamePattern.fileWithSlugSuffix) {
return basename.replace(path.extname(basename), '');
return basename.split('.')[0];
}

return basename;
Expand Down
7 changes: 4 additions & 3 deletions cli/src/services/formatters/document-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import * as chalk from 'chalk';
import Base from './base';

export default class DocumentExportFormatter extends Base {
log(path: string, documentPath: string) {
log(path: string, documentPath: string, language: string) {
console.log(
' ',
chalk.green('↓'),
chalk.bold.white(documentPath),
chalk.gray.dim.underline(path)
chalk.bold.white(path),
chalk.gray.dim(documentPath),
chalk.gray.dim(`→ ${language}`),
);
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/services/formatters/document-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class DocumentFormatFormatter extends Base {
}

log() {
console.log(chalk.magenta(`Formatted files (${this.paths.length})`));
console.log(chalk.magenta(`Formatting files (${this.paths.length})`));
console.log('');

for (const path of this.paths) {
Expand Down
5 changes: 4 additions & 1 deletion cli/src/services/formatters/project-add-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export default class ProjectAddTranslationsFormatter {
.join(', ');

console.log(
chalk.white.bold('Adding translations paths →'),
chalk.magenta('Adding translations paths'),
'→',
chalk.white(languages),
chalk.green('✓')
);

console.log('');
}
}
1 change: 1 addition & 0 deletions cli/src/services/formatters/project-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import * as chalk from 'chalk';
export default class ProjectExportFormatter {
log() {
console.log(chalk.magenta('Writing files locally'));
console.log('');
}
}
3 changes: 2 additions & 1 deletion cli/src/services/formatters/project-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default class ProjectSyncFormatter extends Base {
if (flags.version) logFlags.push(chalk.gray(`${flags.version}`));

console.log(
chalk.white.bold('Syncing sources →'),
chalk.magenta('Syncing sources'),
'→',
chalk.white(
`${fetchFromRevision(project.masterRevision)}`,
logFlags.join('') || null
Expand Down

0 comments on commit d04e302

Please sign in to comment.