Skip to content

Commit

Permalink
chore: bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jan 15, 2025
1 parent 7cd792c commit 6f7190e
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 64 deletions.
28 changes: 18 additions & 10 deletions packages/@aws-cdk/toolkit/.npmignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# Ignore artifacts
# Ignore build artifacts
**/cdk.out
**/*.snapshot
dist
.LAST_PACKAGE
.LAST_BUILD
*.snk
*.ts
!*.d.ts
!*.js
coverage
.nyc_output
*.tgz

# Ignore configs and test files
# Ignore config files
.eslintrc.js
tsconfig.json
*.tsbuildinfo
junit.xml
jest.config.js
bundle.mjs

# Include .jsii
!.jsii

# exclude cdk artifacts
**/cdk.out
**/*.snapshot
# Explicitly allow all required files
!build-info.json
!db.json.gz
# !lib/main.js
# !lib/bridge.js
# !lib/setup-sandbox.js
# !lib/api/bootstrap/bootstrap-template.yaml
!*.d.ts
!*.d.ts.map
!*.js
!LICENSE
!NOTICE
!THIRD_PARTY_LICENSES
44 changes: 44 additions & 0 deletions packages/@aws-cdk/toolkit/bundle.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createRequire } from 'node:module';
import * as path from "node:path";
import * as esbuild from "esbuild";
import * as fs from "fs-extra";

const require = createRequire(import.meta.url);

const cliPackage = path.dirname(require.resolve("aws-cdk/package.json"));
let copyFromCli = (from, to = undefined) => {
return fs.copy(path.join(cliPackage, ...from), path.join(process.cwd(), ...(to ?? from)))
}

await Promise.all([
copyFromCli(["build-info.json"]),
copyFromCli(["/db.json.gz"]),
copyFromCli(["lib", "index_bg.wasm"]),
])

// # Copy all resources that aws_cdk/generate.sh produced, and some othersCall the generator for the
// cp -R $aws_cdk/lib/init-templates ./lib/
// mkdir -p ./lib/api/bootstrap/ && cp $aws_cdk/lib/api/bootstrap/bootstrap-template.yaml ./lib/api/bootstrap/


let bundleCli = {
name: "bundle-aws-cdk",
setup(build) {

// Mark all paths inside aws-cdk as internal
build.onResolve({ filter: /^aws-cdk\/lib/ }, (args) => {
return { path: require.resolve(args.path), external: false }
});
},
};

await esbuild.build({
entryPoints: ["lib/index.ts"],
target: "node18",
platform: "node",
packages: "external",
plugins: [bundleCli],
sourcemap: true,
bundle: true,
outfile: "lib/main.js",
});
4 changes: 1 addition & 3 deletions packages/@aws-cdk/toolkit/lib/actions/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Deployments } from 'aws-cdk/lib/api/deployments';
import { StackActivityProgress } from 'aws-cdk/lib/api/util/cloudformation/stack-activity-monitor';
import { WorkGraph } from 'aws-cdk/lib/util/work-graph';
import { Deployments, StackActivityProgress, WorkGraph } from '../api/aws-cdk';
import { StackSelector } from '../api/cloud-assembly/stack-selector';

export type DeploymentMethod = DirectDeploymentMethod | ChangeSetDeploymentMethod;
Expand Down
27 changes: 27 additions & 0 deletions packages/@aws-cdk/toolkit/lib/api/aws-cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable import/no-extraneous-dependencies */
export { DEFAULT_TOOLKIT_STACK_NAME, SdkProvider } from 'aws-cdk/lib';
export type { SuccessfulDeployStackResult } from 'aws-cdk/lib';
export { formatSdkLoggerContent } from 'aws-cdk/lib/api/aws-auth/sdk-logger';
export { CloudAssembly, sanitizePatterns, StackCollection, ExtendedStackSelection } from 'aws-cdk/lib/api/cxapp/cloud-assembly';
export { prepareDefaultEnvironment, prepareContext, spaceAvailableForContext } from 'aws-cdk/lib/api/cxapp/exec';
export { Deployments } from 'aws-cdk/lib/api/deployments';
export { HotswapMode } from 'aws-cdk/lib/api/hotswap/common';
export { StackActivityProgress } from 'aws-cdk/lib/api/util/cloudformation/stack-activity-monitor';
export { RWLock } from 'aws-cdk/lib/api/util/rwlock';
export type { ILock } from 'aws-cdk/lib/api/util/rwlock';
export { formatTime } from 'aws-cdk/lib/api/util/string-manipulation';
export * as contextproviders from 'aws-cdk/lib/context-providers';
export { ResourceMigrator } from 'aws-cdk/lib/migrator';
export { obscureTemplate, serializeStructure } from 'aws-cdk/lib/serialize';
export { Context, Settings, PROJECT_CONTEXT } from 'aws-cdk/lib/settings';
export { tagsForStack } from 'aws-cdk/lib/tags';
export { CliIoHost } from 'aws-cdk/lib/toolkit/cli-io-host';
export { loadTree, some } from 'aws-cdk/lib/tree';
export { splitBySize } from 'aws-cdk/lib/util';
export { validateSnsTopicArn } from 'aws-cdk/lib/util/validate-notification-arn';
export { WorkGraph } from 'aws-cdk/lib/util/work-graph';
export type { Concurrency } from 'aws-cdk/lib/util/work-graph';
export { WorkGraphBuilder } from 'aws-cdk/lib/util/work-graph-builder';
export type { AssetBuildNode, AssetPublishNode, StackNode } from 'aws-cdk/lib/util/work-graph-types';
export { versionNumber } from 'aws-cdk/lib/version';
export { guessExecutable } from 'aws-cdk/lib/api/cxapp/exec';
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { MissingContext } from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import * as contextproviders from 'aws-cdk/lib/context-providers';
import { Context, PROJECT_CONTEXT } from 'aws-cdk/lib/settings';
import { Context, contextproviders, PROJECT_CONTEXT } from '../../aws-cdk';
import { ToolkitError } from '../../errors';
import { ActionAwareIoHost, debug } from '../../io/private';
import { ToolkitServices } from '../../toolkit/private';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import * as os from 'node:os';
import * as path from 'node:path';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { prepareDefaultEnvironment as oldPrepare, prepareContext, spaceAvailableForContext } from 'aws-cdk/lib/api/cxapp/exec';
import { Settings } from 'aws-cdk/lib/settings';
import { loadTree, some } from 'aws-cdk/lib/tree';
import { splitBySize } from 'aws-cdk/lib/util';
import { versionNumber } from 'aws-cdk/lib/version';
import * as fs from 'fs-extra';
import { lte } from 'semver';
import type { AppSynthOptions } from './source-builder';
import { prepareDefaultEnvironment as oldPrepare, prepareContext, spaceAvailableForContext, Settings, loadTree, some, splitBySize, versionNumber } from '../../../api/aws-cdk';
import { ToolkitError } from '../../errors';
import { ActionAwareIoHost, asLogger, error } from '../../io/private';
import { ToolkitServices } from '../../toolkit/private';

