Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: let TypeScript do resource copying #1824

Merged
merged 3 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/site/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ context.
```ts
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./redis.datasource.json');
import * as config from './redis.datasource.json';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to use TS import for json files.


export class RedisDataSource extends juggler.DataSource {
static dataSourceName = 'redis';
Expand Down
3 changes: 1 addition & 2 deletions docs/site/Testing-your-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ instance:

```ts
import {merge} from 'lodash';

const GEO_CODER_CONFIG = require('../src/datasources/geo.datasource.json');
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';

function givenGeoService() {
const config = merge({}, GEO_CODER_CONFIG, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./calculator.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './calculator.datasource.json';

export class CalculatorDataSource extends juggler.DataSource {
static dataSourceName = 'calculator';

constructor(
@inject('datasources.config.calculator', {optional: true})
dsConfig: AnyObject = config,
dsConfig: object = config,
) {
dsConfig = Object.assign({}, dsConfig, {
// A workaround for the current design flaw where inside our monorepo,
Expand Down
6 changes: 3 additions & 3 deletions examples/todo-list/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {juggler, DataSource} from '@loopback/repository';
const config = require('./db.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';

constructor(
@inject('datasources.config.db', {optional: true})
dsConfig: DataSource = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/todo/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {juggler, DataSource} from '@loopback/repository';
const config = require('./db.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';

constructor(
@inject('datasources.config.db', {optional: true})
dsConfig: DataSource = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/src/datasources/geocoder.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./geocoder.datasource.json');
import * as config from './geocoder.datasource.json';

export class GeocoderDataSource extends juggler.DataSource {
static dataSourceName = 'geocoder';
Expand Down
3 changes: 1 addition & 2 deletions examples/todo/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {merge} from 'lodash';
import * as path from 'path';
import {Todo} from '../src/models/index';
import {GeoPoint} from '../src/services/geocoder.service';
import * as GEO_CODER_CONFIG from '../src/datasources/geocoder.datasource.json';

/*
==============================================================================
Expand Down Expand Up @@ -54,8 +55,6 @@ export const aLocation = {
},
};

const GEO_CODER_CONFIG = require('../src/datasources/geocoder.datasource.json');

export function getProxiedGeoCoderConfig(proxy: HttpCachingProxy) {
return merge({}, GEO_CODER_CONFIG, {
options: {
Expand Down
6 changes: 3 additions & 3 deletions packages/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ Now you run the scripts, such as:

- The following un-official compiler options are available:

| Option | Description |
| -------------------- | ----------------------------------- |
| `--ignore-resources` | Do not copy any resources to outDir |
| Option | Description |
| ------------------ | ------------------------------------------------------------------------------------------------- |
| `--copy-resources` | Copy all non-typescript files from `src` and `test` to `outDir`, preserving their relative paths. |

- lb-tslint

Expand Down
14 changes: 7 additions & 7 deletions packages/build/bin/compile-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ function run(argv, options) {
const isTargetSet = utils.isOptionSet(compilerOpts, '--target');
const isOutDirSet = utils.isOptionSet(compilerOpts, '--outDir');
const isProjectSet = utils.isOptionSet(compilerOpts, '-p', '--project');
const isIgnoreResourcesSet = utils.isOptionSet(
const isCopyResourcesSet = utils.isOptionSet(
compilerOpts,
'--ignore-resources',
'--copy-resources',
);

var target;

// --ignore-resources is not a TS Compiler option so we remove it from the
// --copy-resources is not a TS Compiler option so we remove it from the
// list of compiler options to avoid compiler errors.
if (isIgnoreResourcesSet) {
compilerOpts.splice(compilerOpts.indexOf('--ignore-resources'), 1);
if (isCopyResourcesSet) {
compilerOpts.splice(compilerOpts.indexOf('--copy-resources'), 1);
}

if (!isTargetSet) {
Expand Down Expand Up @@ -128,9 +128,9 @@ function run(argv, options) {
args.push('--outDir', path.relative(cwd, outDir));

// Since outDir is set, ts files are compiled into that directory.
// If ignore-resources flag is not passed, copy resources (non-ts files)
// If copy-resources flag is passed, copy resources (non-ts files)
// to the same outDir as well.
if (rootDir && tsConfigFile && !isIgnoreResourcesSet) {
if (rootDir && tsConfigFile && isCopyResourcesSet) {
const tsConfig = require(tsConfigFile);
const dirs = tsConfig.include
? tsConfig.include.join('|')
Expand Down
1 change: 1 addition & 0 deletions packages/build/config/tsconfig.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"experimentalDecorators": true,
"noImplicitAny": true,
"strictNullChecks": true,
"resolveJsonModule": true,

"lib": ["es2018", "dom", "esnext.asynciterable"],
"module": "commonjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./<%= jsonFileName %>');
import {juggler} from '@loopback/repository';
import * as config from './<%= jsonFileName %>';

export class <%= className %>DataSource extends juggler.DataSource {
static dataSourceName = '<%= name %>';

constructor(
@inject('datasources.config.<%= name %>', {optional: true})
dsConfig: AnyObject = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ function checkBasicDataSourceFiles() {
assert.fileContent(expectedTSFile, /import {inject} from '@loopback\/core';/);
assert.fileContent(
expectedTSFile,
/import {juggler, AnyObject} from '@loopback\/repository';/,
/import {juggler} from '@loopback\/repository';/,
);
assert.fileContent(
expectedTSFile,
/const config = require\('.\/ds.datasource.json'\)/,
/import \* as config from '.\/ds.datasource.json';/,
);
assert.fileContent(
expectedTSFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,14 @@ describe('HttpServer (integration)', () => {
host?: string;
}): HttpServer {
const options: HttpServerOptions = {protocol: 'https', host};
const certDir = path.resolve(__dirname, '../../../fixtures');
Copy link
Contributor

@raymondfeng raymondfeng Oct 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what annoys me.The directory structure is different between source code and the distribution. In this case, developers can be confused as they might assume ../../../fixtures resolve to packages/fixtures based on the source code directory chain.

IMO, we must fix this confusion in #1636 to make sure the relative path to project level files such as package.json or fixtures stays the same between source code and distribution.

if (usePfx) {
const pfxPath = path.join(__dirname, 'pfx.pfx');
const pfxPath = path.join(certDir, 'pfx.pfx');
options.pfx = fs.readFileSync(pfxPath);
options.passphrase = 'loopback4';
} else {
const keyPath = path.join(__dirname, 'key.pem');
const certPath = path.join(__dirname, 'cert.pem');
const keyPath = path.join(certDir, 'key.pem');
const certPath = path.join(certDir, 'cert.pem');
options.key = fs.readFileSync(keyPath);
options.cert = fs.readFileSync(certPath);
}
Expand Down
File renamed without changes.
File renamed without changes.
30 changes: 16 additions & 14 deletions packages/rest/test/integration/rest.server.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as path from 'path';
import * as fs from 'fs';
import {RestServerConfig} from '../..';

const FIXTURES = path.resolve(__dirname, '../../../fixtures');

describe('RestServer (integration)', () => {
it('exports url property', async () => {
// Explicitly setting host to IPv4 address so test runs on Travis
Expand Down Expand Up @@ -79,7 +81,7 @@ describe('RestServer (integration)', () => {
});

it('does not allow static assets to be mounted at /', async () => {
const root = path.join(__dirname, 'fixtures');
const root = FIXTURES;
const server = await givenAServer({
rest: {
port: 0,
Expand Down Expand Up @@ -112,7 +114,7 @@ describe('RestServer (integration)', () => {
});

it('allows static assets via api', async () => {
const root = path.join(__dirname, 'fixtures');
const root = FIXTURES;
const server = await givenAServer({
rest: {
port: 0,
Expand All @@ -130,7 +132,7 @@ describe('RestServer (integration)', () => {
});

it('allows static assets via api after start', async () => {
const root = path.join(__dirname, 'fixtures');
const root = FIXTURES;
const server = await givenAServer({
rest: {
port: 0,
Expand All @@ -148,7 +150,7 @@ describe('RestServer (integration)', () => {
});

it('allows non-static routes after assets', async () => {
const root = path.join(__dirname, 'fixtures');
const root = FIXTURES;
const server = await givenAServer({
rest: {
port: 0,
Expand All @@ -163,7 +165,7 @@ describe('RestServer (integration)', () => {
});

it('serve static assets if matches before other routes', async () => {
const root = path.join(__dirname, 'fixtures');
const root = FIXTURES;
const server = await givenAServer({
rest: {
port: 0,
Expand Down Expand Up @@ -492,8 +494,8 @@ paths:
});

it('supports HTTPS protocol with key and certificate files', async () => {
const keyPath = path.join(__dirname, 'key.pem');
const certPath = path.join(__dirname, 'cert.pem');
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const options = {
port: 0,
protocol: 'https',
Expand All @@ -510,7 +512,7 @@ paths:
});

it('supports HTTPS protocol with a pfx file', async () => {
const pfxPath = path.join(__dirname, 'pfx.pfx');
const pfxPath = path.join(FIXTURES, 'pfx.pfx');
const options = {
port: 0,
protocol: 'https',
Expand All @@ -528,8 +530,8 @@ paths:
});

itSkippedOnTravis('handles IPv6 loopback address in HTTPS', async () => {
const keyPath = path.join(__dirname, 'key.pem');
const certPath = path.join(__dirname, 'cert.pem');
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const server = await givenAServer({
rest: {
port: 0,
Expand All @@ -549,8 +551,8 @@ paths:

// https://github.com/strongloop/loopback-next/issues/1623
itSkippedOnTravis('handles IPv6 address for API Explorer UI', async () => {
const keyPath = path.join(__dirname, 'key.pem');
const certPath = path.join(__dirname, 'cert.pem');
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const server = await givenAServer({
rest: {
port: 0,
Expand All @@ -576,8 +578,8 @@ paths:
});

it('honors HTTPS config binding after instantiation', async () => {
const keyPath = path.join(__dirname, 'key.pem');
const certPath = path.join(__dirname, 'cert.pem');
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const options = {
port: 0,
protocol: 'https',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {TestSandbox, expect} from '../..';
import {resolve} from 'path';
import {remove, pathExists, readFile, writeJSON} from 'fs-extra';

const FIXTURES = resolve(__dirname, '../../../fixtures');

describe('TestSandbox integration tests', () => {
let sandbox: TestSandbox;
let path: string;
const COPY_FILE = 'copy-me.txt';
const COPY_FILE_PATH = resolve(__dirname, '../fixtures', COPY_FILE);
const COPY_FILE_PATH = resolve(FIXTURES, COPY_FILE);

beforeEach(createSandbox);
beforeEach(givenPath);
Expand Down
3 changes: 2 additions & 1 deletion tslint.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"linterOptions": {
"exclude": [
"./packages/cli/generators/*/templates/**/*",
"./packages/cli/test/sandbox/**/*"
"./packages/cli/test/sandbox/**/*",
"**/*.json"
]
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"linterOptions": {
"exclude": [
"./packages/cli/generators/*/templates/**/*",
"./packages/cli/test/sandbox/**/*"
"./packages/cli/test/sandbox/**/*",
"**/*.json"
]
}
}