Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ jobs:
- name: Build packages
run: pnpm build

- name: Check types
run: pnpm typecheck

- name: Set node version to ${{ matrix.node }} for testing
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
Expand Down
11 changes: 6 additions & 5 deletions MAINTENANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ Exact version:
pnpm exec bumpp --release 2.14.0
```

The release command is defined in [`package.json`](./package.json) and uses [`bumpp`](https://www.npmjs.com/package/bumpp) with shared settings in [`bump.config.mjs`](./bump.config.mjs).
The release command is defined in [`package.json`](./package.json) and uses [`bumpp`](https://www.npmjs.com/package/bumpp) with shared settings in [`bump.config.ts`](./bump.config.ts).

It will:

1. Bump both package versions.
2. Run `pnpm build` and `pnpm test`.
2. Run `pnpm build`, `pnpm typecheck`, and `pnpm test`.
3. Create commit `vX.Y.Z`.
4. Create tag `vX.Y.Z`.
5. Push the commit and tag to `origin`.
Expand Down Expand Up @@ -72,11 +72,12 @@ Build both packages with tsdown using Node.js 22.18+, 24.11+, or 26+:
pnpm build
```

Each package bundles its JavaScript into `dist/index.js`. Runtime dependencies remain external, and the handwritten `index.d.ts` declarations are published alongside the bundle. Packing or publishing a package runs its build automatically.
Each package bundles its TypeScript into `dist/index.js` and generates declarations in `dist/index.d.ts`. Runtime dependencies remain external. Packing or publishing a package runs its build automatically.

Run the workspace tests after building:
Check types and run the workspace tests after building:

```bash
pnpm typecheck
pnpm test
```

Expand All @@ -96,5 +97,5 @@ If the GitHub Actions publish job fails:
## Files

- [`package.json`](./package.json)
- [`bump.config.mjs`](./bump.config.mjs)
- [`bump.config.ts`](./bump.config.ts)
- [`.github/workflows/publish.yml`](./.github/workflows/publish.yml)
2 changes: 1 addition & 1 deletion bump.config.mjs → bump.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'bumpp'

