diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index b5538ce4..bef79656 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.9.0 + +### Breaking changes + +- `write` option has been removed in favor of `dry-run` that does the opposite: write by default, `dry-run` to see a preview of changes. +- `namePattern` option `fullDirectory` has been removed in favor of a better `parentDirectory` that can handle nested file structure. + ## 0.8.0 ### Breaking changes diff --git a/cli/examples/nested/accent.json b/cli/examples/nested/accent.json new file mode 100644 index 00000000..80804b60 --- /dev/null +++ b/cli/examples/nested/accent.json @@ -0,0 +1,9 @@ +{ + "files": [ + { + "format": "json", + "source": "nested/**/*/fr.json", + "target": "nested/%document_path%/%slug%.json" + } + ] +} diff --git a/cli/examples/nested/nested/admin/fr.json b/cli/examples/nested/nested/admin/fr.json new file mode 100644 index 00000000..7a9e8644 --- /dev/null +++ b/cli/examples/nested/nested/admin/fr.json @@ -0,0 +1,3 @@ +{ + "key": "value" +} diff --git a/cli/examples/nested/nested/admin/general/fr.json b/cli/examples/nested/nested/admin/general/fr.json new file mode 100644 index 00000000..7a9e8644 --- /dev/null +++ b/cli/examples/nested/nested/admin/general/fr.json @@ -0,0 +1,3 @@ +{ + "key": "value" +} diff --git a/cli/src/commands/sync.ts b/cli/src/commands/sync.ts index 9e611af0..210cb51d 100644 --- a/cli/src/commands/sync.ts +++ b/cli/src/commands/sync.ts @@ -49,8 +49,9 @@ export default class Sync extends Command { description: 'Will be used in the sync call as the "sync_type" param', options: ['smart', 'passive'] }), - write: flags.boolean({ - description: 'Write the file from the export _after_ the operation' + 'dry-run': flags.boolean({ + default: false, + description: 'Do not write the file from the export _after_ the operation' }) }; @@ -69,7 +70,7 @@ export default class Sync extends Command { await new HookRunner(document).run(Hooks.afterSync); } // After syncing (and writing) the files in Accent, the list of documents could have changed. - if (flags.write) await this.refreshProject(); + if (!flags['dry-run']) await this.refreshProject(); if (this.project!.revisions.length > 1 && flags['add-translations']) { new AddTranslationsFormatter().log(this.project!); @@ -83,7 +84,7 @@ export default class Sync extends Command { } } - if (!flags.write) return; + if (flags['dry-run']) return; const formatter = new DocumentExportFormatter(); @@ -116,9 +117,10 @@ export default class Sync extends Command { return document.paths.map(async path => { const operations = await document.sync(this.project!, path, flags); + const documentPath = document.parseDocumentName(path, document.config) if (operations.sync && !operations.peek) formatter.logSync(path); - if (operations.peek) formatter.logPeek(path, operations.peek); + if (operations.peek) formatter.logPeek(path, documentPath, operations.peek); return operations; }); diff --git a/cli/src/services/document.ts b/cli/src/services/document.ts index 1a3d8030..a6159b37 100644 --- a/cli/src/services/document.ts +++ b/cli/src/services/document.ts @@ -44,13 +44,13 @@ export default class Document { formData.append('file', fs.createReadStream(file)); formData.append( 'document_path', - this.parseDocumentName(file, this.config.namePattern) + this.parseDocumentName(file, this.config) ); formData.append('document_format', this.config.format); formData.append('language', masterLanguage); let url = `${this.apiUrl}/sync`; - if (!options.write) url = `${url}/peek`; + if (options['dry-run']) url = `${url}/peek`; if (options['sync-type']) { formData.append('sync_type', options['sync-type']); } @@ -78,7 +78,7 @@ export default class Document { formData.append('language', language); let url = `${this.apiUrl}/add-translations`; - if (!options.write) url = `${url}/peek`; + if (options['dry-run']) url = `${url}/peek`; if (options['merge-type']) { formData.append('merge_type', options['merge-type']); } @@ -95,7 +95,7 @@ export default class Document { fetchLocalFile(documentPath: string, localPath: string) { return this.paths.reduce((memo: string | null, path: string) => { if ( - this.parseDocumentName(path, this.config.namePattern) === documentPath + this.parseDocumentName(path, this.config) === documentPath ) { return localPath; } else { @@ -139,6 +139,20 @@ export default class Document { return this.writeResponseToFile(response, file); } + parseDocumentName(file: string, config: DocumentConfig): string { + if (config.namePattern === NamePattern.parentDirectory) { + const targetPrefixMatch = config.target.match(/(\w+\/)+/); + + if (targetPrefixMatch) { + return path.dirname(file).replace(targetPrefixMatch[0], '') + } else { + return path.dirname(file); + } + } + + return path.basename(file).replace(path.extname(file), ''); + } + private encodeQuery(params: string[][]) { return params .map(([name, value]) => `${name}=${encodeURIComponent(value)}`) @@ -162,22 +176,11 @@ export default class Document { return config; } - private parseDocumentName(file: string, pattern?: NamePattern): string { - if (pattern === NamePattern.parentDirectory) { - return path.basename(path.dirname(file)); - } - - if (pattern === NamePattern.fullDirectory) { - return path.dirname(file); - } - - return path.basename(file).replace(path.extname(file), ''); - } - private writeResponseToFile(response: Response, file: string) { return new Promise((resolve, reject) => { mkdirp.sync(path.dirname(file)); + console.log(response.body) const fileStream = fs.createWriteStream(file, {autoClose: true}); response.body.pipe(fileStream); response.body.on('error', reject); @@ -190,7 +193,7 @@ export default class Document { options: any, operationName: OperationName ): Promise { - if (options.write) { + if (!options['dry-run']) { if (response.status >= 400) { return {[operationName]: {success: false}, peek: false}; } diff --git a/cli/src/services/formatters/commit-operation.ts b/cli/src/services/formatters/commit-operation.ts index eab447a6..e272b183 100644 --- a/cli/src/services/formatters/commit-operation.ts +++ b/cli/src/services/formatters/commit-operation.ts @@ -8,7 +8,8 @@ import {PeekOperation} from '../../types/operation'; const MASTER_ONLY_ACTIONS = ['new', 'renew', 'remove']; export default class CommitOperationFormatter { - logSync(path: string) { + logSync(path: string, documentPath: string) { + console.log(' ', chalk.gray('Name in Accent:'), chalk.gray(documentPath)); console.log(' ', chalk.white(path)); console.log(' ', chalk.green('↑ Successfully synced in Accent')); console.log(''); @@ -32,7 +33,8 @@ export default class CommitOperationFormatter { console.log(''); } - logPeek(path: string, operations: PeekOperation) { + logPeek(path: string, documentPath: string, operations: PeekOperation) { + console.log(' ', chalk.gray('Name in Accent:'), chalk.gray(documentPath)); console.log(' ', chalk.white(path)); if (!Object.keys(operations.stats).length) { diff --git a/cli/src/types/document-config.ts b/cli/src/types/document-config.ts index f0c414f8..a117645e 100644 --- a/cli/src/types/document-config.ts +++ b/cli/src/types/document-config.ts @@ -9,8 +9,7 @@ export enum Hooks { export enum NamePattern { file = 'file', - parentDirectory = 'parentDirectory', - fullDirectory = 'fullDirectory' + parentDirectory = 'parentDirectory' } export interface HookConfig {