npx-import
Advanced tools
Comparing version 1.0.2 to 1.0.3-0
@@ -124,3 +124,3 @@ import semver from 'semver'; | ||
const paths = stdout.split(':'); | ||
const tempPath = paths.find((p) => /\/\.npm\/_npx\//.exec(p)); | ||
const tempPath = paths.find((p) => /\/\.npm\/_npx\/|\\npm-cache\\_npx\\/.exec(p)); | ||
if (!tempPath) | ||
@@ -127,0 +127,0 @@ throw new Error(`Failed to find temporary install directory. Looking for paths matching '/.npm/_npx/' in:\n${JSON.stringify(paths)}`); |
{ | ||
"name": "npx-import", | ||
"version": "1.0.2", | ||
"version": "1.0.3-0", | ||
"description": "Runtime dependencies, installed as if by magic ✨", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -161,3 +161,3 @@ import semver from 'semver' | ||
const paths = stdout.split(':') | ||
const tempPath = paths.find((p) => /\/\.npm\/_npx\//.exec(p)) | ||
const tempPath = paths.find((p) => /\/\.npm\/_npx\/|\\npm-cache\\_npx\\/.exec(p)) | ||
@@ -164,0 +164,0 @@ if (!tempPath) |
@@ -16,2 +16,3 @@ import { afterEach, describe, expect, test, vi } from 'vitest' | ||
pkgParseFailed, | ||
getWindowsNpxPath, | ||
} from './utils' | ||
@@ -163,2 +164,20 @@ import { npxImport, npxResolve } from '../lib' | ||
test(`Should fail if NPX returns weirdness`, async () => { | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand( | ||
`npx --prefer-online -y -p bad-npx-paths@latest node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: '/lol:/wtf:/is/going/on' }) | ||
await npxImportFailed( | ||
'bad-npx-paths', | ||
matchesAllLines( | ||
`Failed to find temporary install directory. Looking for paths matching '/.npm/_npx/' in:`, | ||
`["/lol","/wtf","/is/going/on"]` | ||
) | ||
) | ||
}) | ||
test(`Should attempt to install, passing through whatever happens`, async () => { | ||
@@ -203,5 +222,8 @@ expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand(`npx --prefer-online -y -p @org/pkg@my-tag node -e 'console.log(process.env.PATH)'`, { | ||
shell: true, | ||
}).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectExecaCommand( | ||
`npx --prefer-online -y -p @org/pkg@my-tag node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, '@org/pkg/weird-path.js').returning( | ||
@@ -228,5 +250,8 @@ new Error(`Error [ERR_MODULE_NOT_FOUND]: Cannot find module '${basePath}/weird-path.js'`) | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand(`npx --prefer-online -y -p @org/pkg@my-tag node -e 'console.log(process.env.PATH)'`, { | ||
shell: true, | ||
}).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectExecaCommand( | ||
`npx --prefer-online -y -p @org/pkg@my-tag node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, '@org/pkg/lib/index.js').returning({ foo: 1, bar: 2 }) | ||
@@ -246,2 +271,28 @@ | ||
// TODO: run this on a windows env, otherwise `path.resolve` clobbers the windows like path... | ||
test.skip(`Should call relative import and return`, async () => { | ||
const npxDirectoryHash = randomString(12) | ||
const basePath = `\\Users\\glen\\AppData\\Local\\npm-cache\\_npx\\${npxDirectoryHash}\\node_modules` | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand( | ||
`npx --prefer-online -y -p @org/pkg@my-tag node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getWindowsNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, '@org/pkg/lib/index.js').returning({ foo: 1, bar: 2 }) | ||
const imported = await npxImportSucceeded( | ||
'@org/pkg@my-tag/lib/index.js', | ||
matchesAllLines( | ||
`@org/pkg/lib/index.js not available locally. Attempting to use npx to install temporarily.`, | ||
`Installing... (npx --prefer-online -y -p @org/pkg@my-tag)`, | ||
`Installed into ${basePath}.`, | ||
`To skip this step in future, run: pnpm add -D @org/pkg@my-tag` | ||
) | ||
) | ||
expect(imported).toStrictEqual({ foo: 1, bar: 2 }) | ||
}) | ||
test(`Should prefer offline for exact versions`, async () => { | ||
@@ -252,5 +303,8 @@ const npxDirectoryHash = randomString(12) | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand(`npx --prefer-offline -y -p @org/pkg@3.0.1 node -e 'console.log(process.env.PATH)'`, { | ||
shell: true, | ||
}).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectExecaCommand( | ||
`npx --prefer-offline -y -p @org/pkg@3.0.1 node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, '@org/pkg/lib/index.js').returning({ foo: 1, bar: 2 }) | ||
@@ -307,5 +361,8 @@ | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand(`npx --prefer-offline -y -p pkg-b@1.2.3 node -e 'console.log(process.env.PATH)'`, { | ||
shell: true, | ||
}).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectExecaCommand( | ||
`npx --prefer-offline -y -p pkg-b@1.2.3 node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, 'pkg-b').returning({ name: 'pkg-b', bar: 2, local: false }) | ||
@@ -376,5 +433,8 @@ | ||
expectExecaCommand('npx --version').returning({ stdout: '8.1.2' }) | ||
expectExecaCommand(`npx --prefer-online -y -p pkg-b@latest node -e 'console.log(process.env.PATH)'`, { | ||
shell: true, | ||
}).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectExecaCommand( | ||
`npx --prefer-online -y -p pkg-b@latest node -e 'console.log(process.env.PATH)'`, | ||
{ | ||
shell: true, | ||
} | ||
).returning({ stdout: getNpxPath(npxDirectoryHash) }) | ||
expectRelativeImport(basePath, 'pkg-b').returning({ name: 'pkg-b', bar: 2, local: false }) | ||
@@ -381,0 +441,0 @@ |
@@ -117,1 +117,49 @@ import { expect, MockedFunction } from 'vitest' | ||
} | ||
export function getWindowsNpxPath(npxDirectoryHash: string) { | ||
// https://github.com/geelen/npx-import/issues/5 | ||
return [ | ||
'C', | ||
'\\Users\\glen\\node_modules\\.bin;C', | ||
'\\Users\\node_modules\\.bin;C', | ||
'\\node_modules\\.bin;C', | ||
'\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\@npmcli\\run-script\\lib\\node-gyp-bin;C', | ||
'\\Program Files\\Eclipse Foundation\\jdk-11.0.12.7-hotspot\\bin;C', | ||
'\\Windows\\system32;C', | ||
'\\Windows;C', | ||
'\\Windows\\System32\\Wbem;C', | ||
'\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C', | ||
'\\Windows\\System32\\OpenSSH\\;C', | ||
'\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C', | ||
'\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C', | ||
'\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C', | ||
'\\WINDOWS\\system32;C', | ||
'\\WINDOWS;C', | ||
'\\WINDOWS\\System32\\Wbem;C', | ||
'\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C', | ||
'\\WINDOWS\\System32\\OpenSSH\\;C', | ||
'\\Program Files\\dotnet\\;C', | ||
'\\Program Files\\nodejs\\;C', | ||
'\\Program Files\\PuTTY\\;C', | ||
'\\Program Files\\wooting-analog-sdk\\;C', | ||
'\\ProgramData\\ComposerSetup\\bin;C', | ||
'\\Program Files\\GitHub CLI\\;C', | ||
'\\Program Files\\Git\\cmd;C', | ||
'\\Program Files\\Docker\\Docker\\resources\\bin;C', | ||
'\\ProgramData\\DockerDesktop\\version-bin;C', | ||
'\\Users\\glen\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\;C', | ||
'\\Users\\glen\\AppData\\Local\\Programs\\Python\\Python310\\;C', | ||
'\\Ruby31-x64\\bin;C', | ||
'\\vips-dev-8.13\\bin;C', | ||
'\\php-8.1.7-Win32-vs16-x64;C', | ||
'\\Users\\glen\\AppData\\Local\\Microsoft\\WindowsApps;C', | ||
'\\Users\\glen\\AppData\\Local\\GitHubDesktop\\bin;C', | ||
'\\Users\\glen\\AppData\\Roaming\\npm;C', | ||
'\\Users\\glen\\.dotnet\\tools;C', | ||
'\\ProgramData\\ComposerSetup\\bin;C', | ||
'\\Users\\glen\\AppData\\Roaming\\Composer\\vendor\\bin;C', | ||
'\\Users\\glen\\AppData\\Local\\JetBrains\\Toolbox\\scripts;C', | ||
'\\Users\\glen\\.dotnet\\tools;C', | ||
`\\Users\\glen\\AppData\\Local\\npm-cache\\_npx\\${npxDirectoryHash}\\node_modules\\.bin` | ||
].join(':') | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62155
978
1