Skip to content

Commit 9e41fbf

Browse files
committed
feat: enable support for typescript references
- add script to build typescript project references - reorg project layout for better typescript refs - switch to tsconfig.json - add copy-resources script
1 parent de90b26 commit 9e41fbf

File tree

390 files changed

+1288
-861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

390 files changed

+1288
-861
lines changed

.nycrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"examples/*/dist"
66
],
77
"exclude": [
8-
"packages/*/dist/test/",
8+
"packages/*/dist/__tests__/",
99
"packages/cli/test/",
10-
"examples/*/dist/test/",
10+
"examples/*/dist/__tests__/",
1111
"**/.sandbox/"
1212
],
1313
"extension": [

benchmark/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*/__tests__/

benchmark/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

benchmark/package.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
"performance",
99
"benchmark"
1010
],
11-
"main": "index.js",
11+
"types": "./dist/index.d.ts",
1212
"engines": {
1313
"node": ">=8.9"
1414
},
1515
"scripts": {
1616
"build": "lb-tsc es2017 --outDir dist",
17+
"build:resources": "lb-copy-resources",
1718
"clean": "lb-clean dist",
1819
"pretest": "npm run clean && npm run build",
19-
"test": "lb-mocha \"dist/test\"",
20+
"test": "lb-mocha \"dist/__tests__\"",
2021
"prestart": "npm run build",
2122
"benchmark:routing": "node ./dist/src/rest-routing/routing-table",
2223
"start": "node ."
@@ -26,14 +27,6 @@
2627
},
2728
"author": "",
2829
"license": "MIT",
29-
"files": [
30-
"README.md",
31-
"index.js",
32-
"index.d.ts",
33-
"dist/src",
34-
"dist/index*",
35-
"src"
36-
],
3730
"dependencies": {
3831
"@loopback/example-todo": "^1.2.2",
3932
"@loopback/openapi-spec-builder": "^1.0.1",

benchmark/test/benchmark.integration.ts renamed to benchmark/src/__tests__/benchmark.integration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import {expect} from '@loopback/testlab';
77
import * as request from 'request-promise-native';
88
import {Benchmark} from '..';
9-
import {Autocannon, EndpointStats} from '../src/autocannon';
9+
import {Autocannon, EndpointStats} from '../autocannon';
1010

1111
const debug = require('debug')('test');
1212

File renamed without changes.

benchmark/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

benchmark/tsconfig.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tsconfig",
3+
"extends": "../packages/build/config/tsconfig.common.json",
4+
"compilerOptions": {
5+
"rootDir": "src",
6+
"composite": true,
7+
"target": "es2017",
8+
"outDir": "dist"
9+
},
10+
"references": [
11+
{
12+
"path": "../packages/testlab/tsconfig.json"
13+
},
14+
{
15+
"path": "../examples/todo/tsconfig.json"
16+
},
17+
{
18+
"path": "../packages/openapi-spec-builder/tsconfig.json"
19+
},
20+
{
21+
"path": "../packages/rest/tsconfig.json"
22+
}
23+
],
24+
"include": [
25+
"src/**/*",
26+
"src/**/*.json"
27+
]
28+
}

