Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne St-Pierre authored and EtienneStPierre committed Dec 19, 2023
1 parent f01cb29 commit 56cb763
Show file tree
Hide file tree
Showing 10 changed files with 9,121 additions and 107 deletions.
81 changes: 35 additions & 46 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Accent CLI
Accent CLI
======

[![Version](https://img.shields.io/npm/v/accent-cli.svg)](https://npmjs.org/package/accent-cli)
[![Build Status](https://img.shields.io/travis/v/accent-cli.svg?branch=master)](https://travis-ci.com/mirego/accent-cli)

<!-- toc -->

- [Usage](#usage)
- [Configuration](#configuration)
- [Commands](#commands)
- [GitHub Actions](#github-actions)
- [License](#license)
- [About Mirego](#about-mirego)
* [Usage](#usage)
* [Configuration](#configuration)
* [Commands](#commands)
* [GitHub Actions](#github-actions)
* [License](#license)
* [About Mirego](#about-mirego)
<!-- tocstop -->

# Usage

<!-- usage -->

```sh-session
$ npm install -g accent-cli
$ accent COMMAND
Expand All @@ -28,7 +26,6 @@ USAGE
$ accent COMMAND
...
```

<!-- usagestop -->

# Configuration
Expand All @@ -41,6 +38,9 @@ accent-cli reads from a `accent.json` file. The file should contain valid JSON r
{
"apiUrl": "http://your.accent.instance",
"apiKey": "2nziVSaa8yUJxLkwoZA",
"version": {
"branchVersionPrefix": "release/"
}
"files": [
{
"format": "json",
Expand Down Expand Up @@ -68,28 +68,9 @@ Available ENV variables. (Each variable will override `accent.json` variables if
- `ACCENT_API_URL`: Api Key to your Accent Instance
- `ACCENT_PROJECT`: Your Project uuid

Each operation section `sync` and `addTranslations` can contain the following object:

- `language`: The identifier of the document’s language
- `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

## Version configuration

Format for the `accent.json` file.

- `apiUrl`: The base URL of your Accent Instance
- `apiKey`: Api Key to your Accent Instance
- `project`: Your Project uuid

Available ENV variables. (Each variable will override `accent.json` variables if set)
Version object configuration

- `ACCENT_API_KEY`: The base URL of your Accent Instance
- `ACCENT_API_URL`: Api Key to your Accent Instance
- `ACCENT_PROJECT`: Your Project uuid
- `branchVersionPrefix`: The Git branch prefix use to extract the file version

Each operation section `sync` and `addTranslations` can contain the following object:

Expand Down Expand Up @@ -156,17 +137,27 @@ Here is a list of available hooks. Those are self-explanatory
- `beforeExport`
- `afterExport`

# Commands
## Version

<!-- commands -->
Version can be extracted from the current Git branch name.

- [`accent export`](#accent-export)
- [`accent format`](#accent-format)
- [`accent help [COMMAND]`](#accent-help-command)
- [`accent jipt PSEUDOLANGUAGENAME`](#accent-jipt-pseudolanguagename)
- [`accent lint`](#accent-lint)
- [`accent stats`](#accent-stats)
- [`accent sync`](#accent-sync)
```
"version": {
"branchVersionPrefix": "release/"
}
```

Naming a branch `release/v1.0.0` will cause the `sync` and `stats` CLI commands to be invoked as if `--version=1.0.0` had been specified.

# Commands
<!-- commands -->
* [`accent export`](#accent-export)
* [`accent format`](#accent-format)
* [`accent help [COMMAND]`](#accent-help-command)
* [`accent jipt PSEUDOLANGUAGENAME`](#accent-jipt-pseudolanguagename)
* [`accent lint`](#accent-lint)
* [`accent stats`](#accent-stats)
* [`accent sync`](#accent-sync)

## `accent export`

Expand Down Expand Up @@ -308,7 +299,6 @@ EXAMPLES
```

_See code: [src/commands/sync.ts](https://github.com/mirego/accent/blob/v0.14.2/src/commands/sync.ts)_

<!-- commandsstop -->

# GitHub Actions
Expand All @@ -322,7 +312,7 @@ name: Accent

on:
schedule:
- cron: '0 4 * * *'
- cron: "0 4 * * *"

jobs:
build:
Expand All @@ -336,7 +326,7 @@ jobs:
- run: accent sync --add-translations --merge-type=passive --order-by=key
- uses: mirego/create-pull-request@v5
with:
add-paths: '*.json'
add-paths: "*.json"
commit-message: Update translations
committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Expand All @@ -351,8 +341,7 @@ In this example the translations will be synchronized daily at midnight eastern
# License
`accent-cli` is © 2019 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/accent-cli/blob/master/LICENSE.md) file.

`accent-cli` is © 2019 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/accent-cli/blob/master/LICENSE.md) file.
# About Mirego

[Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).
Expand Down
2 changes: 1 addition & 1 deletion cli/examples/simple/accent.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"version": {
"branchVersionPrefix": "release/"
}
}
}
2 changes: 1 addition & 1 deletion cli/package-lock.json

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

16 changes: 8 additions & 8 deletions cli/src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import * as chalk from 'chalk';

// Command
import { flags } from '@oclif/command';
import Command, { configFlag } from '../base';
import { CLIError } from '@oclif/errors';
import {flags} from '@oclif/command';
import Command, {configFlag} from '../base';
import {CLIError} from '@oclif/errors';

// Services
import Formatter from '../services/formatters/project-stats';
import ProjectFetcher from '../services/project-fetcher';
import { Revision } from '../types/project';
import {Revision} from '../types/project';

export default class Stats extends Command {
static description = 'Fetch stats from the API and display them beautifully';
Expand All @@ -27,16 +27,16 @@ export default class Stats extends Command {
};

async run() {
const { flags } = this.parse(Stats);
const {flags} = this.parse(Stats);

if (this.projectConfig.config.version?.identifier && !flags.version) {
flags.version = this.projectConfig.config.version.identifier
flags.version = this.projectConfig.config.version.identifier;
}

if (flags.version) {
const config = this.projectConfig.config;
const fetcher = new ProjectFetcher();
const response = await fetcher.fetch(config, { versionId: flags.version });
const response = await fetcher.fetch(config, {versionId: flags.version});

this.project = response.project;
}
Expand All @@ -61,7 +61,7 @@ export default class Stats extends Command {
chalk.red(
`Project${versionFormat} has ${conflictsCount} strings to be reviewed`
),
{ exit: 1 }
{exit: 1}
);
}
}
Expand Down
28 changes: 14 additions & 14 deletions cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Vendor
import { flags } from '@oclif/command';
import { existsSync } from 'fs';
import {flags} from '@oclif/command';
import {existsSync} from 'fs';

// Command
import Command, { configFlag } from '../base';
import Command, {configFlag} from '../base';

// Formatters
import AddTranslationsFormatter from '../services/formatters/project-add-translations';
Expand All @@ -17,10 +17,10 @@ import CommitOperationFormatter from '../services/formatters/commit-operation';
import DocumentExportFormatter from '../services/formatters/document-export';
import HookRunner from '../services/hook-runner';

import { fetchFromRevision } from '../services/revision-slug-fetcher';
import {fetchFromRevision} from '../services/revision-slug-fetcher';

// Types
import { Hooks } from '../types/document-config';
import {Hooks} from '../types/document-config';

export default class Sync extends Command {
static description =
Expand Down Expand Up @@ -70,12 +70,12 @@ export default class Sync extends Command {
};

async run() {
const { flags } = this.parse(Sync);
const {flags} = this.parse(Sync);
const t0 = process.hrtime.bigint();
const documents = this.projectConfig.files();

if (this.projectConfig.config.version?.identifier && !flags.version) {
flags.version = this.config.version
flags.version = this.config.version;
}

// From all the documentConfigs, do the sync or peek operations and log the results.
Expand Down Expand Up @@ -121,7 +121,7 @@ export default class Sync extends Command {
const targets = new DocumentPathsFetcher().fetch(this.project!, document);

for (const target of targets) {
const { path, language, documentPath } = target;
const {path, language, documentPath} = target;
const localFile = document.fetchLocalFile(documentPath, path);
formatter.log(path, documentPath, language);

Expand All @@ -136,7 +136,7 @@ export default class Sync extends Command {
}

private async syncDocumentConfig(document: Document) {
const { flags } = this.parse(Sync);
const {flags} = this.parse(Sync);
const formatter = new CommitOperationFormatter();

for (const path of document.paths) {
Expand All @@ -152,25 +152,25 @@ export default class Sync extends Command {
}

private async addTranslationsDocumentConfig(document: Document) {
const { flags } = this.parse(Sync);
const {flags} = this.parse(Sync);
const formatter = new CommitOperationFormatter();
const masterLanguage = fetchFromRevision(this.project!.masterRevision);

const targets = new DocumentPathsFetcher()
.fetch(this.project!, document)
.filter(({ language }) => language !== masterLanguage);
.filter(({language}) => language !== masterLanguage);

const existingTargets = targets.filter(({ path }) => existsSync(path));
const existingTargets = targets.filter(({path}) => existsSync(path));

if (existingTargets.length === 0) {
targets.forEach(({ path }) => formatter.logEmptyExistingTarget(path));
targets.forEach(({path}) => formatter.logEmptyExistingTarget(path));
}
if (targets.length === 0) {
formatter.logEmptyTarget(document.config.source);
}

for (const target of existingTargets) {
const { path, language } = target;
const {path, language} = target;
const documentPath = document.parseDocumentName(path, document.config);
const operation = await document.addTranslations(
path,
Expand Down
23 changes: 16 additions & 7 deletions cli/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Vendor
import { error } from '@oclif/errors';
import { execSync } from 'child_process';
import {error} from '@oclif/errors';
import {execSync} from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as chalk from 'chalk';
// Services
import Document from './document';

// Types
import { Config } from '../types/config';
import {Config} from '../types/config';

export default class ConfigFetcher {
readonly config: Config;
Expand Down Expand Up @@ -40,8 +40,14 @@ export default class ConfigFetcher {

const branchName = this.getCurrentBranchName();

if (this.config.version?.branchVersionPrefix && branchName.startsWith(this.config.version?.branchVersionPrefix)) {
this.config.version.identifier = this.exctractVersionFromBranch(branchName, this.config.version?.branchVersionPrefix)
if (
this.config.version?.branchVersionPrefix &&
branchName.startsWith(this.config.version?.branchVersionPrefix)
) {
this.config.version.identifier = this.exctractVersionFromBranch(
branchName,
this.config.version?.branchVersionPrefix
);
}

const sameFolderPathWarning: Set<string> = new Set();
Expand All @@ -50,7 +56,7 @@ export default class ConfigFetcher {
const folderPath = this.sourceFolderPath(documentConfig.source);

const sameFolderPath = this.config.files
.filter(({ source }) => source !== documentConfig.source)
.filter(({source}) => source !== documentConfig.source)
.some(
(otherDocumentConfig) =>
this.sourceFolderPath(otherDocumentConfig.source) === folderPath
Expand Down Expand Up @@ -85,7 +91,10 @@ Only your master language should be listed in your files config.`
.replace(/[\n\r\s]+$/, '');
}

private exctractVersionFromBranch(branchName: string, gitBranchVersionMatch: string): string {
private exctractVersionFromBranch(
branchName: string,
gitBranchVersionMatch: string
): string {
return branchName.replace(gitBranchVersionMatch, '');
}
}
Loading

0 comments on commit 56cb763

Please sign in to comment.