Skip to content

Commit

Permalink
new: Add new TypesArtifact for generating a single
Browse files Browse the repository at this point in the history
TS declaration."
  • Loading branch information
milesj committed Oct 13, 2020
1 parent 9064c42 commit 4a049e6
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ coverage/
build/
cjs/
dist/
dts/
esm/
lib/
mjs/
Expand Down
44 changes: 44 additions & 0 deletions api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
// "extends": "my-package/include/api-extractor-base.json"
"projectFolder": ".",
"mainEntryPointFilePath": "<projectFolder>/dts-build/index.d.ts",
"bundledPackages": [],
"compiler": {
"tsconfigFilePath": "<projectFolder>/tsconfig.json"
},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "<projectFolder>/dts/index.d.ts",
"omitTrimmingComments": true
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
},
"ae-missing-release-tag": {
"logLevel": "none"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"author": "Miles Johnson",
"license": "MIT",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"types": "./dts/index.d.ts",
"bin": "./bin/packemon.js",
"sideEffects": false,
"engines": {
Expand Down Expand Up @@ -76,11 +76,13 @@
"@boost/event": "^2.2.0",
"@boost/pipeline": "^2.1.3",
"@boost/terminal": "^2.1.0",
"@microsoft/api-extractor": "^7.10.4",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"babel-plugin-transform-dev": "^2.0.1",
"builtin-modules": "^3.1.0",
"execa": "^4.0.3",
"filesize": "^6.1.0",
"fs-extra": "^9.0.1",
"ink": "^3.0.7",
Expand Down
6 changes: 6 additions & 0 deletions src/Packemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Package from './Package';
import Project from './Project';
import Artifact from './Artifact';
import BundleArtifact from './BundleArtifact';
import TypesArtifact from './TypesArtifact';
import {
ArtifactFlags,
BrowserFormat,
Expand Down Expand Up @@ -81,6 +82,7 @@ export default class Packemon extends Contract<PackemonOptions> {
addExports: bool(),
checkLicenses: bool(),
concurrency: number(1).gte(1),
generateDeclaration: bool(),
skipPrivate: bool(),
timeout: number().gte(0),
};
Expand Down Expand Up @@ -189,6 +191,10 @@ export default class Packemon extends Contract<PackemonOptions> {
pkg.addArtifact(artifact);
});

if (this.options.generateDeclaration) {
pkg.addArtifact(new TypesArtifact(pkg));
}

this.onPackageUpdate.emit([pkg]);
});
}
Expand Down
64 changes: 64 additions & 0 deletions src/TypesArtifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import fs from 'fs-extra';
import execa from 'execa';
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor';
import Artifact from './Artifact';

export default class TypesArtifact extends Artifact {
async build(): Promise<void> {
this.result = {
stats: {},
time: 0,
};

const pkgPath = this.package.path;
const start = Date.now();

// Compile the current project to a DTS folder
await execa(
'tsc',
[
'--declaration',
'--declarationMap',
'--declarationDir',
'dts-build',
'--emitDeclarationOnly',
],
{
cwd: pkgPath.path(),
preferLocal: true,
},
);

// Combine all DTS files into a single file
const result = Extractor.invoke(
ExtractorConfig.loadFileAndPrepare(pkgPath.append('api-extractor.json').path()),
{
localBuild: process.env.NODE_ENV !== 'production',
showVerboseMessages: false,
},
);

if (!result.succeeded) {
console.error(
`Generated types completed with ${result.errorCount} errors and ${result.warningCount} warnings!`,
);
}

// Remove old build
await fs.remove(pkgPath.append('dts-build').path());

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

pack(): void {
this.package.contents.types = './dts/index.d.ts';
}

getLabel(): string {
return 'types';
}

getBuilds(): string[] {
return ['dts'];
}
}
4 changes: 4 additions & 0 deletions src/commands/Distribute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default class DistributeCommand extends Command<Options, Params> {
@Arg.Number('Number of builds to run in parallel')
concurrency: number = os.cpus().length;

@Arg.Flag('Generate a single TypeScript declaration for each package')
generateDeclaration: boolean = false;

@Arg.Flag('Skip `private` packages from being built')
skipPrivate: boolean = false;

Expand All @@ -37,6 +40,7 @@ export default class DistributeCommand extends Command<Options, Params> {
addExports: this.addExports,
checkLicenses: this.checkLicenses,
concurrency: this.concurrency,
generateDeclaration: this.generateDeclaration,
skipPrivate: this.skipPrivate,
timeout: this.timeout,
});
Expand Down
16 changes: 14 additions & 2 deletions src/components/ArtifactRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { Style } from '@boost/cli';
import Artifact from '../Artifact';
import { STATE_LABELS } from '../constants';
import BundleArtifact from '../BundleArtifact';
import BundleBuilds from './BundleBuilds';
import BundleBuild from './BundleBuild';
import Build from './Build';

export interface ArtifactRowProps {
artifact: Artifact;
Expand All @@ -19,7 +20,18 @@ export default function ArtifactRow({ artifact }: ArtifactRowProps) {
<Style type="default">{artifact.getLabel()}</Style>
</Box>

{artifact instanceof BundleArtifact && <BundleBuilds artifact={artifact} />}
{artifact.getBuilds().map((build) => {
const props = {
build,
state: artifact.state,
};

if (artifact instanceof BundleArtifact) {
return <BundleBuild key={build} {...props} stats={artifact.result?.stats[build]} />;
}

return <Build key={build} {...props} />;
})}

<Box marginLeft={2}>
{artifact.isComplete() ? (
Expand Down
24 changes: 24 additions & 0 deletions src/components/Build.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { Box } from 'ink';
import { Style } from '@boost/cli';
import { figures } from '@boost/terminal';
import { STATE_COLORS } from '../constants';
import { ArtifactState } from '../types';

export interface BuildProps {
build: string;
children?: React.ReactNode;
state?: ArtifactState;
}

export default function Build({ build, children, state = 'pending' }: BuildProps) {
return (
<Box marginLeft={1}>
<Style bold type={STATE_COLORS[state] || 'default'}>
{state === 'failed' ? figures.cross : figures.squareSmallFilled} {build.toLowerCase()}
</Style>

{children}
</Box>
);
}
16 changes: 16 additions & 0 deletions src/components/BundleBuild.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import fileSize from 'filesize';
import { Style } from '@boost/cli';
import Build, { BuildProps } from './Build';

export interface BundleBuildProps extends BuildProps {
stats?: { size: number };
}

export default function BundleBuild({ stats, ...props }: BundleBuildProps) {
return (
<Build {...props}>
{stats?.size && <Style type="muted">{` (${fileSize(stats.size)})`}</Style>}
</Build>
);
}
32 changes: 0 additions & 32 deletions src/components/BundleBuilds.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import Packemon from './Packemon';
import Package from './Package';
import Project from './Project';
import Artifact from './Artifact';
import BundleArtifact from './BundleArtifact';
import TypesArtifact from './TypesArtifact';
import { run } from './run';
import { getBabelInputConfig, getBabelOutputConfig } from './babel/config';

export * from './constants';
export * from './types';

export {
Artifact,
BundleArtifact,
TypesArtifact,
getBabelInputConfig,
getBabelOutputConfig,
Package,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface PackemonOptions {
addExports: boolean;
checkLicenses: boolean;
concurrency: number;
generateDeclaration: boolean;
skipPrivate: boolean;
timeout: number;
}
Expand Down
Loading

0 comments on commit 4a049e6

Please sign in to comment.