bin/update-project-refs.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env node
2+
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
3+
// Node module: loopback-next
4+
// This file is licensed under the MIT License.
5+
// License text available at https://opensource.org/licenses/MIT
6+
7+
/**
8+
* This is an internal script to update TypeScript project references based on
9+
* lerna's local package dependencies.
10+
*
11+
* See https://www.typescriptlang.org/docs/handbook/project-references.html
12+
*/
13+
'use strict';
14+
15+
const path = require('path');
16+
const fs = require('fs');
17+
const util = require('util');
18+
const debug = require('debug')('loopback:build');
19+
const buildUtils = require('../packages/build/bin/utils');
20+
21+
const Project = require('@lerna/project');
22+
const PackageGraph = require('@lerna/package-graph');
23+
24+
const TSCONFIG = 'tsconfig.json';
25+
26+
async function updateReferences(options) {
27+
options = options || {};
28+
const dryRun = options.dryRun;
29+
const project = new Project(process.cwd());
30+
const packages = await project.getPackages();
31+
32+
const rootRefs = [];
33+
const graph = new PackageGraph(packages);
34+
35+
for (const p of graph.values()) {
36+
debug('Package %s', p.pkg.name);
37+
const pkgLocation = p.pkg.location;
38+
const tsconfigFile = path.join(pkgLocation, TSCONFIG);
39+
// Skip non-typescript packages
40+
if (!fs.existsSync(tsconfigFile)) {
41+
debug('Skipping non-TS package: %s', p.pkg.name);
42+
continue;
43+
}
44+
rootRefs.push({
45+
path: path.join(path.relative(project.rootPath, pkgLocation), TSCONFIG),
46+
});
47+
const tsconfig = require(tsconfigFile);
48+
const refs = [];
49+
for (const d of p.localDependencies.keys()) {
50+
const depPkg = graph.get(d);
51+
// Skip non-typescript packages
52+
if (!fs.existsSync(path.join(depPkg.pkg.location, TSCONFIG))) {
53+
debug('Skipping non-TS dependency: %s', depPkg.pkg.name);
54+
continue;
55+
}
56+
const relativePath = path.relative(pkgLocation, depPkg.pkg.location);
57+
refs.push({path: path.join(relativePath, TSCONFIG)});
58+
}
59+
tsconfig.compilerOptions = tsconfig.compilerOptions || {};
60+
// composite must be true for project refs
61+
tsconfig.compilerOptions.composite = true;
62+
// outDir & target have to be set in tsconfig instead of CLI for tsc -b
63+
tsconfig.compilerOptions.target =
64+
tsconfig.compilerOptions.target || buildUtils.getCompilationTarget();
65+
tsconfig.compilerOptions.outDir =
66+
tsconfig.compilerOptions.outDir ||
67+
buildUtils.getDistribution(tsconfig.compilerOptions.target);
68+
if (!tsconfig.include) {
69+
// To include ts/json files
70+
tsconfig.include = ['src/**/*', 'src/**/*.json'];
71+
}
72+
tsconfig.references = refs;
73+
74+
// Convert to JSON
75+
const tsconfigJson = JSON.stringify(tsconfig, null, 2);
76+
77+
if (!dryRun) {
78+
// Using `-f` to overwrite tsconfig.json
79+
fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'});
80+
debug('%s has been updated.', tsconfigFile);
81+
} else {
82+
// Otherwise write to console
83+
debug(tsconfigJson);
84+
console.log('%s', p.pkg.name);
85+
refs.forEach(r => console.log(' %s', r.path));
86+
}
87+
}
88+
89+
const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json');
90+
const rootTsconfig = require(rootTsconfigFile);
91+
rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {};
92+
rootTsconfig.compilerOptions.composite = true;
93+
rootTsconfig.references = rootRefs;
94+
95+
// Reset files/include/exclude. The root should use project references now.
96+
rootTsconfig.files = [];
97+
delete rootTsconfig.include;
98+
delete rootTsconfig.exclude;
99+
100+
// Convert to JSON
101+
const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2);
102+
if (!dryRun) {
103+
// Using `-f` to overwrite tsconfig.json
104+
fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', {
105+
encoding: 'utf-8',
106+
});
107+
debug('%s has been updated.', rootTsconfigFile);
108+
console.log('TypeScript project references have been updated.');
109+
} else {
110+
debug(rootTsconfigJson);
111+
console.log('\n%s', path.relative(project.rootPath, rootTsconfigFile));
112+
rootRefs.forEach(r => console.log(' %s', r.path));
113+
console.log(
114+
'\nThis is a dry-run. Please use -f option to update tsconfig files.',
115+
);
116+
}
117+
}
118+
119+
if (require.main === module) {
120+
const dryRun = process.argv[2] !== '-f';
121+
updateReferences({dryRun});
122+
}

