Skip to content

Commit 51474ab

Browse files
fix: validate installed dependencies only if they exists (#134)
2 parents 3bc7208 + 850dbd1 commit 51474ab

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

lib/build/dispatcher/dispatcher.js

+36-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
getAbsoluteLibDirPath,
1717
presets,
1818
getPackageManager,
19+
getProjectJsonFile,
1920
} from '#utils';
2021
import { Messages } from '#constants';
2122
import { vulcan } from '#env';
@@ -24,16 +25,6 @@ const vulcanLibPath = getAbsoluteLibDirPath();
2425
const vulcanRootPath = resolve(vulcanLibPath, '..');
2526
const isWindows = process.platform === 'win32';
2627

27-
/**
28-
* Get the path corresponding to a specific alias defined in the package.json.
29-
* @param {string} alias - The desired alias.
30-
* @returns {string} The path corresponding to the alias.
31-
*/
32-
/**
33-
* Get the path corresponding to a specific alias defined in the package.json.
34-
* @param {string} alias - The desired alias.
35-
* @returns {string} The path corresponding to the alias.
36-
*/
3728
/**
3829
* Get the path corresponding to a specific alias defined in the package.json.
3930
* @param {string} alias - The desired alias.
@@ -51,6 +42,7 @@ async function getAliasPath(alias) {
5142

5243
return aliasPath;
5344
}
45+
5446
/**
5547
* Move requires and imports to file init
5648
* @param {string} entryContent - The file content to be fixed.
@@ -200,6 +192,38 @@ async function folderExistsInProject(folder) {
200192
}
201193
}
202194

195+
/**
196+
* Check if node_modules dir exists
197+
*/
198+
async function checkNodeModulesDir() {
199+
let projectJson;
200+
try {
201+
projectJson = getProjectJsonFile('package.json');
202+
} catch (error) {
203+
if (error.code === 'ENOENT') {
204+
return;
205+
}
206+
207+
feedback.prebuild.error(error);
208+
process.exit(1);
209+
}
210+
211+
if (
212+
projectJson &&
213+
(projectJson.dependencies || projectJson.devDependencies)
214+
) {
215+
const pkgManager = await getPackageManager();
216+
const existNodeModules = await folderExistsInProject('node_modules');
217+
218+
if (!existNodeModules) {
219+
feedback.prebuild.error(
220+
Messages.build.error.install_dependencies_failed(pkgManager),
221+
);
222+
process.exit(1);
223+
}
224+
}
225+
}
226+
203227
/**
204228
* Class representing a Dispatcher for build operations.
205229
* @example
@@ -226,15 +250,8 @@ class Dispatcher {
226250
* Run the build process.
227251
*/
228252
run = async () => {
229-
// Check install depenedencies
230-
const pckManager = await getPackageManager();
231-
const existNodeModules = await folderExistsInProject('node_modules');
232-
if (!existNodeModules) {
233-
feedback.prebuild.error(
234-
Messages.build.error.install_dependencies_failed(pckManager),
235-
);
236-
process.exit(1);
237-
}
253+
await checkNodeModulesDir();
254+
238255
// Load Context based on preset
239256
const { entryContent, prebuild, config } = await loadBuildContext(
240257
this.preset,

lib/utils/getProjectJsonFile/getProjectJsonFile.utils.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ import { readFileSync } from 'fs';
77
* @returns {object} - JSON object with file content.
88
*/
99
function getProjectJsonFile(filePath) {
10-
try {
11-
const packageJsonPath = join(process.cwd(), filePath);
12-
const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
13-
const packageJson = JSON.parse(packageJsonContent);
10+
const packageJsonPath = join(process.cwd(), filePath);
11+
const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
12+
const packageJson = JSON.parse(packageJsonContent);
1413

15-
return packageJson;
16-
} catch (error) {
17-
throw Error(`Error getting '${filePath}' file: ${error}`);
18-
}
14+
return packageJson;
1915
}
2016

2117
export default getProjectJsonFile;

0 commit comments

Comments
 (0)