This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
istyle.ts
85 lines (75 loc) · 2.87 KB
/
istyle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2022
// Carlos Alberto Ruiz Naranjo [[email protected]]
// Ismael Perez Rojo [[email protected] ]
//
// This file is part of colibri2
//
// Colibri is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Colibri is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with colibri2. If not, see <https://www.gnu.org/licenses/>.
const fs = require("fs");
const path_lib = require("path");
import { Base_formatter } from "./base_formatter";
import * as utils from "../process/utils";
import * as file_utils from "../utils/file_utils";
import { OS } from "../process/common";
import { Process } from "../process/process";
import * as common from "./common";
import * as cfg from "../config/config_declaration";
export class Istyle extends Base_formatter {
private binary_linux = 'istyle-linux';
private binary_windows = 'istyle-win32.exe';
private binary_mac = 'istyle-darwin';
constructor() {
super();
}
public async format_from_code(code: string, opt: cfg.e_formatter_istyle): Promise<common.f_result> {
const temp_file = await utils.create_temp_file(code);
const formatted_code = await this.format(temp_file, opt);
file_utils.remove_file(temp_file);
return formatted_code;
}
private get_binary(): string {
const os = utils.get_os();
if (os === OS.MAC) {
return this.binary_mac;
}
else if (os === OS.LINUX) {
return this.binary_linux;
}
else {
return this.binary_windows;
}
}
public async format(file: string, opt: cfg.e_formatter_istyle) {
const binary_name = this.get_binary();
const path_bin = path_lib.join(__dirname, 'bin', 'svistyle', binary_name);
let command = "";
if (opt.style === cfg.e_formatter_istyle_style.indent_only) {
command = `${path_bin} --style=ansi -s${opt.indentation_size} `;
}
else {
command = `${path_bin} --style=${opt.style} -s${opt.indentation_size} `;
}
command += file;
const P = new Process();
const exec_result = await P.exec_wait(command);
const code_formatted = fs.readFileSync(file, "utf8");
const result: common.f_result = {
code_formatted: code_formatted,
command: command,
successful: exec_result.successful,
message: exec_result.stderr,
};
return result;
}
}