Skip to content

Commit

Permalink
fix: do not make JSON the default when writing to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoChiesa committed Oct 3, 2023
1 parent fc1e413 commit b9aae67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ This would effectively override the naming conventions that apigeelint checks.
apigeelint -s sampleProxy/apiproxy -f table.js -w existing-outputdir --quiet
```

The `-w` option can point to an existing directory, in which case the output will be emitted to apigeeLint.json in that
directory. Otherwise, the `-w` option is treated as the name of a file.
The `-w` option can point to an existing directory, in which case the output
will be emitted to a file named apigeelint.out in that directory, in whatever
format you specify. An existing file by that name will be overwritten. If the
`-w` option is not a directory, it is treated as the name of a file, and output
is written there.

If you do not also specify `--quiet` the report will go to both stdout and to the specified filesystem destination.

Expand Down
46 changes: 17 additions & 29 deletions lib/package/bundleLinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function contains(a, obj, f) {
return lh === rh;
};

for (var i = 0; i < a.length; i++) {
for (let i = 0; i < a.length; i++) {
if (f(a[i], obj)) {
if (!a[i]) {
return true;
Expand All @@ -44,21 +44,17 @@ function contains(a, obj, f) {
return false;
}

function exportData(providedPath, report) {
function exportReport(providedPath, stringifiedReport) {
const constructedPath =
fs.existsSync(providedPath) && fs.lstatSync(providedPath).isDirectory()
? path.join(providedPath, "apigeeLint.json")
? path.join(providedPath, "apigeelint.out")
: providedPath;
fs.writeFile(
constructedPath,
JSON.stringify(report, null, 4),
"utf8",
function (err) {
if (err) {
return console.log(err);
}
fs.writeFile(constructedPath, stringifiedReport, "utf8", function (err) {
if (err) {
return console.log(err);
}
);
return null;
});
}

const getPluginPath = () => path.resolve(path.join(__dirname, "plugins"));
Expand All @@ -78,14 +74,14 @@ const listFormatters = () =>
.readdirSync(path.join(__dirname, "third_party/formatters"))
.filter((s) => s.endsWith(".js"));

var lint = function (config, done) {
const lint = function (config, done) {
return new Bundle(config, function (bundle, err) {
if (err) {
return done ? done(null, err) : console.log(err);
}

// for each builtin plugin
let normalizedPath = getPluginPath();
const normalizedPath = getPluginPath();
fs.readdirSync(normalizedPath).forEach(function (file) {
if (!config.plugins || contains(config.plugins, file)) {
try {
Expand All @@ -112,28 +108,20 @@ var lint = function (config, done) {
});
}

var fmt = config.formatter || "json.js",
fmtImpl = getFormatter(fmt),
fmtReport = fmtImpl(bundle.getReport());
const formatter = config.formatter || "json.js",
formatterImpl = getFormatter(formatter),
formattedReport = formatterImpl(bundle.getReport());

if (config.output) {
if (typeof config.output == "function") {
config.output(fmtReport);
config.output(formattedReport);
}
} else {
console.log(fmtReport);
}

if (fmt !== "json.js") {
(fmt = "json.js"),
(fmtImpl = getFormatter(fmt)),
(fmtReport = JSON.parse(fmtImpl(bundle.getReport())));
} else {
fmtReport = JSON.parse(fmtReport);
console.log(formattedReport);
}

if (config.writePath) {
exportData(config.writePath, fmtReport);
exportReport(config.writePath, formattedReport);
}

if (done) {
Expand Down Expand Up @@ -166,7 +154,7 @@ var lint = function (config, done) {

var getFormatter = function (format) {
// default is stylish
var formatterPath;
let formatterPath;

format = format || "stylish.js";
if (typeof format === "string") {
Expand Down

0 comments on commit b9aae67

Please sign in to comment.