a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(lex-cli): simplify formatting code

Mary ef31b005 04634055

+48 -57
+48 -57
packages/lexicons/lex-cli/src/formatter.ts
··· 73 73 * @returns a formatter instance 74 74 */ 75 75 export const createFormatter = async (config: FormatterConfig, root: string): Promise<Formatter> => { 76 - let inner: Formatter; 77 - let concurrency: number; 76 + switch (config.type) { 77 + case 'prettier': { 78 + const prettier = await import('prettier'); 79 + const prettierConfig = await prettier.resolveConfig(root, { editorconfig: true }); 78 80 79 - if (config.type === 'prettier') { 80 - const prettier = await import('prettier'); 81 - const prettierConfig = await prettier.resolveConfig(root, { editorconfig: true }); 81 + return { 82 + async format(code, filepath) { 83 + return prettier.format(code, { ...prettierConfig, parser: inferPrettierParser(filepath) }); 84 + }, 85 + }; 86 + } 87 + case 'command': { 88 + // the template uses {filepath} as a placeholder, which is passed as a 89 + // positional argument to sh to avoid shell injection via filenames 90 + const shellCmd = config.command.replaceAll('{filepath}', '"$1"'); 91 + const semaphore = new Semaphore(config.concurrency); 82 92 83 - // prettier is in-process and CPU-bound, no benefit from concurrency 84 - concurrency = 1; 85 - inner = { 86 - async format(code, filepath) { 87 - return prettier.format(code, { ...prettierConfig, parser: inferPrettierParser(filepath) }); 88 - }, 89 - }; 90 - } else { 91 - // the template uses {filepath} as a placeholder, which is passed as a 92 - // positional argument to sh to avoid shell injection via filenames 93 - const shellCmd = config.command.replaceAll('{filepath}', '"$1"'); 93 + return { 94 + async format(code, filepath) { 95 + const lock = await semaphore.acquire(); 94 96 95 - concurrency = config.concurrency; 96 - inner = { 97 - format(code, filepath) { 98 - return new Promise<string>((resolve, reject) => { 99 - const child = spawn('sh', ['-c', shellCmd, 'sh', filepath], { 100 - stdio: ['pipe', 'pipe', 'pipe'], 101 - }); 97 + try { 98 + return await new Promise<string>((resolve, reject) => { 99 + const child = spawn('sh', ['-c', shellCmd, 'sh', filepath], { 100 + stdio: ['pipe', 'pipe', 'pipe'], 101 + }); 102 102 103 - const stdoutChunks: Buffer[] = []; 104 - const stderrChunks: Buffer[] = []; 103 + const stdoutChunks: Buffer[] = []; 104 + const stderrChunks: Buffer[] = []; 105 105 106 - child.stdout.on('data', (chunk: Buffer) => { 107 - stdoutChunks.push(chunk); 108 - }); 106 + child.stdout.on('data', (chunk: Buffer) => { 107 + stdoutChunks.push(chunk); 108 + }); 109 109 110 - child.stderr.on('data', (chunk: Buffer) => { 111 - stderrChunks.push(chunk); 112 - }); 110 + child.stderr.on('data', (chunk: Buffer) => { 111 + stderrChunks.push(chunk); 112 + }); 113 113 114 - child.on('error', reject); 114 + child.on('error', reject); 115 115 116 - child.on('close', (exitCode: number | null) => { 117 - if (exitCode !== 0) { 118 - const stderr = Buffer.concat(stderrChunks).toString(); 119 - reject(new Error(`formatter exited with code ${exitCode}:\n${stderr}`)); 120 - } else { 121 - resolve(Buffer.concat(stdoutChunks).toString()); 122 - } 123 - }); 116 + child.on('close', (exitCode: number | null) => { 117 + if (exitCode !== 0) { 118 + const stderr = Buffer.concat(stderrChunks).toString(); 119 + reject(new Error(`formatter exited with code ${exitCode}:\n${stderr}`)); 120 + } else { 121 + resolve(Buffer.concat(stdoutChunks).toString()); 122 + } 123 + }); 124 124 125 - child.stdin.end(code); 126 - }); 127 - }, 128 - }; 125 + child.stdin.end(code); 126 + }); 127 + } finally { 128 + lock.release(); 129 + } 130 + }, 131 + }; 132 + } 129 133 } 130 - 131 - const semaphore = new Semaphore(concurrency); 132 - 133 - return { 134 - async format(code, filepath) { 135 - const lock = await semaphore.acquire(); 136 - try { 137 - return await inner.format(code, filepath); 138 - } finally { 139 - lock.release(); 140 - } 141 - }, 142 - }; 143 134 };