Skip to content

Commit

Permalink
Add warning in CLI when files config have same folder path
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Nov 11, 2022
1 parent d20d638 commit aaef6fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cli/examples/config-warning/accent.json
@@ -0,0 +1,15 @@
{
"project": "10cbf933-de92-4740-83fb-491230e940bc",
"files": [
{
"format": "json",
"source": "locales/en.json",
"target": "downloads/en.json"
},
{
"format": "json",
"source": "locales/fr.json",
"target": "downloads/fr.json"
}
]
}
4 changes: 4 additions & 0 deletions cli/examples/config-warning/locales/en.json
@@ -0,0 +1,4 @@
{
"a": "text1",
"b": "text2"
}
4 changes: 4 additions & 0 deletions cli/examples/config-warning/locales/fr.json
@@ -0,0 +1,4 @@
{
"a": "text1",
"b": "text2"
}
9 changes: 9 additions & 0 deletions cli/src/base.ts
Expand Up @@ -30,6 +30,15 @@ export default abstract class extends Command {
if (!this.project) error('Unable to fetch project');

cli.action.stop(chalk.green('✓'));

if (this.projectConfig.warnings.length) {
console.log('');
console.log(chalk.yellow.bold('Warnings:'));
}

this.projectConfig.warnings.forEach((warning) =>
console.log(chalk.yellow(warning))
);
console.log('');
}

Expand Down
32 changes: 32 additions & 0 deletions cli/src/services/config.ts
@@ -1,6 +1,8 @@
// Vendor
import {error} from '@oclif/errors';
import * as fs from 'fs-extra';
import * as path from 'path';
import chalk from 'chalk';

// Services
import Document from './document';
Expand All @@ -10,12 +12,14 @@ import {Config} from '../types/config';

export default class ConfigFetcher {
readonly config: Config;
readonly warnings: string[];

constructor() {
this.config = fs.readJsonSync('accent.json');
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) {
error(
Expand All @@ -32,11 +36,39 @@ export default class ConfigFetcher {
if (!this.config.files) {
error('You must have at least 1 document set in your config');
}

const sameFolderPathWarning: Set<string> = new Set();

this.config.files.forEach((documentConfig) => {
const folderPath = this.sourceFolderPath(documentConfig.source);

const sameFolderPath = this.config.files
.filter(({source}) => source !== documentConfig.source)
.some(
(otherDocumentConfig) =>
this.sourceFolderPath(otherDocumentConfig.source) === folderPath
);

if (sameFolderPath) sameFolderPathWarning.add(folderPath);
});

sameFolderPathWarning.forEach((folderPath) => {
this.warnings.push(
`Some files in your config file as the same folder path: ${chalk.bold(
folderPath
)}
Only your master language should be listed in your files config.`
);
});
}

files(): Document[] {
return this.config.files.map(
(documentConfig) => new Document(documentConfig, this.config)
);
}

private sourceFolderPath(source: string) {
return source.replace(path.basename(source), '');
}
}

0 comments on commit aaef6fd

Please sign in to comment.