Skip to content

Commit

Permalink
internal: Rework so that artifacts control their flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 14, 2020
1 parent 17ddb4e commit 14fbda3
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 270 deletions.
59 changes: 39 additions & 20 deletions src/Artifact.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,64 @@
/* eslint-disable no-empty-function */

import Package from './Package';
import {
ArtifactFlags,
ArtifactState,
Awaitable,
BootOptions,
BuildOptions,
BuildResult,
PackOptions,
} from './types';
import { ArtifactFlags, ArtifactState, Awaitable, BuildResult, PackemonOptions } from './types';

export default abstract class Artifact<T = unknown> {
flags: ArtifactFlags = {};
readonly flags: ArtifactFlags;

package: Package;
readonly package: Package;

result?: BuildResult<T>;
readonly result: BuildResult<T> = {
stats: {},
time: 0,
};

state: ArtifactState = 'pending';

constructor(pkg: Package) {
constructor(pkg: Package, flags: ArtifactFlags = {}) {
this.package = pkg;
this.flags = flags;
}

async run(options: PackemonOptions): Promise<void> {
const start = Date.now();

try {
this.state = 'booting';
await this.boot(options);

this.state = 'building';
await this.build(options);

this.state = 'packing';
await this.pack(options);

this.state = 'passed';
} catch (error) {
this.state = 'failed';

if (error instanceof Error) {
console.error(error.message);
}
}

this.result.time = Date.now() - start;
}

boot(options: BootOptions): Awaitable {}
boot(options: PackemonOptions): Awaitable {}

build(options: BuildOptions): Awaitable {}
build(options: PackemonOptions): Awaitable {}

pack(options: PackOptions): Awaitable {}
pack(options: PackemonOptions): Awaitable {}

isComplete(): boolean {
return this.state === 'passed' || this.state === 'failed' || this.state === 'skipped';
return this.state === 'passed' || this.state === 'failed';
}

isRunning(): boolean {
return this.state === 'booting' || this.state === 'building' || this.state === 'packing';
}

shouldSkip(): boolean {
return this.state === 'skipped' || this.state === 'failed';
return this.state === 'failed';
}

abstract getLabel(): string;
Expand Down
49 changes: 12 additions & 37 deletions src/BundleArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Path, SettingMap, toArray } from '@boost/common';
import { rollup, RollupCache } from 'rollup';
import Artifact from './Artifact';
import { getRollupConfig } from './rollup/config';
import { Format, PackOptions, Platform } from './types';
import { Format, PackemonOptions, Platform } from './types';

export default class BundleArtifact extends Artifact<{ size: number }> {
cache?: RollupCache;
Expand All @@ -19,22 +19,7 @@ export default class BundleArtifact extends Artifact<{ size: number }> {
outputName: string = '';

async build(): Promise<void> {
const rollupConfig = getRollupConfig(this, this.package.getFeatureFlags());

// Skip build because of invalid config
if (!rollupConfig) {
this.state = 'skipped';

return;
}

this.result = {
stats: {},
time: 0,
};

const { output = [], ...input } = rollupConfig;
const start = Date.now();
const { output = [], ...input } = getRollupConfig(this, this.package.getFeatureFlags());
const bundle = await rollup(input);

if (bundle.cache) {
Expand All @@ -44,25 +29,16 @@ export default class BundleArtifact extends Artifact<{ size: number }> {
await Promise.all(
toArray(output).map(async (out) => {
const { originalFormat = 'lib', ...outOptions } = out;
const result = await bundle.write(outOptions);

try {
const result = await bundle.write(outOptions);

this.result!.stats[originalFormat] = {
size: Buffer.byteLength(result.output[0].code),
};
} catch (error) {
this.state = 'failed';

throw error;
}
this.result.stats[originalFormat] = {
size: Buffer.byteLength(result.output[0].code),
};
}),
);

this.result.time = Date.now() - start;
}

pack({ addExports }: PackOptions): void {
pack({ addExports }: PackemonOptions): void {
const pkg = this.package.contents;
const hasLib = this.formats.includes('lib');
const hasUmd = this.formats.includes('umd');
Expand Down Expand Up @@ -143,19 +119,18 @@ export default class BundleArtifact extends Artifact<{ size: number }> {
return this.package.config.platforms[0];
}

getInputPath(): Path | null {
getInputPath(): Path {
const inputPath = this.package.path.append(this.inputPath);

if (inputPath.exists()) {
return inputPath;
}

console.warn(
`Cannot find input "${this.inputPath}" for package "${this.package.getName()}".`,
'Skipping package.',
throw new Error(
`Cannot find input "${
this.inputPath
}" for package "${this.package.getName()}". Skipping package.`,
);

return null;
}

getOutputFile(format: Format): string {
Expand Down
52 changes: 11 additions & 41 deletions src/Package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-param-reassign, @typescript-eslint/member-ordering */
/* eslint-disable @typescript-eslint/member-ordering */

import fs from 'fs-extra';
import spdxLicenses from 'spdx-license-list';
Expand All @@ -7,29 +7,25 @@ import Artifact from './Artifact';
import Project from './Project';
import resolveTsConfig from './helpers/resolveTsConfig';
import {
ArtifactState,
Awaitable,
BootOptions,
BuildOptions,
FeatureFlags,
PackageConfig,
PackemonOptions,
PackemonPackage,
PackemonPackageConfig,
PackOptions,
} from './types';

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

config!: PackageConfig;

contents: PackemonPackage;
readonly contents: PackemonPackage;

jsonPath: Path;
readonly jsonPath: Path;

path: Path;
readonly path: Path;

project: Project;
readonly project: Project;

root: boolean = false;

Expand All @@ -56,24 +52,15 @@ export default class Package {
};
}

async boot(options: BootOptions): Promise<void> {
async run(options: PackemonOptions): Promise<void> {
if (options.checkLicenses) {
this.checkLicense();
}

await this.processArtifacts('booting', (artifact) => artifact.boot(options));
}

async build(options: BuildOptions): Promise<void> {
await this.processArtifacts('building', (artifact) => artifact.build(options));
}

async pack(options: PackOptions): Promise<void> {
await this.processArtifacts('packing', (artifact) => artifact.pack(options));
}
// Process artifacts in parallel
await Promise.all(this.artifacts.map((artifact) => artifact.run(options)));

async complete(): Promise<void> {
await this.processArtifacts('passed');
// Sync `package.json` in case it was modified
await this.syncJsonFile();
}

Expand Down Expand Up @@ -154,23 +141,6 @@ export default class Package {
}
}

protected async processArtifacts(
state: ArtifactState,
callback?: (artifact: Artifact) => Awaitable,
) {
await Promise.all(
this.artifacts
.filter((artifact) => !artifact.shouldSkip())
.map(async (artifact) => {
artifact.state = state;

if (callback) {
await callback(artifact);
}
}),
);
}

protected async syncJsonFile() {
await fs.writeJson(this.jsonPath.path(), this.contents, { spaces: 2 });
}
Expand Down
Loading

0 comments on commit 14fbda3

Please sign in to comment.