Skip to content

Commit

Permalink
Add ways to retrieve the package version (#1471)
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum authored Aug 13, 2022
1 parent bb9fb0d commit 41d03e0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### ✨ Features and improvements

- *...Add new stuff here...*
- Re-enable method to get library version. Either with `import {version} from 'maplibre-gl'`, or on a Map instance as `map.version`.

### 🐞 Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"test-render": "node --loader ts-node/esm --experimental-specifier-resolution=node --max-old-space-size=2048 test/integration/render/render.test.ts",
"test-query": "jest -c ./jest.config.e2e.ts --roots ./test/integration/query",
"test-expression": "jest --roots ./test/integration/expression",
"test-unit": "jest -c ./jest.config.ts --roots ./src",
"test-unit": "jest --roots ./src -c ./jest.config.ts ",
"codegen": "npm run generate-style-code && npm run generate-struct-arrays && npm run generate-style-spec && npm run generate-shaders && npm run generate-debug-index-file",
"benchmark": "node --loader ts-node/esm --experimental-specifier-resolution=node test/bench/run-benchmarks.ts",
"gl-stats": "node --loader ts-node/esm --experimental-specifier-resolution=node test/bench/gl-stats.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ describe('maplibre', () => {
test('workerCount', () => {
expect(typeof maplibre.workerCount === 'number').toBeTruthy();
});

test('version', () => {
expect(typeof maplibre.version === 'string').toBeTruthy();

// Semver regex: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39
// Backslashes are doubled to escape them
const regexp = new RegExp('^([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$');
expect(regexp.test(maplibre.version)).toBeTruthy();
});
});
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import {supported} from '@mapbox/mapbox-gl-supported';

import packageJSON from '../package.json' assert {type: 'json'};
import Map from './ui/map';
import NavigationControl from './ui/control/navigation_control';
import GeolocateControl from './ui/control/geolocate_control';
Expand Down Expand Up @@ -36,6 +36,8 @@ import RasterTileSource from './source/raster_tile_source';
import VectorTileSource from './source/vector_tile_source';
import VideoSource from './source/video_source';

const version = packageJSON.version;

const exported = {
supported,
setRTLTextPlugin,
Expand Down Expand Up @@ -99,6 +101,14 @@ const exported = {
*/
clearPrewarmedResources,

/**
* Returns the package version of the library
* @returns {string} Package version of the library
*/
get version(): string {
return version;
},

/**
* Gets and sets the number of web workers instantiated on a page with GL JS maps.
* By default, it is set to half the number of CPU cores (capped at 6).
Expand Down
11 changes: 11 additions & 0 deletions src/ui/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ afterEach(() => {

describe('Map', () => {

test('version', () => {
const map = createMap({interactive: true, style: null});

expect(typeof map.version === 'string').toBeTruthy();

// Semver regex: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39
// Backslashes are doubled to escape them
const regexp = new RegExp('^([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$');
expect(regexp.test(map.version)).toBeTruthy();
});

test('constructor', () => {
const map = createMap({interactive: true, style: null});
expect(map.getContainer()).toBeTruthy();
Expand Down
11 changes: 11 additions & 0 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {extend, bindAll, warnOnce, uniqueId, isImageBitmap} from '../util/util';
import browser from '../util/browser';
import DOM from '../util/dom';
import packageJSON from '../../package.json' assert {type: 'json'};
import {getImage, GetImageCallback, getJSON, ResourceType} from '../util/ajax';
import {RequestManager} from '../util/request_manager';
import Style from '../style/style';
Expand Down Expand Up @@ -58,6 +59,8 @@ import {Callback} from '../types/callback';
import type {ControlPosition, IControl} from './control/control';
import type {MapGeoJSONFeature} from '../util/vectortile_to_geojson';

const version = packageJSON.version;

/* eslint-enable no-use-before-define */
export type MapOptions = {
hash?: boolean | string;
Expand Down Expand Up @@ -2925,6 +2928,14 @@ class Map extends Camera {
_setCacheLimits(limit: number, checkThreshold: number) {
setCacheLimits(limit, checkThreshold);
}

/**
* Returns the package version of the library
* @returns {string} Package version of the library
*/
get version(): string {
return version;
}
}

export default Map;

0 comments on commit 41d03e0

Please sign in to comment.