From fb0985a2ddcdc602f26bbcf1ca297a124b2a7477 Mon Sep 17 00:00:00 2001 From: Patrick Rodgers Date: Fri, 29 Dec 2023 07:41:29 -0500 Subject: [PATCH] updating buildsystem to single timeline --- tools/buildsystem/bin/buildsystem.ts | 54 +++++++++--------- tools/buildsystem/package-lock.json | 20 +++---- tools/buildsystem/package.json | 4 +- tools/buildsystem/src/behaviors/build.ts | 27 +++++---- .../src/behaviors/copy-asset-files.ts | 2 +- .../src/behaviors/copy-package-files.ts | 56 ++++++++++--------- .../create-resolution-package-files.ts | 23 ++++---- .../src/behaviors/publish-nightly.ts | 4 +- tools/buildsystem/src/behaviors/publish.ts | 4 +- .../src/behaviors/replace-version.ts | 19 ++++--- .../src/behaviors/write-packagejson.ts | 4 +- tools/buildsystem/src/build-timeline.ts | 7 +-- tools/buildsystem/src/types.ts | 4 +- 13 files changed, 117 insertions(+), 111 deletions(-) diff --git a/tools/buildsystem/bin/buildsystem.ts b/tools/buildsystem/bin/buildsystem.ts index a4ebdfc5d..ebf035513 100644 --- a/tools/buildsystem/bin/buildsystem.ts +++ b/tools/buildsystem/bin/buildsystem.ts @@ -51,22 +51,8 @@ BuildSystem.prepare({}, function (env) { // setup other context values from config context.distRoot = config[0].distFolder || "./dist/packages"; - const baseTimeline = new BuildTimeline(); - - // now we apply all our configs - if (activeConfig.behaviors) { - baseTimeline.using(...activeConfig.behaviors); - } - - // read in any moment defined observers - for (let key in BuildMoments) { - if (activeConfig[key]) { - baseTimeline.on[key](...activeConfig[key]); - } - } - // now we make an array of timelines 1/target - const timelines = config[0].targets.map(tsconfigPath => { + context.targets = config[0].targets.map(tsconfigPath => { const tsconfigRoot = resolve(dirname(tsconfigPath)); const parsedTSConfig: TSConfig = importJSON(tsconfigPath); @@ -93,22 +79,34 @@ BuildSystem.prepare({}, function (env) { resolvedPkgSrcRoot: tsconfigRoot, resolvedPkgOutRoot: resolvedOutDir, resolvedPkgDistRoot: context.distRoot, - relativePkgDistModulePath: context.distRoot, + relativePkgDistModulePath: context.distRoot, }); } - - return Object.assign({}, context, { - target: { - tsconfigPath, - tsconfigRoot, - parsedTSConfig, - resolvedOutDir, - packages, - } - }); - }).map(context => new BuildTimeline(baseTimeline, context)); + + return { + tsconfigPath, + tsconfigRoot, + parsedTSConfig, + resolvedOutDir, + packages, + }; + }) + + const timeline = new BuildTimeline(context); + + // now we apply all our configs + if (activeConfig.behaviors) { + timeline.using(...activeConfig.behaviors); + } + + // read in any moment defined observers + for (let key in BuildMoments) { + if (activeConfig[key]) { + timeline.on[key](...activeConfig[key]); + } + } // we start one timeline per target - await Promise.all(timelines.map(tl => tl.start())); + await timeline.start(); }); }); diff --git a/tools/buildsystem/package-lock.json b/tools/buildsystem/package-lock.json index 213de9055..cc1241abc 100644 --- a/tools/buildsystem/package-lock.json +++ b/tools/buildsystem/package-lock.json @@ -1,15 +1,15 @@ { "name": "@pnp/buildsystem", - "version": "4.0.0-beta5", + "version": "4.0.0-beta8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@pnp/buildsystem", - "version": "4.0.0-beta5", + "version": "4.0.0-beta8", "license": "MIT", "dependencies": { - "@pnp/core": "^4.0.0-alpha0-v4nightly.20231227", + "@pnp/core": "^4.0.0-alpha0-v4nightly.20231229", "globby": "^14.0.0", "liftoff": "^4.0.0", "webpack": "^5.89.0", @@ -153,14 +153,14 @@ } }, "node_modules/@pnp/core": { - "version": "4.0.0-alpha0-v4nightly.20231227", - "resolved": "https://registry.npmjs.org/@pnp/core/-/core-4.0.0-alpha0-v4nightly.20231227.tgz", - "integrity": "sha512-k3oS1swDBL582L7F7YISURVe6RB5U8kgC6E8vzVgbFE6mmW5KV1jP7HG6EhDc6yvAeryL0mPKXvz0OlKu51ntQ==", + "version": "4.0.0-alpha0-v4nightly.20231229", + "resolved": "https://registry.npmjs.org/@pnp/core/-/core-4.0.0-alpha0-v4nightly.20231229.tgz", + "integrity": "sha512-3N58DjsoiqQTw3eablg4cO0HjL1RPTzV47DGYGBdeFS6p/IKWLLDG59jevuhKV6eoRVWpvaJguQoDMyiGlRNrg==", "dependencies": { "tslib": "2.4.1" }, "engines": { - "node": ">=14.15.1" + "node": ">=18.12.0" }, "funding": { "type": "individual", @@ -2749,9 +2749,9 @@ } }, "@pnp/core": { - "version": "4.0.0-alpha0-v4nightly.20231227", - "resolved": "https://registry.npmjs.org/@pnp/core/-/core-4.0.0-alpha0-v4nightly.20231227.tgz", - "integrity": "sha512-k3oS1swDBL582L7F7YISURVe6RB5U8kgC6E8vzVgbFE6mmW5KV1jP7HG6EhDc6yvAeryL0mPKXvz0OlKu51ntQ==", + "version": "4.0.0-alpha0-v4nightly.20231229", + "resolved": "https://registry.npmjs.org/@pnp/core/-/core-4.0.0-alpha0-v4nightly.20231229.tgz", + "integrity": "sha512-3N58DjsoiqQTw3eablg4cO0HjL1RPTzV47DGYGBdeFS6p/IKWLLDG59jevuhKV6eoRVWpvaJguQoDMyiGlRNrg==", "requires": { "tslib": "2.4.1" } diff --git a/tools/buildsystem/package.json b/tools/buildsystem/package.json index 33bba9a1c..2e5bc17bc 100644 --- a/tools/buildsystem/package.json +++ b/tools/buildsystem/package.json @@ -1,6 +1,6 @@ { "name": "@pnp/buildsystem", - "version": "4.0.0-beta8", + "version": "4.0.0-beta9", "bin": { "pnpbuild": "bin/buildsystem.js" }, @@ -9,7 +9,7 @@ "type": "module", "typings": "./index", "dependencies": { - "@pnp/core": "^4.0.0-alpha0-v4nightly.20231227", + "@pnp/core": "^4.0.0-alpha0-v4nightly.20231229", "globby": "^14.0.0", "liftoff": "^4.0.0", "webpack": "^5.89.0", diff --git a/tools/buildsystem/src/behaviors/build.ts b/tools/buildsystem/src/behaviors/build.ts index dbe88b055..fc1af104d 100644 --- a/tools/buildsystem/src/behaviors/build.ts +++ b/tools/buildsystem/src/behaviors/build.ts @@ -17,23 +17,26 @@ export function Build(flags?: string[]): TimelinePipe { instance.on.build(async function (this: BuildTimeline) { - const { tsconfigPath } = this.context.target; + const { targets } = this.context; - this.log(`Starting Build for target "${tsconfigPath}"`, 1); + await Promise.all(targets.map((target) => { - return new Promise((res, reject) => { + this.log(`Starting Build for target "${target.tsconfigPath}"`, 1); - exec(`${tscPath} -b ${tsconfigPath} ${stringFlags}`, (error, stdout, _stderr) => { + return new Promise((res, reject) => { - if (error === null) { - this.log(`Completing Build for target "${tsconfigPath}"`, 1); - res(); - } else { - this.log(`Error in Build for target "${tsconfigPath}"`, 3); - reject(stdout); - } + exec(`${tscPath} -b ${target.tsconfigPath} ${stringFlags}`, (error, stdout, _stderr) => { + + if (error === null) { + this.log(`Completing Build for target "${target.tsconfigPath}"`, 1); + res(); + } else { + this.log(`Error in Build for target "${target.tsconfigPath}"`, 3); + reject(stdout); + } + }); }); - }); + })); }); return instance; diff --git a/tools/buildsystem/src/behaviors/copy-asset-files.ts b/tools/buildsystem/src/behaviors/copy-asset-files.ts index 071a21482..394bdb56e 100644 --- a/tools/buildsystem/src/behaviors/copy-asset-files.ts +++ b/tools/buildsystem/src/behaviors/copy-asset-files.ts @@ -27,7 +27,7 @@ export function CopyAssetFiles(path: string, pattern: string[]): TimelinePipe { this.log(`CopyAssetFiles found ${temp.length} files for pattern ${stringPattern} in path '${resolvedPath}'`); - const files = await this.context.target.packages.reduce((p, pkg) => { + const files = await this.context.targets[0].packages.reduce((p, pkg) => { return p.then(async (a) => { diff --git a/tools/buildsystem/src/behaviors/copy-package-files.ts b/tools/buildsystem/src/behaviors/copy-package-files.ts index 294d37a25..e2ace2858 100644 --- a/tools/buildsystem/src/behaviors/copy-package-files.ts +++ b/tools/buildsystem/src/behaviors/copy-package-files.ts @@ -18,33 +18,37 @@ export function CopyPackageFiles(source: "src" | "built", pattern: string[]): Ti instance.on.package(async function (this: BuildTimeline) { - this.log(`Starting CopyPackageFiles with pattern ${stringPattern} on target '${this.context.target.tsconfigPath}'`); - - const files = await this.context.target.packages.reduce((p, pkg) => { - - const fileSourceRoot = resolve(source === "src" ? pkg.resolvedPkgSrcRoot : pkg.resolvedPkgOutRoot); - - return p.then(async (a) => { - - const temp = await (globby)(pattern, { - cwd: fileSourceRoot, + this.context.targets.forEach(async (target) => { + + this.log(`Starting CopyPackageFiles with pattern ${stringPattern} on target '${target.tsconfigPath}'`); + + const files = await target.packages.reduce((p, pkg) => { + + const fileSourceRoot = resolve(source === "src" ? pkg.resolvedPkgSrcRoot : pkg.resolvedPkgOutRoot); + + return p.then(async (a) => { + + const temp = await (globby)(pattern, { + cwd: fileSourceRoot, + }); + + a.push(...temp.map(t => ({ + src: resolve(fileSourceRoot, t), + dest: resolve(pkg.resolvedPkgDistRoot, source === "built" ? pkg.relativePkgDistModulePath : "", t), + }))); + + return a; }); - - a.push(...temp.map(t => ({ - src: resolve(fileSourceRoot, t), - dest: resolve(pkg.resolvedPkgDistRoot, source === "built" ? pkg.relativePkgDistModulePath : "", t), - }))); - - return a; - }); - - }, Promise.resolve<{ src: string, dest: string }[]>([])); - - this.log(`CopyPackageFiles found ${files.length} files for pattern ${stringPattern} in target '${this.context.target.tsconfigPath}'`); - - await Promise.all(files.map(f => buildCopyFile(f.src, f.dest))); - - this.log(`Completing CopyPackageFiles with pattern ${stringPattern} on target '${this.context.target.tsconfigPath}'`); + + }, Promise.resolve<{ src: string, dest: string }[]>([])); + + this.log(`CopyPackageFiles found ${files.length} files for pattern ${stringPattern} in target '${target.tsconfigPath}'`); + + await Promise.all(files.map(f => buildCopyFile(f.src, f.dest))); + + this.log(`Completing CopyPackageFiles with pattern ${stringPattern} on target '${target.tsconfigPath}'`); + + }); }); return instance; diff --git a/tools/buildsystem/src/behaviors/create-resolution-package-files.ts b/tools/buildsystem/src/behaviors/create-resolution-package-files.ts index 7b14fe798..984ee29af 100644 --- a/tools/buildsystem/src/behaviors/create-resolution-package-files.ts +++ b/tools/buildsystem/src/behaviors/create-resolution-package-files.ts @@ -13,26 +13,29 @@ export function CreateResolutionPackageFiles(): TimelinePipe { return (instance: BuildTimeline) => { - instance.on.package(async function (this: BuildTimeline) { + instance.on.package(async function (this: BuildTimeline) { this.log("Creating Resolution package.json files.", 1); - const { target } = this.context; + const { targets } = this.context; const promises = []; - target.packages.forEach((pkg) => { + targets.forEach((target) => { - const filePath = resolve(pkg.resolvedPkgDistRoot, pkg.relativePkgDistModulePath, "package.json"); + target.packages.forEach((pkg) => { - let pkgFile = { - name: pkg.name, - type: /commonjs/i.test(target.parsedTSConfig.compilerOptions.module) ? "commonjs" : "module", - } + const filePath = resolve(pkg.resolvedPkgDistRoot, pkg.relativePkgDistModulePath, "package.json"); - this.log(`Writing module resolution package.json for ${filePath} as ${pkgFile.type}`, 0); + let pkgFile = { + name: pkg.name, + type: /commonjs/i.test(target.parsedTSConfig.compilerOptions.module) ? "commonjs" : "module", + } - promises.push(buildWriteFile(filePath, JSON.stringify(pkgFile))); + this.log(`Writing module resolution package.json for ${filePath} as ${pkgFile.type}`, 0); + + promises.push(buildWriteFile(filePath, JSON.stringify(pkgFile))); + }); }); await Promise.all(promises); diff --git a/tools/buildsystem/src/behaviors/publish-nightly.ts b/tools/buildsystem/src/behaviors/publish-nightly.ts index d1eb7cada..2b9560053 100644 --- a/tools/buildsystem/src/behaviors/publish-nightly.ts +++ b/tools/buildsystem/src/behaviors/publish-nightly.ts @@ -21,14 +21,14 @@ export function PublishNightly(flags: string[], nightlyName: "v3nightly" | "v4ni // this updates all the package.json versions to the nightly pattern instance.on.prePublish(async function (this: BuildTimeline) { - const { target } = this.context; + const { targets } = this.context; const date = new Date(); const versionStr = `-${nightlyName}.${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, "0")}${date.getDate().toString().padStart(2, "0")}`; this.log(`Updating nightly package.json version to ${versionStr}`); - await Promise.all(target.packages.map(pkg => { + await Promise.all(targets[0].packages.map(pkg => { const packageJsonPath = resolve(pkg.resolvedPkgDistRoot, "package.json"); const packageJson = importJSON(packageJsonPath); diff --git a/tools/buildsystem/src/behaviors/publish.ts b/tools/buildsystem/src/behaviors/publish.ts index 089a93f8f..39d8a8b40 100644 --- a/tools/buildsystem/src/behaviors/publish.ts +++ b/tools/buildsystem/src/behaviors/publish.ts @@ -10,11 +10,11 @@ export function Publish(flags?: string[]): TimelinePipe { instance.on.publish(async function (this: BuildTimeline) { - const { target } = this.context; + const { targets } = this.context; const promises: Promise[] = []; - target.packages.forEach(pkg => { + targets[0].packages.forEach(pkg => { promises.push(new Promise((resolve, reject) => { diff --git a/tools/buildsystem/src/behaviors/replace-version.ts b/tools/buildsystem/src/behaviors/replace-version.ts index 97a84b7fe..f61e82d7b 100644 --- a/tools/buildsystem/src/behaviors/replace-version.ts +++ b/tools/buildsystem/src/behaviors/replace-version.ts @@ -20,17 +20,20 @@ export function ReplaceVersion(paths: string[], options?: IReplaceVersionOptions instance.on.postBuild(async function (this: BuildTimeline) { - const { version, target } = this.context; + const { version, targets } = this.context; - this.log(`Replacing package version for target "${target.tsconfigPath}"`, 1); + targets.forEach((target) => { - paths.forEach(async (path) => { + this.log(`Replacing package version for target "${target.tsconfigPath}"`, 1); - const resolvedPath = options?.pathsResolved ? path : resolve(target.resolvedOutDir, path); - this.log(`Resolving path '${path}' to '${resolvedPath}'.`, 0); - const file = await readFile(resolve(resolvedPath)); - await buildWriteFile(resolvedPath, file.toString().replace(options.versionMask, version)); - }); + paths.forEach(async (path) => { + + const resolvedPath = options?.pathsResolved ? path : resolve(target.resolvedOutDir, path); + this.log(`Resolving path '${path}' to '${resolvedPath}'.`, 0); + const file = await readFile(resolve(resolvedPath)); + await buildWriteFile(resolvedPath, file.toString().replace(options.versionMask, version)); + }); + }); }); return instance; diff --git a/tools/buildsystem/src/behaviors/write-packagejson.ts b/tools/buildsystem/src/behaviors/write-packagejson.ts index 54fbf115d..5dfb52feb 100644 --- a/tools/buildsystem/src/behaviors/write-packagejson.ts +++ b/tools/buildsystem/src/behaviors/write-packagejson.ts @@ -10,11 +10,11 @@ export function WritePackageJSON(transform?: (p: any) => typeof p): TimelinePipe instance.on.postBuild(async function (this: BuildTimeline) { - const { version, target } = this.context; + const { version, targets } = this.context; const promises = []; - target.packages.forEach((pkg) => { + targets[0].packages.forEach((pkg) => { let pkgFile = importJSON(resolve(pkg.resolvedPkgSrcRoot, "package.json")); diff --git a/tools/buildsystem/src/build-timeline.ts b/tools/buildsystem/src/build-timeline.ts index 3d53cb886..92a96d94f 100644 --- a/tools/buildsystem/src/build-timeline.ts +++ b/tools/buildsystem/src/build-timeline.ts @@ -28,13 +28,8 @@ export class BuildTimeline extends Timeline { protected InternalResolve = Symbol.for("Queryable_Resolve"); protected InternalReject = Symbol.for("Queryable_Reject"); - constructor(templateTimeline?: BuildTimeline, protected context?: Partial) { + constructor(protected context?: Partial) { super(BuildMoments); - - if (typeof templateTimeline !== "undefined") { - this.observers = templateTimeline.observers; - this._inheritingObservers = true; - } } public start(): Promise { diff --git a/tools/buildsystem/src/types.ts b/tools/buildsystem/src/types.ts index eda33360f..be564a754 100644 --- a/tools/buildsystem/src/types.ts +++ b/tools/buildsystem/src/types.ts @@ -33,7 +33,7 @@ export interface IBuildContext { version: string; configName: string; distRoot: string; - target: { + targets: { tsconfigPath: string; tsconfigRoot: string; parsedTSConfig: TSConfig; @@ -46,5 +46,5 @@ export interface IBuildContext { resolvedPkgDistRoot: string; relativePkgDistModulePath: string; }[]; - }; + }[]; }