Skip to content

Commit

Permalink
Merge pull request #1 from kristianheljas/dev
Browse files Browse the repository at this point in the history
Publish to NPM
  • Loading branch information
kristianheljas committed Oct 2, 2021
2 parents b8bf953 + 940a8e3 commit 440687c
Show file tree
Hide file tree
Showing 76 changed files with 3,814 additions and 494 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# EJS templates
*.ejs

# NPM dependencies
/node_modules

Expand All @@ -7,5 +10,8 @@
# Node red user directory
/.node-red

# Yarn cache directory
/.yarn

# https://marketplace.visualstudio.com/items?itemName=xyz.local-history
/.history
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module.exports = {
project: './tsconfig.json',
},
},
{
files: ['src/editor/**/*', 'src/nodes/**/editor.ts'],
rules: {
// Broswer code should be able to log warning and errors to console
'no-console': ['warn', { allow: ['warn', 'error'] }],
},
},
{
files: ['./*.{js,ts}', 'scripts/**/*.{js,ts}'],
rules: {
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
# Node-red user directory
/.node-red

# Yarn cache directory
/.yarn

# https://marketplace.visualstudio.com/items?itemName=xyz.local-history
/.history
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A collection on [Node-RED](https://nodered.org/) nodes interfacing with [Zigbee2

Inspired by [node-red-contrib-zigbee2mqtt](https://flows.nodered.org/node/node-red-contrib-zigbee2mqtt) with following key differences:

- Written in Typescript
- Written in (mostly) Typescript
- Uses built-in Node-RED MQTT configuration nodes
- Uses the new [Zigbee2MQTT API](https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html)

Expand Down
38 changes: 33 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,39 @@
"postpack": "gulp postpack",
"prepare": "is-ci || node scripts/not-project-root.js || (husky install && yarn --cwd .node-red)"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"@babel/core": "^7.13.10",
"@babel/preset-env": "^7.13.12",
"@babel/preset-typescript": "^7.13.0",
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"@schemastore/package": "^0.0.6",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/ejs": "^3.0.6",
"@types/eslint": "^7.2.7",
"@types/express": "^4.17.11",
"@types/fancy-log": "^1.3.1",
"@types/gulp": "^4.0.8",
"@types/lodash": "^4.14.168",
"@types/minimist": "^1.2.1",
"@types/node": "^14.14.35",
"@types/node-red": "^1.1.1",
"@types/nodemon": "^1.19.0",
"@types/rimraf": "^3.0.0",
"@types/select2": "^4.0.53",
"@types/through2": "^2.0.36",
"@types/undertaker": "^1.2.6",
"@types/vinyl": "^2.0.4",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"babel-loader": "^8.2.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"css-loader": "^5.2.0",
"ejs": "^3.1.6",
"eslint": "^7.22.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-airbnb-typescript": "^12.3.1",
Expand All @@ -62,26 +78,38 @@
"lint-staged": "^10.5.4",
"minimist": "^1.2.5",
"mqtt": "^4.2.6",
"mqtt-packet": "^6.9.0",
"node-red": "^1.2.9",
"nodemon": "^2.0.7",
"plugin-error": "^1.0.1",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"sass": "^1.32.8",
"sass-loader": "^11.0.1",
"select2": "^4.1.0-rc.0",
"semantic-release": "^17.4.2",
"sort-package-json": "^1.49.0",
"style-loader": "^2.0.0",
"through2": "^4.0.2",
"ts-node": "^9.1.1",
"typescript": "^4.2.3",
"undertaker": "^1.3.0"
"undertaker": "^1.3.0",
"vinyl": "^2.2.1",
"webpack": "^5.28.0",
"webpack-cli": "^4.5.0"
},
"engines": {
"node": ">=12"
},
"node-red": {
"nodes": {
"z2m-broker": "dist/nodes/broker.js",
"z2m-bridge-state": "dist/nodes/bridge/state.js",
"z2m-bridge-restart": "dist/nodes/bridge/restart.js",
"z2m-bridge-logs": "dist/nodes/bridge/logs.js"
"z2m-core": "dist/nodes/core/index.js",
"z2m-broker": "dist/nodes/broker/index.js",
"z2m-bridge-state": "dist/nodes/bridge-state/index.js",
"z2m-bridge-restart": "dist/nodes/bridge-restart/index.js",
"z2m-bridge-logs": "dist/nodes/bridge-logs/index.js",
"z2m-device-in": "dist/nodes/device-in/index.js",
"z2m-device-out": "dist/nodes/device-out/index.js"
}
}
}
3 changes: 3 additions & 0 deletions release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
{ type: 'chore', scope: 'deps', release: 'patch' },
{ type: 'docs', scope: 'readme', release: 'patch' },
{ scope: 'no-release', release: false },
{ scope: 'release-patch', release: 'patch' },
{ scope: 'release-minor', release: 'minor' },
{ scope: 'release-major', release: 'major' },
],
},
],
Expand Down
26 changes: 16 additions & 10 deletions scripts/gulp/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import minimist from 'minimist';
import nodemon from 'nodemon';
import rimraf from 'rimraf';
import { TaskFunction } from 'undertaker';
import compileHtmlPlugin from './compileHtmlPlugin';

