diff --git a/docs/site/Repositories.md b/docs/site/Repositories.md index 5e9db032e5b3..1fd7b71feb18 100644 --- a/docs/site/Repositories.md +++ b/docs/site/Repositories.md @@ -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'; export class RedisDataSource extends juggler.DataSource { static dataSourceName = 'redis'; diff --git a/docs/site/Testing-your-application.md b/docs/site/Testing-your-application.md index 37834d244925..f7290a133391 100644 --- a/docs/site/Testing-your-application.md +++ b/docs/site/Testing-your-application.md @@ -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, { diff --git a/examples/soap-calculator/src/datasources/calculator.datasource.ts b/examples/soap-calculator/src/datasources/calculator.datasource.ts index ab65f4c96b18..de30e29b93b1 100644 --- a/examples/soap-calculator/src/datasources/calculator.datasource.ts +++ b/examples/soap-calculator/src/datasources/calculator.datasource.ts @@ -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, diff --git a/examples/todo-list/src/datasources/db.datasource.ts b/examples/todo-list/src/datasources/db.datasource.ts index 942301e1e165..f97cc8ad360b 100644 --- a/examples/todo-list/src/datasources/db.datasource.ts +++ b/examples/todo-list/src/datasources/db.datasource.ts @@ -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); } diff --git a/examples/todo/src/datasources/db.datasource.ts b/examples/todo/src/datasources/db.datasource.ts index 2e5d07a46140..a20ac68a30bd 100644 --- a/examples/todo/src/datasources/db.datasource.ts +++ b/examples/todo/src/datasources/db.datasource.ts @@ -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); } diff --git a/examples/todo/src/datasources/geocoder.datasource.ts b/examples/todo/src/datasources/geocoder.datasource.ts index 93d713777cf9..699e3950363a 100644 --- a/examples/todo/src/datasources/geocoder.datasource.ts +++ b/examples/todo/src/datasources/geocoder.datasource.ts @@ -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'; diff --git a/examples/todo/test/helpers.ts b/examples/todo/test/helpers.ts index e5c3d5d377ab..754d597d993c 100644 --- a/examples/todo/test/helpers.ts +++ b/examples/todo/test/helpers.ts @@ -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'; /* ============================================================================== @@ -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: { diff --git a/packages/build/README.md b/packages/build/README.md index 79691e59feab..00f2b4e27b09 100644 --- a/packages/build/README.md +++ b/packages/build/README.md @@ -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 diff --git a/packages/build/bin/compile-package.js b/packages/build/bin/compile-package.js index 7b4859778e5f..1152b0beef6b 100755 --- a/packages/build/bin/compile-package.js +++ b/packages/build/bin/compile-package.js @@ -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) { @@ -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('|') diff --git a/packages/build/config/tsconfig.common.json b/packages/build/config/tsconfig.common.json index 641d3bf4f7de..a5e4d37efebe 100644 --- a/packages/build/config/tsconfig.common.json +++ b/packages/build/config/tsconfig.common.json @@ -5,6 +5,7 @@ "experimentalDecorators": true, "noImplicitAny": true, "strictNullChecks": true, + "resolveJsonModule": true, "lib": ["es2018", "dom", "esnext.asynciterable"], "module": "commonjs", diff --git a/packages/cli/generators/datasource/templates/datasource.ts.ejs b/packages/cli/generators/datasource/templates/datasource.ts.ejs index aaed644e8e65..cfe9df24d3d4 100644 --- a/packages/cli/generators/datasource/templates/datasource.ts.ejs +++ b/packages/cli/generators/datasource/templates/datasource.ts.ejs @@ -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); } diff --git a/packages/cli/test/integration/generators/datasource.integration.js b/packages/cli/test/integration/generators/datasource.integration.js index 86556e4e91c9..e049ad7ebdb1 100644 --- a/packages/cli/test/integration/generators/datasource.integration.js +++ b/packages/cli/test/integration/generators/datasource.integration.js @@ -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, diff --git a/packages/http-server/test/integration/cert.pem b/packages/http-server/fixtures/cert.pem similarity index 100% rename from packages/http-server/test/integration/cert.pem rename to packages/http-server/fixtures/cert.pem diff --git a/packages/http-server/test/integration/key.pem b/packages/http-server/fixtures/key.pem similarity index 100% rename from packages/http-server/test/integration/key.pem rename to packages/http-server/fixtures/key.pem diff --git a/packages/http-server/test/integration/pfx.pfx b/packages/http-server/fixtures/pfx.pfx similarity index 100% rename from packages/http-server/test/integration/pfx.pfx rename to packages/http-server/fixtures/pfx.pfx diff --git a/packages/http-server/test/integration/http-server.integration.ts b/packages/http-server/test/integration/http-server.integration.ts index 9dbaef7d54c2..532e3afd6fc0 100644 --- a/packages/http-server/test/integration/http-server.integration.ts +++ b/packages/http-server/test/integration/http-server.integration.ts @@ -211,13 +211,14 @@ describe('HttpServer (integration)', () => { host?: string; }): HttpServer { const options: HttpServerOptions = {protocol: 'https', host}; + const certDir = path.resolve(__dirname, '../../../fixtures'); 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); } diff --git a/packages/rest/test/integration/cert.pem b/packages/rest/fixtures/cert.pem similarity index 100% rename from packages/rest/test/integration/cert.pem rename to packages/rest/fixtures/cert.pem diff --git a/packages/rest/test/integration/fixtures/index.html b/packages/rest/fixtures/index.html similarity index 100% rename from packages/rest/test/integration/fixtures/index.html rename to packages/rest/fixtures/index.html diff --git a/packages/rest/test/integration/key.pem b/packages/rest/fixtures/key.pem similarity index 100% rename from packages/rest/test/integration/key.pem rename to packages/rest/fixtures/key.pem diff --git a/packages/rest/test/integration/pfx.pfx b/packages/rest/fixtures/pfx.pfx similarity index 100% rename from packages/rest/test/integration/pfx.pfx rename to packages/rest/fixtures/pfx.pfx diff --git a/packages/rest/test/integration/rest.server.integration.ts b/packages/rest/test/integration/rest.server.integration.ts index 0bfb2d078b98..3e7601208799 100644 --- a/packages/rest/test/integration/rest.server.integration.ts +++ b/packages/rest/test/integration/rest.server.integration.ts @@ -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 @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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', @@ -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', @@ -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, @@ -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, @@ -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', diff --git a/packages/testlab/test/fixtures/copy-me.txt b/packages/testlab/fixtures/copy-me.txt similarity index 100% rename from packages/testlab/test/fixtures/copy-me.txt rename to packages/testlab/fixtures/copy-me.txt diff --git a/packages/testlab/test/integration/test-sandbox.integration.ts b/packages/testlab/test/integration/test-sandbox.integration.ts index fc419deee096..9b3ebd1b66f2 100644 --- a/packages/testlab/test/integration/test-sandbox.integration.ts +++ b/packages/testlab/test/integration/test-sandbox.integration.ts @@ -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); diff --git a/tslint.build.json b/tslint.build.json index 0de4205d0047..888f5ae175b8 100644 --- a/tslint.build.json +++ b/tslint.build.json @@ -4,7 +4,8 @@ "linterOptions": { "exclude": [ "./packages/cli/generators/*/templates/**/*", - "./packages/cli/test/sandbox/**/*" + "./packages/cli/test/sandbox/**/*", + "**/*.json" ] } } diff --git a/tslint.json b/tslint.json index 57fe820a65da..3935f4397a66 100644 --- a/tslint.json +++ b/tslint.json @@ -4,7 +4,8 @@ "linterOptions": { "exclude": [ "./packages/cli/generators/*/templates/**/*", - "./packages/cli/test/sandbox/**/*" + "./packages/cli/test/sandbox/**/*", + "**/*.json" ] } }