Getting version number of package
This tutorial is part of package functions documentation in Iconify Tools.
Function getPackageVersion() is a simple function that retrieves version number from package.json in target directory.
It is used by downloadNPMPackage() to retrieve version number of package that has already been downloaded and extracted. It can also be used to get version number of package downloaded from Git repository (as shown in example below).
Usage
Function has the following parameter:
- target, string. Target directory, without "/package.json".
Function returns version number as string.
Also see getNPMVersion().
Function is asynchronous.
Example
example.ts
ts
import { downloadGitHubRepo, getPackageVersion } from '@iconify/tools';
// GITHUB_TOKEN=ghp_12345 node example.js
const token = process.env.GITHUB_TOKEN || '';
(async () => {
// Download GitHub repository
const result = await downloadGitHubRepo({
target: 'downloads/bi',
user: 'twbs',
repo: 'icons',
branch: 'main',
token,
});
// Get version from downloaded package
const version = await getPackageVersion(result.contentsDir);
// '1.7.0'
console.log('Version:', version);
})();