Skip to content

Commit 5b01479

Browse files
author
Rhys Madigan
committedMar 9, 2024
Extend existing example
1 parent 3b69ab4 commit 5b01479

14 files changed

+7048
-16
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
/.pids
55
/.pants.workdir.file_lock*
66

7+
node_modules
8+
9+
# Build output when build invoked with `npm run build`
10+
dist_npm

‎.prettierrc.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example prettier config - try changing this and running `pants fmt ::`
2+
# See https://prettier.io/docs/en/configuration.html for more options
3+
singleQuote: true

‎BUILD

+15-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
package_json(name="hello_pkg")
1+
package_json(
2+
name="hello_pkg",
3+
scripts=[
4+
node_test_script(
5+
coverage_args=["--coverage", "--coverage-directory=.coverage/"],
6+
coverage_output_files=[".coverage/lcov-report/index.html"],
7+
coverage_output_directories=[".coverage/lcov-report"],
8+
),
9+
node_build_script(
10+
entry_point="build",
11+
extra_env_vars=["FOO=BAR"],
12+
output_files=["dist_npm/index.js"],
13+
),
14+
],
15+
)

‎README.md

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ Try these out in this repo.
1717

1818
```
1919
pants dependencies src/index.js
20+
pants dependencies --transitive src/index.js
21+
```
22+
23+
## Lint and format
24+
25+
```
26+
pants lint ::
27+
pants fmt ::
28+
```
29+
30+
## Test
31+
32+
```
33+
pants test ::
34+
pants test --use-coverage ::
35+
```
36+
37+
The example is configured to use Jest as the test runner via `package.json` "scripts.test" key.
38+
Mocha is also supported by the Javascript backend.
39+
40+
## Package
41+
42+
The example uses esbuild to package the source into a single file in the `dist` directory.
43+
44+
```
45+
pants package ::
2046
```
2147

2248
## Generate lockfiles

‎package-lock.json

+6,921-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
2-
"main": "src/index.js",
3-
"name": "demo",
4-
"version": "0.0.1"
5-
}
2+
"main": "src/index.js",
3+
"name": "demo",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"test": "jest",
7+
"build": "esbuild --bundle --platform=node --target=node12 --outdir=./dist_npm src/index.js"
8+
},
9+
"devDependencies": {
10+
"esbuild": "^0.20.1",
11+
"jest": "^29.7.0"
12+
}
13+
}

‎pants.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[GLOBAL]
2-
pants_version = "2.18.0"
2+
pants_version = "2.19.0"
33

44
backend_packages.add = [
55
"pants.backend.experimental.javascript",

‎src/BUILD

+7
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
javascript_sources()
2+
3+
javascript_tests(
4+
name="tests",
5+
# Set this so the two test files run in the same process
6+
# (necessary for coverage)
7+
batch_compatibility_tag="hello1",
8+
)

‎src/dep.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
const hello = () => console.log("Hello World!");
1+
// Example of a transitive dep for index.js
2+
const dep2fn = require('./dep2.js');
3+
4+
const hello = () => {
5+
dep2fn();
6+
console.log('Hello World!');
7+
};
28

39
module.exports = hello;

‎src/dep2.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const dep2fn = () => console.log('Hello from Dep2 function!');
2+
3+
module.exports = dep2fn;

‎src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
const dep = require("./dep.js");
1+
const hello = require('./dep.js');
2+
const { isEven } = require('./numbers.js');
3+
4+
if (isEven(4)) {
5+
hello();
6+
}

‎src/isEven.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { isEven } = require('./numbers.js');
2+
3+
describe('isEven', () => {
4+
test('returns true if number is even', () => {
5+
expect(isEven(2)).toBe(true);
6+
});
7+
8+
test('returns false if number is odd', () => {
9+
expect(isEven(3)).toBe(false);
10+
});
11+
12+
test('throws an error if number is negative', () => {
13+
expect(() => isEven(-1)).toThrow();
14+
});
15+
16+
test('throws an error if number is not a number', () => {
17+
expect(() => isEven('1')).toThrow();
18+
});
19+
});

‎src/isOdd.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Adding a separarate test file for demo purposes
2+
3+
const { isOdd } = require('./numbers.js');
4+
5+
describe('isOdd', () => {
6+
test('returns true if number is odd', () => {
7+
expect(isOdd(3)).toBe(true);
8+
});
9+
10+
test('returns false if number is even', () => {
11+
expect(isOdd(2)).toBe(false);
12+
});
13+
});

‎src/numbers.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function isEven(number) {
2+
if (number < 0) throw new Error('Number must be positive');
3+
if (typeof number !== 'number') throw new Error('Number must be a number');
4+
return number % 2 === 0;
5+
}
6+
7+
function isOdd(number) {
8+
return !isEven(number);
9+
}
10+
11+
module.exports = { isEven, isOdd };

0 commit comments

Comments
 (0)
Please sign in to comment.