@eik/common
Advanced tools
Comparing version 4.0.0-next.1 to 4.0.0-next.2
@@ -0,1 +1,8 @@ | ||
# [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) | ||
@@ -2,0 +9,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,9 @@ | ||
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 { typeSlug, removeTrailingSlash, resolveFiles } = require('../helpers'); | ||
@@ -40,25 +30,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 +54,3 @@ */ | ||
/** @type {EikjsonSchema["name"]} */ | ||
get name() { | ||
@@ -92,2 +60,3 @@ return this[_config].name; | ||
/** @type {EikjsonSchema["version"]} */ | ||
get version() { | ||
@@ -116,2 +85,3 @@ return this[_config].version; | ||
/** @type {[string, string][]} */ | ||
get token() { | ||
@@ -121,2 +91,3 @@ return this[_tokens].get(this.server); | ||
/** @type {EikjsonSchema["files"]} */ | ||
get files() { | ||
@@ -127,3 +98,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 +112,7 @@ get out() { | ||
/** | ||
* Serializes internal values to an object | ||
* | ||
* @returns {EikjsonSchema} object consistent with EikjsonSchema | ||
*/ | ||
toJSON() { | ||
@@ -143,2 +122,7 @@ return { ...this[_config] }; | ||
/** | ||
* Validates config values against the eik JSON schema | ||
* | ||
* @return {void} | ||
*/ | ||
validate() { | ||
@@ -149,25 +133,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 +171,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, | ||
}; |
{ | ||
"name": "@eik/common", | ||
"version": "4.0.0-next.1", | ||
"version": "4.0.0-next.2", | ||
"description": "Common utilities for Eik modules", | ||
"main": "lib/index.js", | ||
"types": "types/index.d.ts", | ||
"files": [ | ||
"CHANGELOG.md", | ||
"package.json", | ||
"lib" | ||
"lib", | ||
"types" | ||
], | ||
@@ -19,3 +21,4 @@ "scripts": { | ||
"style:format": "prettier -w .", | ||
"typecheck": "tsc" | ||
"typecheck": "tsc", | ||
"prepublish": "npm run typecheck" | ||
}, | ||
@@ -22,0 +25,0 @@ "repository": { |
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
62231
58
1579