Skip to content

Commit

Permalink
ensure prettier installed when formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
swk777 committed Sep 9, 2024
1 parent d2a9ed7 commit 5298c9c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
41 changes: 37 additions & 4 deletions packages/api/session.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { fileExists } from './fs-utils.mjs';
import { validFilename } from '@srcbook/shared';
import { pathToCodeFile } from './srcbook/path.mjs';
import { exec } from 'node:child_process';
import { npmInstall } from './exec.mjs';

const sessions: Record<string, SessionType> = {};

Expand Down Expand Up @@ -297,10 +298,42 @@ export function updateCell(session: SessionType, cell: CellType, updates: CellUp
return updateCodeCell(session, cell, updates);
}
}

export async function formatCode(filePath: string) {
async function ensurePrettierInstalled(dir: string): Promise<boolean> {
const prettierPath = Path.join(dir, 'node_modules', 'prettier');
try {
const command = `npx prettier ${filePath}`;
// check if prettier is installed
await fs.access(prettierPath);
return true;
} catch (error) {
return new Promise<boolean>((resolve) => {
try {
npmInstall({
cwd: dir,
packages: ['prettier'],
stdout: () => {},
stderr: (err) => console.error(err),
onExit: (exitCode) => {
if (exitCode === 0) {
resolve(true);
} else {
console.error('Failed to install Prettier:', exitCode);
resolve(false);
}
},
});
} catch (installError) {
console.error('Failed to initiate Prettier installation:', installError);
resolve(false);
}
});
}
}
export async function formatCode(dir: string, fileName: string) {
try {
await ensurePrettierInstalled(dir);

const codeFilePath = pathToCodeFile(dir, fileName);
const command = `npx prettier ${codeFilePath}`;

return new Promise((resolve, reject) => {
exec(command, async (error, stdout) => {
Expand All @@ -319,7 +352,7 @@ export async function formatCode(filePath: string) {
}
export async function formatAndUpdateCodeCell(session: SessionType, cell: CodeCellType) {
try {
const formattedCode = await formatCode(pathToCodeFile(session.dir, cell.filename));
const formattedCode = await formatCode(session.dir, cell.filename);
return updateCodeCell(session, cell, { source: formattedCode } as { source: string });
} catch (error) {
return Promise.resolve({
Expand Down
8 changes: 3 additions & 5 deletions packages/api/srcbook/config.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
export function buildJSPackageJson() {
return {
type: 'module',
dependencies: {
devDependencies: {
prettier: 'latest',
},
prettier: {
tabWidth: 2,
semi: true,
singleQuote: true,
printWidth: 100,
},
};
}
Expand All @@ -20,13 +18,13 @@ export function buildTSPackageJson() {
tsx: 'latest',
typescript: 'latest',
'@types/node': 'latest',
},
devDependencies: {
prettier: 'latest',
},
prettier: {
tabWidth: 2,
semi: true,
singleQuote: true,
printWidth: 100,
},
};
}
Expand Down

0 comments on commit 5298c9c

Please sign in to comment.