Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a monorepo (project references) support for via the --build flag #19

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions fixtures/monorepo/package-lock.json

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

15 changes: 15 additions & 0 deletions fixtures/monorepo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "project-build",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"workspaces": [
"package*"
],
"license": "ISC"
}
13 changes: 13 additions & 0 deletions fixtures/monorepo/package1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "package1",
"version": "1.0.0",
"description": "",
"main": "dist/src/lib/add.js",
"types": "dist/src/lib/add.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions fixtures/monorepo/package1/src/lib/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function add (x: number, y: number): number {
return x + y
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/package1/src/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/monorepo/package1/src/test/add2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add2', () => {
strictEqual(add(3, 2), 5)
})
7 changes: 7 additions & 0 deletions fixtures/monorepo/package1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../tsconfig.base.json",
"references": [
]
}

15 changes: 15 additions & 0 deletions fixtures/monorepo/package2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "package2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"package1": "file:../package1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions fixtures/monorepo/package2/src/lib/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { add as add2 } from 'package1'
export function add (x: number, y: number): number {
return add2(x, y)
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/package2/src/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('package2-add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/monorepo/package2/src/test/add2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('package2-add2', () => {
strictEqual(add(3, 2), 5)
})
11 changes: 11 additions & 0 deletions fixtures/monorepo/package2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"references": [
{ "path": "../package1" }
]
}

