Skip to content

Commit

Permalink
Run Prettier serially
Browse files Browse the repository at this point in the history
This doesn't appear to significantly change the execution time based on
some very rudimentary testing.
  • Loading branch information
72636c committed Oct 10, 2021
1 parent 6ae1a1b commit af20f6a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 49 deletions.
7 changes: 7 additions & 0 deletions .changeset/pretty-kings-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"skuba": patch
---

format, lint: Run Prettier serially on files

This aims to reduce the memory footprint of `skuba lint`.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"normalize-package-data": "^3.0.0",
"npm-run-path": "^4.0.1",
"npm-which": "^3.0.1",
"p-limit": "3.1.0",
"picomatch": "^2.2.2",
"prettier": "2.4.1",
"read-pkg-up": "^7.0.1",
Expand Down
2 changes: 0 additions & 2 deletions src/cli/__snapshots__/format.int.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ Initialising Prettier...
Detected project root: <random>
Discovering files...
Discovered 6 files.
Reading files...
Formatting files...
b.md
parser: markdown
Expand All @@ -77,7 +76,6 @@ tsconfig.json
parser: json
a/a/a.ts
parser: typescript
Writing 0 files...
Processed 6 files in <random>s.
"
`;
Expand Down
2 changes: 0 additions & 2 deletions src/cli/__snapshots__/lint.int.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Prettier │ Initialising Prettier...
Prettier │ Detected project root: <random>
Prettier │ Discovering files...
Prettier │ Discovered 6 files.
Prettier │ Reading files...
Prettier │ Linting files...
Prettier │ b.md
Prettier │ parser: markdown
Expand All @@ -79,7 +78,6 @@ Prettier │ tsconfig.json
Prettier │ parser: json
Prettier │ a/a/a.ts
Prettier │ parser: typescript
Prettier │ Writing 0 files...
Prettier │ Processed 6 files in <random>s.
tsc │ TSFILE: <random>/lib/tsconfig.tsbuildinfo
tsc │ Files: <random>
Expand Down
62 changes: 25 additions & 37 deletions src/cli/adapter/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import path from 'path';

import fs from 'fs-extra';
import pLimit from 'p-limit';
import { Options, check, format, getFileInfo, resolveConfig } from 'prettier';

import { crawlDirectory } from '../../utils/dir';
import { Logger } from '../../utils/logging';
import { getConsumerManifest } from '../../utils/manifest';

const fsLimit = pLimit(25);

interface File {
data: string;
options: Options;
Expand All @@ -20,7 +17,7 @@ interface File {
interface Result {
count: number;
errored: Array<{ err?: unknown; filepath: string }>;
touched: Array<{ data: string; filepath: string }>;
touched: string[];
unparsed: string[];
}

Expand All @@ -29,7 +26,7 @@ const formatOrLintFile = (
logger: Logger,
mode: 'format' | 'lint',
result: Result,
) => {
): string | undefined => {
logger.debug(filepath);

logger.debug(' parser:', parser ?? '-');
Expand Down Expand Up @@ -67,7 +64,8 @@ const formatOrLintFile = (
return;
}

result.touched.push({ data: formatted, filepath });
result.touched.push(filepath);
return formatted;
};

export interface PrettierOutput {
Expand Down Expand Up @@ -119,40 +117,30 @@ export const runPrettier = async (
unparsed: [],
};

logger.debug('Reading files...');

const files = await Promise.all(
filepaths.map<Promise<File>>(async (filepath) => {
const [config, data, fileInfo] = await Promise.all([
fsLimit(() => resolveConfig(filepath)),
fsLimit(() => fs.promises.readFile(filepath, 'utf-8')),
// Infer parser upfront so we can know to ignore unsupported file types.
fsLimit(() => getFileInfo(filepath, { resolveConfig: false })),
]);

return {
data,
filepath,
options: { ...config, filepath },
parser: fileInfo.inferredParser,
};
}),
);

logger.debug(mode === 'format' ? 'Formatting' : 'Linting', 'files...');

for (const file of files) {
formatOrLintFile(file, logger, mode, result);
for (const filepath of filepaths) {
const [config, data, fileInfo] = await Promise.all([
resolveConfig(filepath),
fs.promises.readFile(filepath, 'utf-8'),
// Infer parser upfront so we can know to ignore unsupported file types.
getFileInfo(filepath, { resolveConfig: false }),
]);

const file: File = {
data,
filepath,
options: { ...config, filepath },
parser: fileInfo.inferredParser,
};

const formatted = formatOrLintFile(file, logger, mode, result);

if (typeof formatted === 'string') {
await fs.promises.writeFile(filepath, formatted);
}
}

logger.debug(`Writing ${logger.pluralise(result.touched.length, 'file')}...`);

await Promise.all(
result.touched.map(({ data, filepath }) =>
fsLimit(() => fs.promises.writeFile(filepath, data)),
),
);

const end = process.hrtime.bigint();

logger.plain(
Expand All @@ -166,7 +154,7 @@ export const runPrettier = async (
logger.plain(
`Formatted ${logger.pluralise(result.touched.length, 'file')}:`,
);
for (const { filepath } of result.touched) {
for (const filepath of result.touched) {
logger.warn(filepath);
}
}
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6296,13 +6296,6 @@ p-is-promise@^3.0.0:
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-3.0.0.tgz#58e78c7dfe2e163cf2a04ff869e7c1dba64a5971"
integrity sha512-Wo8VsW4IRQSKVXsJCn7TomUaVtyfjVDn3nUP7kE967BQk0CwFpdbZs0X0uk5sW9mkBa9eNM7hCMaG93WUAwxYQ==

[email protected], p-limit@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
dependencies:
yocto-queue "^0.1.0"

p-limit@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8"
Expand All @@ -6317,6 +6310,13 @@ p-limit@^2.2.0:
dependencies:
p-try "^2.0.0"

p-limit@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
dependencies:
yocto-queue "^0.1.0"

p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
Expand Down

0 comments on commit af20f6a

Please sign in to comment.