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

Pass multiple filenames and --inplace to pgFormatter #50

Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion dist/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ By default, output is written to stdout. (use --write option to edit files in-pl
},
noSpaceFunction: {
type: "boolean",
describe: "Remove the space character between a function call and the open parenthesis that follow",
describe: "Remove the space character between a function call and the open parenthesis that follows",
},
configFile: {
type: "string",
Expand All @@ -95,6 +95,11 @@ By default, output is written to stdout. (use --write option to edit files in-pl
type: "string",
describe: "Path to a custom pg_format version",
},
chunkSize: {
type: "number",
describe: "How many files to pass to pgFormatter at once",
default: "25",
},
})
.demandCommand(1, "").argv;
const filesOrGlobs = parsedArguments._;
Expand Down
26 changes: 17 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCommandArguments = exports.buildCommand = exports.CaseOptionEnum = exports.formatSql = exports.formatFiles = void 0;
const path = require("path");
const fs = require("fs");
const globby = require("globby");
const child_process_1 = require("child_process");
/**
Expand All @@ -11,21 +10,27 @@ const child_process_1 = require("child_process");
* @param options
*/
function formatFiles(filesOrGlobs, editInPlace, options = {}, log = console.log) {
let paths = globby.sync(filesOrGlobs);
var _a;
// both the editInPlace and options.write properties mean the same thing, ensure they're aligned here
editInPlace = (_a = editInPlace !== null && editInPlace !== void 0 ? editInPlace : options.write) !== null && _a !== void 0 ? _a : false;
options.write = editInPlace;
const paths = globby.sync(filesOrGlobs);
let formatted = "";
for (let path of paths) {
let startTime = process.hrtime();
let command = `${buildCommand(options)} ${path}`;
const chunkSize = Number(options.chunkSize || 25);
for (let i = 0; i < paths.length; i += chunkSize) {
const pathsChunk = paths.slice(i, i + chunkSize);
const filenamesWrapped = `"${pathsChunk.join('" "')}"`;
const startTime = process.hrtime();
const command = `${buildCommand(options)} ${filenamesWrapped}`;
// Run pgFormatter
let output = child_process_1.execSync(command, {
const output = child_process_1.execSync(command, {
encoding: "utf8",
});
const elapsedTimeMs = Math.round(process.hrtime(startTime)[1] / 1000000);
formatted += output;
if (editInPlace) {
// Override file with formatted SQL and log progress
fs.writeFileSync(path, output);
log(`${path} [${elapsedTimeMs}ms]`);
const filesOnNewLines = pathsChunk.map((p) => ` ${p}`);
log(`[${pathsChunk.length} files in ${elapsedTimeMs}ms]:\n${filesOnNewLines.join("\n")}\n`);
}
}
return formatted;
Expand Down Expand Up @@ -56,6 +61,9 @@ function buildCommand(options) {
exports.buildCommand = buildCommand;
function buildCommandArguments(options) {
let commandArgs = "";
if (options.write) {
commandArgs += ` --inplace`;
}
if (options.spaces) {
commandArgs += ` --spaces ${options.spaces}`;
}
Expand Down
1 change: 1 addition & 0 deletions dist/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IOptions {
extraFunction?: string;
configFile?: string;
noSpaceFunction?: boolean;
chunkSize?: number;
perlBinPath?: string;
pgFormatterPath?: string;
}
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ By default, output is written to stdout. (use --write option to edit files in-pl
type: "string",
describe: "Path to a custom pg_format version",
},
chunkSize: {
type: "number",
describe: "How many files to pass to pgFormatter at once",
default: "25",
},
})
.demandCommand(1, "").argv;

Expand Down
28 changes: 19 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from "path";
import * as fs from "fs";
import * as globby from "globby";
import { execSync } from "child_process";
import { IOptions } from "./options";
Expand All @@ -15,14 +14,22 @@ export function formatFiles(
options: IOptions = {},
log: (text: string) => void = console.log
) {
let paths = globby.sync(filesOrGlobs);
// both the editInPlace and options.write properties mean the same thing, ensure they're aligned here
editInPlace = editInPlace ?? options.write ?? false;
options.write = editInPlace;

const paths = globby.sync(filesOrGlobs);
let formatted = "";
const chunkSize = Number(options.chunkSize || 25);

for (let i = 0; i < paths.length; i += chunkSize) {
const pathsChunk = paths.slice(i, i + chunkSize);
const filenamesWrapped = `"${pathsChunk.join('" "')}"`;

for (let path of paths) {
let startTime = process.hrtime();
let command = `${buildCommand(options)} ${path}`;
const startTime = process.hrtime();
const command = `${buildCommand(options)} ${filenamesWrapped}`;
// Run pgFormatter
let output = execSync(command, {
const output = execSync(command, {
encoding: "utf8",
});

Expand All @@ -31,9 +38,8 @@ export function formatFiles(
formatted += output;

if (editInPlace) {
// Override file with formatted SQL and log progress
fs.writeFileSync(path, output);
log(`${path} [${elapsedTimeMs}ms]`);
const filesOnNewLines = pathsChunk.map((p) => ` ${p}`);
log(`[${pathsChunk.length} files in ${elapsedTimeMs}ms]:\n${filesOnNewLines.join("\n")}\n`);
}
}

Expand Down Expand Up @@ -67,6 +73,10 @@ export function buildCommand(options: IOptions) {
export function buildCommandArguments(options: IOptions) {
let commandArgs = "";

if (options.write) {
commandArgs += ` --inplace`;
}

if (options.spaces) {
commandArgs += ` --spaces ${options.spaces}`;
}
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IOptions {
extraFunction?: string;
configFile?: string;
noSpaceFunction?: boolean;
chunkSize?: number;

perlBinPath?: string;
pgFormatterPath?: string;
Expand Down
46 changes: 46 additions & 0 deletions test/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,50 @@ FROM
people
`);
});

it("formats multiple files in-place", function () {
const multipleFiles = [
"test/support/module_test01.sql",
"test/support/module_test02.sql",
"test/support/module_test03.sql",
"test/support/module_test04.sql",
]
for (const tmpFile of multipleFiles) {
fs.copyFileSync(queryFilePath, tmpFile);
}

const expectedFormattedContent = `\
SELECT
id,
first_name
FROM
people
`;
let stdout = "";
let logger = function (message: string) {
stdout += message;
};
formatFiles(multipleFiles, true, { noComment: true, spaces: 2, write: true, chunkSize: 3 }, logger);

// assert all the file contents were updated as expected
for (const tmpFile of multipleFiles) {
const updatedContents = fs.readFileSync(tmpFile, { encoding: "utf-8" });
expect(updatedContents).to.equal(expectedFormattedContent);
fs.unlinkSync(tmpFile);
}

// and that the stdout included all the filenames, and some timing info
const stdoutLines = stdout.split("\n");
const expectedStdoutLineSubstrings = [
"[3 files in ",
multipleFiles[0],
multipleFiles[1],
multipleFiles[2],
"[1 files in ",
multipleFiles[3],
];
for (let i = 0; i < expectedStdoutLineSubstrings.length; i++) {
expect(stdoutLines[i]).to.contain(expectedStdoutLineSubstrings[i]);
}
});
});