-
Notifications
You must be signed in to change notification settings - Fork 203
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
Feat: Add Prettier Support to TypeScript Notebook #231
Changes from all commits
aa78e91
8cf1a1d
5ed5a39
dfabe78
800d13c
36a717c
34737ea
0a68c3d
a394861
7d436a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@srcbook/shared': minor | ||
'@srcbook/api': minor | ||
'@srcbook/web': minor | ||
--- | ||
|
||
Add Prettier Support to Code Notebook |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ import { | |
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> = {}; | ||
|
||
|
@@ -296,7 +298,70 @@ export function updateCell(session: SessionType, cell: CellType, updates: CellUp | |
return updateCodeCell(session, cell, updates); | ||
} | ||
} | ||
|
||
async function ensurePrettierInstalled(dir: string): Promise<boolean> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some thoughts here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I stand behind number 3 above but in the interest of getting this out sooner than later, we can ignore that piece for now and leave this code (though would be good to use depcheck). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh I just noticed we are doing 1) here already, nice |
||
const prettierPath = Path.join(dir, 'node_modules', 'prettier'); | ||
try { | ||
// check if prettier is installed | ||
await fs.access(prettierPath); | ||
return true; | ||
} catch (error) { | ||
return new Promise<boolean>((resolve) => { | ||
try { | ||
npmInstall({ | ||
cwd: dir, | ||
packages: ['prettier'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should install this in the devDependencies |
||
args: ['--save-dev'], | ||
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 (_, stdout, stderr) => { | ||
if (stderr) { | ||
console.error(`exec error: ${stderr}`); | ||
reject(stderr); | ||
return; | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
} catch (error) { | ||
console.error('Formatting error:', error); | ||
throw error; | ||
} | ||
} | ||
export async function formatAndUpdateCodeCell(session: SessionType, cell: CodeCellType) { | ||
try { | ||
const formattedCode = await formatCode(session.dir, cell.filename); | ||
return updateCodeCell(session, cell, { source: formattedCode } as { source: string }); | ||
} catch (error) { | ||
return Promise.resolve({ | ||
success: false, | ||
errors: error, | ||
} as UpdateResultType); | ||
} | ||
} | ||
export function sessionToResponse(session: SessionType) { | ||
const result: Pick<SessionType, 'id' | 'cells' | 'language' | 'tsconfig.json' | 'openedAt'> = { | ||
id: session.id, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also just noticed this is
minor
, but for now I would like to keep all the versions onpatch
. Do you mind regenerating this changeset such that these are allpatch
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to regen just change the text to patch and it'll work