Skip to content

Commit

Permalink
Fix cli format command
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Nov 17, 2022
1 parent d40d32d commit 26d574a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cli/src/commands/format.ts
Expand Up @@ -21,11 +21,12 @@ export default class Format extends Command {
'order-by': flags.string({
default: 'index',
description: 'Order of the keys',
options: ['index', 'key'],
options: ['index', 'key', '-index', '-key'],
}),
};

async run() {
const {flags} = this.parse(Format);
const documents = this.projectConfig.files();
const t0 = process.hrtime();
const formattedPaths: FormattedFile[] = [];
Expand All @@ -44,7 +45,7 @@ export default class Format extends Command {

if (fs.existsSync(path)) {
const beforeContent = fs.readFileSync(path);
await document.format(path, language);
await document.format(path, language, flags);
const unchanged = fs.readFileSync(path).equals(beforeContent);

formattedPaths.push({path, unchanged});
Expand Down
7 changes: 6 additions & 1 deletion cli/src/services/document.ts
Expand Up @@ -25,6 +25,10 @@ const throwOnServerError = async (response: Response) => {
}
};

interface FormatOptions {
'order-by': string;
}

export default class Document {
paths: string[];
readonly apiKey: string;
Expand All @@ -46,14 +50,15 @@ export default class Document {
this.paths = new Tree(this.config).list();
}

async format(file: string, language: string) {
async format(file: string, language: string, options: FormatOptions) {
const formData = new FormData();

formData.append('file', fs.createReadStream(file));
formData.append('document_path', this.parseDocumentName(file, this.config));
formData.append('document_format', this.config.format);
formData.append('language', language);
if (this.projectId) formData.append('project_id', this.projectId);
if (options['order-by']) formData.append('order_by', options['order-by']);

const url = `${this.apiUrl}/format`;

Expand Down

0 comments on commit 26d574a

Please sign in to comment.