Skip to content

Commit 0e29ec6

Browse files
committed
fs: add directory autodetection to fsPromises.symlink()
1 parent 8d0f49c commit 0e29ec6

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

doc/api/fs.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,18 +1387,27 @@ changes:
13871387
13881388
<!-- YAML
13891389
added: v10.0.0
1390+
changes:
1391+
- version: REPLACEME
1392+
pr-url: https://github.com/nodejs/node/pull/00000
1393+
description: If the `type` argument is `null` or omitted, Node.js will
1394+
autodetect `target` type and automatically
1395+
select `dir` or `file`.
1396+
13901397
-->
13911398
13921399
* `target` {string|Buffer|URL}
13931400
* `path` {string|Buffer|URL}
1394-
* `type` {string} **Default:** `'file'`
1401+
* `type` {string|null} **Default:** `null`
13951402
* Returns: {Promise} Fulfills with `undefined` upon success.
13961403
13971404
Creates a symbolic link.
13981405
13991406
The `type` argument is only used on Windows platforms and can be one of `'dir'`,
1400-
`'file'`, or `'junction'`. Windows junction points require the destination path
1401-
to be absolute. When using `'junction'`, the `target` argument will
1407+
`'file'`, or `'junction'`. If the `type` argument is not a string, Node.js will
1408+
autodetect `target` type and use `'file'` or `'dir'`. If the `target` does not
1409+
exist, `'file'` will be used. Windows junction points require the destination
1410+
path to be absolute. When using `'junction'`, the `target` argument will
14021411
automatically be normalized to absolute path.
14031412
14041413
### `fsPromises.truncate(path[, len])`

lib/internal/fs/promises.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ const {
111111
const getDirectoryEntriesPromise = promisify(getDirents);
112112
const validateRmOptionsPromise = promisify(validateRmOptions);
113113

114+
const isWindows = process.platform === 'win32';
115+
114116
let cpPromises;
115117
function lazyLoadCpPromises() {
116118
return cpPromises ??= require('internal/fs/cp/cp').cpFn;
@@ -687,7 +689,11 @@ async function readlink(path, options) {
687689
}
688690

689691
async function symlink(target, path, type_) {
690-
const type = (typeof type_ === 'string' ? type_ : null);
692+
let type = (typeof type_ === 'string' ? type_ : null);
693+
if (isWindows && type === null) {
694+
const absoluteTarget = pathModule.resolve(`${path}`, '..', `${target}`);
695+
type = (await stat(absoluteTarget))?.isDirectory() ? 'dir' : 'file';
696+
}
691697
target = getValidatedPath(target, 'target');
692698
path = getValidatedPath(path);
693699
return binding.symlink(preprocessSymlinkDestination(target, type, path),

test/parallel/test-fs-symlink-dir.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if (!common.canCreateSymLink())
1212
const assert = require('assert');
1313
const path = require('path');
1414
const fs = require('fs');
15+
const fsPromises = fs.promises;
1516

1617
const tmpdir = require('../common/tmpdir');
1718
tmpdir.refresh();
@@ -36,11 +37,18 @@ function testAsync(target, path) {
3637
}));
3738
}
3839

40+
async function testPromises(target, path) {
41+
await fsPromises.symlink(target, path);
42+
fs.readdirSync(path);
43+
}
44+
3945
for (const linkTarget of linkTargets) {
4046
fs.mkdirSync(path.resolve(tmpdir.path, linkTarget));
4147
for (const linkPath of linkPaths) {
4248
testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`);
4349
testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
50+
testPromises(linkTarget, `${linkPath}-${path.basename(linkTarget)}-promises`)
51+
.then(common.mustCall());
4452
}
4553
}
4654

@@ -57,10 +65,17 @@ for (const linkTarget of linkTargets) {
5765
}));
5866
}
5967

68+
async function testPromises(target, path) {
69+
await fsPromises.symlink(target, path);
70+
assert(!fs.existsSync(path));
71+
}
72+
6073
for (const linkTarget of linkTargets.map((p) => p + '-broken')) {
6174
for (const linkPath of linkPaths) {
6275
testSync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-sync`);
6376
testAsync(linkTarget, `${linkPath}-${path.basename(linkTarget)}-async`);
77+
testPromises(linkTarget, `${linkPath}-${path.basename(linkTarget)}-promises`)
78+
.then(common.mustCall());
6479
}
6580
}
6681
}

0 commit comments

Comments
 (0)