@unibeautify/cli
Version:
CLI for Unibeautify
149 lines • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cosmiconfig = require("cosmiconfig");
const fs = require("fs");
const levenshtein = require("fast-levenshtein");
const utils_1 = require("../utils");
const BaseCommand_1 = require("./BaseCommand");
class BeautifyCommand extends BaseCommand_1.BaseCommand {
constructor() {
super(...arguments);
this.stdin = process.stdin;
}
beautify(programArgs) {
const { language, filePath } = programArgs;
return utils_1.setupUnibeautify().then(unibeautify => {
return this.validateLanguage(language, unibeautify).then(() => {
return Promise.all([
this.readConfig(programArgs),
this.readText(filePath),
]).then(([config, text]) => {
const data = {
filePath: filePath,
languageName: language,
options: config || {},
text,
};
return unibeautify
.beautify(data)
.then((result) => {
return this.writeToFileOrStdout({ result, programArgs }).then(() => result);
})
.catch((error) => {
return this.handleError(error, 1);
});
});
});
});
}
readConfig(programArgs) {
const { configFile, configJson, filePath } = programArgs;
if (configJson) {
return this.parseConfig(configJson);
}
else {
return this.configFile({ configFile, filePath });
}
}
readText(filePath) {
if (filePath) {
return this.readFile(filePath);
}
else {
return this.readFromStdin();
}
}
parseConfig(configJson) {
try {
return Promise.resolve(JSON.parse(configJson));
}
catch (error) {
return this.handleError(error, 2);
}
}
configFile({ configFile, filePath, }) {
const configExplorer = cosmiconfig("unibeautify", { stopDir: filePath });
const loadConfigPromise = configFile
? configExplorer.load(configFile)
: configExplorer.search(filePath);
return loadConfigPromise
.then(result => (result ? result.config : null))
.catch(error => Promise.reject(new Error(`Could not load configuration file ${configFile}`)));
}
readFromStdin() {
return new Promise((resolve, reject) => {
let text = "";
if (this.stdin.isTTY) {
resolve(text);
return;
}
this.stdin.on("data", (data) => {
text = data.toString();
});
this.stdin.on("end", () => {
resolve(text);
});
this.stdin.on("error", (err) => {
if (err.code === "EPIPE") {
return this.handleError(err, 1);
}
process.emit("warning", err);
});
this.stdin.resume();
});
}
readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (error, data) => {
if (error) {
return reject(error);
}
return resolve(data.toString());
});
});
}
writeToFileOrStdout({ result, programArgs, }) {
const { inplace, filePath } = programArgs;
if (inplace && filePath) {
return this.writeFile(result, filePath);
}
else {
return Promise.resolve(this.writeOut(result));
}
}
writeFile(text, filePath) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, text, error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
validateLanguage(language, unibeautify) {
if (!language) {
const error = new Error("A language is required.");
return this.handleError(error, 1);
}
const langs = unibeautify.findLanguages({ name: language });
if (langs.length === 0) {
const allLanguages = utils_1.getAllLanguages();
const distances = allLanguages.map(lang => levenshtein.get(lang.toLowerCase(), language.toLowerCase()));
const bestDistance = Math.min(...distances);
const distanceThreshold = 2;
const bestMatchLanguages = allLanguages.filter((lang, index) => distances[index] <= Math.min(distanceThreshold, bestDistance));
if (bestMatchLanguages.length > 0) {
const limit = 3;
const error = new Error(`Language '${language}' was not found. Did you mean:\n${bestMatchLanguages
.slice(0, limit)
.map(lang => ` - ${lang}`)
.join("\n")}`);
return this.handleError(error, 1);
}
}
return Promise.resolve();
}
}
exports.BeautifyCommand = BeautifyCommand;
//# sourceMappingURL=BeautifyCommand.js.map