@eik/common
Advanced tools
Comparing version 3.0.0-next.1 to 3.0.0
@@ -0,1 +1,59 @@ | ||
# [3.0.0](https://github.com/eik-lib/common/compare/v2.0.3...v3.0.0) (2021-08-12) | ||
### Bug Fixes | ||
* ensure absolute file paths work as expected ([4f6ce5e](https://github.com/eik-lib/common/commit/4f6ce5e251dcbf0df0b51cc36f73b10585b96538)) | ||
* include eik json schema file in package when publishing ([f0c3c45](https://github.com/eik-lib/common/commit/f0c3c45439a637e8a3cb3a475bca70f2a77c1901)) | ||
* update require statements to avoid circular dependencies ([4359bec](https://github.com/eik-lib/common/commit/4359bec140539c6144be48f7b9ff774b9601d068)) | ||
### Features | ||
* add extension, mime-type and content-type to file location ([a9b4fe1](https://github.com/eik-lib/common/commit/a9b4fe15cf39425070b9bd260183656933d94c5d)) | ||
* Add hapi support for local assets ([7528c32](https://github.com/eik-lib/common/commit/7528c3277c537e7e0a4228c949988bc7e7a90074)) | ||
* add mappings() method to eik config object ([f989ae0](https://github.com/eik-lib/common/commit/f989ae0e2de0aa5646fd33d0f5854bc7e74c8734)), closes [/github.com/eik-lib/issues/issues/2#issuecomment-779099732](https://github.com//github.com/eik-lib/issues/issues/2/issues/issuecomment-779099732) | ||
* refactor!: packageURL removed, localAssets fixed and updated ([90fd181](https://github.com/eik-lib/common/commit/90fd1818f69384d4edeb056dc2c8367e51b21d44)) | ||
* refactor!: remove pathsAndFiles methods ([90d8a12](https://github.com/eik-lib/common/commit/90d8a12e4df8e3a37f295c6136e94bf4ade68cb4)) | ||
* feat!: preserve directory structure when globbing in config ([dff2830](https://github.com/eik-lib/common/commit/dff28301f9bc6e37ef9db32455fa64f5a7a9e80a)) | ||
### BREAKING CHANGES | ||
* packageURL was removed as it no longer makes sense given the changes to eik json files config | ||
localAssets has been refactored to use the new mappings method of eik config | ||
* Consumers of the Eik Config class will all need to be updated to use the newer .mappings() method instead. | ||
* directory structures are no longer flattened | ||
# [4.0.0-next.4](https://github.com/eik-lib/common/compare/v4.0.0-next.3...v4.0.0-next.4) (2021-02-23) | ||
### Bug Fixes | ||
* include eik json schema file in package when publishing ([f0c3c45](https://github.com/eik-lib/common/commit/f0c3c45439a637e8a3cb3a475bca70f2a77c1901)) | ||
# [4.0.0-next.3](https://github.com/eik-lib/common/compare/v4.0.0-next.2...v4.0.0-next.3) (2021-02-23) | ||
### Bug Fixes | ||
* update require statements to avoid circular dependencies ([4359bec](https://github.com/eik-lib/common/commit/4359bec140539c6144be48f7b9ff774b9601d068)) | ||
# [4.0.0-next.2](https://github.com/eik-lib/common/compare/v4.0.0-next.1...v4.0.0-next.2) (2021-02-23) | ||
### Bug Fixes | ||
* ensure absolute file paths work as expected ([4f6ce5e](https://github.com/eik-lib/common/commit/4f6ce5e251dcbf0df0b51cc36f73b10585b96538)) | ||
# [4.0.0-next.1](https://github.com/eik-lib/common/compare/v3.0.0-next.1...v4.0.0-next.1) (2021-02-19) | ||
### Features | ||
* Add hapi support for local assets ([7528c32](https://github.com/eik-lib/common/commit/7528c3277c537e7e0a4228c949988bc7e7a90074)) | ||
# [3.0.0-next.1](https://github.com/eik-lib/common/compare/v2.0.3...v3.0.0-next.1) (2021-02-18) | ||
@@ -2,0 +60,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
module.exports = class CustomError extends Error { | ||
@@ -4,0 +2,0 @@ /** |
@@ -1,2 +0,1 @@ | ||
// @ts-check | ||
/* eslint-disable no-continue */ | ||
@@ -8,18 +7,11 @@ | ||
const assert = require('assert'); | ||
const { promisify } = require('util'); | ||
const { extname, join, isAbsolute } = require('path'); | ||
const isGlob = require('is-glob'); | ||
const glob = promisify(require('glob')); | ||
const NoFilesMatchedError = require('./no-files-matched-error'); | ||
const SingleDestMultipleSourcesError = require('./single-dest-multiple-source-error'); | ||
const FileMapping = require('./file-mapping'); | ||
const LocalFileLocation = require('./local-file-location'); | ||
const RemoteFileLocation = require('./remote-file-location'); | ||
const schemas = require('../schemas'); | ||
const { | ||
typeSlug, | ||
pathDiff, | ||
addTrailingSlash, | ||
removeTrailingSlash, | ||
} = require('../helpers'); | ||
const { removeTrailingSlash } = require('../helpers/path-slashes'); | ||
const typeSlug = require('../helpers/type-slug'); | ||
const resolveFiles = require('../helpers/resolve-files'); | ||
@@ -40,25 +32,2 @@ const _config = Symbol('config'); | ||
/** | ||
* Uses an Eik JSON "files" definition to resolve files on disk into a data structure | ||
* | ||
* @param {{[k: string]: string;}} files | ||
* @param {string} cwd | ||
* | ||
* @returns {Promise<[string, string, string[]][]>} | ||
*/ | ||
const resolveFiles = async (files, cwd) => | ||
Promise.all( | ||
Object.entries(files).map(([dest, src]) => { | ||
let replaced = src.replace(addTrailingSlash(cwd), ''); | ||
if (extname(replaced) === '' && isGlob(replaced) === false) { | ||
replaced = join(replaced, '/**/*'); | ||
} | ||
return Promise.all([ | ||
dest, | ||
replaced, | ||
glob(replaced, { cwd, nodir: true }), | ||
]); | ||
}), | ||
); | ||
/** | ||
* @typedef {import ("../../eikjson").EikjsonSchema} EikjsonSchema | ||
@@ -87,2 +56,3 @@ */ | ||
/** @type {EikjsonSchema["name"]} */ | ||
get name() { | ||
@@ -92,2 +62,3 @@ return this[_config].name; | ||
/** @type {EikjsonSchema["version"]} */ | ||
get version() { | ||
@@ -116,2 +87,3 @@ return this[_config].version; | ||
/** @type {[string, string][]} */ | ||
get token() { | ||
@@ -121,2 +93,3 @@ return this[_tokens].get(this.server); | ||
/** @type {EikjsonSchema["files"]} */ | ||
get files() { | ||
@@ -127,3 +100,6 @@ return this[_config].files; | ||
/** | ||
* Normalized relative directory path with leading ./ and trailing / chars stripped | ||
* Normalized relative directory path with any leading ./ or | ||
* trailing / characters stripped. Defaults to .eik | ||
* | ||
* @returns {string} out path string | ||
*/ | ||
@@ -138,2 +114,7 @@ get out() { | ||
/** | ||
* Serializes internal values to an object | ||
* | ||
* @returns {EikjsonSchema} object consistent with EikjsonSchema | ||
*/ | ||
toJSON() { | ||
@@ -143,2 +124,7 @@ return { ...this[_config] }; | ||
/** | ||
* Validates config values against the eik JSON schema | ||
* | ||
* @return {void} | ||
*/ | ||
validate() { | ||
@@ -149,25 +135,38 @@ schemas.assert.eikJSON(this[_config]); | ||
/** | ||
* Resolves file locations on disk based on values defined in files property | ||
* of config object. | ||
* | ||
* @returns {Promise<FileMapping[]>} | ||
*/ | ||
async mappings() { | ||
const files = normalizeFilesDefinition(this.files); | ||
const resolvedFiles = await resolveFiles(files, this.cwd); | ||
const normalizedFiles = normalizeFilesDefinition(this.files); | ||
const resolvedFiles = await resolveFiles(normalizedFiles, this.cwd); | ||
return resolvedFiles.flatMap(([dest, src, srcFilePaths]) => { | ||
if (srcFilePaths.length === 0) { | ||
throw new NoFilesMatchedError(src); | ||
return resolvedFiles.flatMap((files) => { | ||
const { destination, source } = files; | ||
const filesArray = Array.from(files); | ||
if (filesArray.length === 0) { | ||
throw new NoFilesMatchedError(source); | ||
} | ||
if (extname(dest) !== '' && srcFilePaths.length > 1) { | ||
throw new SingleDestMultipleSourcesError(dest); | ||
if (extname(destination) !== '' && filesArray.length > 1) { | ||
throw new SingleDestMultipleSourcesError(destination); | ||
} | ||
return srcFilePaths.map((filePath) => { | ||
const source = new LocalFileLocation(filePath, this.cwd); | ||
const destination = new RemoteFileLocation( | ||
extname(dest) ? dest : join(dest, pathDiff(src, filePath)), | ||
join(typeSlug(this.type), this.name, this.version), | ||
return filesArray.map((localFile) => { | ||
const shouldMapFilename = extname(destination); | ||
const relativePathname = shouldMapFilename | ||
? destination | ||
: join(destination, localFile.relative); | ||
const packagePathname = join( | ||
typeSlug(this.type), | ||
this.name, | ||
this.version, | ||
); | ||
const remoteDestination = new RemoteFileLocation( | ||
relativePathname, | ||
packagePathname, | ||
this.server, | ||
); | ||
return new FileMapping(source, destination); | ||
return new FileMapping(localFile, remoteDestination); | ||
}); | ||
@@ -174,0 +173,0 @@ }); |
/* eslint-disable no-unused-vars */ | ||
// @ts-check | ||
@@ -4,0 +3,0 @@ /** |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const CustomError = require('./custom-error'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
/** | ||
@@ -7,3 +5,3 @@ * @type {(value: unknown, message?: string) => asserts value} | ||
const assert = require('assert'); | ||
const { join, extname } = require('path'); | ||
const { join, extname, isAbsolute } = require('path'); | ||
const mime = require('mime-types'); | ||
@@ -16,10 +14,15 @@ | ||
/** | ||
* @param {string} path relative path to file on disk | ||
* @param {string} cwd current working directory | ||
* @param {string} path path to file on disk relative to basePath | ||
* @param {string} basePath basePath to the file's location on disk | ||
*/ | ||
constructor(path, cwd) { | ||
constructor(path, basePath) { | ||
assert(typeof path === 'string', '"path" must be of type "string"'); | ||
assert(typeof cwd === 'string', '"cwd" must be of type "string"'); | ||
assert( | ||
typeof basePath === 'string', | ||
'"basePath" must be of type "string"', | ||
); | ||
assert(!isAbsolute(path), '"path" must be a relative path'); | ||
/** | ||
* @type {string} path relative path to file on disk | ||
* @type {string} path to file on disk relative to this.basePath | ||
*/ | ||
@@ -29,13 +32,14 @@ this.relative = path; | ||
/** | ||
* @type {string} cwd current working directory | ||
* @type {string} absolute path to root files location on disk | ||
*/ | ||
this.cwd = cwd; | ||
this.basePath = basePath; | ||
/** | ||
* @type {string} absolute absolute path to file on disk | ||
* @type {string} absolute path to file on disk, | ||
* this is a concatentation of this.basePath and this.relative | ||
*/ | ||
this.absolute = join(cwd, path); | ||
this.absolute = join(basePath, path); | ||
/** | ||
* @type {string} extension file extension with . character. eg. .json | ||
* @type {string} file extension with "." character included. (eg. ".json") | ||
*/ | ||
@@ -45,3 +49,3 @@ this.extension = extname(path); | ||
/** | ||
* @type {string} contentType full content-type header for file | ||
* @type {string} full content-type header value for file | ||
*/ | ||
@@ -52,3 +56,3 @@ this.contentType = | ||
/** | ||
* @type {string} mimeType mime type of file | ||
* @type {string} mime type of file | ||
*/ | ||
@@ -55,0 +59,0 @@ this.mimeType = |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const CustomError = require('./custom-error'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const CustomError = require('./custom-error'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const CustomError = require('./custom-error'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check; | ||
const { isReadableStream } = require('../stream'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
/** | ||
@@ -27,3 +25,3 @@ * @type {(value: unknown, message?: string) => asserts value} | ||
/** | ||
* @type {string} packagePathname pathname to package root | ||
* @type {string} pathname to package root | ||
*/ | ||
@@ -33,3 +31,3 @@ this.packagePathname = new URL(packagePathname, origin).pathname; | ||
/** | ||
* @type {string} filePathname pathname to file relative to package root | ||
* @type {string} pathname to file relative to package root | ||
*/ | ||
@@ -39,3 +37,3 @@ this.filePathname = new URL(filePathname, origin).pathname; | ||
/** | ||
* @type {URL} url WHATWG URL object containing the full remote URL for the file | ||
* @type {URL} WHATWG URL object containing the full remote URL for the file | ||
*/ | ||
@@ -42,0 +40,0 @@ this.url = new URL(join(packagePathname, filePathname), origin); |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const CustomError = require('./custom-error'); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
// @ts-check | ||
const { readFileSync, writeFileSync } = require('fs'); | ||
@@ -4,0 +2,0 @@ const { join } = require('path'); |
@@ -6,3 +6,3 @@ const localAssets = require('./local-assets'); | ||
const typeTitle = require('./type-title'); | ||
const pathDiff = require('./path-diff'); | ||
const resolveFiles = require('./resolve-files'); | ||
const { | ||
@@ -21,3 +21,2 @@ addTrailingSlash, | ||
typeTitle, | ||
pathDiff, | ||
addTrailingSlash, | ||
@@ -27,2 +26,3 @@ removeTrailingSlash, | ||
removeLeadingSlash, | ||
resolveFiles, | ||
}; |
@@ -18,4 +18,4 @@ /* eslint-disable no-await-in-loop */ | ||
assert( | ||
app.decorateReply || app.name === 'app', | ||
'App must be an Express or Fastify app instance', | ||
app.decorateReply || app.name === 'app' || app.route, | ||
'App must be an Express, Fastify or Hapi app instance', | ||
); | ||
@@ -30,17 +30,33 @@ assert( | ||
(await eik.mappings()).forEach((mapping) => { | ||
app.get(mapping.destination.url.pathname, (req, res) => { | ||
if (res.set) { | ||
// express | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.set('content-type', mapping.source.contentType); | ||
fs.createReadStream(mapping.source.absolute).pipe(res); | ||
} else if (res.type) { | ||
// fastify | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.type(mapping.source.contentType); | ||
res.send(fs.createReadStream(mapping.source.absolute)); | ||
} | ||
}); | ||
const { pathname } = mapping.destination.url; | ||
const { contentType, absolute: path } = mapping.source; | ||
if (app.get) { | ||
app.get(pathname, (req, res) => { | ||
if (res.set) { | ||
// express | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.set('content-type', contentType); | ||
fs.createReadStream(path).pipe(res); | ||
} else if (res.type) { | ||
// fastify | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.type(contentType); | ||
res.send(fs.createReadStream(path)); | ||
} | ||
}); | ||
} else { | ||
// hapi | ||
app.route({ | ||
method: 'GET', | ||
path: pathname, | ||
handler(req, h) { | ||
return h | ||
.response(fs.createReadStream(path)) | ||
.header('Access-Control-Allow-Origin', '*') | ||
.type(contentType); | ||
}, | ||
}); | ||
} | ||
}); | ||
} | ||
module.exports = localAssets; |
{ | ||
"name": "@eik/common", | ||
"version": "3.0.0-next.1", | ||
"version": "3.0.0", | ||
"description": "Common utilities for Eik modules", | ||
"main": "lib/index.js", | ||
"types": "types/index.d.ts", | ||
"files": [ | ||
"CHANGELOG.md", | ||
"package.json", | ||
"lib" | ||
"lib", | ||
"types", | ||
"eikjson.d.ts" | ||
], | ||
@@ -19,3 +22,4 @@ "scripts": { | ||
"style:format": "prettier -w .", | ||
"typecheck": "tsc" | ||
"typecheck": "tsc", | ||
"prepublish": "npm run typecheck" | ||
}, | ||
@@ -34,4 +38,4 @@ "repository": { | ||
"dependencies": { | ||
"ajv": "^7.0.0", | ||
"ajv-formats": "^1.5.1", | ||
"ajv": "^8.6.2", | ||
"ajv-formats": "^2.1.0", | ||
"glob": "^7.1.6", | ||
@@ -45,21 +49,23 @@ "is-glob": "^4.0.1", | ||
"devDependencies": { | ||
"@hapi/hapi": "^20.1.0", | ||
"@semantic-release/changelog": "5.0.1", | ||
"@semantic-release/commit-analyzer": "8.0.1", | ||
"@semantic-release/git": "9.0.0", | ||
"@semantic-release/github": "7.2.0", | ||
"@semantic-release/npm": "7.0.10", | ||
"@semantic-release/release-notes-generator": "9.0.1", | ||
"eslint": "7.20.0", | ||
"@semantic-release/github": "7.2.3", | ||
"@semantic-release/npm": "7.1.3", | ||
"@semantic-release/release-notes-generator": "9.0.3", | ||
"eslint": "7.32.0", | ||
"eslint-config-airbnb-base": "14.2.1", | ||
"eslint-config-prettier": "7.2.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"eslint-plugin-prettier": "3.3.1", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-import": "2.24.0", | ||
"eslint-plugin-prettier": "3.4.0", | ||
"express": "4.17.1", | ||
"fastify": "3.12.0", | ||
"fastify": "3.20.1", | ||
"json-schema-to-typescript": "^10.1.3", | ||
"prettier": "2.2.1", | ||
"semantic-release": "17.3.9", | ||
"prettier": "2.3.2", | ||
"semantic-release": "17.4.4", | ||
"stoppable": "1.1.0", | ||
"tap": "14.11.0" | ||
"tap": "14.11.0", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
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
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
61329
59
1463
0
20
+ Addedajv@8.17.1(transitive)
+ Addedajv-formats@2.1.1(transitive)
+ Addedfast-uri@3.0.3(transitive)
- Removedajv@7.2.4(transitive)
- Removedajv-formats@1.6.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removeduri-js@4.4.1(transitive)
Updatedajv@^8.6.2
Updatedajv-formats@^2.1.0