From 94d01310b90574ebfe02b560efd6e3a711ec6d7d Mon Sep 17 00:00:00 2001 From: baldo Date: Wed, 8 Apr 2020 01:55:48 +0200 Subject: [PATCH] Quick and dirty typescript setup to get going. --- .gitignore | 1 + Gruntfile.js | 5 +- README.md | 6 +- REFACTOR.md | 1 + package-lock.json | 6 ++ package.json | 10 ++- publish.sh | 3 +- server/app.js | 2 +- server/resources/mailResource.js | 2 +- server/resources/monitoringResource.js | 2 +- server/resources/nodeResource.js | 2 +- server/resources/taskResource.js | 2 +- server/services/monitoringService.js | 2 +- .../{config.js => config.js.template} | 0 server/tsconfig.json | 66 +++++++++++++++++++ server/utils/resources.js | 2 +- server/validation/constraints.js | 1 + 17 files changed, 99 insertions(+), 14 deletions(-) rename server/templates/{config.js => config.js.template} (100%) create mode 100644 server/tsconfig.json create mode 120000 server/validation/constraints.js diff --git a/.gitignore b/.gitignore index 2a09fea..f3c5acb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules *.swp config.json README.html +/server-build/ diff --git a/Gruntfile.js b/Gruntfile.js index 9b81afb..560d9f6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -115,7 +115,7 @@ module.exports = function (grunt) { '<%= yeoman.app %>/scripts/{,**/}*.js', 'shared/{,**/}*.js', 'server/{,**/}*.js', - '!server/templates/{,**/}*.js' + '!server/templates/{,**/}*.template' ] }, @@ -356,10 +356,11 @@ module.exports = function (grunt) { }, { expand: true, - cwd: 'server', + cwd: 'server-build', dest: '<%= yeoman.dist %>/server', src: [ '{,**/}*.html', + '{,**/}*.template', '{,**/}*.js', '{,**/}*.sql', '{,**/}*.txt' diff --git a/README.md b/README.md index a2cbe9a..baacafb 100644 --- a/README.md +++ b/README.md @@ -333,7 +333,7 @@ fastd-Key und die MAC-Adresse angeben. ### Build -`grunt clean build` +`npm run clean && npm run build` Der Output landet dann unter `dist/`. @@ -341,14 +341,14 @@ Der Output landet dann unter `dist/`. ### Server starten 1. Zunächst eine `config.json` anlegen wie oben unter "Installation / Konfiguration" beschrieben. -2. `node server/main.js -c config.json` +2. `npm run server:run -- -c config.json` Der Server ist dann erreichbar unter [http://localhost:8080](http://localhost:8080). ### Life-Reload vom Client -`grunt serve` +`npm client:serve` Der Client ist dann erreichbar via [http://localhost:9000](http://localhost:9000), erwartet aber, dass der Server für die REST-API auch läuft (s. o.) und auf Port `8080` erreichbar ist. diff --git a/REFACTOR.md b/REFACTOR.md index 1ee47ec..0b0f2a6 100644 --- a/REFACTOR.md +++ b/REFACTOR.md @@ -3,6 +3,7 @@ ## Short term * Integrate typescript in the build and start migrating the server code. +* Find a nice way to integrate typescript with grunt. ## Mid term diff --git a/package-lock.json b/package-lock.json index acdfb62..fc8eed2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9070,6 +9070,12 @@ "mime-types": "~2.1.24" } }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + }, "typical": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/typical/-/typical-4.0.0.tgz", diff --git a/package.json b/package.json index f9c0a3d..a6f54f0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,13 @@ "bin": { "ffffng": "server/main.js" }, + "scripts": { + "build": "npm run server:build && grunt build", + "clean": "rm -rf server-build/ && grunt clean", + "client:serve": "grunt serve", + "server:build": "tsc -b server && ln -sfv ../server/db/patches ./server-build/db/ && ln -sfv ../server/templates ./server-build/", + "server:run": "npm run server:build && node server-build/main.js" + }, "dependencies": { "async": "^3.1.0", "body-parser": "^1.19.0", @@ -73,7 +80,8 @@ "imagemin-gifsicle": "^6.0.1", "jshint-stylish": "^2.2.1", "load-grunt-tasks": "^5.1.0", - "time-grunt": "^2.0.0" + "time-grunt": "^2.0.0", + "typescript": "^3.8.3" }, "engines": { "node": ">=10.0.0" diff --git a/publish.sh b/publish.sh index a625456..a5cce38 100755 --- a/publish.sh +++ b/publish.sh @@ -41,7 +41,8 @@ fi echo if confirm "Continue publishing?"; then - ./node_modules/.bin/grunt clean build + npm run clean + npm run build cd dist npm publish diff --git a/server/app.js b/server/app.js index 12b32a6..f15c4ef 100644 --- a/server/app.js +++ b/server/app.js @@ -43,7 +43,7 @@ module.exports = (() => { router.use(compress()); function serveTemplate (mimeType, req, res, next) { - return fs.readFile(templateDir + '/' + req.path, 'utf8', function (err, body) { + return fs.readFile(templateDir + '/' + req.path + '.template', 'utf8', function (err, body) { if (err) { return next(err); } diff --git a/server/resources/mailResource.js b/server/resources/mailResource.js index 5645bdc..bdde844 100644 --- a/server/resources/mailResource.js +++ b/server/resources/mailResource.js @@ -1,6 +1,6 @@ 'use strict'; -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const ErrorTypes = require('../utils/errorTypes') const Logger = require('../logger') const MailService = require('../services/mailService') diff --git a/server/resources/monitoringResource.js b/server/resources/monitoringResource.js index 976dc7f..725d6ce 100644 --- a/server/resources/monitoringResource.js +++ b/server/resources/monitoringResource.js @@ -2,7 +2,7 @@ const _ = require('lodash') -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const ErrorTypes = require('../utils/errorTypes') const Logger = require('../logger') const MonitoringService = require('../services/monitoringService') diff --git a/server/resources/nodeResource.js b/server/resources/nodeResource.js index 941c5ab..7b56b34 100644 --- a/server/resources/nodeResource.js +++ b/server/resources/nodeResource.js @@ -3,7 +3,7 @@ const _ = require('lodash') const deepExtend = require('deep-extend') -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const ErrorTypes = require('../utils/errorTypes') const Logger = require('../logger') const MonitoringService = require('../services/monitoringService') diff --git a/server/resources/taskResource.js b/server/resources/taskResource.js index 5969816..178fe4b 100644 --- a/server/resources/taskResource.js +++ b/server/resources/taskResource.js @@ -2,7 +2,7 @@ const _ = require('lodash') -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const ErrorTypes = require('../utils/errorTypes') const Resources = require('../utils/resources') const Scheduler = require('../jobs/scheduler') diff --git a/server/services/monitoringService.js b/server/services/monitoringService.js index a5a8ece..c8fe534 100644 --- a/server/services/monitoringService.js +++ b/server/services/monitoringService.js @@ -6,7 +6,7 @@ const moment = require('moment') const request = require('request') const config = require('../config').config -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const Database = require('../db/database').db const DatabaseUtil = require('../utils/databaseUtil') const ErrorTypes = require('../utils/errorTypes') diff --git a/server/templates/config.js b/server/templates/config.js.template similarity index 100% rename from server/templates/config.js rename to server/templates/config.js.template diff --git a/server/tsconfig.json b/server/tsconfig.json new file mode 100644 index 0000000..a46576d --- /dev/null +++ b/server/tsconfig.json @@ -0,0 +1,66 @@ +{ + "compilerOptions": { + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "lib": ["es2018"], /* Specify library files to be included in the compilation. */ + "allowJs": true, /* Allow javascript files to be compiled. */ + "checkJs": false, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "../server-build", /* Redirect output structure to the directory. */ + "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + + /* Advanced Options */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + } +} diff --git a/server/utils/resources.js b/server/utils/resources.js index 7b0e82a..39cf467 100644 --- a/server/utils/resources.js +++ b/server/utils/resources.js @@ -2,7 +2,7 @@ const _ = require('lodash') -const Constraints = require('../../shared/validation/constraints') +const Constraints = require('../validation/constraints') const ErrorTypes = require('../utils/errorTypes') const Logger = require('../logger') const Validator = require('../validation/validator') diff --git a/server/validation/constraints.js b/server/validation/constraints.js new file mode 120000 index 0000000..5b44939 --- /dev/null +++ b/server/validation/constraints.js @@ -0,0 +1 @@ +../../shared/validation/constraints.js \ No newline at end of file