Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add npm deploy and move backend source to /api folder #3422

Merged
merged 10 commits into from
Nov 16, 2023
27 changes: 27 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.github
.vscode
.yarn
/api/
/src/
/certs/
/docker/
/docs/
/kubernetes/
/test/
/pkg/
/store/


/.*
/*.ts
/*.js
/kustomization.yaml
/fakeNodes.json
/nodemon.json
/package.sh
/index.html

/tsconfig.*

/*.md
/*.tgz
3 changes: 1 addition & 2 deletions app.ts → api/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import express, { Request, RequestHandler, Response, Router } from 'express'
import { version } from './package.json'
import history from 'connect-history-api-fallback'
import cors from 'cors'
import csrf from 'csurf'
Expand Down Expand Up @@ -1158,7 +1157,7 @@ app.post(

// update versions to actual ones
settings.gateway.versions = {
app: version, // don't use getVersion here as it may include commit sha
app: utils.pkgJson.version, // don't use getVersion here as it may include commit sha
driver: libVersion,
server: serverVersion,
}
Expand Down
File renamed without changes.
17 changes: 9 additions & 8 deletions config/app.ts → api/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
export const snippetsDir: string = joinPath(storeDir, 'snippets')

export const tmpDir: string = joinPath(storeDir, '.tmp')
export const backupsDir: string = process.env.BACKUPS_DIR || joinPath(storeDir, 'backups')
export const backupsDir: string =
process.env.BACKUPS_DIR || joinPath(storeDir, 'backups')
export const nvmBackupsDir: string = joinPath(backupsDir, 'nvm')
export const storeBackupsDir: string = joinPath(backupsDir, 'store')


export const defaultUser: string = 'admin'
export const defaultPsw: string = 'zwave'
export const defaultUser: string = 'admin'
export const defaultPsw: string = 'zwave'
// lgtm [js/hardcoded-credentials]
export const sessionSecret: string = process.env.SESSION_SECRET || 'DEFAULT_SESSION_SECRET_CHANGE_ME'
export const base: string = process.env.BASE_PATH || '/'
export const port: string | number = process.env.PORT || 8091
export const host: string = process.env.HOST // by default undefined, so it will listen on all interfaces both ipv4 and ipv6
export const sessionSecret: string =
process.env.SESSION_SECRET || 'DEFAULT_SESSION_SECRET_CHANGE_ME'
Dismissed Show dismissed Hide dismissed
export const base: string = process.env.BASE_PATH || '/'
export const port: string | number = process.env.PORT || 8091
export const host: string = process.env.HOST // by default undefined, so it will listen on all interfaces both ipv4 and ipv6
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/BackupManager.ts → api/lib/BackupManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import store from '../config/store'
import { module } from '../lib/logger'
import { module } from './logger'
import jsonStore, { STORE_BACKUP_PREFIX } from './jsonStore'
import Cron from 'croner'
import { readdir, unlink } from 'fs/promises'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/Gateway.ts → api/lib/Gateway.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as fs from 'fs'
import * as path from 'path'
import * as utils from '../lib/utils'
import * as utils from './utils'
import { AlarmSensorType, SetValueAPIOptions } from 'zwave-js'
import { CommandClasses, ValueID } from '@zwave-js/core'
import * as Constants from './Constants'
import { LogLevel, module } from '../lib/logger'
import { LogLevel, module } from './logger'
import hassCfg from '../hass/configurations'
import hassDevices from '../hass/devices'
import { storeDir } from '../config/app'
Expand Down
5 changes: 2 additions & 3 deletions lib/MqttClient.ts → api/lib/MqttClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
IStore,
connect,
} from 'mqtt'
import { allSettled, parseJSON, sanitizeTopic } from './utils'
import { allSettled, parseJSON, sanitizeTopic, pkgJson } from './utils'
import { module } from './logger'
import { version as appVersion } from '../package.json'
import { TypedEventEmitter } from './EventEmitter'
import { storeDir } from '../config/app'
import { ensureDir } from 'fs-extra'
Expand Down Expand Up @@ -203,7 +202,7 @@ class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
if (this.client) {
this.client.publish(
this.getClientTopic(MqttClient.VERSION_TOPIC),
JSON.stringify({ value: appVersion, time: Date.now() }),
JSON.stringify({ value: pkgJson.version, time: Date.now() }),
{ retain: this.config.retain, qos: this.config.qos },
)
}
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions lib/ZwaveClient.ts → api/lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ import * as utils from './utils'
import { serverVersion, ZwavejsServer } from '@zwave-js/server'
import { ensureDir, exists, mkdirp, writeFile } from 'fs-extra'
import { Server as SocketServer } from 'socket.io'
import * as pkgjson from '../package.json'
import { TypedEventEmitter } from './EventEmitter'
import { GatewayValue } from './Gateway'

Expand Down Expand Up @@ -2135,7 +2134,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
: true,
},
userAgent: {
[pkgjson.name]: pkgjson.version,
[utils.pkgJson.name]: utils.pkgJson.version,
},
}

Expand Down Expand Up @@ -2677,9 +2676,9 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
if (this._driver) {
this._driver.enableStatistics({
applicationName:
pkgjson.name +
utils.pkgJson.name +
(this.cfg.serverEnabled ? ' / zwave-js-server' : ''),
applicationVersion: pkgjson.version,
applicationVersion: utils.pkgJson.version,
})
logger.info('Zwavejs usage statistics ENABLED')
}
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 13 additions & 6 deletions lib/utils.ts → api/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// eslint-disable-next-line one-var
import * as appRoot from 'app-root-path'
import { version } from '../package.json'
import { ValueID } from 'zwave-js'
import path from 'path'
import path, { resolve } from 'path'
import crypto from 'crypto'
import { readFileSync } from 'fs'

// don't use import here, it will break the build
// eslint-disable-next-line @typescript-eslint/no-var-requires
export const pkgJson = require('../../package.json')

let VERSION: string

export interface Snippet {
Expand Down Expand Up @@ -77,13 +79,16 @@ export function fileDate(date?: Date) {
return date.toISOString().slice(-24).replace(/\D/g, '').slice(0, 14)
}

/** Where package.json is */
export const basePath = resolve(__dirname, '..', '..')

