Files
smartnpm/readme.md

353 lines
11 KiB
Markdown
Raw Permalink Normal View History

2023-07-25 18:14:51 +02:00
# @push.rocks/smartnpm
**Smart npm interface for Node.js 🚀**
2017-08-15 17:17:29 +02:00
[![npm](https://img.shields.io/npm/v/@push.rocks/smartnpm.svg)](https://www.npmjs.com/package/@push.rocks/smartnpm)
[![TypeScript](https://img.shields.io/badge/TypeScript-4.x-blue.svg)](https://www.typescriptlang.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
> A powerful TypeScript library to programmatically interact with npm registries, retrieve package information, search packages, and handle package downloads with full caching support.
## 🎯 Features
- 📦 **Package Information Retrieval** - Get comprehensive metadata about any npm package
- 🔍 **Advanced Package Search** - Search npm registry with multiple filter options
- 💾 **Package Downloads** - Download and extract packages to disk programmatically
- 📁 **File Extraction** - Extract specific files or directories from packages without full download
- 🏷️ **Version & Tag Management** - Work with specific versions, dist-tags, and version ranges
-**Smart Caching** - Built-in caching system for improved performance
- 🌐 **Custom Registry Support** - Use with npm, Verdaccio, or any npm-compatible registry
- 🗂️ **Virtual Directory Creation** - Load packages as virtual file systems in memory
## 📥 Installation
```bash
2024-04-14 18:03:33 +02:00
npm install @push.rocks/smartnpm --save
```
2017-08-15 17:17:29 +02:00
Or using pnpm (recommended):
2018-09-01 16:40:42 +02:00
```bash
pnpm add @push.rocks/smartnpm
```
## 🚀 Quick Start
2017-08-15 17:17:29 +02:00
2024-04-14 18:03:33 +02:00
```typescript
import { NpmRegistry } from '@push.rocks/smartnpm';
2020-03-17 00:38:58 +00:00
// Initialize with default npm registry
2024-04-14 18:03:33 +02:00
const npmRegistry = new NpmRegistry();
2020-03-17 00:38:58 +00:00
// Or use a custom registry
2024-04-14 18:03:33 +02:00
const customRegistry = new NpmRegistry({
npmRegistryUrl: 'https://your-registry.example.com'
2024-04-14 18:03:33 +02:00
});
```
## 📘 Usage Examples
### Package Information Retrieval
Get detailed information about any npm package:
2024-04-14 18:03:33 +02:00
```typescript
import { NpmRegistry } from '@push.rocks/smartnpm';
2024-04-14 18:03:33 +02:00
const npmRegistry = new NpmRegistry();
async function getPackageDetails() {
const packageInfo = await npmRegistry.getPackageInfo('@angular/core');
console.log(`Package: ${packageInfo.name}`);
console.log(`Latest Version: ${packageInfo.version}`);
console.log(`Description: ${packageInfo.description}`);
console.log(`License: ${packageInfo.license}`);
// Access all versions
packageInfo.allVersions.forEach(version => {
console.log(`- ${version.version}: ${version.date}`);
});
// Access dist tags
packageInfo.allDistTags.forEach(tag => {
console.log(`Tag ${tag.name}: ${tag.targetVersion}`);
});
}
2024-04-14 18:03:33 +02:00
```
### 🔍 Advanced Package Search
Search the npm registry with powerful filters:
2024-04-14 18:03:33 +02:00
```typescript
async function searchPackages() {
// Search with multiple criteria
2024-04-14 18:03:33 +02:00
const searchResults = await npmRegistry.searchOnNpm({
name: 'webpack-plugin',
keywords: ['webpack', 'plugin', 'build'],
author: 'webpack-contrib',
maintainer: 'sokra',
scope: '@webpack',
deprecated: false,
unstable: false,
insecure: false,
boostExact: true,
scoreEffect: 15.3,
qualityWeight: 1.95,
popularityWeight: 3.3,
maintenanceWeight: 2.05
});
console.log(`Found ${searchResults.length} packages`);
searchResults.forEach(pkg => {
console.log(`📦 ${pkg.name}@${pkg.version}`);
console.log(` Score: ${pkg.searchScore}`);
console.log(` Quality: ${pkg.score.detail.quality}`);
console.log(` Popularity: ${pkg.score.detail.popularity}`);
console.log(` Maintenance: ${pkg.score.detail.maintenance}`);
2024-04-14 18:03:33 +02:00
});
}
```
### 💾 Download Packages
Download and extract npm packages to your filesystem:
2024-04-14 18:03:33 +02:00
```typescript
async function downloadPackage() {
// Download latest version
await npmRegistry.savePackageToDisk('express', './downloads/express');
// Download specific version
const packageInfo = await npmRegistry.getPackageInfo('express');
const specificVersion = packageInfo.allVersions.find(v => v.version === '4.18.0');
if (specificVersion) {
await specificVersion.saveToDisk('./downloads/express-4.18.0');
}
}
```
### 📁 Extract Specific Files
Extract individual files or directories from packages without downloading the entire package:
2024-04-14 18:03:33 +02:00
```typescript
async function extractSpecificFiles() {
// Get a single file
const readmeFile = await npmRegistry.getFileFromPackage(
'typescript',
'README.md'
);
if (readmeFile) {
console.log('README Contents:', readmeFile.contentBuffer.toString());
}
// Get a file from specific version
const packageJson = await npmRegistry.getFileFromPackage(
'react',
'package.json',
{ version: '18.0.0' }
);
// Get all files from a directory
const sourceFiles = await npmRegistry.getFilesFromPackage(
'@angular/core',
'src/',
{ distTag: 'latest' }
);
sourceFiles.forEach(file => {
console.log(`📄 ${file.path} (${file.contentBuffer.length} bytes)`);
});
2024-04-14 18:03:33 +02:00
}
```
### 🏷️ Version Management
Work with specific versions and dist tags:
2024-04-14 18:03:33 +02:00
```typescript
async function versionManagement() {
const pkg = await npmRegistry.getPackageInfo('vue');
// Get best matching version for a range
const bestVersion = pkg.getBestMatchingVersion('^3.0.0');
console.log(`Best matching version: ${bestVersion}`);
// Work with dist tags
const latestTag = pkg.allDistTags.find(t => t.name === 'latest');
const nextTag = pkg.allDistTags.find(t => t.name === 'next');
console.log(`Latest: ${latestTag?.targetVersion}`);
console.log(`Next: ${nextTag?.targetVersion}`);
// Get files from specific dist tag
const files = await npmRegistry.getFilesFromPackage(
'vue',
'dist/',
{ distTag: 'next' }
);
}
2024-04-14 18:03:33 +02:00
```
### 🗂️ Virtual Directory
Load packages as virtual file systems in memory:
2024-04-14 18:03:33 +02:00
```typescript
async function virtualDirectory() {
// Create virtual directory from package
const virtualDir = await npmRegistry.getPackageAsSmartfileVirtualDir('@angular/cli');
// Work with files in memory
const allFiles = virtualDir.getFileArray();
allFiles.forEach(file => {
console.log(`Virtual file: ${file.path}`);
});
// Export virtual directory to disk if needed
await virtualDir.saveToDisk('./output/angular-cli');
}
```
2024-04-14 18:03:33 +02:00
### ⚡ Caching
The library includes built-in intelligent caching:
```typescript
// Files are automatically cached
const file1 = await npmRegistry.getFileFromPackage('lodash', 'package.json');
// This will be served from cache
const file2 = await npmRegistry.getFileFromPackage('lodash', 'package.json');
// Cache is registry-specific and version-aware
const specificVersion = await npmRegistry.getFileFromPackage(
'lodash',
'README.md',
{ version: '4.17.21' }
);
```
## 🏗️ API Reference
### NpmRegistry Class
#### Constructor
```typescript
new NpmRegistry(options?: INpmRegistryConstructorOptions)
```
Options:
- `npmRegistryUrl`: Custom registry URL (default: `https://registry.npmjs.org`)
#### Methods
##### `getPackageInfo(packageName: string): Promise<NpmPackage>`
Retrieves comprehensive information about a package.
##### `searchOnNpm(searchObject: ISearchObject): Promise<NpmPackage[]>`
Searches the npm registry with advanced filters.
##### `savePackageToDisk(packageName: string, targetDir: string): Promise<void>`
Downloads and extracts a package to the filesystem.
##### `getFileFromPackage(packageName: string, filePath: string, options?): Promise<SmartFile>`
Extracts a single file from a package.
##### `getFilesFromPackage(packageName: string, filePath: string, options?): Promise<SmartFile[]>`
Extracts multiple files from a package directory.
##### `getPackageAsSmartfileVirtualDir(packageName: string): Promise<VirtualDirectory>`
Creates an in-memory virtual directory from a package.
### NpmPackage Class
#### Properties
- `name`: Package name
- `version`: Current version
- `description`: Package description
- `license`: License type
- `allVersions`: Array of all available versions
- `allDistTags`: Array of all dist tags
- `dependencies`: Package dependencies
- `keywords`: Package keywords
- `maintainers`: Package maintainers
- `dist`: Distribution information
#### Methods
##### `getBestMatchingVersion(versionRange: string): string`
Finds the best matching version for a semver range.
##### `saveToDisk(targetDir: string): Promise<void>`
Saves the package to disk.
##### `getFileFromPackage(filePath: string, options?): Promise<SmartFile>`
Gets a file from the package.
##### `getFilesFromPackage(filePath: string, options?): Promise<SmartFile[]>`
Gets multiple files from the package.
## 🔧 Advanced Configuration
### Custom Registry with Authentication
```typescript
const privateRegistry = new NpmRegistry({
npmRegistryUrl: 'https://private-registry.company.com'
});
// Note: Authentication should be configured via .npmrc or npm config
```
### Error Handling
```typescript
try {
const pkg = await npmRegistry.getPackageInfo('non-existent-package');
} catch (error) {
console.error('Package not found:', error.message);
2024-04-14 18:03:33 +02:00
}
// Safe file extraction
const file = await npmRegistry.getFileFromPackage('express', 'README.md');
if (file) {
// File exists
console.log('File size:', file.contentBuffer.length);
} else {
// File doesn't exist
console.log('File not found');
}
2024-04-14 18:03:33 +02:00
```
## 🎯 Common Use Cases
- **CI/CD Pipelines**: Automatically download and verify package contents
- **Security Scanning**: Extract and analyze package files without installation
- **Documentation Generation**: Pull README files and docs from packages
- **Dependency Analysis**: Analyze package structures and dependencies
- **Registry Mirroring**: Sync packages between registries
- **Package Validation**: Verify package contents before deployment
- **Automated Updates**: Check for new versions and update notifications
2024-04-14 18:03:33 +02:00
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.