Skip to content

Commit

Permalink
Fix CLI where files are being synced from the project document instea…
Browse files Browse the repository at this point in the history
…d of from the filesystem

CLI sync should not create files as this is the role of the versionning
system (git). This allow a project to contain file from 2 different
repos (like a webapp and a API CMS).

This is a fix from a recent addition that was also consider a fix. The
fix was wrong so the document-paths-fetcher.ts file is patched.

The stats command also reflect which document from Accent’s project is
found in the filesystem.
  • Loading branch information
simonprev committed Jan 21, 2024
1 parent de6dd81 commit 89dfea0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
7 changes: 7 additions & 0 deletions cli/src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {CLIError} from '@oclif/errors';
import Formatter from '../services/formatters/project-stats';
import ProjectFetcher from '../services/project-fetcher';
import {Revision} from '../types/project';
import DocumentPathsFetcher from '../services/document-paths-fetcher';

export default class Stats extends Command {
static description = 'Fetch stats from the API and display them beautifully';
Expand Down Expand Up @@ -44,9 +45,15 @@ export default class Stats extends Command {
this.project = response.project;
}

const documents = this.projectConfig.files();
const targets = documents.flatMap((document) => {
return new DocumentPathsFetcher().fetch(this.project!, document);
});

const formatter = new Formatter(
this.project!,
this.projectConfig.config,
targets,
flags.version
);

Expand Down
2 changes: 2 additions & 0 deletions cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default class Sync extends Command {
formatter.logSync(path, documentPath);
}
}
console.log('');
}

private async addTranslationsDocumentConfig(document: Document) {
Expand Down Expand Up @@ -191,5 +192,6 @@ export default class Sync extends Command {
);
}
}
console.log('');
}
}
2 changes: 1 addition & 1 deletion cli/src/services/document-paths-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class DocumentPathsFetcher {
fetch(project: Project, document: Document): DocumentPath[] {
const languageSlugs = fetchFromRevisions(project.revisions);
const documentPaths: Set<string> = new Set();
project.documents.entries.forEach(({path}) => documentPaths.add(path));

document.paths.forEach((documentPath) => {
const name = document.parseDocumentName(documentPath, document.config);
documentPaths.add(name);
Expand Down
2 changes: 0 additions & 2 deletions cli/src/services/formatters/commit-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default class CommitOperationFormatter {
chalk.bold.white(path),
chalk.gray.dim(documentPath)
);
console.log('');
}

logAddTranslations(path: string, documentPath: string) {
Expand All @@ -25,7 +24,6 @@ export default class CommitOperationFormatter {
chalk.bold.white(path),
chalk.gray.dim(documentPath)
);
console.log('');
}

logEmptyExistingTarget(path: string) {
Expand Down
55 changes: 48 additions & 7 deletions cli/src/services/formatters/project-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,29 @@ import {
} from '../revision-slug-fetcher';
import Base from './base';

interface DocumentTarget {
documentPath: string;
path: string;
language: string;
}

export default class ProjectStatsFormatter extends Base {
private readonly project: Project;
private readonly config: Config;
private readonly targets: DocumentTarget[];
private readonly version: string | undefined;

constructor(project: Project, config: Config, version?: string) {
constructor(
project: Project,
config: Config,
targets: DocumentTarget[],
version?: string
) {
super();
this.project = project;
this.config = config;
this.version = version;
this.targets = targets;
}

percentageReviewedString(number: number, translationsCount: number) {
Expand Down Expand Up @@ -142,11 +155,39 @@ export default class ProjectStatsFormatter extends Base {
`(${this.project.documents.meta.totalEntries})`
)
);
this.project.documents.entries.forEach((document: Document) => {
console.log(chalk.gray('Format:'), chalk.white.bold(document.format));
console.log(chalk.gray('Path:'), chalk.white.bold(document.path));
console.log('');
});
this.project.documents.entries
.sort((a, b) => {
const existingTargetA = this.targets.find(
(target) => a.path === target.documentPath
);
const existingTargetB = this.targets.find(
(target) => b.path === target.documentPath
);

if (existingTargetA) return -1;
if (existingTargetB) return 1;
return 0;
})
.forEach((document: Document) => {
const existingTarget = this.targets.find(
(target) => document.path === target.documentPath
);

if (existingTarget) {
console.log(
chalk.white(document.path),
chalk.whiteBright.underline(existingTarget.path)
);
} else {
console.log(
chalk.gray.dim(document.path),
chalk.gray.dim('-'),
chalk.gray.dim(document.format),
chalk.gray.dim('(No matches in config file)')
);
}
});
console.log('');
}

if (this.project.versions.meta.totalEntries !== 0) {
Expand Down Expand Up @@ -196,7 +237,7 @@ export default class ProjectStatsFormatter extends Base {

console.log(
chalk.magenta('Project URL:'),
chalk.gray.dim(`${this.config.apiUrl}/app/projects/${this.project.id}`)
chalk.gray(`${this.config.apiUrl}/app/projects/${this.project.id}`)
);
console.log('');
}
Expand Down

0 comments on commit 89dfea0

Please sign in to comment.