diff --git a/cli/README.md b/cli/README.md index da6cd8d2f..00974e36c 100644 --- a/cli/README.md +++ b/cli/README.md @@ -38,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", @@ -65,6 +68,10 @@ 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 +Version object configuration + +- `branchVersionPrefix`: The Git branch prefix use to extract the file version + Each operation section `sync` and `addTranslations` can contain the following object: - `language`: The identifier of the document’s language @@ -130,6 +137,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 * [`accent export`](#accent-export) @@ -323,7 +342,6 @@ 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. - # 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). diff --git a/cli/examples/simple/accent.json b/cli/examples/simple/accent.json index 521492d56..3276ea730 100644 --- a/cli/examples/simple/accent.json +++ b/cli/examples/simple/accent.json @@ -5,5 +5,8 @@ "source": "localization.json", "target": "%document_path%.json" } - ] + ], + "version": { + "branchVersionPrefix": "release/" + } } diff --git a/cli/src/commands/stats.ts b/cli/src/commands/stats.ts index c78f28d18..21f6d54c9 100644 --- a/cli/src/commands/stats.ts +++ b/cli/src/commands/stats.ts @@ -32,6 +32,10 @@ export default class Stats extends Command { async run() { const {flags} = this.parse(Stats); + if (this.projectConfig.config.version?.tag && !flags.version) { + flags.version = this.projectConfig.config.version.tag; + } + if (flags.version) { const config = this.projectConfig.config; const fetcher = new ProjectFetcher(); diff --git a/cli/src/commands/sync.ts b/cli/src/commands/sync.ts index 0cef76c79..066e0ee64 100644 --- a/cli/src/commands/sync.ts +++ b/cli/src/commands/sync.ts @@ -74,6 +74,10 @@ export default class Sync extends Command { const t0 = process.hrtime.bigint(); const documents = this.projectConfig.files(); + if (this.projectConfig.config.version?.tag && !flags.version) { + flags.version = this.config.version; + } + // From all the documentConfigs, do the sync or peek operations and log the results. const syncFormatter = new SyncFormatter(); syncFormatter.log(this.project!, flags); diff --git a/cli/src/services/config.ts b/cli/src/services/config.ts index 40d434980..6b8d77b7a 100644 --- a/cli/src/services/config.ts +++ b/cli/src/services/config.ts @@ -1,9 +1,9 @@ // Vendor 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'; @@ -19,6 +19,7 @@ export default class ConfigFetcher { this.config.apiKey = process.env.ACCENT_API_KEY || this.config.apiKey; this.config.apiUrl = process.env.ACCENT_API_URL || this.config.apiUrl; this.config.project = process.env.ACCENT_PROJECT || this.config.project; + this.warnings = []; if (!this.config.apiKey) { @@ -37,6 +38,18 @@ export default class ConfigFetcher { error('You must have at least 1 document set in your config'); } + if ( + this.config.version?.branchVersionPrefix && + this.getCurrentBranchName().startsWith( + this.config.version?.branchVersionPrefix + ) + ) { + this.config.version.tag = this.extractVersionFromBranch( + this.getCurrentBranchName(), + this.config.version?.branchVersionPrefix + ); + } + const sameFolderPathWarning: Set = new Set(); this.config.files.forEach((documentConfig) => { @@ -71,4 +84,21 @@ Only your master language should be listed in your files config.` private sourceFolderPath(source: string) { return source.replace(path.basename(source), ''); } + + private getCurrentBranchName() { + try { + return execSync('git rev-parse --abbrev-ref HEAD') + .toString('utf8') + .replace(/[\n\r\s]+$/, ''); + } catch { + return ''; + } + } + + private extractVersionFromBranch( + branchName: string, + gitBranchVersionMatch: string + ): string { + return branchName.replace(gitBranchVersionMatch, ''); + } } diff --git a/cli/src/services/project-fetcher.ts b/cli/src/services/project-fetcher.ts index e7b99ab7f..9cb45677e 100644 --- a/cli/src/services/project-fetcher.ts +++ b/cli/src/services/project-fetcher.ts @@ -32,7 +32,7 @@ export default class ProjectFetcher { } private async graphql(config: Config, params: object) { - const query = `query ProjectDetails($project_id: ID! $version_id: ID) { + const query = `query ProjectDetails($project_id: ID! $versionId: ID) { viewer { user { fullname diff --git a/cli/src/types/config.ts b/cli/src/types/config.ts index baea47441..7d804ea8e 100644 --- a/cli/src/types/config.ts +++ b/cli/src/types/config.ts @@ -1,9 +1,11 @@ // Types import {DocumentConfig} from './document-config'; +import {VersionConfig} from './version-config'; export interface Config { apiUrl: string; apiKey: string; - project: string | null | undefined; + project?: string | null; + version?: VersionConfig; files: DocumentConfig[]; } diff --git a/cli/src/types/version-config.ts b/cli/src/types/version-config.ts new file mode 100644 index 000000000..b3ff488d5 --- /dev/null +++ b/cli/src/types/version-config.ts @@ -0,0 +1,4 @@ +export interface VersionConfig { + branchVersionPrefix?: string; + tag?: string; +}