/**
* Get the base root path to application directory. When we are in a `pkg` environment
* the path of the snapshot is not writable
*/
export function getPath(write: boolean): string {
if (write && hasProperty(process, 'pkg')) return process.cwd()
else return appRoot.toString()
else return basePath
}

/**
Expand Down Expand Up @@ -167,9 +172,11 @@ export function getVersion(): string {
.trim()
}

VERSION = `${version}${rev ? '.' + rev.substring(0, 7) : ''}`
VERSION = `${pkgJson.version}${
rev ? '.' + rev.substring(0, 7) : ''
}`
} catch {
VERSION = version
VERSION = pkgJson.version
}
}

Expand Down
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"watch": ["**/*.ts", "package.json", "tsconfig.json", "../node-zwave-js/packages/*/build/**/*.js"],
"ext": "ts,json,js",
"ignore": ["pkg", "store", "node_modules", "src", "dist", "server"],
"exec": "node --inspect=7004 --trace-warnings -r ./esbuild-register bin/www.ts"
"exec": "node --inspect=7004 --trace-warnings -r ./esbuild-register api/bin/www.ts"
}
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"dev": "vite",
"dev-https": "SERVER_SSL='true' yarn run dev",
"dev:server": "nodemon",
"server": "ts-node -r esbuild-register bin/www.ts",
"server": "ts-node -r esbuild-register api/bin/www.ts",
"fake-stick": "yarn mock-server -c server_config.js",
"start": "node --preserve-symlinks server/bin/www.js",
"lint": "npm-run-all 'lint:*'",
Expand Down Expand Up @@ -119,7 +119,6 @@
"@zwave-js/server": "^1.33.0",
"@zwave-js/winston-daily-rotate-file": "^4.5.6-1",
"ansi_up": "^6.0.2",
"app-root-path": "^3.1.0",
"archiver": "^6.0.1",
"axios": "^1.5.1",
"axios-progress-bar": "^1.2.0",
Expand Down Expand Up @@ -168,7 +167,6 @@
"@actions/github": "^6.0.0",
"@babel/register": "^7.22.15",
"@tsconfig/node18": "^18.2.2",
"@types/app-root-path": "^1.2.7",
"@types/archiver": "^5.3.4",
"@types/chai": "^4.3.9",
"@types/chai-as-promised": "^7.1.7",
Expand Down
7 changes: 1 addition & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
"strict": false
},
"include": [
"app.ts",
"bin/www.ts",
"config/*.ts",
"hass/*.ts",
"lib/*.ts",
"types/*.ts"
"api/**/*.ts",
],
}
16 changes: 0 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2511,13 +2511,6 @@ __metadata:
languageName: node
linkType: hard

"@types/app-root-path@npm:^1.2.7":
version: 1.2.7
resolution: "@types/app-root-path@npm:1.2.7"
checksum: 288bb3af23cfa237e45d513435b4f85fb6af481065b70f5efdfe39e950de4f2b29d1fb4cdeb6773fbd247b69bc8e405dcf5a548dbe11833de3f6186570b322f7
languageName: node
linkType: hard

"@types/archiver@npm:^5.3.4":
version: 5.3.4
resolution: "@types/archiver@npm:5.3.4"
Expand Down Expand Up @@ -3552,13 +3545,6 @@ __metadata:
languageName: node
linkType: hard

"app-root-path@npm:^3.1.0":
version: 3.1.0
resolution: "app-root-path@npm:3.1.0"
checksum: e3db3957aee197143a0f6c75e39fe89b19e7244f28b4f2944f7276a9c526d2a7ab2d115b4b2d70a51a65a9a3ca17506690e5b36f75a068a7e5a13f8c092389ba
languageName: node
linkType: hard

"append-field@npm:^1.0.0":
version: 1.0.0
resolution: "append-field@npm:1.0.0"
Expand Down Expand Up @@ -14386,7 +14372,6 @@ __metadata:
"@mdi/js": 7.3.67
"@release-it/conventional-changelog": ^7.0.2
"@tsconfig/node18": ^18.2.2
"@types/app-root-path": ^1.2.7
"@types/archiver": ^5.3.4
"@types/chai": ^4.3.9
"@types/chai-as-promised": ^7.1.7
Expand All @@ -14412,7 +14397,6 @@ __metadata:
"@zwave-js/server": ^1.33.0
"@zwave-js/winston-daily-rotate-file": ^4.5.6-1
ansi_up: ^6.0.2
app-root-path: ^3.1.0
archiver: ^6.0.1
axios: ^1.5.1
axios-progress-bar: ^1.2.0
Expand Down
Loading