@dxworks/node-utils v0.2.0
@dxworks/node-utils
npm i @dxworks/node-utils
DX Works' NodeJS Utility functions and re-export of @dxworks/utils
- arrayIsEmpty
- callAll
- camelCaseToDash
- compose
- css
- dashToCamelCase
- escape
- unescape
- get
- hashString
- html
- pipe
- removeWhiteSpace
- _throw
- trueTypeOf
- ueid
Usage example: import { exportName } from '@dxworks/node-utils'
Modules
buildIndex()
A utility for building out index html pages at dest with a given page function
Example
await buildIndex({
dest: /* <path to folder where file should be built> */,
tpl: /* <string template of file> */,
})
buildPlaysets()
A utility that builds html files of your playground playsets and returns a route object that contains routes to
Example
await buildPlaysets({
src: /* <path to playset dir> */,
root: /* <path to root of public dir> */,
ext: /* <path playset file extension eg playset.js> */,
files: /* <glob of playset file paths> */,
});
buildSvgIcon()
A utility for generating the boiler plate of an svg-icon component. It will generate the component in the same directory as the icons.
Example: use
await buildSvgIcon(/* Directory of svg icons*/);
Sample boilerplate
import {
createShadow,
html,
css,
attr,
query,
} from '@dxworks/dom-utils';
const style = css`
:host {
--fill: var(--fill, considerFallbackFill)
display: inline-block;
}
.icon {
display: none;
fill: var(--fill);
}
:host([size='iconSize']){
--icon-size: var(--iconSize);
}
:host([icon]) > .icon {
display: inline-block;
height: var(--icon-size);
width: var(--icon-size);
fill: var(--fill);
}
`;
const template = () => html`
<-- Your sprite will go here -->
<svg class="icon"><use xlink:href=""></use></svg>
`;
createShadow({
tag: 'svg-icon',
template,
style,
component: base => class extends base {
static get observedAttributes() {
return [
'icon',
];
}
handleIcon(arg, value) {
const { set } = attr(query('use', this.shadowRoot));
return arg
? set('xlink:href', `#${value}`)
: set('xlink:href', null);
}
attributeChangedCallback(name, oldValue, newValue) {
const hasValue = newValue !== null;
switch (name) {
case 'icon': {
this.handleIcon(hasValue, newValue);
break;
}
default: {
break;
}
}
}
},
});
copyFiles()
A simple utility for copying files from one place to another.
Example
await copyFiles({
dest: /* path to files dir */,
files: /* glob of file paths */,
})
reloadScript
String fragment to place in html files to enable auto-reloading when using servor.
Example
import { reloadScript, html } from '@dxworks/node-utils';
html`${reloadScript}`;
servor()
This is a fork of the servor project using hapi. It is a start static file server for rapid SPA/Hybrid-SPA development. It returns a server object to enable graceful stopping of server.
It provides a JS API, dynamic port assignment when needed, graceful stopping of the server, event sourcing, and chokidar for more accurate watching.
Example
const server = await servor({
port: /* defaults to 5000 */
browser: /* defaults to true will open browse tab */,
reload: /* default to true will reload when files in roo change */,
root: /* path to public dir */,
});
/* server can be stopped like so */
server.stop();