Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert clang-format wrapper to a Promise-based API #10

Merged
merged 1 commit into from
Sep 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ const path = require("path");
const { program } = require("commander");
const { clangFormatter, walk } = require("./index");
const { STATUS } = require("./result");
const { version } = require("./package.json");

async function run(directory, cmd) {
const dir = directory || path.resolve(".");
@@ -48,7 +49,7 @@ async function run(directory, cmd) {
const timer = setInterval(() => {}, 100);
try {
program
.version("0.2.0")
.version(version)
.description(
`Node.js runner for LLVM clang-format

30 changes: 19 additions & 11 deletions clang-format-spawn.js → format.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const process_spawn = require("child_process").spawn;
const spawn = require("child_process").spawn;
const fs = require("fs").promises;
const os = require("os");
const path = require("path");

const { ko } = require("./result");

async function spawn(source, done, stdio) {
async function format(source) {
let executable;
if (os.platform() === "win32") {
executable = path.resolve(__dirname, "bin", "win32", "clang-format.exe");
@@ -24,18 +24,26 @@ async function spawn(source, done, stdio) {
if (err) {
return Promise.reject(ko(executable, err));
}
const process = process_spawn(executable, [source], { stdio: stdio });
process.on("close", (exitCode) => {
if (exitCode) {
done(`clang-format exited with error code ${exitCode}`);
} else {
done(null);
}
let formatted = "";

const stdio = ["ignore", "pipe", process.stderr];
const clangFormat = spawn(executable, [source], { stdio: stdio });
clangFormat.stdout.on("data", (data) => {
formatted += data.toString();
});

return new Promise((resolve, reject) => {
clangFormat.on("close", (exitCode) => {
if (exitCode) {
reject(ko(source, `clang-format exited with error code ${exitCode}`));
} else {
resolve(formatted);
}
});
});
return Promise.resolve(process);
} catch (err) {
return Promise.reject(ko(null, err));
}
}

module.exports = spawn;
module.exports = format;
50 changes: 17 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs").promises;
const path = require("path");

const clangFormatSpawn = require("./clang-format-spawn");
const format = require("./format");
const { ok, ko } = require("./result");

const IGNORE_DIRECTORIES = Object.freeze([
@@ -63,39 +63,23 @@ async function clangFormatter(files, checkMode, sourceRoot) {
}

async function formatSource(source, relative, checkMode) {
return new Promise(async (resolve, reject) => {
let formatted = "";
const done = async (err) => {
if (err) {
return reject(ko(relative, err));
}
try {
const contents = await fs.readFile(source);
if (formatted.toString() === contents.toString()) {
resolve(ok(relative));
} else if (checkMode) {
resolve(ko(relative));
} else {
await fs.writeFile(source, formatted.toString());
resolve(ok(relative));
}
} catch (error) {
reject(ko(relative, error));
}
};
try {
const formatter = await clangFormatSpawn(source, done, [
"ignore",
"pipe",
process.stderr,
]);
formatter.stdout.on("data", (data) => {
formatted += data.toString();
});
} catch (err) {
reject(err);
try {
const contents = await fs.readFile(source);
const formatted = await format(source);
if (formatted.toString() === contents.toString()) {
return Promise.resolve(ok(relative));
}
});
// Contents did not match
if (checkMode) {
// check mode, so fail.
return Promise.resolve(ko(relative));
}
// write formatted contents to disk
await fs.writeFile(source, formatted.toString());
return Promise.resolve(ok(relative));
} catch (err) {
return Promise.reject(ko(relative, err));
}
}

module.exports = {
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@artichoke/clang-format",
"version": "0.2.0",
"version": "0.3.0",
"private": true,
"description": "Artichoke Ruby clang-format runner",
"keywords": [