Skip to content

Commit c2789e5

Browse files
committed
fix: validate installed dependencies only if they exists
1 parent 1ca16a1 commit c2789e5

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

lib/build/dispatcher/dispatcher.js

+33-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
presets,
1818
getExportedFunctionBody,
1919
getPackageManager,
20+
getProjectJsonFile,
2021
relocateImportsAndRequires,
2122
} from '#utils';
2223
import { Messages } from '#constants';
@@ -201,6 +202,37 @@ async function folderExistsInProject(folder) {
201202
return Promise.resolve(false);
202203
}
203204
}
205+
/**
206+
* Check if node_modules dir exists
207+
*/
208+
async function checkNodeModulesDir() {
209+
let projectJson;
210+
try {
211+
projectJson = getProjectJsonFile('package.json');
212+
} catch (error) {
213+
if (error.code === 'ENOENT') {
214+
return;
215+
}
216+
217+
feedback.prebuild.error(error);
218+
process.exit(1);
219+
}
220+
221+
if (
222+
projectJson &&
223+
(projectJson.dependencies || projectJson.devDependencies)
224+
) {
225+
const pkgManager = await getPackageManager();
226+
const existNodeModules = await folderExistsInProject('node_modules');
227+
228+
if (!existNodeModules) {
229+
feedback.prebuild.error(
230+
Messages.build.error.install_dependencies_failed(pkgManager),
231+
);
232+
process.exit(1);
233+
}
234+
}
235+
}
204236

205237
/**
206238
* Class representing a Dispatcher for build operations.
@@ -231,15 +263,7 @@ class Dispatcher {
231263
* Run the build process.
232264
*/
233265
run = async () => {
234-
// Check install depenedencies
235-
const pckManager = await getPackageManager();
236-
const existNodeModules = await folderExistsInProject('node_modules');
237-
if (!existNodeModules) {
238-
feedback.prebuild.error(
239-
Messages.build.error.install_dependencies_failed(pckManager),
240-
);
241-
process.exit(1);
242-
}
266+
await checkNodeModulesDir();
243267

244268
// Load Context based on preset
245269
const { entryContent, prebuild, config } = await loadBuildContext(

lib/utils/generateTimestamp/generateTimestamp.utils.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import generateTimestamp from './index.js';
1+
import generateTimestamp from './generateTimestamp.utils.js';
22

33
describe('generateTimestamp utils', () => {
44
test('Should generate a timestamp string in the format "YYYYMMDDHHmmss".', async () => {

lib/utils/getExportedFunctionBody/getExportedFunctionBody.utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parser from '@babel/parser';
1+
import * as parser from '@babel/parser';
22
import _traverse from '@babel/traverse';
33
import _generate from '@babel/generator';
44
import * as t from '@babel/types';

lib/utils/getExportedFunctionBody/getExportedFunctionBody.utils.test.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getExportedFunctionBody from '../lib/utils/getExportedFunctionBody/getExportedFunctionBody.utils.js';
1+
import getExportedFunctionBody from './getExportedFunctionBody.utils.js';
22

33
describe('getExportedFunctionBody', () => {
44
it('should extract the body from a default exported function declaration', () => {
@@ -12,7 +12,9 @@ describe('getExportedFunctionBody', () => {
1212
return 'Hello, world!';
1313
`;
1414

15-
expect(getExportedFunctionBody(inputCode)).toBe(expectedOutput);
15+
expect(getExportedFunctionBody(inputCode).replace(/\s/g, '')).toBe(
16+
expectedOutput.replace(/\s/g, ''),
17+
);
1618
});
1719

1820
it('should extract the body from a default exported anonymous function', () => {
@@ -26,7 +28,9 @@ describe('getExportedFunctionBody', () => {
2628
return 'Hello, world!';
2729
`;
2830

29-
expect(getExportedFunctionBody(inputCode)).toBe(expectedOutput);
31+
expect(getExportedFunctionBody(inputCode).replace(/\s/g, '')).toBe(
32+
expectedOutput.replace(/\s/g, ''),
33+
);
3034
});
3135

3236
it('should extract the body from a default exported arrow function', () => {
@@ -40,7 +44,9 @@ describe('getExportedFunctionBody', () => {
4044
return 'Hello, world!';
4145
`;
4246

43-
expect(getExportedFunctionBody(inputCode)).toBe(expectedOutput);
47+
expect(getExportedFunctionBody(inputCode).replace(/\s/g, '')).toBe(
48+
expectedOutput.replace(/\s/g, ''),
49+
);
4450
});
4551

4652
it('should extract the body from a default exported function assigned to a variable', () => {
@@ -55,7 +61,9 @@ describe('getExportedFunctionBody', () => {
5561
return 'Hello, world!';
5662
`;
5763

58-
expect(getExportedFunctionBody(inputCode)).toBe(expectedOutput);
64+
expect(getExportedFunctionBody(inputCode).replace(/\s/g, '')).toBe(
65+
expectedOutput.replace(/\s/g, ''),
66+
);
5967
});
6068

6169
it('should throw an error if there are no default exports', () => {
@@ -65,6 +73,8 @@ describe('getExportedFunctionBody', () => {
6573
}
6674
`;
6775

68-
expect(() => getExportedFunctionBody(inputCode)).toThrow('No default exports were found for entrypoint in the provided code.');
76+
expect(() => getExportedFunctionBody(inputCode)).toThrow(
77+
'No default exports were found for entrypoint in the provided code.',
78+
);
6979
});
70-
});
80+
});

0 commit comments

Comments
 (0)