From 02d801e8aec9b5eabfc35618428f351f1ff6cb8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wojda=C5=82owicz?= Date: Thu, 22 Apr 2021 19:35:23 +0200 Subject: [PATCH] add bin/create_node_symlink.php --- bin/create_node_symlink.php | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bin/create_node_symlink.php diff --git a/bin/create_node_symlink.php b/bin/create_node_symlink.php new file mode 100644 index 0000000..10d69b4 --- /dev/null +++ b/bin/create_node_symlink.php @@ -0,0 +1,45 @@ + `' . 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);