Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ test.concurrent('throws error if package.json does not have name', async (): Pro
expect(err.message).toContain(reporter.lang('unknownPackageName'));
}
});

test.concurrent('creates cmd file on Windows', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');
const prefix = await mkdir('prefix-folder');

await fs.mkdirp(path.join(prefix, 'bin'));

await runLink([], {linkFolder, prefix}, 'package-with-bin', async (config, reporter): Promise<void> => {
const linkFolderExisted = await fs.exists(path.join(linkFolder, 'b-package'));
expect(linkFolderExisted).toEqual(true);

if (process.platform === 'win32') {
const cmdExisted = await fs.exists(path.join(prefix, 'bin/file.cmd'));
expect(cmdExisted).toEqual(true);
}

const binExisted = await fs.exists(path.join(prefix, 'bin/file'));
expect(binExisted).toEqual(true);
});
});
33 changes: 33 additions & 0 deletions __tests__/commands/unlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,36 @@ test.concurrent('throws error if package.json does not have name', async (): Pro
expect(err.message).toContain(reporter.lang('unknownPackageName'));
}
});

test.concurrent('creates cmd file on Windows', async (): Promise<void> => {
const linkFolder = await mkdir('link-folder');
const prefix = await mkdir('prefix-folder');

await fs.mkdirp(path.join(prefix, 'bin'));

await runLink([], {linkFolder, prefix}, 'package-with-bin', async (config, reporter): Promise<void> => {
const linkFolderExisted = await fs.exists(path.join(linkFolder, 'b-package'));
expect(linkFolderExisted).toEqual(true);

if (process.platform === 'win32') {
const cmdExisted = await fs.exists(path.join(prefix, 'bin/file.cmd'));
expect(cmdExisted).toEqual(true);
}

const binExisted = await fs.exists(path.join(prefix, 'bin/file'));
expect(binExisted).toEqual(true);
});

await runUnlink([], {linkFolder, prefix}, 'package-with-bin', async (config, reporter): Promise<void> => {
const linkFolderExisted = await fs.exists(path.join(linkFolder, 'b-package'));
expect(linkFolderExisted).toEqual(false);

if (process.platform === 'win32') {
const cmdExisted = await fs.exists(path.join(prefix, 'bin/file.cmd'));
expect(cmdExisted).toEqual(false);
}

const binExisted = await fs.exists(path.join(prefix, 'bin/file'));
expect(binExisted).toEqual(false);
});
});
Empty file.
5 changes: 5 additions & 0 deletions __tests__/fixtures/link/package-with-bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "b-package",
"private": true,
"bin": {"file": "file.js"}
}
8 changes: 7 additions & 1 deletion src/cli/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import {MessageError} from '../../errors.js';
import * as promise from '../../util/promise.js';
import * as fs from '../../util/fs.js';
import {getBinFolder as getGlobalBinFolder} from './global';

const invariant = require('invariant');
const cmdShim = promise.promisify(require('cmd-shim'));
const path = require('path');

export async function getRegistryFolder(config: Config, name: string): Promise<string> {
Expand Down Expand Up @@ -73,7 +75,11 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
if (await fs.exists(binDestLoc)) {
reporter.warn(reporter.lang('binLinkCollision', binName));
} else {
await fs.symlink(binSrcLoc, binDestLoc);
if (process.platform === 'win32') {
await cmdShim(binSrcLoc, binDestLoc);
} else {
await fs.symlink(binSrcLoc, binDestLoc);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/commands/unlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
const binDestLoc = path.join(globalBinFolder, binName);
if (await fs.exists(binDestLoc)) {
await fs.unlink(binDestLoc);
if (process.platform === 'win32') {
await fs.unlink(binDestLoc + '.cmd');
}
}
}
}
Expand Down