-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for adding new npm packages BEN-1072 #22
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
Changes from all commits
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,30 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF | |||||||||||||||||||||
|
||||||||||||||||||||||
await sandbox.files.write(filesToWrite); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Check if package.json was written and install only new dependencies | ||||||||||||||||||||||
const packageJsonFile = transformedFiles.find(file => file.path === 'package.json'); | ||||||||||||||||||||||
if (packageJsonFile) { | ||||||||||||||||||||||
console.log('package.json detected, checking for new dependencies...'); | ||||||||||||||||||||||
try { | ||||||||||||||||||||||
const newPackages = extractNewPackages(packageJsonFile.content); | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (newPackages.length > 0) { | ||||||||||||||||||||||
console.log('Installing new packages:', newPackages); | ||||||||||||||||||||||
const installCmd = `cd /app && npm install ${newPackages.join(' ')} --no-save`; | ||||||||||||||||||||||
const result = await sandbox.commands.run(installCmd); | ||||||||||||||||||||||
console.log('New packages installed successfully:', result.stdout); | ||||||||||||||||||||||
if (result.stderr) { | ||||||||||||||||||||||
console.warn('npm install warnings:', result.stderr); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
console.log('No new packages to install'); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||
console.error('Failed to install new packages:', error); | ||||||||||||||||||||||
// Don't throw here, let the sandbox continue - users can still work with basic dependencies | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Get all files from the sandbox using the improved filter logic | ||||||||||||||||||||||
const allFiles = await fetchAllSandboxFiles(sandbox); | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -40,3 +64,31 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF | |||||||||||||||||||||
}; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
function extractNewPackages(packageJsonContent: string): string[] { | ||||||||||||||||||||||
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. ❌ Exclusion of Base Packages and FormattingThe function excludes packages from
view all inputs Stack Trace
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. ✅ JSON Parsing and Dependencies ExtractionThe function correctly parses a valid JSON input string to extract the dependencies object.
view all inputs Unit Tests// Unit Test for "JSON Parsing and Dependencies Extraction": The function correctly parses a valid JSON input string to extract the dependencies object.
function benchify_dependencies(dependencies) {
const packageJson = JSON.stringify({ dependencies });
const result = extractNewPackages(packageJson);
const basePackages = ['react', 'react-dom', '@vitejs/plugin-react', 'tailwindcss', '@tailwindcss/vite', 'typescript', 'vite'];
for (const newPackage of result) {
const pkgName = newPackage.split('@')[0];
expect(basePackages).not.toContain(pkgName);
}
}
it('benchify_dependencies_exec_test_passing_0', () => {
const args = superjson.parse(
'{"json":[[{"\'rXHL][":"k]ZV?{L","IX608m?":"zg0>|w*K,","rB3rpfy":"}N\'[SOYq","/":"2IV","W&jY;{e^G":"bI{ {iI","~THB":"nQL\\\\","Lw;/{Qmz\\\\m":"%mQJP"}]]}',
);
benchify_dependencies(...args);
}); |
||||||||||||||||||||||
try { | ||||||||||||||||||||||
const packageJson = JSON.parse(packageJsonContent); | ||||||||||||||||||||||
const dependencies = packageJson.dependencies || {}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Base packages that are already installed in the template | ||||||||||||||||||||||
const basePackages = [ | ||||||||||||||||||||||
'react', | ||||||||||||||||||||||
'react-dom', | ||||||||||||||||||||||
'@vitejs/plugin-react', | ||||||||||||||||||||||
'tailwindcss', | ||||||||||||||||||||||
'@tailwindcss/vite', | ||||||||||||||||||||||
'typescript', | ||||||||||||||||||||||
'vite' | ||||||||||||||||||||||
]; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Find packages that aren't in our base template | ||||||||||||||||||||||
const newPackages = Object.entries(dependencies) | ||||||||||||||||||||||
.filter(([pkg]) => !basePackages.includes(pkg)) | ||||||||||||||||||||||
.map(([pkg, version]) => `${pkg}@${version}`); | ||||||||||||||||||||||
|
||||||||||||||||||||||
return newPackages; | ||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||
console.error('Error parsing package.json:', error); | ||||||||||||||||||||||
return []; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
✅ Graceful Error Handling
The function returns an empty list if JSON parsing fails, indicating graceful error handling.
superjson.parse('{"json":[["au"]]}')...
view full inputview all inputs
The test has passed, which means that the
extractNewPackages
function is correctly returning an empty list when given a malformed JSON string as input. This behavior aligns with the expected property description, indicating that the function is handling JSON parsing errors gracefully.Unit Tests