Skip to content

Commit

Permalink
feat: add helper package "dist-util"
Browse files Browse the repository at this point in the history
Extract code dealing with Node.js versions and different `dist` folders
from packages' `index.js` files into a shared `@loopback/dist-util`
package.
  • Loading branch information
bajtos committed May 3, 2018
1 parent 9424b40 commit 532f153
Show file tree
Hide file tree
Showing 44 changed files with 183 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ coverage
.nyc_output
api-docs
**/*.tgz
**/dist*
packages/*/dist*
examples/*/dist*
**/package
.sandbox
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages/*/api-docs
packages/cli/generators/*/templates
dist*/
packages/*/dist*
examples/*/dist*
*.json
*.md
4 changes: 1 addition & 3 deletions examples/hello-world/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
const application = require(dist);
const application = require('@loopback/dist-util').loadDist(__dirname);

module.exports = application;

Expand Down
1 change: 1 addition & 0 deletions examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"license": "MIT",
"dependencies": {
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/rest": "^0.8.1"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions examples/log-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions examples/log-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dependencies": {
"@loopback/context": "^0.8.1",
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/openapi-v3": "^0.8.1",
"@loopback/rest": "^0.8.1",
"chalk": "^2.3.2",
Expand Down
4 changes: 1 addition & 3 deletions examples/rpc-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
const application = require(dist);
const application = require('@loopback/dist-util').loadDist(__dirname);

module.exports = application;

Expand Down
1 change: 1 addition & 0 deletions examples/rpc-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@loopback/context": "^0.8.1",
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"express": "^4.16.3",
"p-event": "^1.3.0"
},
Expand Down
4 changes: 1 addition & 3 deletions examples/todo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
const application = require(dist);
const application = require('@loopback/dist-util').loadDist(__dirname);

module.exports = application;

Expand Down
1 change: 1 addition & 0 deletions examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@loopback/boot": "^0.8.2",
"@loopback/context": "^0.8.1",
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/openapi-v3": "^0.8.1",
"@loopback/openapi-v3-types": "^0.5.0",
"@loopback/repository": "^0.8.1",
Expand Down
4 changes: 1 addition & 3 deletions packages/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@loopback/context": "^0.8.1",
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/openapi-v3": "^0.8.1",
"@loopback/rest": "^0.8.1",
"passport": "^0.4.0",
Expand Down
4 changes: 1 addition & 3 deletions packages/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/boot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@loopback/context": "^0.8.1",
"@loopback/core": "^0.6.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/repository": "^0.8.1",
"@types/debug": "0.0.30",
"@types/glob": "^5.0.35",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/generators/app/templates/index.js.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const application = (module.exports = require('./dist'));
const application = require('@loopback/dist-util').loadDist(__dirname);

module.exports = application;

if (require.main === module) {
// Run the application
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/generators/extension/templates/index.js.ejs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist');
module.exports = require('@loopback/dist-util').loadDist(__dirname);
4 changes: 3 additions & 1 deletion packages/cli/generators/project/templates/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@
"@loopback/boot": "<%= project.dependencies['@loopback/boot'] -%>",
<% if (project.projectType === 'application') { -%>
"@loopback/core": "<%= project.dependencies['@loopback/core'] -%>",
"@loopback/dist-util": "<%= project.dependencies['@loopback/dist-util'] -%>",
"@loopback/rest": "<%= project.dependencies['@loopback/rest'] -%>",
"@loopback/openapi-v3": "<%= project.dependencies['@loopback/openapi-v3'] -%>"
<% } else { -%>
"@loopback/core": "<%= project.dependencies['@loopback/core'] -%>"
"@loopback/core": "<%= project.dependencies['@loopback/core'] -%>",
"@loopback/dist-util": "<%= project.dependencies['@loopback/dist-util'] -%>"
<% } -%>
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"@loopback/example-hello-world": "^0.5.3",
"@loopback/example-log-extension": "^0.7.3",
"@loopback/example-rpc-server": "^0.5.3",
"@loopback/example-todo": "^0.7.3"
"@loopback/example-todo": "^0.7.3",
"@loopback/dist-util": "^0.1.0"
}
},
"copyright.owner": "IBM Corp.",
Expand Down
4 changes: 1 addition & 3 deletions packages/context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"copyright.owner": "IBM Corp.",
"license": "MIT",
"dependencies": {
"@loopback/dist-util": "^0.1.0",
"@loopback/metadata": "^0.6.1",
"debug": "^3.1.0",
"uuid": "^3.2.1"
Expand Down
4 changes: 1 addition & 3 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"copyright.owner": "IBM Corp.",
"license": "MIT",
"dependencies": {
"@loopback/context": "^0.8.1"
"@loopback/context": "^0.8.1",
"@loopback/dist-util": "^0.1.0"
},
"devDependencies": {
"@loopback/build": "^0.6.0",
Expand Down
1 change: 1 addition & 0 deletions packages/dist-util/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 6 additions & 0 deletions packages/dist-util/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "es5"
}
25 changes: 25 additions & 0 deletions packages/dist-util/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2018. All Rights Reserved.
Node module: @loopback/dist-util
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
56 changes: 56 additions & 0 deletions packages/dist-util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# @loopback/dist-util

Utilities to work with `dist` folders used by different Node.js versions.

version|directory
-|-
6.x LTS|_not supported_
8.x LTS|`dist8`
9.x|`dist8`
10.x|`dist10`
newer|`dist10`

## Installation

Run the following command to install this package:

```
$ npm install @loopback/dist-util
```

## Basic Use

Configure your TypeScript build to produce two distribution versions:

- `dist8` compiled for `es2017` target
- `dist10` compiled for `es2018` target

Put the following line to your main `index.js` file:

```js
module.exports = require('@loopback/dist-util').loadDist(__dirname);
// calls `require(__dirname + '/dist8')` or `require(__dirname + '/dist10')`
```

It is also possible to obtain the name of the correct `dist` directory
without loading the dist files:

```js
const dist = require('@loopback/dist-util').getDist();
console.log(dist);
// prints `dist8` or `dist10`
```

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
25 changes: 25 additions & 0 deletions packages/dist-util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/dist-util
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const path = require('path');

function getDist() {
const nodeMajorVersion = +process.versions.node.split('.')[0];
if (nodeMajorVersion < 8) {
throw new Error(
`Node.js version ${process.versions.node} is not supported.` +
'Please use Node.js 8.x or newer.');
}
return nodeMajorVersion >= 10 ? './dist10' : './dist8';
}

function loadDist(projectRootDir) {
const dist = getDist();
return require(path.join(projectRootDir, dist));
}

module.exports = {getDist, loadDist};
24 changes: 24 additions & 0 deletions packages/dist-util/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@loopback/dist-util",
"version": "0.1.0",
"description": "Select dist directory based on Node.js major version.",
"homepage": "https://github.com/strongloop/loopback-next/tree/master/packages/dist-select",
"author": {
"name": "IBM"
},
"engines": {
"node": ">=8"
},
"files": [
"index.js"
],
"keywords": [
"LoopBack"
],
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next"
},
"copyright.owner": "IBM Corp.",
"license": "MIT"
}
4 changes: 1 addition & 3 deletions packages/metadata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"devDependencies": {
"@loopback/build": "^0.6.0",
"@loopback/dist-util": "^0.1.0",
"@loopback/testlab": "^0.8.0",
"@types/debug": "^0.0.30",
"@types/lodash": "^4.14.106",
Expand Down
4 changes: 1 addition & 3 deletions packages/openapi-spec-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/openapi-spec-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"Testing"
],
"dependencies": {
"@loopback/dist-util": "^0.1.0",
"@loopback/openapi-v3-types": "^0.5.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/openapi-v3-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/openapi-v3-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"node": ">=8"
},
"dependencies": {
"@loopback/dist-util": "^0.1.0",
"openapi3-ts": "^0.11.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/openapi-v3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
const dist = nodeMajorVersion >= 10 ? './dist10' : './dist8';
module.exports = require(dist);
module.exports = require('@loopback/dist-util').loadDist(__dirname);
1 change: 1 addition & 0 deletions packages/openapi-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"dependencies": {
"@loopback/context": "^0.8.1",
"@loopback/dist-util": "^0.1.0",
"@loopback/openapi-v3-types": "^0.5.0",
"@loopback/repository-json-schema": "^0.7.0",
"debug": "^3.1.0",
Expand Down
Loading

0 comments on commit 532f153

Please sign in to comment.