export { guessExecutable } from 'aws-cdk/lib/api/cxapp/exec';
export { guessExecutable } from '../../../api/aws-cdk';

type Env = { [key: string]: string };
type Context = { [key: string]: any };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as cxapi from '@aws-cdk/cx-api';
import { ILock, RWLock } from 'aws-cdk/lib/api/util/rwlock';
import { Context } from 'aws-cdk/lib/settings';

import * as fs from 'fs-extra';
import type { ICloudAssemblySource } from '../';
import { ContextAwareCloudAssembly, ContextAwareCloudAssemblyProps } from './context-aware-source';
import { execInChildProcess } from './exec';
import { assemblyFromDirectory, changeDir, guessExecutable, prepareDefaultEnvironment, withContext, withEnv } from './prepare-source';
import { Context, ILock, RWLock } from '../../aws-cdk';
import { ToolkitError } from '../../errors';
import { debug } from '../../io/private';
import { ToolkitServices } from '../../toolkit/private';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cxapi from '@aws-cdk/cx-api';
import { CloudAssembly, sanitizePatterns, StackCollection, ExtendedStackSelection as CliExtendedStackSelection } from 'aws-cdk/lib/api/cxapp/cloud-assembly';
import { major } from 'semver';
import { CloudAssembly, sanitizePatterns, StackCollection, ExtendedStackSelection as CliExtendedStackSelection } from '../aws-cdk';
import { ExtendedStackSelection, StackSelectionStrategy, StackSelector } from './stack-selector';
import { ICloudAssemblySource } from './types';
import { ToolkitError } from '../errors';
Expand Down
28 changes: 22 additions & 6 deletions packages/@aws-cdk/toolkit/lib/api/errors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const TOOLKIT_ERROR_SYMBOL = Symbol.for('@aws-cdk/core.ToolkitError');
const AUTHENTICATION_ERROR_SYMBOL = Symbol.for('@aws-cdk/core.AuthenticationError');
const TOOLKIT_ERROR_SYMBOL = Symbol.for('@aws-cdk/toolkit.ToolkitError');
const AUTHENTICATION_ERROR_SYMBOL = Symbol.for('@aws-cdk/toolkit.AuthenticationError');
const ASSEMBLY_ERROR_SYMBOL = Symbol.for('@aws-cdk/toolkit.AssemblyError');

