-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example with multiple bundles (#201)
- Loading branch information
Showing
6 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Multiple Bundles | ||
|
||
This example shows how you could build a simple module along with a CLI. | ||
|
||
## How to use | ||
|
||
Within this directory, run `yarn` to install dependencies. | ||
|
||
To build the main module, run `yarn build:main`. You'll notice the resulting `dist/main/index.js` only includes `main.js` and its `chalk` dependency. | ||
|
||
To build the CLI, run `yarn build:cli`. You'll notice the resulting `dist/cli/index.js` includes its `args` dependency in addition to everything required for `main.js`. | ||
|
||
_To build both concurrently, run `yarn build`._ | ||
|
||
To test the main module, run `yarn test`. You'll notice it uses a `strip-ansi` dependency that is not included in any of the builds. | ||
|
||
To use the built CLI, run `./dist/cli/index.js color` or `./dist/cli/index.js number`. If this were published to npm as "lucky", you would be able to run `lucky color` or `lucky number` because of the `bin` declaration in `package.json`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env node | ||
|
||
const args = require("args"); | ||
|
||
const { color, number } = require("./main"); | ||
|
||
args | ||
.command("color", "Receive a lucky color", () => { | ||
console.log(color()); | ||
}) | ||
.command("number", "Receive a lucky number", () => { | ||
console.log(number()); | ||
}); | ||
|
||
args.parse(process.argv, { | ||
name: "lucky" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const chalk = require("chalk"); | ||
|
||
exports.color = function color() { | ||
const color = Math.random() > 0.5 ? "red" : "blue"; | ||
|
||
return `Your lucky color is ${chalk[color](color)}`; | ||
}; | ||
|
||
exports.number = function number(min = 0, max = 100) { | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
//The maximum is exclusive and the minimum is inclusive | ||
const num = Math.floor(Math.random() * (max - min)) + min; | ||
|
||
return `Your lucky number is ${chalk.bold(num)}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const assert = require("assert"); | ||
const chalk = require("chalk"); | ||
const stripAnsi = require("strip-ansi"); | ||
|
||
const { color, number } = require("./main"); | ||
|
||
function getValue(fn) { | ||
return stripAnsi( | ||
fn() | ||
.split(/\s/) | ||
.pop() | ||
); | ||
} | ||
|
||
// color is a function | ||
assert(typeof color === "function"); | ||
|
||
// color is red or blue for this example | ||
const c = getValue(color); | ||
assert(c === "red" || c === "blue"); | ||
|
||
// number is a function | ||
assert(typeof number === "function"); | ||
|
||
// number returns an integer between 0 and 100 by default | ||
const n = getValue(number); | ||
assert(n > 0 && n <= 100); | ||
|
||
console.log(chalk.green("Tests pass!")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "multiple-bundles", | ||
"private": true, | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"main": "./dist/main/index.js", | ||
"bin": { | ||
"lucky": "./dist/cli/index.js" | ||
}, | ||
"scripts": { | ||
"build:cli": "ncc build cli.js -o dist/cli", | ||
"build:main": "ncc build main.js -o dist/main", | ||
"build": "concurrently 'npm:build:cli' 'npm:build:main'", | ||
"test": "node main.test.js" | ||
}, | ||
"dependencies": { | ||
"args": "^5.0.0", | ||
"chalk": "^2.4.1" | ||
}, | ||
"devDependencies": { | ||
"@zeit/ncc": "latest", | ||
"concurrently": "^4.1.0", | ||
"strip-ansi": "^5.0.0" | ||
} | ||
} |
Oops, something went wrong.