-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: adds utility launch for debugging a single test file #4432
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback-next | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| /* | ||
| ================================================================================ | ||
| This is used in the launch.json to enable you to debug a test file written in | ||
| typescript. This function attempts to convert the passed typescript file to | ||
| the best-guess output javascript file. | ||
|
|
||
| It walks up the filesystem from the current file, stops at package.json, and | ||
| looks in `dist` | ||
|
|
||
| Ideally, we could somehow use the typescript compiler and tsconfig.json to get | ||
| the explicit output file instead of trying to guess it, but there might be | ||
| overhead. | ||
|
|
||
| Ex: | ||
| ```jsonc | ||
| { | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "node", | ||
| "request": "launch", | ||
| "name": "Debug Current Test File", | ||
| "program": "${workspaceRoot}/bin/mocha-current-file", | ||
| "runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"], | ||
| "cwd": "${workspaceRoot}", | ||
mschnee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "autoAttachChildProcesses": true, | ||
| "args": [ | ||
| "--config", | ||
| "${workspaceRoot}/packages/build/config/.mocharc.json", | ||
| "-t", | ||
| "0", | ||
| "${file}" | ||
| ], | ||
| "disableOptimisticBPs": true | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| ================================================================================ | ||
| */ | ||
|
|
||
| 'use strict'; | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| function findDistFile(filename) { | ||
| const absolutePath = path.resolve(filename); | ||
| const systemRoot = path.parse(absolutePath).root; | ||
| let currentDir = path.dirname(absolutePath); | ||
|
|
||
| let isPackageRoot = fs.existsSync(path.resolve(currentDir, 'package.json')); | ||
| while (!isPackageRoot) { | ||
| if (path.dirname(currentDir) === systemRoot) { | ||
mschnee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new Error( | ||
| `Could not find a package.json file in the path heirarchy of ${absolutePath}`, | ||
| ); | ||
| } | ||
| currentDir = path.join(currentDir, '..'); | ||
| isPackageRoot = fs.existsSync(path.resolve(currentDir, 'package.json')); | ||
| } | ||
| const base = path.resolve(currentDir); | ||
| const relative = path.relative(currentDir, absolutePath); | ||
| const resultPath = relative.replace(/^src/, 'dist').replace(/\.ts$/, '.js'); | ||
| return path.resolve(base, resultPath); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: the current code has a theoretical possibility of infinite loop as it does not test the root of a file system. A more robust implementation can be: let pkgFile = path.resolve(filename, '../package.json');
// Find root directory of the file system
const root = path.parse(pkgFile).root;
while (!fs.existsSync(pkgFile)) {
if (path.dirname(pkgFile) === root) {
// We reach the root of file system
pkgFile = undefined;
break;
}
// Try the parent directory level package.json
pkgFile = path.resolve(pkgFile, '../../package.json');
}
if (!pkgFile) return filename; // Or throw an error
const projectRoot = path.dirname(pkgFile);
const relative = path.relative(projectRoot, path.resolve(filename));
const jsFilePath = relative.replace(/^src/, 'dist').replace(/\.ts$/, '.js');
return path.resolve(projectRoot, jsFilePath);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a small change to the current function, and tested it by invoking this cli against both a test file inside of the |
||
|
|
||
| const newFile = findDistFile(process.argv.splice(-1)[0]); | ||
|
|
||
| if (newFile) { | ||
| require('../packages/build/node_modules/mocha/lib/cli').main( | ||
| [...process.argv, newFile].slice(2), | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.