docs/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
"engines": {
1010
"node": ">=8.9"
1111
},
12-
"files": [
13-
"**/*"
14-
],
1512
"keywords": [
1613
"LoopBack",
1714
"docs"

docs/site/Controllers.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ with a test to verify that the error is thrown properly.
242242
{% include code-caption.html content="test/integration/controllers/hello.controller.integration.ts" %}
243243

244244
```ts
245-
import {HelloController} from '../../../src/controllers';
246-
import {HelloRepository} from '../../../src/repositories';
245+
// test/integration/controllers/hello.controller.integration.ts
246+
import {HelloController} from '../../../controllers';
247+
import {HelloRepository} from '../../../repositories';
247248
import {testdb} from '../../fixtures/datasources/testdb.datasource';
248249
import {expect} from '@loopback/testlab';
249250
import {HttpErrors} from '@loopback/rest';

docs/site/Testing-your-application.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ helper method; for example:
129129
{% include code-caption.html content="test/helpers/database.helpers.ts" %}
130130

131131
```ts
132-
import {ProductRepository, CategoryRepository} from '../../src/repositories';
132+
import {ProductRepository, CategoryRepository} from '../../repositories';
133133
import {testdb} from '../fixtures/datasources/testdb.datasource';
134134

135135
export async function givenEmptyDatabase() {
@@ -413,8 +413,8 @@ detailed explanation.
413413

414414
```ts
415415
import {expect, sinon} from '@loopback/testlab';
416-
import {ProductRepository} from '../../../src/repositories';
417-
import {ProductController} from '../../../src/controllers';
416+
import {ProductRepository} from '../../../repositories';
417+
import {ProductController} from '../../../controllers';
418418

419419
describe('ProductController (unit)', () => {
420420
let repository: ProductRepository;
@@ -456,7 +456,7 @@ valid data to create a new model instance.
456456
{% include code-caption.html content="test/unit/models/person.model.unit.ts" %}
457457

458458
```ts
459-
import {Person} from '../../../src/models';
459+
import {Person} from '../../../models';
460460
import {givenPersonData} from '../../helpers/database.helpers';
461461
import {expect} from '@loopback/testlab';
462462

@@ -547,7 +547,7 @@ import {
547547
givenEmptyDatabase,
548548
givenCategory,
549549
} from '../../helpers/database.helpers';
550-
import {CategoryRepository} from '../../../src/repositories';
550+
import {CategoryRepository} from '../../../repositories';
551551
import {expect} from '@loopback/testlab';
552552
import {testdb} from '../../fixtures/datasources/testdb.datasource';
553553

@@ -580,8 +580,8 @@ ingredient.
580580
```ts
581581
import {expect} from '@loopback/testlab';
582582
import {givenEmptyDatabase, givenProduct} from '../../helpers/database.helpers';
583-
import {ProductController} from '../../../src/controllers';
584-
import {ProductRepository} from '../../../src/repositories';
583+
import {ProductController} from '../../../controllers';
584+
import {ProductRepository} from '../../../repositories';
585585
import {testdb} from '../../fixtures/datasources/testdb.datasource';
586586

587587
describe('ProductController (integration)', () => {
@@ -628,11 +628,8 @@ of the service proxy by invoking the provider. This helper should be typically
628628
invoked once before the integration test suite begins.
629629

630630
```ts
631-
import {
632-
GeoService,
633-
GeoServiceProvider,
634-
} from '../../src/services/geo.service.ts';
635-
import {GeoDataSource} from '../../src/datasources/geo.datasource.ts';
631+
import {GeoService, GeoServiceProvider} from '../../services/geo.service.ts';
632+
import {GeoDataSource} from '../../datasources/geo.datasource.ts';
636633

637634
describe('GeoService', () => {
638635
let service: GeoService;
@@ -652,7 +649,7 @@ instance:
652649

653650
```ts
654651
import {merge} from 'lodash';
655-
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';
652+
import * as GEO_CODER_CONFIG from '../datasources/geo.datasource.json';
656653

657654
function givenGeoService() {
658655
const config = merge({}, GEO_CODER_CONFIG, {

examples/hello-world/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*/__tests__/

examples/hello-world/index.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/hello-world/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/hello-world/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
"engines": {
77
"node": ">=8.9"
88
},
9+
"types": "./dist/index.d.ts",
910
"scripts": {
10-
"acceptance": "lb-mocha \"dist/test/acceptance/**/*.js\"",
11+
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
1112
"build:apidocs": "lb-apidocs",
1213
"build": "lb-tsc es2017 --outDir dist",
13-
"build:watch": "lb-tsc es2017 --outDir dist --watch",
14+
"build:resources": "lb-copy-resources",
15+
"build:watch": "lb-tsc --watch",
1416
"clean": "lb-clean *example-hello-world*.tgz dist package api-docs",
1517
"verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean",
1618
"lint": "npm run prettier:check && npm run tslint",
@@ -21,9 +23,9 @@
2123
"tslint": "lb-tslint",
2224
"tslint:fix": "npm run tslint -- --fix",
2325
"pretest": "npm run clean && npm run build",
24-
"test": "lb-mocha --allow-console-logs \"dist/test\"",
26+
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
2527
"posttest": "npm run lint",
26-
"test:dev": "lb-mocha --allow-console-logs dist/test/**/*.js && npm run posttest",
28+
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
2729
"prestart": "npm run build",
2830
"start": "node ."
2931
},

examples/hello-world/test/acceptance/application.acceptance.ts renamed to examples/hello-world/src/__tests__/acceptance/application.acceptance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
expect,
1010
givenHttpServerConfig,
1111
} from '@loopback/testlab';
12-
import {HelloWorldApplication} from '../../src/application';
12+
import {HelloWorldApplication} from '../../application';
1313

1414
describe('Application', () => {
1515
let app: HelloWorldApplication;

examples/hello-world/tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)