From 25683a35fc0a3c87e503d99f56c1ec2e9cf3371e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 11 May 2021 19:26:03 +0000 Subject: [PATCH] Simplify the pcoess of creating js code --- gulpfile.ts | 135 +++++++++++++++++++++++----------------------- package-lock.json | 12 ----- package.json | 2 - 3 files changed, 67 insertions(+), 82 deletions(-) diff --git a/gulpfile.ts b/gulpfile.ts index 3b3874a..000bdf6 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -11,7 +11,6 @@ import sass = require("gulp-sass"); import terser = require("gulp-terser"); import merge = require("merge-stream"); import minimist = require("minimist"); -import PromiseQueue = require("promise-queue"); import { parseArgsStringToArgv } from "string-argv"; import Path = require("upath"); import buffer = require("vinyl-buffer"); @@ -237,10 +236,11 @@ export async function Build(): Promise * @returns * The pipeline to execute. */ -export async function Library(): Promise +export function Library(): NodeJS.ReadWriteStream { - let streams: Array> = []; - let queue = new PromiseQueue(); + let errorMessages: string[] = []; + let streams: NodeJS.ReadWriteStream[] = []; + let queue: NodeJS.ReadWriteStream[] = []; let tsConfigFile = settings.TypeScriptProjectRoot("tsconfig.json"); // eslint-disable-next-line @typescript-eslint/no-var-requires let tsConfig = require(tsConfigFile); @@ -252,13 +252,15 @@ export async function Library(): Promise debug: settings.Debug }; - { - let errorMessages: string[] = []; - let files = (tsConfig.files as string[]).map( - (file) => Path.relative(settings.TypeScriptSourceRoot(), settings.TypeScriptProjectRoot(file))); + let files = (tsConfig.files as string[]).map( + (file) => Path.relative(settings.TypeScriptSourceRoot(), settings.TypeScriptProjectRoot(file))); - for (let file of files) + for (let file of files) + { + let builder = (): NodeJS.ReadWriteStream => { + let stream: NodeJS.ReadWriteStream; + let bundler = browserify( { ...optionBase, @@ -281,84 +283,81 @@ export async function Library(): Promise project: tsConfigFile }); - let bundle = async (): Promise => - { - return new Promise( - (resolve) => + stream = bundler.bundle().on( + "error", + (error) => + { + let message: string = error.message; + + if (!errorMessages.includes(message)) { - let stream = bundler.bundle().on( - "error", - (error) => - { - let message: string = error.message; + let result = new RegExp(`^(${error["fileName"]})\\((\\d+|\\d+(,\\d+){1,3})\\): .* TS([\\d]+): (.*)$`).exec(message); + errorMessages.push(message); + console.log(`${Path.relative(process.cwd(), result[1])}(${result[2]}): ${result[4]} ${result[5]}`); + } + } + ).pipe( + vinylSourceStream(Path.changeExt(file, "js")) + ).pipe( + buffer() + ).pipe( + gulpIf( + !settings.Debug, + terser() + ) + ).pipe( + gulp.dest(settings.LibraryPath()) + ).on( + "end", + () => + { + if (settings.Watch) + { + if (queue.includes(stream)) + { + queue.splice(queue.indexOf(stream), 1); + } - if (!errorMessages.includes(message)) - { - let result = new RegExp(`^(${error["fileName"]})\\((\\d+|\\d+(,\\d+){1,3})\\): .* TS([\\d]+): (.*)$`).exec(message); - errorMessages.push(message); - console.log(`${Path.relative(process.cwd(), result[1])}(${result[2]}): ${result[4]} ${result[5]}`); - } + if (queue.length === 0) + { + log.info(watchFinishMessage(errorMessages.length)); + + if (errorMessages.length === 0) + { + syncer.reload("*.js"); } - ).pipe( - vinylSourceStream(Path.changeExt(file, "js")) - ).pipe( - buffer() - ).pipe( - gulpIf( - !settings.Debug, - terser() - ) - ).pipe( - gulp.dest(settings.LibraryPath()) - ); - stream.on( - "end", - () => - { - if (settings.Watch && ((queue.getQueueLength() + queue.getPendingLength()) === 1)) - { - if (errorMessages.length === 0) - { - syncer.reload("*.js"); - } - - log.info(watchFinishMessage(errorMessages.length)); - } - - errorMessages.splice(0, errorMessages.length); - resolve(stream); - }); - }); - }; + errorMessages.splice(0, errorMessages.length); + } + } + }); if (settings.Watch) { - bundler.on( + bundler.once( "update", () => { - if ((queue.getQueueLength() + queue.getPendingLength()) === 0) + console.log(`Update called for ${file}: ${queue.length}`); + + if (queue.length === 0) { log.info(incrementalMessage); } - queue.add( - async () => - { - return bundle(); - }); + queue.push(builder()); }); } - let build = (): Promise => queue.add(bundle); - build.displayName = Build.displayName; - build.description = Build.description; - streams.push(build()); - } + return stream; + }; + + let stream = builder(); + queue.push(stream); + streams.push(stream); } - return merge(await Promise.all(streams)) as NodeJS.ReadWriteStream; + return merge(streams); } Library.description = "Builds the TypeScript- and JavaScript-library."; diff --git a/package-lock.json b/package-lock.json index 13f350f..cdebfa1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -793,12 +793,6 @@ "integrity": "sha1-akDsfr0kGO5p7jl+SOQhaSaKEL8=", "dev": true }, - "@types/promise-queue": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@types/promise-queue/-/promise-queue-2.2.0.tgz", - "integrity": "sha512-9QLtid6GxEWqpF+QImxBRG6bSVOHtpAm2kXuIyEvZBbSOupLvqhhJv8uaHbS8kUL8FDjzH3RWcSyC/52WOVtGw==", - "dev": true - }, "@types/serve-static": { "version": "1.13.9", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz", @@ -7221,12 +7215,6 @@ "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", "dev": true }, - "promise-queue": { - "version": "2.2.5", - "resolved": "https://registry.npmjs.org/promise-queue/-/promise-queue-2.2.5.tgz", - "integrity": "sha1-L29ffA9tCBCelnZZx5uIqe1ek7Q=", - "dev": true - }, "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", diff --git a/package.json b/package.json index 52ef6cd..b44fa81 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "@types/merge-stream": "^1.1.2", "@types/minimist": "^1.2.1", "@types/node": "^15.0.2", - "@types/promise-queue": "^2.2.0", "@types/vinyl-buffer": "^1.0.0", "@types/vinyl-source-stream": "0.0.30", "@types/watchify": "^3.11.0", @@ -63,7 +62,6 @@ "minimist": "^1.2.5", "node-sass-tilde-importer": "^1.0.2", "popper.js": "^1.16.0", - "promise-queue": "^2.2.5", "string-argv": "^0.3.1", "ts-node": "^9.1.1", "tsify": "^5.0.4",