UNPKG

@unibeautify/beautifier-yapf

Version:
142 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const unibeautify_1 = require("unibeautify"); const readPkgUp = require("read-pkg-up"); const tmp = require("tmp"); const fs = require("fs"); const path = require("path"); const os = require("os"); const { pkg } = readPkgUp.sync({ cwd: __dirname }); exports.beautifier = { name: "YAPF", package: pkg, options: { Python: { indent_width: "indent_size", use_tabs: [["indent_style"], options => options.indent_style === "tab"], }, }, dependencies: [ { type: unibeautify_1.DependencyType.Executable, name: "YAPF", program: "yapf", parseVersion: [/yapf (\d+\.\d+\.\d+)/], }, ], resolveConfig: ({ filePath, projectPath }) => { const configFiles = [".style.yapf", "setup.cfg"]; return findFile({ finishPath: projectPath, startPath: filePath, fileNames: configFiles, }) .then(configFile => ({ filePath: configFile })) .catch(err => { // tslint:disable-next-line no-console console.log(err); return Promise.resolve({}); }); }, beautify({ text, dependencies, options, beautifierConfig, }) { const yapf = dependencies.get("YAPF"); const style = beautifierConfig && beautifierConfig.filePath ? `${beautifierConfig.filePath}` : stringifyOptions(options); const config = style ? `--style=${style}` : ""; return tmpFile({ postfix: ".py" }).then(filePath => { const basePath = os.tmpdir(); const args = relativizePaths([config, "--in-place", filePath], basePath); return writeFile(filePath, text).then(() => yapf .run({ args, options: { cwd: basePath, }, }) .then(({ exitCode, stderr }) => { if (exitCode) { return Promise.reject(stderr); } return readFile(filePath); })); }); }, }; function findFile({ finishPath = "/", startPath = finishPath, fileNames, }) { const filePaths = fileNames.map(fileName => path.join(startPath, fileName)); return Promise.all(filePaths.map(doesFileExist)) .then(exists => filePaths.filter((filePath, index) => exists[index])) .then(foundFilePaths => { if (foundFilePaths.length > 0) { return foundFilePaths[0]; } if (startPath === finishPath) { return Promise.reject("No config file found"); } const parentDir = path.resolve(startPath, "../"); return findFile({ startPath: parentDir, finishPath, fileNames }); }); } function doesFileExist(filePath) { return new Promise(resolve => { fs.access(filePath, fs.constants.R_OK, error => resolve(!error)); }); } function tmpFile(options) { return new Promise((resolve, reject) => tmp.file(Object.assign({ prefix: "unibeautify-" }, options), (err, path, fd) => { if (err) { return reject(err); } return resolve(path); })); } function writeFile(filePath, contents) { return new Promise((resolve, reject) => { fs.writeFile(filePath, contents, error => { if (error) { return reject(error); } return resolve(); }); }); } function readFile(filePath) { return new Promise((resolve, reject) => { fs.readFile(filePath, (error, data) => { if (error) { return reject(error); } return resolve(data.toString()); }); }); } function relativizePaths(args, basePath) { return args.map(arg => { const isTmpFile = typeof arg === "string" && !arg.includes(":") && path.isAbsolute(arg) && path.dirname(arg).startsWith(basePath); if (isTmpFile) { return path.relative(basePath, arg); } return arg; }); } function stringifyOptions(options) { const ops = Object.keys(options) .map(optionKey => { const value = options[optionKey]; if (value === undefined) { return; } return `${optionKey}: ${value}`; }) .filter(Boolean); if (ops.length > 0) { return `{${ops.join(", ")}}`; } return undefined; } exports.default = exports.beautifier; //# sourceMappingURL=index.js.map