const argv = minimist(process.argv.slice(2));

Expand All @@ -16,8 +17,11 @@ const stdio = 'inherit';

const binFolder = './node_modules/.bin/';

const webpackBin = `${binFolder}/webpack`;
const webpackArgs = ['--config', argv.project || 'webpack.config.ts'];

const tscBin = `${binFolder}/tsc`;
const tscArgs = ['--project', argv.project || 'tsconfig.json'];
const tscArgs = ['--project', argv.project || 'tsconfig.build.json'];

const nodeRedBin = `${binFolder}/node-red`;

Expand All @@ -30,16 +34,18 @@ export const clean: TaskFunction = (done) => {
/* #endregion */
/* #region Build tasks */

export const buildFrontend: TaskFunction = () => spawn(webpackBin, webpackArgs, { stdio });

export const buildTypescript: TaskFunction = () => spawn(tscBin, tscArgs, { stdio });
buildTypescript.flags = {
'--project': 'Specify custom tsconfig.json file',
};

export const copyHtml: TaskFunction = () => src('src/**/*.html').pipe(dest('dist'));
export const compileHtml: TaskFunction = () =>
src(['./src/nodes/**/index.ts'], { base: 'src' }).pipe(compileHtmlPlugin()).pipe(dest('dist'));

/* #endregion */
/* #region Development tasks */

export const watchFrontend: TaskFunction = () => spawn(webpackBin, [...webpackArgs, '--watch'], { stdio });

export const watchTypescript: TaskFunction = () =>
spawn(
tscBin,
Expand All @@ -59,7 +65,8 @@ export const watchTypescript: TaskFunction = () =>
},
);

export const watchHtml: TaskFunction = () => watch('src/**/*.html', { ignoreInitial: false }, copyHtml);
export const watchHtml: TaskFunction = () =>
watch(['src/nodes/**/*', 'src/**/*.ejs'], { ignoreInitial: false }, compileHtml);

export const waitForBuild: TaskFunction = async () => {
// Waits for initial watcher process to finish building the first time
Expand All @@ -83,8 +90,7 @@ export const startDevServer: TaskFunction = (done) => {
ext: '*',
script: `${nodeRedBin}`,
args: ['--userDir', '.node-red'],
stdin: false,
delay: 200,
delay: 1000,
});

monitor.on('restart', () => log('file change detected, restarting server...'));
Expand All @@ -93,5 +99,5 @@ export const startDevServer: TaskFunction = (done) => {

/* #endregion */

export const buildAll = parallel(buildTypescript, copyHtml);
export const watchAll = parallel(watchTypescript, watchHtml);
export const buildAll = parallel(buildTypescript, buildFrontend, compileHtml);
export const watchAll = parallel(watchTypescript, watchFrontend, watchHtml);
68 changes: 68 additions & 0 deletions scripts/gulp/compileHtmlPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable no-param-reassign */
import ejs from 'ejs';
import fs from 'fs';
import { join } from 'path';
import PluginError from 'plugin-error';
import { Transform } from 'stream';
import through, { TransformFunction } from 'through2';
import Vinyl from 'vinyl';
import { PACKAGE_NAME, PACKAGE_VERSION } from '../../src/core/constants';

const { stat, readFile } = fs.promises;

const exists = async (path: string): Promise<boolean> =>
stat(path)
.then(() => true)
.catch(() => false);

const transform: TransformFunction = async (file: Vinyl, encoding, callback) => {
if (!file.isBuffer()) {
callback(new PluginError('Only buffers are supported for ejs compilation!', __filename));
return;
}

file.extname = '.html';

// If html file already exists, skip everything else
if (await exists(file.path)) {
file.contents = await readFile(file.path);
callback(null, file);
return;
}

const { cwd, base, dirname, stem } = file;
const viewsPath = join(cwd, base, 'views');
const editorViewsPath = join(cwd, base, 'editor');

const nodePath = join(dirname, 'node.ts');
const node = require(nodePath).default; // eslint-disable-line

const templatePath = join(dirname, `${stem}.ejs`);

const templateData: ejs.Data = {
PACKAGE_NAME,
PACKAGE_VERSION,
env: process.env,
node,
};

const templateOptions: ejs.Options = {
views: [dirname, editorViewsPath, viewsPath],
};

try {
if (await exists(templatePath)) {
file.contents = await ejs.renderFile(templatePath, templateData, templateOptions).then(Buffer.from);
callback(null, file);
return;
}

const defaultTemplate = join(viewsPath, 'default-index.ejs');
file.contents = await ejs.renderFile(defaultTemplate, templateData, templateOptions).then(Buffer.from);
callback(null, file);
} catch (err) {
callback(new PluginError('Something went wrong when compiling template!', err));
}
};

export default (): Transform => through.obj(transform);
Loading

0 comments on commit 440687c

Please sign in to comment.