Skip to content

Commit

Permalink
feat(jsii): move sources to "src" and outputs to "lib"
Browse files Browse the repository at this point in the history
BREAKING CHANGE: sources in jsii projects are expected to be under `src` while `lib` is only `.d.ts` and `.js` files
  • Loading branch information
Elad Ben-Israel committed Jun 19, 2020
1 parent 5ab6608 commit 2333739
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 48 deletions.
76 changes: 72 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Name|Description
[Project](#projen-project)|*No description*
[Semver](#projen-semver)|*No description*
[TypeScriptLibraryProject](#projen-typescriptlibraryproject)|*No description*
[TypescriptConfig](#projen-typescriptconfig)|*No description*
[Version](#projen-version)|*No description*


Expand All @@ -41,6 +42,7 @@ Name|Description
[PeerDependencyOptions](#projen-peerdependencyoptions)|*No description*
[ProjectOptions](#projen-projectoptions)|*No description*
[TypeScriptLibraryProjectOptions](#projen-typescriptlibraryprojectoptions)|*No description*
[TypescriptConfigOptions](#projen-typescriptconfigoptions)|*No description*


**Enums**
Expand Down Expand Up @@ -312,9 +314,10 @@ new Jest(project: NodeProject, options?: JestOptions)
<span style="text-decoration: underline">Parameters:</span>
* **project** (<code>[NodeProject](#projen-nodeproject)</code>) *No description*
* **options** (<code>[JestOptions](#projen-jestoptions)</code>) *No description*
* **globalCoverageThreshold** (<code>[CoverageThreshold](#projen-coveragethreshold)</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **coverage** (<code>boolean</code>) Collect coverage. <span style="text-decoration: underline">*Default*</span>: true
* **coverageThreshold** (<code>[CoverageThreshold](#projen-coveragethreshold)</code>) Specify the global coverage thresholds. <span style="text-decoration: underline">*Optional*</span>
* **ignorePatterns** (<code>Array<string></code>) Defines `testPathIgnorePatterns` and `coveragePathIgnorePatterns`. <span style="text-decoration: underline">*Default*</span>: "/node_modules/"
* **typescript** (<code>boolean</code>) Typescript support:. <span style="text-decoration: underline">*Optional*</span>
* **typescript** (<code>[TypescriptConfig](#projen-typescriptconfig)</code>) Configure for typescript. <span style="text-decoration: underline">*Optional*</span>



Expand Down Expand Up @@ -368,12 +371,16 @@ new JsiiProject(options: JsiiProjectOptions)
* **eslint** (<code>boolean</code>) Install eslint. <span style="text-decoration: underline">*Default*</span>: true
* **java** (<code>[JsiiJavaTarget](#projen-jsiijavatarget)</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **jest** (<code>boolean</code>) Use jest for unit tests. <span style="text-decoration: underline">*Default*</span>: true
* **jestOptions** (<code>[JestOptions](#projen-jestoptions)</code>) Jest options. <span style="text-decoration: underline">*Default*</span>: defaults
* **jsiiVersion** (<code>[Semver](#projen-semver)</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **license** (<code>string</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **mergify** (<code>boolean</code>) Add mergify configuration. <span style="text-decoration: underline">*Default*</span>: true
* **outdir** (<code>string</code>) Compiler artifacts output directory. <span style="text-decoration: underline">*Default*</span>: "lib"
* **python** (<code>[JsiiPythonTarget](#projen-jsiipythontarget)</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **rootdir** (<code>string</code>) *No description* <span style="text-decoration: underline">*Default*</span>: "."
* **srcdir** (<code>string</code>) Typescript sources directory. <span style="text-decoration: underline">*Default*</span>: "src"
* **stability** (<code>string</code>) *No description* <span style="text-decoration: underline">*Optional*</span>
* **testdir** (<code>string</code>) Tests directory. <span style="text-decoration: underline">*Default*</span>: "test"


### Methods
Expand Down Expand Up @@ -945,6 +952,46 @@ new TypeScriptLibraryProject(options: TypeScriptLibraryProjectOptions)



## class TypescriptConfig 🔹 <a id="projen-typescriptconfig"></a>



<span style="text-decoration: underline">Implements</span>: [IConstruct](#constructs-iconstruct)
<span style="text-decoration: underline">Extends</span>: [Construct](#constructs-construct)

### Initializer




<span style="text-decoration: underline">Usage:</span>

```ts
new TypescriptConfig(project: Project, options?: TypescriptConfigOptions)
```

<span style="text-decoration: underline">Parameters:</span>
* **project** (<code>[Project](#projen-project)</code>) *No description*
* **options** (<code>[TypescriptConfigOptions](#projen-typescriptconfigoptions)</code>) *No description*
* **compilerOptions** (<code>any</code>) Compiler options to use. <span style="text-decoration: underline">*Default*</span>: see above
* **exclude** (<code>Array<string></code>) *No description* <span style="text-decoration: underline">*Default*</span>: node_modules is excluded by default
* **fileName** (<code>string</code>) *No description* <span style="text-decoration: underline">*Default*</span>: "tsconfig.json"
* **include** (<code>Array<string></code>) The directory in which typescript sources reside. <span style="text-decoration: underline">*Default*</span>: all .ts files recursively



### Properties


Name | Type | Description
-----|------|-------------
**compilerOptions**🔹 | <code>any</code> | <span></span>
**exclude**🔹 | <code>Array<string></code> | <span></span>
**fileName**🔹 | <code>string</code> | <span></span>
**include**🔹 | <code>Array<string></code> | <span></span>



## class Version 🔹 <a id="projen-version"></a>


Expand Down Expand Up @@ -1046,9 +1093,10 @@ Name | Type | Description

Name | Type | Description
-----|------|-------------
**globalCoverageThreshold**?🔹 | <code>[CoverageThreshold](#projen-coveragethreshold)</code> | <span style="text-decoration: underline">*Optional*</span>
**coverage**?🔹 | <code>boolean</code> | Collect coverage.<br/><span style="text-decoration: underline">*Default*</span>: true
**coverageThreshold**?🔹 | <code>[CoverageThreshold](#projen-coveragethreshold)</code> | Specify the global coverage thresholds.<br/><span style="text-decoration: underline">*Optional*</span>
**ignorePatterns**?🔹 | <code>Array<string></code> | Defines `testPathIgnorePatterns` and `coveragePathIgnorePatterns`.<br/><span style="text-decoration: underline">*Default*</span>: "/node_modules/"
**typescript**?🔹 | <code>boolean</code> | Typescript support:.<br/><span style="text-decoration: underline">*Optional*</span>
**typescript**?🔹 | <code>[TypescriptConfig](#projen-typescriptconfig)</code> | Configure for typescript.<br/><span style="text-decoration: underline">*Optional*</span>



Expand Down Expand Up @@ -1107,12 +1155,14 @@ Name | Type | Description
**eslint**?🔹 | <code>boolean</code> | Install eslint.<br/><span style="text-decoration: underline">*Default*</span>: true
**java**?🔹 | <code>[JsiiJavaTarget](#projen-jsiijavatarget)</code> | <span style="text-decoration: underline">*Optional*</span>
**jest**?🔹 | <code>boolean</code> | Use jest for unit tests.<br/><span style="text-decoration: underline">*Default*</span>: true
**jestOptions**?🔹 | <code>[JestOptions](#projen-jestoptions)</code> | Jest options.<br/><span style="text-decoration: underline">*Default*</span>: defaults
**jsiiVersion**?🔹 | <code>[Semver](#projen-semver)</code> | <span style="text-decoration: underline">*Optional*</span>
**keywords**?🔹 | <code>Array<string></code> | <span style="text-decoration: underline">*Optional*</span>
**license**?🔹 | <code>string</code> | <span style="text-decoration: underline">*Optional*</span>
**maxNodeVersion**?🔹 | <code>string</code> | Minimum node.js version to require via `engines` (inclusive).<br/><span style="text-decoration: underline">*Default*</span>: no max
**mergify**?🔹 | <code>boolean</code> | Add mergify configuration.<br/><span style="text-decoration: underline">*Default*</span>: true
**minNodeVersion**?🔹 | <code>string</code> | Node.js version to require via package.json `engines` (inclusive).<br/><span style="text-decoration: underline">*Default*</span>: no "engines" specified
**outdir**?🔹 | <code>string</code> | Compiler artifacts output directory.<br/><span style="text-decoration: underline">*Default*</span>: "lib"
**peerDependencies**?🔹 | <code>Map<string, [Semver](#projen-semver)></code> | <span style="text-decoration: underline">*Optional*</span>
**peerDependencyOptions**?🔹 | <code>[PeerDependencyOptions](#projen-peerdependencyoptions)</code> | <span style="text-decoration: underline">*Optional*</span>
**projenDevDependency**?🔹 | <code>boolean</code> | Indicates of "projen" should be installed as a devDependency.<br/><span style="text-decoration: underline">*Default*</span>: true
Expand All @@ -1121,7 +1171,9 @@ Name | Type | Description
**releaseToNpm**?🔹 | <code>boolean</code> | Automatically release to npm when new versions are introduced.<br/><span style="text-decoration: underline">*Default*</span>: true
**releaseWorkflow**?🔹 | <code>boolean</code> | Define a GitHub workflow for releasing from "master" when new versions are bumped.<br/><span style="text-decoration: underline">*Default*</span>: true
**rootdir**?🔹 | <code>string</code> | <span style="text-decoration: underline">*Default*</span>: "."
**srcdir**?🔹 | <code>string</code> | Typescript sources directory.<br/><span style="text-decoration: underline">*Default*</span>: "src"
**stability**?🔹 | <code>string</code> | <span style="text-decoration: underline">*Optional*</span>
**testdir**?🔹 | <code>string</code> | Tests directory.<br/><span style="text-decoration: underline">*Default*</span>: "test"
**workflowBootstrapSteps**?🔹 | <code>Array<any></code> | Workflow steps to use in order to bootstrap this repo.<br/><span style="text-decoration: underline">*Default*</span>: [ { run: `npx projen${PROJEN_VERSION}` }, { run: 'yarn install --frozen-lockfile' } ]
**workflowContainerImage**?🔹 | <code>string</code> | Container image to use for GitHub workflows.<br/><span style="text-decoration: underline">*Default*</span>: default image
**workflowNodeVersion**?🔹 | <code>string</code> | The node version to use in GitHub workflows.<br/><span style="text-decoration: underline">*Default*</span>: same as `minNodeVersion`
Expand Down Expand Up @@ -1321,6 +1373,22 @@ Name | Type | Description



## struct TypescriptConfigOptions 🔹 <a id="projen-typescriptconfigoptions"></a>






Name | Type | Description
-----|------|-------------
**compilerOptions**?🔹 | <code>any</code> | Compiler options to use.<br/><span style="text-decoration: underline">*Default*</span>: see above
**exclude**?🔹 | <code>Array<string></code> | <span style="text-decoration: underline">*Default*</span>: node_modules is excluded by default
**fileName**?🔹 | <code>string</code> | <span style="text-decoration: underline">*Default*</span>: "tsconfig.json"
**include**?🔹 | <code>Array<string></code> | The directory in which typescript sources reside.<br/><span style="text-decoration: underline">*Default*</span>: all .ts files recursively



## enum Stability 🔹 <a id="projen-stability"></a>


Expand Down
36 changes: 24 additions & 12 deletions lib/jest.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { Construct } from 'constructs';
import { NodeProject } from './node-project';
import { Semver } from './semver';
import { TypescriptConfig } from './typescript';

export interface JestOptions {

/**
* Specify the global coverage thresholds
* Collect coverage.
* @default true
*/
readonly coverage?: CoverageThreshold;
readonly coverage?: boolean;

/**
* Typescript support:
*
* - Uses `ts-jest` as a preprocessor and configuration preset.
* - Only matches *.ts files
* Specify the global coverage thresholds
*/
readonly typescript?: boolean;
readonly coverageThreshold?: CoverageThreshold;

/**
* Defines `testPathIgnorePatterns` and `coveragePathIgnorePatterns`
* @default "/node_modules/"
*/
readonly ignorePatterns?: string[];

/**
* Configure for typescript.
*/
readonly typescript?: TypescriptConfig;
}

export interface CoverageThreshold {
Expand Down Expand Up @@ -48,7 +53,7 @@ export class Jest extends Construct {

const config: any = {
clearMocks: true,
collectCoverage: true,
collectCoverage: options.coverage ?? true,
coveragePathIgnorePatterns: ignorePatterns,
testPathIgnorePatterns: ignorePatterns,
};
Expand All @@ -62,15 +67,23 @@ export class Jest extends Construct {
'**/?(*.)+(spec|test).ts?(x)',
];

// specify tsconfig.json
config.globals = {
'ts-jest': {
tsConfig: options.typescript.fileName,
},
}

// add relevant deps
project.addDevDependencies({
'@types/jest': Semver.caret('25.2.1'),
'ts-jest': Semver.caret('26.1.0'),
});;
});
}

if (options.coverage) {
if (options.coverageThreshold) {
config.coverageThreshold = {
global: options.coverage,
global: options.coverageThreshold,
};
}

Expand All @@ -82,6 +95,5 @@ export class Jest extends Construct {
});

project.addFields({ jest: config });

}
}
69 changes: 66 additions & 3 deletions lib/jsii-project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NodeProject, CommonOptions } from './node-project';
import { Semver } from './semver';
import { Eslint } from './eslint';
import { Jest } from './jest';
import { Jest, JestOptions } from './jest';
import { Mergify } from './mergify';
import { JsiiDocgen } from './jsii-docgen';
import { Lazy } from 'constructs';
import { TypescriptConfig } from './typescript';

const DEFAULT_JSII_VERSION = '1.6.0';
const DEFAULT_JSII_IMAGE = 'jsii/superchain';
Expand Down Expand Up @@ -49,6 +50,12 @@ export interface JsiiProjectOptions extends CommonOptions {
*/
readonly jest?: boolean;

/**
* Jest options
* @default - defaults
*/
readonly jestOptions?: JestOptions;

/**
* Add mergify configuration
* @default true
Expand All @@ -60,6 +67,27 @@ export interface JsiiProjectOptions extends CommonOptions {
* @default true
*/
readonly docgen?: boolean;

/**
* Compiler artifacts output directory
*
* @default "lib"
*/
readonly outdir?: string;

/**
* Typescript sources directory.
*
* @default "src"
*/
readonly srcdir?: string;

/**
* Tests directory.
*
* @default "test"
*/
readonly testdir?: string;
}

export enum Stability {
Expand Down Expand Up @@ -98,13 +126,17 @@ export class JsiiProject extends NodeProject {
...options,
});

const srcdir = options.srcdir ?? 'src';
const outdir = options.outdir ?? 'lib';
const testdir = options.testdir ?? 'test';

this.compileCommands = new Array<string>();;

if (!options.authorEmail && !options.authorUrl) {
throw new Error('at least "authorEmail" or "authorUrl" are required for jsii projects');
}

this.addFields({ types: 'lib/index.d.ts' });
this.addFields({ types: `${outdir}/index.d.ts` });

// this is an unhelpful warning
const jsiiFlags = '--silence-warnings=reserved-word';
Expand All @@ -131,9 +163,20 @@ export class JsiiProject extends NodeProject {
jsii: {
outdir: 'dist',
targets,
tsc: {
outDir: outdir,
rootDir: srcdir,
},
},
});

this.gitignore.exclude(`/${outdir}`);
this.gitignore.include(`/${srcdir}`);
this.gitignore.include(`/${testdir}`);
this.npmignore.include(`/${outdir}`);
this.npmignore.exclude(`/${srcdir}`);
this.npmignore.exclude(`/${testdir}`);

this.publishToNpm();

if (options.java) {
Expand Down Expand Up @@ -206,7 +249,27 @@ export class JsiiProject extends NodeProject {
}

if (options.jest ?? true) {
new Jest(this, { typescript: true });

// create a special tsconfig just file tests
const tsconfig = new TypescriptConfig(this, {
compilerOptions: {
outDir: outdir,
rootDir: srcdir,
},
fileName: 'tsconfig.jest.json',
include: [
`${srcdir}/**/*.ts`,
`${testdir}/**/*.ts`,
],
exclude: [
'node_modules',
],
});

new Jest(this, {
typescript: tsconfig,
...options.jestOptions,
});
}

const mergify = options.mergify ?? true;
Expand Down
Loading

0 comments on commit 2333739

Please sign in to comment.