export default defineConfig({
recursive: true,
execute: 'pnpm build && pnpm test',
execute: 'pnpm build && pnpm typecheck && pnpm test',
commit: 'v',
tag: 'v',
})
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@
"scripts": {
"build": "pnpm -r build",
"test": "pnpm -r test",
"typecheck": "tsc --noEmit",
"release": "bumpp",
"lint": "oxlint --fix",
"format": "oxfmt"
},
"devDependencies": {
"@types/node": "^24.19.2",
"bumpp": "^11.1.0",
"lint-staged": "^17.4.1",
"oxfmt": "^0.65.0",
"oxlint": "^1.80.0",
"tsdown": "^0.23.7",
"typescript": "^6.0.3",
"vitest": "^4.1.12",
"yorkie": "^2.0.0"
},
"lint-staged": {
"*": [
"oxfmt --no-error-on-unmatched-pattern"
],
"*.js": [
"*.{js,ts}": [
"oxlint --fix"
]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/launch-editor-middleware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Both the line and column numbers are optional. `file://` URIs are also supported

## API

See [`index.d.ts`](./index.d.ts) for the full type definitions and the available arguments.
See [`index.ts`](https://github.com/vitejs/launch-editor/blob/main/packages/launch-editor-middleware/index.ts) for the full type definitions and the available arguments.

## Custom editor support

Expand Down
36 changes: 0 additions & 36 deletions packages/launch-editor-middleware/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IncomingMessage, ServerResponse } from 'node:http'
import { Socket } from 'node:net'
import path from 'node:path'
import { afterEach, describe, expect, test, vi } from 'vitest'

Expand All @@ -12,87 +14,79 @@ vi.mock('launch-editor', () => ({

afterEach(() => {
vi.clearAllMocks()
vi.restoreAllMocks()
})

const STATUS_NOT_ALTERED = -1
class MockResponse {
constructor() {
this.statusCode = STATUS_NOT_ALTERED
this.body = ''
}
const STATUS_NOT_ALTERED = 200

end(body) {
this.body = body || ''
}
}

class MockRequest {
constructor(url) {
this.url = url
}
function createRequestResponse(url: string) {
const req = new IncomingMessage(new Socket())
req.url = url
const res = new ServerResponse(req)
const end = vi.spyOn(res, 'end').mockReturnValue(res)
return { req, res, end }
}

describe('launchEditorMiddleware', () => {
test('returns a 500 if no file query param', async () => {
const middleware = launchEditorMiddleware('vim', undefined, noop)

const req = new MockRequest('https://localhost/')
const res = new MockResponse(null)
const { req, res, end } = createRequestResponse('https://localhost/')

middleware(req, res)

expect(res.statusCode).toBe(500)
expect(res.body).toBe('launch-editor-middleware: required query param "file" is missing.')
expect(end).toHaveBeenCalledExactlyOnceWith(
'launch-editor-middleware: required query param "file" is missing.',
)
})

test('launches editor with specified file', async () => {
const middleware = launchEditorMiddleware('vim', undefined, noop)

const file = 'mock/file:100'
const req = new MockRequest(`https://localhost/?file=${file}`)
const res = new MockResponse(null)
const { req, res, end } = createRequestResponse(`https://localhost/?file=${file}`)

middleware(req, res)

expect(launch).toHaveBeenCalledExactlyOnceWith(path.resolve(file), 'vim', noop)
expect(res.statusCode).toBe(STATUS_NOT_ALTERED)
expect(res.body).toBe('')
expect(end).toHaveBeenCalledExactlyOnceWith()
})

test('falsy file parameter returns 500', async () => {
const middleware = launchEditorMiddleware('vim', undefined, noop)

const req = new MockRequest('https://localhost/?file=')
const res = new MockResponse(null)
const { req, res, end } = createRequestResponse('https://localhost/?file=')

middleware(req, res)

expect(res.statusCode).toBe(500)
expect(res.body).toBe('launch-editor-middleware: required query param "file" is missing.')
expect(end).toHaveBeenCalledExactlyOnceWith(
'launch-editor-middleware: required query param "file" is missing.',
)
})

test('returns 500 on invalid URL', async () => {
const middleware = launchEditorMiddleware('vim', undefined, noop)

const req = new MockRequest('some invalid URL')
const res = new MockResponse(null)
const { req, res, end } = createRequestResponse('some invalid URL')

middleware(req, res)

expect(res.statusCode).toBe(500)
expect(res.body).toBe('launch-editor-middleware: invalid URL.')
expect(end).toHaveBeenCalledExactlyOnceWith('launch-editor-middleware: invalid URL.')
})

test('correctly parse the url even if it contains only the path', async () => {
const middleware = launchEditorMiddleware('vim', undefined, noop)

const file = 'mock/file:100'
const req = new MockRequest(`/__open-in-editor?file=${file}`)
const res = new MockResponse(null)
const { req, res, end } = createRequestResponse(`/__open-in-editor?file=${file}`)

middleware(req, res)

expect(res.statusCode).toBe(STATUS_NOT_ALTERED)
expect(res.body).toBe('')
expect(end).toHaveBeenCalledExactlyOnceWith()
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type * as http from 'node:http'
import path from 'node:path'
import launch from 'launch-editor'

type Middleware = (req: http.IncomingMessage, res: http.ServerResponse) => void
type ErrorCallback = (fileName: string, errorMessage: string | null) => void
Expand All @@ -10,7 +12,7 @@ type ErrorCallback = (fileName: string, errorMessage: string | null) => void
*
* @param onErrorCallback Optional callback for handling errors.
*/
declare function launchEditorMiddleware(onErrorCallback?: ErrorCallback): Middleware
function launchEditorMiddleware(onErrorCallback?: ErrorCallback): Middleware

/**
* A middleware to launch an editor to open a file at a specific line and column.
Expand All @@ -21,7 +23,7 @@ declare function launchEditorMiddleware(onErrorCallback?: ErrorCallback): Middle
* parsed using `shell-quote`.
* @param onErrorCallback Optional callback for handling errors.
*/
declare function launchEditorMiddleware(
function launchEditorMiddleware(
specifiedEditor?: string,
onErrorCallback?: ErrorCallback,
): Middleware
Expand All @@ -36,10 +38,48 @@ declare function launchEditorMiddleware(
* @param srcRoot Optional source root directory to resolve relative file paths.
* @param onErrorCallback Optional callback for handling errors.
*/
declare function launchEditorMiddleware(
function launchEditorMiddleware(
specifiedEditor?: string,
srcRoot?: string,
onErrorCallback?: ErrorCallback,
): Middleware
function launchEditorMiddleware(
specifiedEditor?: string | ErrorCallback,
srcRoot?: string | ErrorCallback,
onErrorCallback?: ErrorCallback,
): Middleware {
if (typeof specifiedEditor === 'function') {
onErrorCallback = specifiedEditor
specifiedEditor = undefined
}

if (typeof srcRoot === 'function') {
onErrorCallback = srcRoot
srcRoot = undefined
}

srcRoot = srcRoot || process.cwd()

return function middleware(req, res) {
const requestUrl = req.url ?? ''
const fullUrl = requestUrl.startsWith('http') ? requestUrl : `http://localhost${requestUrl}`
const url = URL.parse(fullUrl)
if (!url) {
res.statusCode = 500
res.end(`launch-editor-middleware: invalid URL.`)
return
}

const file = url.searchParams.get('file')
if (!file) {
res.statusCode = 500
res.end(`launch-editor-middleware: required query param "file" is missing.`)
} else {
const resolved = file.startsWith('file://') ? file : path.resolve(srcRoot, file)
launch(resolved, specifiedEditor, onErrorCallback)
res.end()
}
}
}

export default launchEditorMiddleware
7 changes: 3 additions & 4 deletions packages/launch-editor-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
"url": "git+https://github.com/vitejs/launch-editor.git"
},
"files": [
"dist",
"index.d.ts"
"dist"
],
"type": "module",
"main": "dist/index.js",
"types": "index.d.ts",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsdown index.js --no-fixed-extension --no-dts",
"build": "tsdown index.ts --no-fixed-extension --dts",
"prepack": "pnpm run build",
"test": "vitest run"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest'

import constructWindowsLaunchCommand from './construct-windows-launch-command.js'
import constructWindowsLaunchCommand from './construct-windows-launch-command.ts'

describe('constructWindowsLaunchCommand', () => {
test('joins the editor and arguments', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//
// `"` cannot appear in Windows paths, so it is outside the untrusted path input
// this sanitization protects.
function escapeCmdArgs(cmdArgs) {
function escapeCmdArgs(cmdArgs: string) {
return cmdArgs.replace(/([&|<>,;=^])/g, '^$1')
}

Expand All @@ -31,7 +31,7 @@ function escapeCmdArgs(cmdArgs) {
// However, there's a case that it's concatenated with line number and column number
// which is separated by `:`. We need to double quote the whole string in this case.
// Also, if the string contains the escape character `^`, it needs to be quoted, too.
function doubleQuoteIfNeeded(str) {
function doubleQuoteIfNeeded(str: string) {
if (str.includes('^')) {
// If a string includes an escaped character, not only does it need to be quoted,
// but the quotes need to be escaped too.
Expand All @@ -42,7 +42,7 @@ function doubleQuoteIfNeeded(str) {
return str
}

function constructWindowsLaunchCommand(editor, args) {
function constructWindowsLaunchCommand(editor: string, args: string[]) {
// CMD treats line breaks as command separators. They cannot appear in regular Windows
// file names, but NTFS alternate data stream names may contain control characters.
if (args.some((arg) => /[\r\n]/.test(arg))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, test, vi } from 'vitest'

import getArgumentsForFile from './get-args.js'
import getArgumentsForFile from './get-args.ts'
const absoluteFileName = path.resolve('file')

describe('getArgumentsForFile', () => {
Expand Down Expand Up @@ -203,7 +203,7 @@ describe('getArgumentsForFile', () => {

test(`${editor} protects an option-like file name ${position}`, () => {
const args = getArgumentsForFile(editor, fileName, lineNumber, 5)
const fileArgument = args.at(-1)
const fileArgument = args.at(-1) as string

expect(path.resolve(fileArgument)).toBe(path.resolve(fileName))
expect(
Expand Down
Loading
Loading