Fix document path parsing for parent directory

This commit is contained in:
Simon Prévost 2019-12-05 07:52:07 -05:00
parent 3d652a4a30
commit 30546d3181
8 changed files with 54 additions and 26 deletions

View File

@ -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

View File

@ -0,0 +1,9 @@
{
"files": [
{
"format": "json",
"source": "nested/**/*/fr.json",
"target": "nested/%document_path%/%slug%.json"
}
]
}

View File

@ -0,0 +1,3 @@
{
"key": "value"
}

View File

@ -0,0 +1,3 @@
{
"key": "value"
}

View File

@ -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;
});

View File

@ -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<OperationResponse> {
if (options.write) {
if (!options['dry-run']) {
if (response.status >= 400) {
return {[operationName]: {success: false}, peek: false};
}

View File

@ -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) {

View File

@ -9,8 +9,7 @@ export enum Hooks {
export enum NamePattern {
file = 'file',
parentDirectory = 'parentDirectory',
fullDirectory = 'fullDirectory'
parentDirectory = 'parentDirectory'
}
export interface HookConfig {