-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69e5eb9
commit 02d801e
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
const NODE_MODULES_FOLDER_NAME = 'node_modules'; | ||
const PATH_TO_NODE_MODULES = 'tests' . DIRECTORY_SEPARATOR . 'Application' . DIRECTORY_SEPARATOR . 'node_modules'; | ||
|
||
/* cannot use `file_exists` or `stat` as gives false on symlinks if target path does not exist yet */ | ||
if (@lstat(NODE_MODULES_FOLDER_NAME)) | ||
{ | ||
if (is_link(NODE_MODULES_FOLDER_NAME) || is_dir(NODE_MODULES_FOLDER_NAME)) { | ||
echo '> `' . NODE_MODULES_FOLDER_NAME . '` already exists as a link or folder, keeping existing as may be intentional.' . PHP_EOL; | ||
exit(0); | ||
} else { | ||
echo '> Invalid symlink `' . NODE_MODULES_FOLDER_NAME . '` detected, recreating...' . PHP_EOL; | ||
if (!@unlink(NODE_MODULES_FOLDER_NAME)) { | ||
echo '> Could not delete file `' . NODE_MODULES_FOLDER_NAME . '`.' . PHP_EOL; | ||
exit(1); | ||
} | ||
} | ||
} | ||
|
||
/* try to create the symlink using PHP internals... */ | ||
$success = @symlink(PATH_TO_NODE_MODULES, NODE_MODULES_FOLDER_NAME); | ||
|
||
/* if case it has failed, but OS is Windows... */ | ||
if (!$success && strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | ||
/* ...then try a different approach which does not require elevated permissions and folder to exist */ | ||
echo '> This system is running Windows, creation of links requires elevated privileges,' . PHP_EOL; | ||
echo '> and target path to exist. Fallback to NTFS Junction:' . PHP_EOL; | ||
exec(sprintf('mklink /J %s %s 2> NUL', NODE_MODULES_FOLDER_NAME, PATH_TO_NODE_MODULES), $output, $returnCode); | ||
$success = $returnCode === 0; | ||
if (!$success) { | ||
echo '> Failed o create the required symlink' . PHP_EOL; | ||
exit(2); | ||
} | ||
} | ||
|
||
$path = @readlink(NODE_MODULES_FOLDER_NAME); | ||
/* check if link points to the intended directory */ | ||
if ($path && realpath($path) === realpath(PATH_TO_NODE_MODULES)) { | ||
echo '> Successfully created the symlink.' . PHP_EOL; | ||
exit(0); | ||
} | ||
|
||
echo '> Failed to create the symlink to `' . NODE_MODULES_FOLDER_NAME . '`.' . PHP_EOL; | ||
exit(3); |