diff --git a/.gitignore b/.gitignore index 089b9203..aa0afbdc 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ build # dotenv environment variables file .env + +!build.js diff --git a/packages/server/build.js b/packages/server/build.js new file mode 100755 index 00000000..d1b8338e --- /dev/null +++ b/packages/server/build.js @@ -0,0 +1,63 @@ +import {rm} from 'fs/promises'; +import {createLogger} from '@alwatr/logger'; +import {context, analyzeMetafile} from 'esbuild'; + +const logger = createLogger('@alwatr/storage-server-build', true); + +const srcDir = 'src'; +const outDir = 'dist'; +const srcFilename = 'index'; + +async function build(cleanMode, watchMode, debugMode, prettyMode) { + logger.logMethodArgs?.('build', {cleanMode, watchMode, debugMode, prettyMode}); + + const esbuildContext = await context({ + entryPoints: [`${srcDir}/${srcFilename}.ts`], + + logLevel: 'info', + platform: 'node', + target: 'es2018', + format: 'esm', + conditions: debugMode ? ['development'] : undefined, + + minify: !prettyMode, + treeShaking: true, + sourcemap: true, + sourcesContent: debugMode, + bundle: true, + splitting: false, + charset: 'utf8', + legalComments: 'none', + metafile: true, + outbase: srcDir, + outdir: outDir, + entryNames: watchMode ? '[name]' : '[dir]/[name]' + }); + + if (cleanMode) { + logger.logOther?.('๐Ÿงน Cleaning...'); + await rm(outDir, {recursive: true, force: true}); + } + + if (watchMode) { + logger.logOther?.('๐Ÿ‘€ Watching...'); + esbuildContext.watch(); + } else { + logger.logOther?.('๐Ÿš€ Building...'); + const buildInfo = await esbuildContext.rebuild(); + await esbuildContext.dispose(); + + if (debugMode) { + logger.logOther?.(await analyzeMetafile(buildInfo.metafile)); + } + + logger.logOther?.('โœ… Done.'); + } +} + +const cleanMode = process.argv.includes('--clean'); +const watchMode = process.argv.includes('--watch'); +const debugMode = process.argv.includes('--debug'); +const prettyMode = process.argv.includes('--pretty'); + +build(cleanMode, watchMode, debugMode, prettyMode); diff --git a/packages/server/package.json b/packages/server/package.json index c00eb9e7..7cc1865a 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -40,16 +40,17 @@ "cb": "run-s clean build", "s": "yarn run start", "w": "yarn run watch", + "l": "run-s 'lint:*'", + "lint:ts": "tsc --build", "start": "NODE_OPTIONS=--enable-source-maps run-s clean build serve", - "build": "yarn run build:ts", - "build:ts": "tsc --build", - "build:es": "esbuild src/index.ts --platform=node --target=node19 --bundle --format=esm --minify --sourcemap --outdir=dist --out-extension:.js=.mjs", + "build": "yarn run build:es", + "build:es": "yarn node ./build.js", "clean": "rm -rf dist build .tsbuildinfo", - "serve": "yarn node --enable-source-maps dist/index.mjs", - "serve:debug": "yarn node --inspect --enable-source-maps dist/index.mjs", + "serve": "yarn node --enable-source-maps dist/index.js", + "serve:debug": "yarn node --inspect --enable-source-maps dist/index.js", "watch": "run-s clean build && run-p watch:es watch:node", - "watch:node": "nodemon -w dist/ --enable-source-maps dist/index.mjs", - "watch:debug-node": "nodemon -w dist/ --inspect --enable-source-maps dist/index.mjs", + "watch:node": "nodemon -w dist/ --enable-source-maps dist/index.js", + "watch:debug-node": "nodemon -w dist/ --inspect --enable-source-maps dist/index.js", "watch:ts": "yarn run build:ts --watch --preserveWatchOutput", "watch:es": "yarn run build:es --watch" }, @@ -59,6 +60,11 @@ "@alwatr/storage-engine": "workspace:^", "@alwatr/type": "^1.2.1", "@alwatr/util": "^1.3.1", - "@types/node": "^20.10.0" + "@types/node": "^20.10.0", + "esbuild": "^0.19.7", + "nodemon": "^3.0.1", + "npm-run-all": "^4.1.5", + "tslib": "^2.6.2", + "typescript": "^5.3.2" } } diff --git a/packages/server/tsconfig.json b/packages/server/tsconfig.json index 70b279eb..ac8700ca 100644 --- a/packages/server/tsconfig.json +++ b/packages/server/tsconfig.json @@ -5,8 +5,7 @@ "composite": true, "tsBuildInfoFile": ".tsbuildinfo", "rootDir": "src", - "outDir": "build" - // "noEmit": true + "noEmit": true }, "include": ["src/**/*.ts"], diff --git a/yarn.lock b/yarn.lock index d85842e4..74bff03b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -136,6 +136,11 @@ __metadata: "@alwatr/type": "npm:^1.2.1" "@alwatr/util": "npm:^1.3.1" "@types/node": "npm:^20.10.0" + esbuild: "npm:^0.19.7" + nodemon: "npm:^3.0.1" + npm-run-all: "npm:^4.1.5" + tslib: "npm:^2.6.2" + typescript: "npm:^5.3.2" languageName: unknown linkType: soft @@ -194,6 +199,160 @@ __metadata: languageName: node linkType: hard +"@esbuild/android-arm64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/android-arm64@npm:0.19.7" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/android-arm@npm:0.19.7" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/android-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/android-x64@npm:0.19.7" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/darwin-arm64@npm:0.19.7" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/darwin-x64@npm:0.19.7" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/freebsd-arm64@npm:0.19.7" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/freebsd-x64@npm:0.19.7" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-arm64@npm:0.19.7" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-arm@npm:0.19.7" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-ia32@npm:0.19.7" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-loong64@npm:0.19.7" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + +"@esbuild/linux-mips64el@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-mips64el@npm:0.19.7" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-ppc64@npm:0.19.7" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-riscv64@npm:0.19.7" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-s390x@npm:0.19.7" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/linux-x64@npm:0.19.7" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/netbsd-x64@npm:0.19.7" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/openbsd-x64@npm:0.19.7" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/sunos-x64@npm:0.19.7" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/win32-arm64@npm:0.19.7" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/win32-ia32@npm:0.19.7" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.19.7": + version: 0.19.7 + resolution: "@esbuild/win32-x64@npm:0.19.7" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -1617,7 +1776,7 @@ __metadata: languageName: node linkType: hard -"abbrev@npm:^1.0.0": +"abbrev@npm:1, abbrev@npm:^1.0.0": version: 1.1.1 resolution: "abbrev@npm:1.1.1" checksum: 3f762677702acb24f65e813070e306c61fafe25d4b2583f9dfc935131f774863f3addd5741572ed576bd69cabe473c5af18e1e108b829cb7b6b4747884f726e6 @@ -2252,7 +2411,7 @@ __metadata: languageName: node linkType: hard -"chokidar@npm:^3.4.3": +"chokidar@npm:^3.4.3, chokidar@npm:^3.5.2": version: 3.5.3 resolution: "chokidar@npm:3.5.3" dependencies: @@ -3099,6 +3258,83 @@ __metadata: languageName: node linkType: hard +"esbuild@npm:^0.19.7": + version: 0.19.7 + resolution: "esbuild@npm:0.19.7" + dependencies: + "@esbuild/android-arm": "npm:0.19.7" + "@esbuild/android-arm64": "npm:0.19.7" + "@esbuild/android-x64": "npm:0.19.7" + "@esbuild/darwin-arm64": "npm:0.19.7" + "@esbuild/darwin-x64": "npm:0.19.7" + "@esbuild/freebsd-arm64": "npm:0.19.7" + "@esbuild/freebsd-x64": "npm:0.19.7" + "@esbuild/linux-arm": "npm:0.19.7" + "@esbuild/linux-arm64": "npm:0.19.7" + "@esbuild/linux-ia32": "npm:0.19.7" + "@esbuild/linux-loong64": "npm:0.19.7" + "@esbuild/linux-mips64el": "npm:0.19.7" + "@esbuild/linux-ppc64": "npm:0.19.7" + "@esbuild/linux-riscv64": "npm:0.19.7" + "@esbuild/linux-s390x": "npm:0.19.7" + "@esbuild/linux-x64": "npm:0.19.7" + "@esbuild/netbsd-x64": "npm:0.19.7" + "@esbuild/openbsd-x64": "npm:0.19.7" + "@esbuild/sunos-x64": "npm:0.19.7" + "@esbuild/win32-arm64": "npm:0.19.7" + "@esbuild/win32-ia32": "npm:0.19.7" + "@esbuild/win32-x64": "npm:0.19.7" + dependenciesMeta: + "@esbuild/android-arm": + optional: true + "@esbuild/android-arm64": + optional: true + "@esbuild/android-x64": + optional: true + "@esbuild/darwin-arm64": + optional: true + "@esbuild/darwin-x64": + optional: true + "@esbuild/freebsd-arm64": + optional: true + "@esbuild/freebsd-x64": + optional: true + "@esbuild/linux-arm": + optional: true + "@esbuild/linux-arm64": + optional: true + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": + optional: true + "@esbuild/linux-mips64el": + optional: true + "@esbuild/linux-ppc64": + optional: true + "@esbuild/linux-riscv64": + optional: true + "@esbuild/linux-s390x": + optional: true + "@esbuild/linux-x64": + optional: true + "@esbuild/netbsd-x64": + optional: true + "@esbuild/openbsd-x64": + optional: true + "@esbuild/sunos-x64": + optional: true + "@esbuild/win32-arm64": + optional: true + "@esbuild/win32-ia32": + optional: true + "@esbuild/win32-x64": + optional: true + bin: + esbuild: bin/esbuild + checksum: ec72d9523559005cf9d15c9c9295406633a0c38a20b63d76f22de211902d4cfbe1bfc713a19b4125355cbde5cdf80a1702c13f20cdc035fdbe7aa52d706de59c + languageName: node + linkType: hard + "escalade@npm:^3.1.1": version: 3.1.1 resolution: "escalade@npm:3.1.1" @@ -4172,6 +4408,13 @@ __metadata: languageName: node linkType: hard +"ignore-by-default@npm:^1.0.1": + version: 1.0.1 + resolution: "ignore-by-default@npm:1.0.1" + checksum: 9ab6e70e80f7cc12735def7ecb5527cfa56ab4e1152cd64d294522827f2dcf1f6d85531241537dc3713544e88dd888f65cb3c49c7b2cddb9009087c75274e533 + languageName: node + linkType: hard + "ignore-walk@npm:^6.0.0": version: 6.0.3 resolution: "ignore-walk@npm:6.0.3" @@ -5612,6 +5855,26 @@ __metadata: languageName: node linkType: hard +"nodemon@npm:^3.0.1": + version: 3.0.1 + resolution: "nodemon@npm:3.0.1" + dependencies: + chokidar: "npm:^3.5.2" + debug: "npm:^3.2.7" + ignore-by-default: "npm:^1.0.1" + minimatch: "npm:^3.1.2" + pstree.remy: "npm:^1.1.8" + semver: "npm:^7.5.3" + simple-update-notifier: "npm:^2.0.0" + supports-color: "npm:^5.5.0" + touch: "npm:^3.1.0" + undefsafe: "npm:^2.0.5" + bin: + nodemon: bin/nodemon.js + checksum: 471a218227949b38926de78237004c91e226b63ee06f433cf85c2f1c1f8b6bfbef9bceaa8d27786e7cfb539eb84da357d01741884d08a3ae172bebecd0f1de5b + languageName: node + linkType: hard + "nopt@npm:^6.0.0": version: 6.0.0 resolution: "nopt@npm:6.0.0" @@ -5634,6 +5897,17 @@ __metadata: languageName: node linkType: hard +"nopt@npm:~1.0.10": + version: 1.0.10 + resolution: "nopt@npm:1.0.10" + dependencies: + abbrev: "npm:1" + bin: + nopt: ./bin/nopt.js + checksum: ddfbd892116a125fd68849ef564dd5b1f0a5ba0dbbf18782e9499e2efad8f4d3790635b47c6b5d3f7e014069e7b3ce5b8112687e9ae093fcd2678188c866fe28 + languageName: node + linkType: hard + "normalize-package-data@npm:^2.3.2, normalize-package-data@npm:^2.5.0": version: 2.5.0 resolution: "normalize-package-data@npm:2.5.0" @@ -6410,6 +6684,13 @@ __metadata: languageName: node linkType: hard +"pstree.remy@npm:^1.1.8": + version: 1.1.8 + resolution: "pstree.remy@npm:1.1.8" + checksum: 30f78c88ce6393cb3f7834216cb6e282eb83c92ccb227430d4590298ab2811bc4a4745f850a27c5178e79a8f3e316591de0fec87abc19da648c2b3c6eb766d14 + languageName: node + linkType: hard + "punycode@npm:^2.1.0, punycode@npm:^2.1.1": version: 2.3.1 resolution: "punycode@npm:2.3.1" @@ -6970,6 +7251,15 @@ __metadata: languageName: node linkType: hard +"simple-update-notifier@npm:^2.0.0": + version: 2.0.0 + resolution: "simple-update-notifier@npm:2.0.0" + dependencies: + semver: "npm:^7.5.3" + checksum: 2a00bd03bfbcbf8a737c47ab230d7920f8bfb92d1159d421bdd194479f6d01ebc995d13fbe13d45dace23066a78a3dc6642999b4e3b38b847e6664191575b20c + languageName: node + linkType: hard + "slash@npm:^3.0.0": version: 3.0.0 resolution: "slash@npm:3.0.0" @@ -7282,7 +7572,7 @@ __metadata: languageName: node linkType: hard -"supports-color@npm:^5.3.0": +"supports-color@npm:^5.3.0, supports-color@npm:^5.5.0": version: 5.5.0 resolution: "supports-color@npm:5.5.0" dependencies: @@ -7415,6 +7705,17 @@ __metadata: languageName: node linkType: hard +"touch@npm:^3.1.0": + version: 3.1.0 + resolution: "touch@npm:3.1.0" + dependencies: + nopt: "npm:~1.0.10" + bin: + nodetouch: ./bin/nodetouch.js + checksum: dacb4a639401b83b0a40b56c0565e01096e5ecf38b22a4840d9eeb642a5bea136c6a119e4543f9b172349a5ee343b10cda0880eb47f7d7ddfd6eac59dcf53244 + languageName: node + linkType: hard + "tr46@npm:^3.0.0": version: 3.0.0 resolution: "tr46@npm:3.0.0" @@ -7629,7 +7930,7 @@ __metadata: languageName: node linkType: hard -"typescript@npm:^5.2.2": +"typescript@npm:^5.2.2, typescript@npm:^5.3.2": version: 5.3.2 resolution: "typescript@npm:5.3.2" bin: @@ -7639,7 +7940,7 @@ __metadata: languageName: node linkType: hard -"typescript@patch:typescript@npm%3A^5.2.2#optional!builtin": +"typescript@patch:typescript@npm%3A^5.2.2#optional!builtin, typescript@patch:typescript@npm%3A^5.3.2#optional!builtin": version: 5.3.2 resolution: "typescript@patch:typescript@npm%3A5.3.2#optional!builtin::version=5.3.2&hash=e012d7" bin: @@ -7684,6 +7985,13 @@ __metadata: languageName: node linkType: hard +"undefsafe@npm:^2.0.5": + version: 2.0.5 + resolution: "undefsafe@npm:2.0.5" + checksum: 96c0466a5fbf395917974a921d5d4eee67bca4b30d3a31ce7e621e0228c479cf893e783a109af6e14329b52fe2f0cb4108665fad2b87b0018c0df6ac771261d5 + languageName: node + linkType: hard + "undici-types@npm:~5.26.4": version: 5.26.5 resolution: "undici-types@npm:5.26.5"