25 changes: 25 additions & 0 deletions fixtures/monorepo/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"outDir": "dist",
"sourceMap": true,
"target": "ES2022",
"composite": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"strict": true,
"resolveJsonModule": true,
"removeComments": true,
"newLine": "lf",
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"lib": [
"ESNext"
],
"incremental": true
}
}
7 changes: 7 additions & 0 deletions fixtures/monorepo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "package1" },
{ "path": "package2" }
]
}
23 changes: 16 additions & 7 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export default async function runWithTypeScript (config) {

let prefix = ''
let tscPath
const typescriptCliArgs = []

if (tsconfigPath && config.typescript !== false) {
const tsconfig = JSON.parse(await readFile(tsconfigPath))
const _require = createRequire(tsconfigPath)
const typescriptPathCWD = _require.resolve('typescript')
tscPath = join(typescriptPathCWD, '..', '..', 'bin', 'tsc')
Expand All @@ -38,19 +40,22 @@ export default async function runWithTypeScript (config) {

// Watch is handled aftterwards
if (!config.watch) {
if (Array.isArray(tsconfig.references) && tsconfig.references.length > 0) {
typescriptCliArgs.push('--build')
}
const start = Date.now()
await execa('node', [tscPath], { cwd: dirname(tsconfigPath) })
await execa('node', [tscPath, ...typescriptCliArgs], { cwd: dirname(tsconfigPath) })
process.stdout.write(`TypeScript compilation complete (${Date.now() - start}ms)\n`)
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
message: `TypeScript compilation complete (${Date.now() - start}ms)`,
typescriptCliArgs
}
})
}
}
const tsconfig = JSON.parse(await readFile(tsconfigPath))
const outDir = tsconfig.compilerOptions.outDir
if (outDir) {
prefix = join(dirname(tsconfigPath), outDir)
Expand Down Expand Up @@ -83,17 +88,21 @@ export default async function runWithTypeScript (config) {
let p

if (config.watch) {
typescriptCliArgs.push('--watch')
p = deferred()
const start = Date.now()
tscChild = execa('node', [tscPath, '--watch'], { cwd })
let start = Date.now()
tscChild = execa('node', [tscPath, ...typescriptCliArgs], { cwd })
tscChild.stdout.setEncoding('utf8')
tscChild.stdout.on('data', (data) => {
if (data.includes('Watching for file changes')) {
if (data.includes('File change detected')) {
start = Date.now()
} else if (data.includes('Watching for file changes')) {
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
message: `TypeScript compilation complete (${Date.now() - start}ms)`,
typescriptCliArgs
}
})

Expand Down
25 changes: 25 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { tspl } from '@matteo.collina/tspl'
import runWithTypeScript from '../lib/run.js'
import { join } from 'desm'
import { execa } from 'execa'

test('ts-esm', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 4 })
Expand Down Expand Up @@ -141,7 +142,31 @@

await completed
})
test('monorepo', async (t) => {
const { strictEqual, completed, match, deepEqual } = tspl(t, { plan: 5 })
const config = {
files: [],
cwd: join(import.meta.url, '..', 'fixtures', 'monorepo/package2')
}

await execa('npm', ['install'], { cwd: join(import.meta.url, '..', 'fixtures', 'monorepo') })
const stream = await runWithTypeScript(config)

Check failure on line 153 in test/basic.test.js

View workflow job for this annotation

GitHub Actions / test (18.x, ubuntu-latest)

monorepo

[Error [ERR_TEST_FAILURE]: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.] { failureType: 'testCodeFailure', cause: Error: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations. at makeError (file:///home/runner/work/borp/borp/node_modules/execa/lib/error.js:60:11) at handlePromise (file:///home/runner/work/borp/borp/node_modules/execa/index.js:124:26) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async runWithTypeScript (file:///home/runner/work/borp/borp/lib/run.js:47:9) at async TestContext.<anonymous> (file:///home/runner/work/borp/borp/test/basic.test.js:153:18) at async Test.run (node:internal/test_runner/test:632:9) at async Test.processPendingSubtests (node:internal/test_runner/test:374:7) { shortMessage: 'Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', command: 'node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', escapedCommand: 'node "/home/runner/work/borp/borp/node_modules/typescript/bin/tsc" --build', exitCode: 2, signal: undefined, signalDescription: undefined, stdout: "src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.", stderr: '', cwd: '/home/runner/work/borp/borp/fixtures/monorepo/package2', failed: true, timedOut: false, isCanceled: false, killed: false }, code: 'ERR_TEST_FAILURE' }

Check failure on line 153 in test/basic.test.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

monorepo

[Error [ERR_TEST_FAILURE]: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: Error: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations. at makeError (file:///home/runner/work/borp/borp/node_modules/execa/lib/error.js:60:11) at handlePromise (file:///home/runner/work/borp/borp/node_modules/execa/index.js:124:26) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async runWithTypeScript (file:///home/runner/work/borp/borp/lib/run.js:47:9) at async TestContext.<anonymous> (file:///home/runner/work/borp/borp/test/basic.test.js:153:18) at async Test.run (node:internal/test_runner/test:632:9) at async Test.processPendingSubtests (node:internal/test_runner/test:374:7) { shortMessage: 'Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', command: 'node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', escapedCommand: 'node "/home/runner/work/borp/borp/node_modules/typescript/bin/tsc" --build', exitCode: 2, signal: undefined, signalDescription: undefined, stdout: "src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.", stderr: '', cwd: '/home/runner/work/borp/borp/fixtures/monorepo/package2', failed: true, timedOut: false, isCanceled: false, killed: false } }

Check failure on line 153 in test/basic.test.js

View workflow job for this annotation

GitHub Actions / test (21.x, ubuntu-latest)

monorepo

[Error [ERR_TEST_FAILURE]: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: Error: Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations. at makeError (file:///home/runner/work/borp/borp/node_modules/execa/lib/error.js:60:11) at handlePromise (file:///home/runner/work/borp/borp/node_modules/execa/index.js:124:26) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async runWithTypeScript (file:///home/runner/work/borp/borp/lib/run.js:47:9) at async TestContext.<anonymous> (file:///home/runner/work/borp/borp/test/basic.test.js:153:18) at async Test.run (node:internal/test_runner/test:632:9) at async Test.processPendingSubtests (node:internal/test_runner/test:374:7) { shortMessage: 'Command failed with exit code 2: node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', command: 'node /home/runner/work/borp/borp/node_modules/typescript/bin/tsc --build', escapedCommand: 'node "/home/runner/work/borp/borp/node_modules/typescript/bin/tsc" --build', exitCode: 2, signal: undefined, signalDescription: undefined, stdout: "src/lib/add.ts(1,29): error TS2307: Cannot find module 'package1' or its corresponding type declarations.", stderr: '', cwd: '/home/runner/work/borp/borp/fixtures/monorepo/package2', failed: true, timedOut: false, isCanceled: false, killed: false } }

const names = new Set(['package2-add', 'package2-add2'])

stream.once('data', (test) => {
strictEqual(test.type, 'test:diagnostic')
match(test.data.message, /TypeScript compilation complete \(\d+ms\)/)
deepEqual(test.data.typescriptCliArgs, ['--build'])
})

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

await completed
})
test('only-src', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 4 })
const config = {
Expand Down
Loading