diff --git a/package.json b/package.json index 53fa58c..dca5917 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,8 @@ }, "main": "./out/main", "activationEvents": [ - "onDebugResolve:nativescript" + "onDebugResolve:nativescript", + "onDebugDynamicConfigurations:nativescript" ], "contributes": { "configuration": { @@ -128,7 +129,12 @@ "label": "NativeScript", "languages": [ "javascript", - "typescript" + "typescript", + "javascriptreact", + "typescriptreact", + "vue", + "svelte", + "tsrx" ], "initialConfigurations": [ { @@ -514,6 +520,21 @@ }, { "language": "typescript" + }, + { + "language": "javascriptreact" + }, + { + "language": "typescriptreact" + }, + { + "language": "vue" + }, + { + "language": "svelte" + }, + { + "language": "tsrx" } ] } diff --git a/src/debug-adapter/configurationProvider.ts b/src/debug-adapter/configurationProvider.ts new file mode 100644 index 0000000..596801e --- /dev/null +++ b/src/debug-adapter/configurationProvider.ts @@ -0,0 +1,50 @@ +import * as vscode from 'vscode'; +import { isNativeScriptProject } from '../project/projectInfo'; + +export function launchConfigurations(platform: NodeJS.Platform = process.platform): vscode.DebugConfiguration[] { + const platforms = platform === 'darwin' ? ['iOS', 'Android'] : ['Android']; + return platforms.map((name) => ({ + name: `Launch on ${name}`, + type: 'nativescript', + request: 'launch', + platform: name.toLowerCase(), + appRoot: '${workspaceFolder}', + sourceMaps: true, + watch: true, + })); +} + +/** Lists launch configurations in the Run and Debug view, so NativeScript workspaces need no launch.json. */ +export class DynamicConfigurationProvider implements vscode.DebugConfigurationProvider { + constructor(private readonly platform: NodeJS.Platform = process.platform) {} + + public provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined): vscode.DebugConfiguration[] { + return folder && isNativeScriptProject(folder.uri.fsPath) ? launchConfigurations(this.platform) : []; + } +} + +/** + * Has no provideDebugConfigurations on purpose: a new launch.json is then seeded from package.json's + * initialConfigurations, which also include the test and attach configurations. + */ +export class ConfigurationResolver implements vscode.DebugConfigurationProvider { + constructor(private readonly platform: NodeJS.Platform = process.platform) {} + + /** F5 without a launch.json arrives as an empty configuration; anything else is used as written. */ + public async resolveDebugConfiguration( + _folder: vscode.WorkspaceFolder | undefined, + config: vscode.DebugConfiguration, + ): Promise { + if (config.type || config.request || config.name) return config; + + const choices = launchConfigurations(this.platform); + if (choices.length === 1) return choices[0]; + + const picked = await vscode.window.showQuickPick( + choices.map((configuration) => ({ label: configuration.name, configuration })), + { placeHolder: 'Select the platform to debug on' }, + ); + // undefined cancels the launch. + return picked?.configuration; + } +} diff --git a/src/main.ts b/src/main.ts index 6cdfaf2..106d761 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as utils from './common/utilities'; +import { ConfigurationResolver, DynamicConfigurationProvider } from './debug-adapter/configurationProvider'; import { DebugManager } from './debug-adapter/debugManager'; import { NativeScriptDebugAdapter } from './debug-adapter/nativeScriptDebugAdapter'; import { AndroidProject } from './project/androidProject'; @@ -39,6 +40,12 @@ export function activate(context: vscode.ExtensionContext): void { return new vscode.DebugAdapterInlineImplementation(adapter); }, }), + vscode.debug.registerDebugConfigurationProvider('nativescript', new ConfigurationResolver()), + vscode.debug.registerDebugConfigurationProvider( + 'nativescript', + new DynamicConfigurationProvider(), + vscode.DebugConfigurationProviderTriggerKind.Dynamic, + ), ); context.subscriptions.push( diff --git a/src/project/projectInfo.ts b/src/project/projectInfo.ts index c757705..46e1c87 100644 --- a/src/project/projectInfo.ts +++ b/src/project/projectInfo.ts @@ -52,6 +52,15 @@ export function getAppDirectory(appRoot: string): string { return readConfigString(appRoot, 'appPath') ?? (fs.existsSync(path.join(appRoot, 'src')) ? 'src' : 'app'); } +export function isNativeScriptProject(appRoot: string): boolean { + if ([...CONFIG_FILES, 'nsconfig.json'].some((name) => fs.existsSync(path.join(appRoot, name)))) return true; + const packageJson = readPackageJson(appRoot); + return ['dependencies', 'devDependencies'].some((field) => { + const deps = packageJson?.[field]; + return typeof deps === 'object' && deps !== null && '@nativescript/core' in deps; + }); +} + export function getBundler(appRoot: string): Bundler { const bundler = readConfigString(appRoot, 'bundler'); return bundler === 'vite' || bundler === 'rspack' ? bundler : 'webpack'; diff --git a/src/tests/configurationProvider.test.ts b/src/tests/configurationProvider.test.ts new file mode 100644 index 0000000..99fed33 --- /dev/null +++ b/src/tests/configurationProvider.test.ts @@ -0,0 +1,100 @@ +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import * as vscode from 'vscode'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { + ConfigurationResolver, + DynamicConfigurationProvider, + launchConfigurations, +} from '../debug-adapter/configurationProvider'; + +const showQuickPick = vi.mocked(vscode.window.showQuickPick); + +describe('launchConfigurations', () => { + it('offers iOS and Android on macOS', () => { + expect(launchConfigurations('darwin').map((c) => [c.name, c.platform as unknown])).toEqual([ + ['Launch on iOS', 'ios'], + ['Launch on Android', 'android'], + ]); + }); + + it('offers only Android elsewhere', () => { + expect(launchConfigurations('linux').map((c) => c.platform as unknown)).toEqual(['android']); + }); + + it('builds launch configurations with HMR/watch on', () => { + expect(launchConfigurations('linux')[0]).toEqual({ + name: 'Launch on Android', + type: 'nativescript', + request: 'launch', + platform: 'android', + appRoot: '${workspaceFolder}', + sourceMaps: true, + watch: true, + }); + }); +}); + +describe('DynamicConfigurationProvider', () => { + let root: string; + const folder = () => ({ uri: vscode.Uri.file(root), name: 'app', index: 0 }) as unknown as vscode.WorkspaceFolder; + + beforeEach(() => { + root = fs.mkdtempSync(path.join(os.tmpdir(), 'ns-config-provider-')); + }); + + afterEach(() => { + fs.rmSync(root, { recursive: true, force: true }); + }); + + it('lists launch configurations for a NativeScript workspace', () => { + fs.writeFileSync(path.join(root, 'nativescript.config.ts'), 'export default {};\n'); + expect(new DynamicConfigurationProvider('darwin').provideDebugConfigurations(folder())).toEqual( + launchConfigurations('darwin'), + ); + }); + + it('lists nothing for other workspaces or without a folder', () => { + const provider = new DynamicConfigurationProvider('darwin'); + expect(provider.provideDebugConfigurations(folder())).toEqual([]); + expect(provider.provideDebugConfigurations(undefined)).toEqual([]); + }); +}); + +describe('ConfigurationResolver', () => { + it('passes configurations from launch.json through unchanged', async () => { + const config = { type: 'nativescript', request: 'launch', name: 'Mine', platform: 'ios' }; + await expect(new ConfigurationResolver('darwin').resolveDebugConfiguration(undefined, config)).resolves.toBe( + config, + ); + expect(showQuickPick).not.toHaveBeenCalled(); + }); + + it('asks for the platform when started without a launch.json', async () => { + showQuickPick.mockImplementationOnce((items) => Promise.resolve((items as vscode.QuickPickItem[])[1])); + + const resolved = await new ConfigurationResolver('darwin').resolveDebugConfiguration( + undefined, + {} as vscode.DebugConfiguration, + ); + + expect(resolved).toEqual(launchConfigurations('darwin')[1]); + }); + + it('cancels the launch when the platform pick is dismissed', async () => { + await expect( + new ConfigurationResolver('darwin').resolveDebugConfiguration(undefined, {} as vscode.DebugConfiguration), + ).resolves.toBeUndefined(); + }); + + it('uses Android without asking when it is the only platform', async () => { + const resolved = await new ConfigurationResolver('win32').resolveDebugConfiguration( + undefined, + {} as vscode.DebugConfiguration, + ); + + expect(resolved).toEqual(launchConfigurations('win32')[0]); + expect(showQuickPick).not.toHaveBeenCalled(); + }); +}); diff --git a/src/tests/projectInfo.test.ts b/src/tests/projectInfo.test.ts index 94607bc..49e85d3 100644 --- a/src/tests/projectInfo.test.ts +++ b/src/tests/projectInfo.test.ts @@ -2,7 +2,14 @@ import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; -import { findLocalCli, getAppDirectory, getBundler, isVitestProject, readPackageJson } from '../project/projectInfo'; +import { + findLocalCli, + getAppDirectory, + getBundler, + isNativeScriptProject, + isVitestProject, + readPackageJson, +} from '../project/projectInfo'; describe('projectInfo', () => { let appRoot: string; @@ -101,6 +108,23 @@ describe('projectInfo', () => { }); }); + describe('isNativeScriptProject', () => { + it.each(['nativescript.config.ts', 'nativescript.config.js', 'nsconfig.json'])('detects %s', (file) => { + writeAppFile(file, '{}'); + expect(isNativeScriptProject(appRoot)).toBe(true); + }); + + it('detects an @nativescript/core dependency', () => { + writeAppFile('package.json', JSON.stringify({ dependencies: { '@nativescript/core': '~9.1.0' } })); + expect(isNativeScriptProject(appRoot)).toBe(true); + }); + + it('is false for other projects', () => { + writeAppFile('package.json', JSON.stringify({ dependencies: { react: '19' } })); + expect(isNativeScriptProject(appRoot)).toBe(false); + }); + }); + describe('findLocalCli', () => { const installCli = (dir: string, bin = 'ns') => { const file = path.join(appRoot, dir, 'node_modules', '.bin', bin);