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 Etienne St-Pierre committed Dec 19, 2023
1 parent 714f6bc commit 52a7d03
Show file tree
Hide file tree
Showing 11 changed files with 9,104 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
elixir 1.15.6-otp-25
erlang 24.1
erlang 25.0.2
nodejs 16.15.1
38 changes: 17 additions & 21 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 +71,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,6 +140,18 @@ Here is a list of available hooks. Those are self-explanatory
- `beforeExport`
- `afterExport`

## Version

Version can be extracted from the current Git branch name.

```
"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 -->
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, '');
}
}
18 changes: 9 additions & 9 deletions cli/src/services/project-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// Vendor
import { CLIError } from '@oclif/errors';
import {CLIError} from '@oclif/errors';
import * as chalk from 'chalk';
import fetch from 'node-fetch';

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

export default class ProjectFetcher {
async fetch(config: Config, params?: object): Promise<ProjectViewer> {
const response = await this.graphql(config, params || {});
try {
const data = (await response.json()) as { data: any };
const data = (await response.json()) as {data: any};

if (!data.data) {
throw new CLIError(
chalk.red(`Can’t find the project for the key: ${config.apiKey}`),
{ exit: 1 }
{exit: 1}
);
}

Expand All @@ -26,7 +26,7 @@ export default class ProjectFetcher {
chalk.red(
`Can’t fetch the project on ${config.apiUrl} with key ${config.apiKey}`
),
{ exit: 1 }
{exit: 1}
);
}
}
Expand Down Expand Up @@ -106,11 +106,11 @@ export default class ProjectFetcher {
}
}`;

const configParams = config.project ? { project_id: config.project } : {};
const variables = { ...configParams, ...params };
const configParams = config.project ? {project_id: config.project} : {};
const variables = {...configParams, ...params};

return await fetch(`${config.apiUrl}/graphql`, {
body: JSON.stringify({ query, variables }),
body: JSON.stringify({query, variables}),
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${config.apiKey}`,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Types
import { DocumentConfig } from './document-config';
import { VersionConfig } from './version-config';
import {DocumentConfig} from './document-config';
import {VersionConfig} from './version-config';

export interface Config {
apiUrl: string;
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
depends_on:
- postgresql
environment:
- DUMMY_LOGIN_ENABLED=1
- PORT=4000
- DATABASE_URL=postgres://postgres@postgresql:5432/accent_development
postgresql:
Expand Down
Loading

0 comments on commit 52a7d03

Please sign in to comment.