I have a projects with the following structure:
project-root
├── some-dir
│ ├── alice.json
│ ├── bob.json
│ └── dave.json
└── ...
I want to create symlinks like the following ones:
foo
->alice.json
I chose to use the fs.symlink
function:
fs.symlink(srcpath, dstpath[, type], callback)
Asynchronous symlink(2). No arguments other than a possible exception are given to the completion callback. The
type
argument can be set to'dir'
,'file'
, or'junction'
(default is'file'
) and is only available on Windows (ignored on other platforms). Note that Windows junction points require the destination path to be absolute. When using'junction'
, the destination argument will automatically be normalized to absolute path.
So, I did:
require("fs").symlink(
projectRoot + "/some-dir/alice.json"
, projectRoot + "/some-dir/foo"
, function (err) { console.log(err || "Done."); }
);
This creates the foo
symlink. However, since the paths are absolute the symlink uses absolute path as well.
How can I make the symlink path relative to a directory (in this case to some-dir
)?
This will prevent errors when the parent directories are renamed or the project is moved on another machine.
The dirty alternative I see is using exec("ln -s alice.json foo", { cwd: pathToSomeDir }, callback);
, but I would like to avoid that and use the NodeJS API.
So, how can I make relative symlinks using absolute paths in NodeJS?
projectRoot
as the working directory anyway? – Opuscule