From af20f6a2c49bff34c2dbc2d28e4b619926a908b2 Mon Sep 17 00:00:00 2001 From: Ryan Ling Date: Sun, 10 Oct 2021 22:12:51 +1100 Subject: [PATCH] Run Prettier serially This doesn't appear to significantly change the execution time based on some very rudimentary testing. --- .changeset/pretty-kings-exist.md | 7 +++ package.json | 1 - src/cli/__snapshots__/format.int.test.ts.snap | 2 - src/cli/__snapshots__/lint.int.test.ts.snap | 2 - src/cli/adapter/prettier.ts | 62 ++++++++----------- yarn.lock | 14 ++--- 6 files changed, 39 insertions(+), 49 deletions(-) create mode 100644 .changeset/pretty-kings-exist.md diff --git a/.changeset/pretty-kings-exist.md b/.changeset/pretty-kings-exist.md new file mode 100644 index 000000000..fa746f958 --- /dev/null +++ b/.changeset/pretty-kings-exist.md @@ -0,0 +1,7 @@ +--- +"skuba": patch +--- + +format, lint: Run Prettier serially on files + +This aims to reduce the memory footprint of `skuba lint`. diff --git a/package.json b/package.json index bf84632e0..acf120601 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cli/__snapshots__/format.int.test.ts.snap b/src/cli/__snapshots__/format.int.test.ts.snap index c135090d6..3e2a2c480 100644 --- a/src/cli/__snapshots__/format.int.test.ts.snap +++ b/src/cli/__snapshots__/format.int.test.ts.snap @@ -63,7 +63,6 @@ Initialising Prettier... Detected project root: Discovering files... Discovered 6 files. -Reading files... Formatting files... b.md parser: markdown @@ -77,7 +76,6 @@ tsconfig.json parser: json a/a/a.ts parser: typescript -Writing 0 files... Processed 6 files in s. " `; diff --git a/src/cli/__snapshots__/lint.int.test.ts.snap b/src/cli/__snapshots__/lint.int.test.ts.snap index eddec8a7e..0f87e126d 100644 --- a/src/cli/__snapshots__/lint.int.test.ts.snap +++ b/src/cli/__snapshots__/lint.int.test.ts.snap @@ -65,7 +65,6 @@ Prettier │ Initialising Prettier... Prettier │ Detected project root: Prettier │ Discovering files... Prettier │ Discovered 6 files. -Prettier │ Reading files... Prettier │ Linting files... Prettier │ b.md Prettier │ parser: markdown @@ -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 s. tsc │ TSFILE: /lib/tsconfig.tsbuildinfo tsc │ Files: diff --git a/src/cli/adapter/prettier.ts b/src/cli/adapter/prettier.ts index 6d290a07c..038a07521 100644 --- a/src/cli/adapter/prettier.ts +++ b/src/cli/adapter/prettier.ts @@ -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; @@ -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[]; } @@ -29,7 +26,7 @@ const formatOrLintFile = ( logger: Logger, mode: 'format' | 'lint', result: Result, -) => { +): string | undefined => { logger.debug(filepath); logger.debug(' parser:', parser ?? '-'); @@ -67,7 +64,8 @@ const formatOrLintFile = ( return; } - result.touched.push({ data: formatted, filepath }); + result.touched.push(filepath); + return formatted; }; export interface PrettierOutput { @@ -119,40 +117,30 @@ export const runPrettier = async ( unparsed: [], }; - logger.debug('Reading files...'); - - const files = await Promise.all( - filepaths.map>(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( @@ -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); } } diff --git a/yarn.lock b/yarn.lock index abb474688..cca4572f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== -p-limit@3.1.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-limit@^1.1.0: version "1.3.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8" @@ -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"