Skip to content

Commit

Permalink
fix: Improve debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 14, 2020
1 parent 09ee668 commit d28d6e3
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 82 deletions.
2 changes: 2 additions & 0 deletions src/Artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default abstract class Artifact<T extends object = {}> {
return this.state === 'failed';
}

startup() {}

protected logWithSource(
message: string,
level: 'info' | 'warn' | 'error',
Expand Down
26 changes: 15 additions & 11 deletions src/BundleArtifact.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { isObject, Path, SettingMap, toArray } from '@boost/common';
import { createDebugger } from '@boost/debug';
import { createDebugger, Debugger } from '@boost/debug';
import hash from 'string-hash';
import { rollup, RollupCache } from 'rollup';
import Artifact from './Artifact';
import { NODE_SUPPORTED_VERSIONS, NPM_SUPPORTED_VERSIONS } from './constants';
import { getRollupConfig } from './rollup/config';
import { Format, BuildOptions, BundleBuild, Support, Platform } from './types';

const debug = createDebugger('packemon:bundle');

export default class BundleArtifact extends Artifact<BundleBuild> {
cache?: RollupCache;

Expand All @@ -21,6 +19,8 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
// Name of the output file without extension
outputName: string = '';

protected debug!: Debugger;

static generateBuild(
format: Format,
support: Support,
Expand Down Expand Up @@ -55,15 +55,19 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
};
}

startup() {
this.debug = createDebugger(['packemon', 'bundle', this.package.getSlug(), this.outputName]);
}

async cleanup(): Promise<void> {
debug('Cleaning %s bundle artifacts', this.outputName);
this.debug('Cleaning bundle artifacts');

// Visualizer stats
await this.removeFiles([this.package.project.root.append(this.getStatsFileName())]);
}

async build(options: BuildOptions): Promise<void> {
debug('Building %s bundle artifact with Rollup', this.outputName);
this.debug('Building bundle artifact with Rollup');

const features = this.package.getFeatureFlags();

Expand Down Expand Up @@ -93,7 +97,7 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
toArray(output).map(async (out, index) => {
const { originalFormat = 'lib', ...outOptions } = out;

debug('\tWriting %s output', originalFormat);
this.debug('- Writing `%s` output', originalFormat);

const result = await bundle.write(outOptions);

Expand Down Expand Up @@ -163,8 +167,6 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
}

protected addEnginesToPackageJson() {
debug('Adding `engines` to %s `package.json`', this.package.getName());

const pkg = this.package.packageJson;

// Update with the lowest supported node version
Expand All @@ -180,6 +182,8 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
.find((build) => build.platform === 'node');

if (nodeBuild) {
this.debug('Adding `engines` to `package.json`');

if (!pkg.engines) {
pkg.engines = {};
}
Expand All @@ -194,7 +198,7 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
}

protected addEntryPointsToPackageJson() {
debug('Adding entry points to %s `package.json`', this.package.getName());
this.debug('Adding entry points to `package.json`');

const pkg = this.package.packageJson;

Expand All @@ -219,8 +223,6 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
}

protected addExportsToPackageJson() {
debug('Adding `exports` to %s `package.json`', this.package.getName());

const pkg = this.package.packageJson;
const paths: SettingMap = {};
let hasLib = false;
Expand All @@ -243,6 +245,8 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
}

if (Object.keys(paths).length > 0) {
this.debug('Adding `exports` to `package.json`');

if (!pkg.exports) {
pkg.exports = {};
}
Expand Down
38 changes: 31 additions & 7 deletions src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
import fs from 'fs-extra';
import ts from 'typescript';
import { Memoize, Path, toArray } from '@boost/common';
import { createDebugger, Debugger } from '@boost/debug';
import Artifact from './Artifact';
import Project from './Project';
import PackageValidator from './PackageValidator';
import {
FeatureFlags,
PackageConfig,
BuildOptions,
PackemonPackage,
PackemonPackageConfig,
TSConfigStructure,
ValidateOptions,
} from './types';

export default class Package {
readonly artifacts: Artifact[] = [];

configs: PackageConfig[] = [];

readonly debug!: Debugger;

readonly packageJson: PackemonPackage;

readonly packageJsonPath: Path;
Expand All @@ -36,15 +37,20 @@ export default class Package {
this.path = path;
this.packageJsonPath = this.path.append('package.json');
this.packageJson = contents;
this.debug = createDebugger(['packemon', 'package', this.getSlug()]);
}

addArtifact(artifact: Artifact): Artifact {
this.artifacts.push(artifact);

artifact.startup();

return artifact;
}

async build(options: BuildOptions): Promise<void> {
this.debug('Building artifacts');

// Build artifacts in parallel
await Promise.all(
this.artifacts.map(async (artifact) => {
Expand Down Expand Up @@ -73,26 +79,32 @@ export default class Package {
}

async cleanup(): Promise<void> {
await Promise.all(this.artifacts.map((artifact) => artifact.cleanup()));
}
this.debug('Cleaning artifacts');

async validate(options: ValidateOptions): Promise<PackageValidator> {
return new PackageValidator(this.path, this.packageJson).validate(options);
await Promise.all(this.artifacts.map((artifact) => artifact.cleanup()));
}

getName(): string {
return this.packageJson.name;
}

@Memoize()
// eslint-disable-next-line complexity
getFeatureFlags(): FeatureFlags {
const flags: FeatureFlags = this.root ? {} : this.project.rootPackage.getFeatureFlags();
this.debug('Loading feature flags');

const flags: FeatureFlags =
this.root || !this.project.isWorkspacesEnabled()
? {}
: this.project.rootPackage.getFeatureFlags();

flags.workspaces = this.project.workspaces;

// React
if (this.hasDependency('react')) {
flags.react = true;

this.debug('- React');
}

// TypeScript
Expand All @@ -106,6 +118,12 @@ export default class Package {
flags.typescript = true;
flags.decorators = Boolean(tsConfig?.options.experimentalDecorators);
flags.strict = Boolean(tsConfig?.options.strict);

this.debug(
`- TypeScript (${flags.strict ? 'strict' : 'non-strict'}, ${
flags.decorators ? 'decorators' : 'non-decorators'
})`,
);
}

// Flow
Expand All @@ -117,11 +135,17 @@ export default class Package {
flowconfigPath.exists()
) {
flags.flow = true;

this.debug('- Flow');
}

return flags;
}

getSlug(): string {
return this.path.name(true);
}

hasDependency(name: string): boolean {
const pkg = this.packageJson;

Expand Down
Loading

0 comments on commit d28d6e3

Please sign in to comment.