Skip to content

Commit

Permalink
docs(example): my-calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
jjangga0214 committed Dec 5, 2023
1 parent 499565c commit 9140b1b
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/cjs-js/haetae.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default configure({
env: {
foo: 'bar',
},
run: () => ({ hi: 'there' }),
run: () => console.log('hi!'),
},
},
})
2 changes: 1 addition & 1 deletion examples/cjs-ts-1/haetae.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default configure({
env: {
foo: 'bar',
},
run: () => ({ hi: 'there' }),
run: () => console.log('hi!'),
},
},
})
2 changes: 1 addition & 1 deletion examples/cjs-ts-2/haetae.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default configure({
env: {
foo: 'bar',
},
run: () => ({ hi: 'there' }),
run: () => console.log('hi!'),
},
},
})
8 changes: 4 additions & 4 deletions examples/esm-js/haetae.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { configure } from 'haetae'

export default configure({
commands: {
foo: {
helloworld: {
run: () => ({ hello: 'world' }),
},
bar: {
hi: {
env: {
hi: 'there',
foo: 'bar',
},
run: () => ({ hello: 'world' }),
run: () => console.log('hi!'),
},
},
})
2 changes: 1 addition & 1 deletion examples/esm-ts/haetae.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default configure({
env: {
foo: 'bar',
},
run: () => ({ hi: 'there' }),
run: () => console.log('hi!'),
},
},
})
3 changes: 3 additions & 0 deletions examples/my-calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# my-calculator-test

This is an example project guided by the official *Getting Started* docs.
25 changes: 25 additions & 0 deletions examples/my-calculator/haetae.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { configure, $, git, js, utils } from 'haetae'

export default configure({
commands: {
test: {
run: async () => {
// An array of changed files
const changedFiles = await git.changedFiles()
// console.log(changedFiles)
// An array of test files that (transitively) depend on changed files
const affectedTestFiles = await js.dependOn({
dependents: ['**/*.test.js'], // glob pattern
// dependents: await utils.glob(['**/*.test.js']), // glob pattern
dependencies: changedFiles,
})

if (affectedTestFiles.length > 0) {
// Equals to "pnpm jest /path/to/foo.test.ts /path/to/bar.test.ts ..."
// Change 'pnpm jest' to your test runner.
await $`pnpm jest ${affectedTestFiles}`
}
},
},
},
})
17 changes: 17 additions & 0 deletions examples/my-calculator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "my-calculator",
"version": "0.0.0",
"type": "module",
"private": true,
"scripts": {
"haetae": "haetae",
"ht": "haetae",
"jest": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test": "haetae test"
},
"devDependencies": {
"cross-env": "^7.0.3",
"haetae": "workspace:*",
"jest": "^29.5.0"
}
}
3 changes: 3 additions & 0 deletions examples/my-calculator/src/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function add(a, b) {
return a + b
}
10 changes: 10 additions & 0 deletions examples/my-calculator/src/exponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import multiply from './multiply.js'

export default function exponent(a, b) {
let agg = 1
for (let i = 0; i < b; i += 1) {
agg = multiply(agg, a)
}

return agg // === a^b
}
10 changes: 10 additions & 0 deletions examples/my-calculator/src/multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import add from './add.js'

export default function multiply(a, b) {
let agg = 0
for (let i = 0; i < b; i += 1) {
agg = add(agg, a)
}

return agg // === a * b
}
5 changes: 5 additions & 0 deletions examples/my-calculator/src/subtract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import add from './add.js'

export default function subtract(a, b) {
return add(a, -b) // a - b
}
9 changes: 9 additions & 0 deletions examples/my-calculator/test/add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import add from '../src/add.js'

describe('add()', () => {
test('basic usage', () => {
expect(add(1, 2)).toBe(3)
expect(add(4, 2)).toBe(6)
expect(add(12, 5)).toBe(17)
})
})
9 changes: 9 additions & 0 deletions examples/my-calculator/test/exponent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import exponent from '../src/exponent.js'

describe('exponent()', () => {
test('basic usage', () => {
expect(exponent(0, 2)).toBe(0)
expect(exponent(1, 3)).toBe(1)
expect(exponent(2, 4)).toBe(16)
})
})
9 changes: 9 additions & 0 deletions examples/my-calculator/test/multiply.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import multiply from '../src/multiply.js'

describe('multiply()', () => {
test('basic usage', () => {
expect(multiply(1, 1)).toBe(1)
expect(multiply(2, 4)).toBe(8)
expect(multiply(3, 3)).toBe(9)
})
})
9 changes: 9 additions & 0 deletions examples/my-calculator/test/subtract.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import subtract from '../src/subtract.js'

describe('subtract()', () => {
test('basic usage', () => {
expect(subtract(1, 2)).toBe(-1)
expect(subtract(4, 2)).toBe(2)
expect(subtract(12, 5)).toBe(7)
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"lint:ci": "eslint \"*.{js,cjs,mjs,jsx,ts,cts,mts,tsx,md,mdx}\"",
"lint:md": "pnpm lint:md:ci --fix",
"lint:md:ci": "markdownlint",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage",
"jest": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest --coverage",
"husky-skip": "cross-env HUSKY=0",
"commit": "git cz",
"prepare": "husky install",
Expand Down
14 changes: 13 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9140b1b

Please sign in to comment.