Skip to content

Commit d847127

Browse files
committed
updates
- accept `driveLetter` option for mapping - accept `persistent` option - export debug namespace - dependencies
1 parent 6b29f56 commit d847127

22 files changed

+4445
-4797
lines changed

.github/workflows/coverage.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ jobs:
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
node: [ 18, 20, 21 ]
13+
node: [ 22, 24 ]
1414
env:
15-
DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}
15+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
16+
1617
name: Node ${{ matrix.node }}
1718
steps:
18-
- uses: actions/checkout@v4
19+
- uses: actions/checkout@v5
1920

2021
- name: Setup Node
21-
uses: actions/setup-node@v4
22+
uses: actions/setup-node@v5
2223
with:
2324
node-version: ${{ matrix.node }}
25+
cache: 'npm'
2426

2527
- name: Install Package
2628
run: |
@@ -33,11 +35,9 @@ jobs:
3335
- name: Run Coverage Testing
3436
run: npm run coverage
3537

36-
- name: DeepSource
37-
if: ${{ github.event_name != 'pull_request' && env.DEEPSOURCE_DSN != '' && matrix.node == 20 }}
38+
- name: Codecov
39+
if: ${{ (success() || failure()) && github.event_name != 'pull_request' && env.CODECOV_TOKEN != '' && matrix.node == 22 }}
3840
run: |
39-
# Install deepsource CLI
40-
curl https://deepsource.io/cli | sh
41-
42-
# From the root directory, run the report coverage command
43-
./bin/deepsource report --analyzer test-coverage --key javascript --value-file ./coverage/lcov.info
41+
curl -Os https://uploader.codecov.io/latest/linux/codecov
42+
chmod +x codecov
43+
./codecov -t ${CODECOV_TOKEN}

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
[![npm (scoped)](https://img.shields.io/npm/v/%40cityssm/windows-unc-path-connect)](https://www.npmjs.com/package/@cityssm/windows-unc-path-connect)
44
[![DeepSource](https://app.deepsource.com/gh/cityssm/node-windows-unc-path-connect.svg/?label=active+issues&show_trend=true&token=kMenIfdyEcHVDtebaPlgkjFy)](https://app.deepsource.com/gh/cityssm/node-windows-unc-path-connect/)
5-
[![Maintainability](https://api.codeclimate.com/v1/badges/0c0f2c336c22c3c889fa/maintainability)](https://codeclimate.com/github/cityssm/node-windows-unc-path-connect/maintainability)
6-
[![DeepSource](https://app.deepsource.com/gh/cityssm/node-windows-unc-path-connect.svg/?label=code+coverage&show_trend=false&token=kMenIfdyEcHVDtebaPlgkjFy)](https://app.deepsource.com/gh/cityssm/node-windows-unc-path-connect/)
75
[![Coverage Testing](https://github.com/cityssm/node-windows-unc-path-connect/actions/workflows/coverage.yml/badge.svg)](https://github.com/cityssm/node-windows-unc-path-connect/actions/workflows/coverage.yml)
86

97
Ensures a UNC path that requires a user name and password
@@ -33,8 +31,14 @@ const success = connectToUncPath(
3331
password: 'p@ss'
3432
},
3533
{
36-
// Attempt to clean up connection on application shutdown
37-
deleteOnExit: true
34+
// Optionally attempt to clean up connection on application shutdown.
35+
deleteOnExit: true,
36+
37+
// Optionally assign a drive letter.
38+
driveLetter: 'M:',
39+
40+
// Optionally persist the connection after restart.
41+
persistent: true
3842
}
3943
)
4044

debug.config.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare const DEBUG_NAMESPACE = "windows-unc-path-connect";
2+
export declare const DEBUG_ENABLE_NAMESPACES = "windows-unc-path-connect:*";

debug.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DEBUG_NAMESPACE = 'windows-unc-path-connect';
2+
export const DEBUG_ENABLE_NAMESPACES = `${DEBUG_NAMESPACE}:*`;

debug.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const DEBUG_NAMESPACE = 'windows-unc-path-connect'
2+
3+
export const DEBUG_ENABLE_NAMESPACES = `${DEBUG_NAMESPACE}:*`

eslint.config.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from 'eslint-config-cityssm';
1+
export { default } from 'eslint-config-cityssm/eslint.packageConfig.js';

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from 'eslint-config-cityssm';
1+
export { default } from 'eslint-config-cityssm/eslint.packageConfig.js';

eslint.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from 'eslint-config-cityssm'
1+
export { default } from 'eslint-config-cityssm/eslint.packageConfig.js'

index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ConnectOptions, UncPath, UncPathOptions } from './types.js';
1+
import type { ConnectOptions, UncPathOptions } from './types.js';
22
export declare function connectToUncPath(uncPathOptions: UncPathOptions, connectOptions?: Partial<ConnectOptions>): boolean;
3-
export declare function disconnectUncPath(uncPath: UncPath): boolean;
4-
export type { UncPath, UncPathOptions, UncPathOptionsWithoutCredentials, UncPathOptionsWithCredentials, ConnectOptions } from './types.js';
5-
export { isWindows, uncPathIsSafe, uncPathOptionsAreSafe } from './validators.js';
3+
export declare function disconnectUncPath(uncPathOrDriveLetter: string): boolean;
4+
export type { ConnectOptions, UncPath, UncPathOptions, UncPathOptionsWithCredentials, UncPathOptionsWithoutCredentials } from './types.js';
5+
export { driveLetterIsSafe, isWindows, uncPathIsSafe, uncPathOptionsAreSafe } from './validators.js';

index.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
import { execSync } from 'node:child_process';
22
import Debug from 'debug';
33
import exitHook from 'exit-hook';
4-
import { isWindows, uncPathIsSafe, uncPathOptionsAreSafe, uncPathOptionsHaveCredentials } from './validators.js';
5-
const debug = Debug('windows-unc-path-connect');
4+
import { DEBUG_NAMESPACE } from './debug.config.js';
5+
import { driveLetterIsSafe, isWindows, uncPathIsSafe, uncPathOptionsAreSafe, uncPathOptionsHaveCredentials } from './validators.js';
6+
const timeoutMilliseconds = 30_000;
7+
const debug = Debug(`${DEBUG_NAMESPACE}:index`);
68
export function connectToUncPath(uncPathOptions, connectOptions) {
79
if (!isWindows() || !uncPathOptionsAreSafe(uncPathOptions)) {
810
return false;
911
}
1012
debug(`Connecting to share: ${uncPathOptions.uncPath}`);
11-
let command = `net use "${uncPathOptions.uncPath}"`;
13+
const commandPieces = ['net use'];
14+
if (connectOptions?.driveLetter) {
15+
commandPieces.push(connectOptions.driveLetter);
16+
}
17+
commandPieces.push(`"${uncPathOptions.uncPath}"`);
18+
if (connectOptions?.persistent) {
19+
commandPieces.push('/persistent:yes');
20+
}
1221
if (uncPathOptionsHaveCredentials(uncPathOptions)) {
13-
command += ` /user:"${uncPathOptions.userName}" "${uncPathOptions.password}"`;
22+
commandPieces.push(`/user:"${uncPathOptions.userName}" "${uncPathOptions.password}"`);
1423
}
1524
try {
16-
const output = execSync(command, { stdio: 'pipe' });
25+
const output = execSync(commandPieces.join(' '), {
26+
stdio: 'pipe',
27+
timeout: timeoutMilliseconds
28+
});
1729
debug(output.toString().trim());
1830
if (connectOptions?.deleteOnExit ?? false) {
1931
exitHook(() => {
20-
disconnectUncPath(uncPathOptions.uncPath);
32+
disconnectUncPath(connectOptions?.driveLetter ?? uncPathOptions.uncPath);
2133
});
2234
}
2335
return output.includes('command completed successfully');
@@ -28,20 +40,30 @@ export function connectToUncPath(uncPathOptions, connectOptions) {
2840
.includes('Multiple connections to a server or shared resource');
2941
}
3042
}
31-
export function disconnectUncPath(uncPath) {
32-
if (!isWindows() || !uncPathIsSafe(uncPath)) {
43+
export function disconnectUncPath(uncPathOrDriveLetter) {
44+
if (!isWindows() ||
45+
(!uncPathIsSafe(uncPathOrDriveLetter) &&
46+
!driveLetterIsSafe(uncPathOrDriveLetter))) {
3347
return false;
3448
}
35-
debug(`Disconnecting share: ${uncPath}`);
36-
const command = `net use "${uncPath}" /delete`;
49+
debug(`Disconnecting share: ${uncPathOrDriveLetter}`);
50+
const command = `net use "${uncPathOrDriveLetter}" /delete`;
3751
try {
38-
const output = execSync(command, { stdio: 'pipe' });
52+
const output = execSync(command, {
53+
stdio: 'pipe',
54+
timeout: timeoutMilliseconds
55+
});
3956
debug(output.toString().trim());
4057
return (output.includes('deleted successfully') ||
4158
output.includes('connection could not be found'));
4259
}
43-
catch {
60+
catch (error) {
61+
const errorString = error.toString();
62+
if (errorString.includes('network connection could not be found')) {
63+
return true;
64+
}
65+
debug(errorString);
4466
return false;
4567
}
4668
}
47-
export { isWindows, uncPathIsSafe, uncPathOptionsAreSafe } from './validators.js';
69+
export { driveLetterIsSafe, isWindows, uncPathIsSafe, uncPathOptionsAreSafe } from './validators.js';

0 commit comments

Comments
 (0)