@eik/common
Advanced tools
Comparing version 1.3.3 to 1.4.0
@@ -0,1 +1,8 @@ | ||
# [1.4.0](https://github.com/eik-lib/common/compare/v1.3.3...v1.4.0) (2021-01-19) | ||
### Features | ||
* Remove unneeded file ([72c07df](https://github.com/eik-lib/common/commit/72c07dfb3e8673a55535470dd41b7ed09e492b08)) | ||
## [1.3.3](https://github.com/eik-lib/common/compare/v1.3.2...v1.3.3) (2020-12-31) | ||
@@ -2,0 +9,0 @@ |
const localAssets = require('./local-assets'); | ||
const packageURL = require('./package-url'); | ||
const getDefaults = require('./get-defaults'); | ||
const configStore = require('./config-store'); | ||
module.exports = { localAssets, packageURL }; | ||
module.exports = { localAssets, packageURL, getDefaults, configStore }; |
/* eslint-disable no-await-in-loop */ | ||
const assert = require('assert'); | ||
const { promisify } = require('util'); | ||
const { join, extname, dirname, basename } = require('path'); | ||
const { join, extname, basename } = require('path'); | ||
const fs = require('fs'); | ||
const glob = promisify(require('glob')); | ||
const configStore = require("./config-store") | ||
@@ -15,56 +14,54 @@ let eik; | ||
* @param {object} app - express js or fastify app instance | ||
* @param {string} eikJSONPath - (optional) path to eik.json file | ||
* @param {string} rootEikDirectory - (optional) path to folder where eik configuration file can be found | ||
*/ | ||
async function localAssets( | ||
app, | ||
eikJSONPath = join(process.cwd(), './eik.json'), | ||
rootEikDirectory = process.cwd(), | ||
) { | ||
assert(app.decorateReply || app.name === 'app', 'App must be an Express or Fastify app instance'); | ||
assert(typeof eikJSONPath === 'string' && eikJSONPath, 'Path to eik.json must be provided and must be of type string'); | ||
assert(typeof rootEikDirectory === 'string' && rootEikDirectory, 'Path to folder for eik config must be provided and must be of type string'); | ||
// ensure eik.json only loaded 1x | ||
if (!eik) eik = JSON.parse(fs.readFileSync(eikJSONPath)); | ||
for (const [pathname, file] of Object.entries(eik.files)) { | ||
const filePaths = await glob(file, { cwd: dirname(eikJSONPath) }); | ||
for (const filePath of filePaths) { | ||
const ext = extname(filePath); | ||
const pathnameHasExt = !!extname(pathname); | ||
const value = pathnameHasExt | ||
? join(`/pkg/${eik.name}/${eik.version}`, pathname) | ||
: join( | ||
`/pkg/${eik.name}/${eik.version}`, | ||
pathname, | ||
basename(filePath), | ||
); | ||
const fileOnDisk = join(dirname(eikJSONPath), filePath); | ||
let contentType; | ||
switch (ext) { | ||
case '.js': | ||
contentType = 'application/javascript'; | ||
break; | ||
case '.map': | ||
case '.json': | ||
contentType = 'application/json'; | ||
break; | ||
case '.css': | ||
contentType = 'text/css'; | ||
break; | ||
default: | ||
contentType = 'application/octet-stream'; | ||
if (!eik) eik = configStore.findInDirectory(rootEikDirectory); | ||
(await eik.pathsAndFiles()).forEach(([pathname, filePath]) => { | ||
const ext = extname(filePath); | ||
const pathnameHasExt = !!extname(pathname); | ||
const value = pathnameHasExt | ||
? join(`/pkg/${eik.name}/${eik.version}`, pathname) | ||
: join( | ||
`/pkg/${eik.name}/${eik.version}`, | ||
pathname, | ||
basename(filePath), | ||
); | ||
const fileOnDisk = join(eik.cwd, filePath); | ||
let contentType; | ||
switch (ext) { | ||
case '.js': | ||
contentType = 'application/javascript'; | ||
break; | ||
case '.map': | ||
case '.json': | ||
contentType = 'application/json'; | ||
break; | ||
case '.css': | ||
contentType = 'text/css'; | ||
break; | ||
default: | ||
contentType = 'application/octet-stream'; | ||
} | ||
app.get(value, (req, res) => { | ||
if (res.set) { | ||
// express | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.set('content-type', contentType); | ||
fs.createReadStream(fileOnDisk).pipe(res); | ||
} else if (res.type) { | ||
// fastify | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.type(contentType); | ||
res.send(fs.createReadStream(fileOnDisk)); | ||
} | ||
app.get(value, (req, res) => { | ||
if (res.set) { | ||
// express | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.set('content-type', contentType); | ||
fs.createReadStream(fileOnDisk).pipe(res); | ||
} else if (res.type) { | ||
// fastify | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.type(contentType); | ||
res.send(fs.createReadStream(fileOnDisk)); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
module.exports = localAssets; |
@@ -1,29 +0,19 @@ | ||
const { join, dirname } = require('path'); | ||
const fs = require('fs'); | ||
const fmap = require('../utils/files'); | ||
const { join } = require('path'); | ||
const configStore = require('./config-store'); | ||
let eik; | ||
async function packageURL( | ||
key, | ||
{ | ||
eikJSONPath = join(process.cwd(), './eik.json'), | ||
} = {}, | ||
) { | ||
if (!eik) eik = JSON.parse(fs.readFileSync(eikJSONPath)); | ||
const files = await fmap({ [key]: eik.files[key] }, '/', { cwd: dirname(eikJSONPath) }); | ||
async function packageURL(key, { configRootDir = process.cwd() } = {}) { | ||
if (!eik) eik = configStore.findInDirectory(configRootDir); | ||
const mappingList = (await eik.pathsAndFiles()).filter( | ||
([, , originalDest]) => originalDest === key, | ||
); | ||
if (!files.size) | ||
throw new Error('Pattern did not match any files on filesystem'); | ||
if (mappingList.length === 0) throw new Error('No files specified'); | ||
const isGlobPattern = eik.files[key].includes('*'); | ||
if (!isGlobPattern && files.has(eik.files[key])) | ||
throw new Error('Specified file not found on disk'); | ||
const result = []; | ||
const result = mappingList.map( | ||
([destPath]) => | ||
new URL(join(`pkg`, eik.name, eik.version, destPath), eik.server), | ||
); | ||
for (const file of Array.from(files.values())) { | ||
result.push(new URL(join(`pkg`, eik.name, eik.version, file), eik.server)); | ||
} | ||
if (result.length === 1) return result[0]; | ||
@@ -30,0 +20,0 @@ return result; |
@@ -5,6 +5,6 @@ 'use strict'; | ||
const ReadFile = require('./classes/read-file'); | ||
const EikConfig = require('./classes/eik-config'); | ||
const schemas = require('./schemas'); | ||
const stream = require('./stream'); | ||
const helpers = require('./helpers'); | ||
const utils = require('./utils'); | ||
@@ -14,6 +14,6 @@ module.exports = { | ||
ReadFile, | ||
EikConfig, | ||
schemas, | ||
stream, | ||
helpers, | ||
utils, | ||
}; |
{ | ||
"name": "@eik/common", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "Common utilities for Eik modules", | ||
@@ -40,13 +40,13 @@ "main": "lib/index.js", | ||
"@semantic-release/github": "7.2.0", | ||
"@semantic-release/npm": "7.0.9", | ||
"@semantic-release/npm": "7.0.10", | ||
"@semantic-release/release-notes-generator": "9.0.1", | ||
"eslint": "7.16.0", | ||
"eslint": "7.18.0", | ||
"eslint-config-airbnb-base": "14.2.1", | ||
"eslint-config-prettier": "7.1.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"eslint-plugin-prettier": "3.3.0", | ||
"eslint-plugin-prettier": "3.3.1", | ||
"express": "4.17.1", | ||
"fastify": "3.9.2", | ||
"prettier": "2.2.1", | ||
"semantic-release": "17.3.0", | ||
"semantic-release": "17.3.1", | ||
"stoppable": "1.1.0", | ||
@@ -53,0 +53,0 @@ "tap": "14.11.0" |
@@ -37,3 +37,3 @@ # Eik Commons | ||
Using invididual schema validators | ||
Using individual schema validators | ||
@@ -40,0 +40,0 @@ ##### name |
25755
23
548