/**
* Represents a general toolkit error in the AWS CDK Toolkit.
*/
class ToolkitError extends Error {
export class ToolkitError extends Error {
/**
* Determines if a given error is an instance of ToolkitError.
*/
public static isToolkitError(x: any): x is ToolkitError {
return x !== null && typeof(x) === 'object' && TOOLKIT_ERROR_SYMBOL in x;
}

/**
* Determines if a given error is an instance of AssemblyError.
*/
public static isAssemblyError(x: any): x is AssemblyError {
return this.isToolkitError(x) && ASSEMBLY_ERROR_SYMBOL in x;
}

/**
* Determines if a given error is an instance of AuthenticationError.
*/
Expand All @@ -36,13 +44,21 @@ class ToolkitError extends Error {
/**
* Represents an authentication-specific error in the AWS CDK Toolkit.
*/
class AuthenticationError extends ToolkitError {
export class AuthenticationError extends ToolkitError {
constructor(message: string) {
super(message, 'authentication');
Object.setPrototypeOf(this, AuthenticationError.prototype);
Object.defineProperty(this, AUTHENTICATION_ERROR_SYMBOL, { value: true });
}
}

// Export classes for internal usage only
export { ToolkitError, AuthenticationError };
/**
* Represents an authentication-specific error in the AWS CDK Toolkit.
*/
export class AssemblyError extends ToolkitError {
constructor(message: string) {
super(message, 'assembly');
Object.setPrototypeOf(this, AssemblyError.prototype);
Object.defineProperty(this, ASSEMBLY_ERROR_SYMBOL, { value: true });
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/toolkit/lib/api/io/private/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as util from 'node:util';
import type { Logger } from '@smithy/types';
import { formatSdkLoggerContent } from 'aws-cdk/lib/api/aws-auth/sdk-logger';
import type { IIoHost, IoMessage, IoMessageCodeCategory, IoMessageLevel, IoRequest } from '../io-host';
import { debug, error, info, messageCode, trace, warn } from './messages';
import { ActionAwareIoHost } from './types';
import type { ToolkitAction } from '../../../toolkit';
import { formatSdkLoggerContent } from '../../aws-cdk';

export function withAction(ioHost: IIoHost, action: ToolkitAction) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/toolkit/lib/api/io/private/timer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatTime } from 'aws-cdk/lib/api/util/string-manipulation';
import { formatTime } from '../../aws-cdk';

/**
* Helper class to measure the time of code.
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/toolkit/lib/api/toolkit/private/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SdkProvider } from 'aws-cdk/lib';

import { SdkProvider } from '../../aws-cdk';
import { ActionAwareIoHost } from '../../io/private';

/**
Expand Down
27 changes: 12 additions & 15 deletions packages/@aws-cdk/toolkit/lib/toolkit.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@

import * as path from 'node:path';
import * as cxapi from '@aws-cdk/cx-api';
import { DEFAULT_TOOLKIT_STACK_NAME, SdkProvider, SuccessfulDeployStackResult } from 'aws-cdk/lib/api';
import { StackCollection } from 'aws-cdk/lib/api/cxapp/cloud-assembly';
import { Deployments } from 'aws-cdk/lib/api/deployments';
import { HotswapMode } from 'aws-cdk/lib/api/hotswap/common';
import { StackActivityProgress } from 'aws-cdk/lib/api/util/cloudformation/stack-activity-monitor';
import { ResourceMigrator } from 'aws-cdk/lib/migrator';
import { obscureTemplate, serializeStructure } from 'aws-cdk/lib/serialize';
import { tagsForStack } from 'aws-cdk/lib/tags';
import { CliIoHost } from 'aws-cdk/lib/toolkit/cli-io-host';
import { validateSnsTopicArn } from 'aws-cdk/lib/util/validate-notification-arn';
import { Concurrency } from 'aws-cdk/lib/util/work-graph';
import { WorkGraphBuilder } from 'aws-cdk/lib/util/work-graph-builder';
import { AssetBuildNode, AssetPublishNode, StackNode } from 'aws-cdk/lib/util/work-graph-types';
import * as chalk from 'chalk';
import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
Expand All @@ -25,6 +12,7 @@ import { RollbackOptions } from './actions/rollback';
import { SynthOptions } from './actions/synth';
import { patternsArrayForWatch, WatchOptions } from './actions/watch';
import { SdkOptions } from './api/aws-auth';
import { DEFAULT_TOOLKIT_STACK_NAME, SdkProvider, SuccessfulDeployStackResult, StackCollection, Deployments, HotswapMode, StackActivityProgress, ResourceMigrator, obscureTemplate, serializeStructure, tagsForStack, CliIoHost, validateSnsTopicArn, Concurrency, WorkGraphBuilder, AssetBuildNode, AssetPublishNode, StackNode } from './api/aws-cdk';
import { CachedCloudAssemblySource, IdentityCloudAssemblySource, StackAssembly, ICloudAssemblySource } from './api/cloud-assembly';
import { CloudAssemblySourceBuilder } from './api/cloud-assembly/private/source-builder';
import { StackSelectionStrategy } from './api/cloud-assembly/stack-selector';
Expand Down Expand Up @@ -76,7 +64,7 @@ export interface ToolkitOptions {
/**
* The AWS CDK Programmatic Toolkit
*/
export class Toolkit extends CloudAssemblySourceBuilder {
export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposable {
/**
* The toolkit stack name used for bootstrapping resources.
*/
Expand All @@ -88,7 +76,7 @@ export class Toolkit extends CloudAssemblySourceBuilder {
private readonly ioHost: IIoHost;
private _sdkProvider?: SdkProvider;

public constructor(private readonly props: ToolkitOptions) {
public constructor(private readonly props: ToolkitOptions = {}) {
super();

// @todo open ioHost up
Expand All @@ -97,6 +85,15 @@ export class Toolkit extends CloudAssemblySourceBuilder {

this.toolkitStackName = props.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
}

public async dispose(): Promise<void> {
// nothing to do yet
}

public async [Symbol.asyncDispose](): Promise<void> {
await this.dispose();
}

/**
* Access to the AWS SDK
*/
Expand Down
Loading

0 comments on commit 6f7190e

Please sign in to comment.