Skip to content

Commit

Permalink
OP-377 - Add bin directory with create symlink file, update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonKostrubiec committed Jul 24, 2024
1 parent 1398815 commit 812d79f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/bin/*
!/bin/.gitkeep

/vendor/
Expand Down
45 changes: 45 additions & 0 deletions bin/create_node_symlink.php
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);
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
"phpspec/phpspec": "^7.2",
"behat/behat": "^3.6.1",
"behat/mink-selenium2-driver": "^1.4",
"behat/mink": "^1.7",
"behat/mink-browserkit-driver": "^1.3",
"friends-of-behat/mink": "^1.8",
"friends-of-behat/mink-browserkit-driver": "^1.4",
"friends-of-behat/mink-debug-extension": "^2.0.0",
Expand Down

0 comments on commit 812d79f

Please sign in to comment.