Skip to content

Commit

Permalink
feat(Util): Add version and URL utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Feb 22, 2020
1 parent b73f832 commit 274dd52
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 24 deletions.
18 changes: 2 additions & 16 deletions ts/bindings/jsonld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,9 @@ import fs from 'fs-extra'
import fromEntries from 'object.fromentries'
import path from 'path'
import { readSchemas } from '../helpers'
import { jsonLdUrl } from '../util'

/**
* Get the Schema major version for use in generated URLs
*/
const VERSION_MAJOR = fs
.readJSONSync(path.join(__dirname, '..', '..', 'package.json'))
.version.split('.')[0]

/**
* The base URL for the Stencila JSON-LD context.
*
* This gets prefixed to all keys during JSON-LD expansion
* so the trailing slash is important so that for e.g.
* `CodeChunk` gets expanded to `http://schema.stenci.la/v0/jsonld/CodeChunk`
* (which in gets redirected to `https://unpkg.com/@stencila/schema@0.32.1/dist/CodeChunk.jsonld`)
*/
const STENCILA_CONTEXT_URL = `http://schema.stenci.la/v${VERSION_MAJOR}/jsonld/`
const STENCILA_CONTEXT_URL = jsonLdUrl()

/**
* The destination directory for generated JSON-LD files
Expand Down
10 changes: 2 additions & 8 deletions ts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ import cloneDeep from 'lodash.clonedeep'
import path from 'path'
import log from './log'
import Schema from './schema-interface'
import { versionMajor } from './util/version'

const SCHEMA_SOURCE_DIR = path.join(__dirname, '..', 'schema')
const SCHEMA_DEST_DIR = path.join(__dirname, '..', 'public')

/**
* Get the Schema major version for use in generated URLs
*/
const VERSION_MAJOR = fs
.readJSONSync(path.join(__dirname, '..', 'package.json'))
.version.split('.')[0]

/**
* The base URL for JSON Schema `$id`s.
*/
const SCHEMA_DEST_URL = 'https://schema.stenci.la'
const ID_BASE_URL = `${SCHEMA_DEST_URL}/v${VERSION_MAJOR}`
const ID_BASE_URL = `${SCHEMA_DEST_URL}/v${versionMajor()}`

/**
* The base URL for source files.
Expand Down
2 changes: 2 additions & 0 deletions ts/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './guards'
export * from './node-type'
export * from './type-map'
export * from './type-maps'
export * from './urls'
export * from './version'
11 changes: 11 additions & 0 deletions ts/util/urls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pkg from '../../package.json'
import { jsonLdUrl } from './urls'

test('jsonLdUrl', () => {
const expectedBase = `http://schema.stenci.la/v${
pkg.version.split('.')[0]
}/jsonld/`
expect(jsonLdUrl()).toMatch(expectedBase)
expect(jsonLdUrl('CodeChunk')).toMatch(expectedBase + 'CodeChunk')
expect(jsonLdUrl('outputs')).toMatch(expectedBase + 'outputs')
})
18 changes: 18 additions & 0 deletions ts/util/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { versionMajor } from './version'

/**
* Get the URL to the JSON-LD for this schema's `@context` or
* for a specific term e.g. `CodeChunk`, `outputs`.
*
* The `@context`'s URL needs to have a trailing slash because
* it gets prefixed to all keys during JSON-LD expansion.
* e.g. the term `CodeChunk` gets expanded to `http://schema.stenci.la/v0/jsonld/CodeChunk`
* (which in gets redirected to `https://unpkg.com/@stencila/schema@0.32.1/dist/CodeChunk.jsonld`)
*
* @param term The term (type or property) to generate the
* URL for. Defaults to empty string i.e. the context.
*/
export function jsonLdUrl(term = '') {
const version = versionMajor()
return `http://schema.stenci.la/v${version}/jsonld/${term}`
}
19 changes: 19 additions & 0 deletions ts/util/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pkg from '../../package.json'
import { version, versionMajor, versionMinor } from './version'

test('version', () => {
expect(version()).toEqual(pkg.version)
})

test('versionMajor', () => {
expect(versionMajor()).toEqual(pkg.version.split('.')[0])
})

test('versionMinor', () => {
expect(versionMinor()).toEqual(
pkg.version
.split('.')
.slice(0, 2)
.join('.')
)
})
39 changes: 39 additions & 0 deletions ts/util/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'fs'
import path from 'path'

/**
* Lazily read, cached, package version
*/
let VERSION: string

/**
* Get the version string (e.g "1.2.3") for this package
*/
export function version(): string {
if (VERSION === undefined) {
const json = fs.readFileSync(
path.join(__dirname, '..', '..', 'package.json'),
'utf8'
)
const pkg = JSON.parse(json)
VERSION = pkg.version
}
return VERSION
}

/**
* Get the major version string (e.g "1") for this package
*/
export function versionMajor(): string {
return version().split('.')[0]
}

/**
* Get the minor version string (e.g "1.2") for this package
*/
export function versionMinor(): string {
return version()
.split('.')
.slice(0, 2)
.join('.')
}

0 comments on commit 274dd52

Please sign in to comment.