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
25 changes: 23 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
},
"main": "./out/main",
"activationEvents": [
"onDebugResolve:nativescript"
"onDebugResolve:nativescript",
"onDebugDynamicConfigurations:nativescript"
],
"contributes": {
"configuration": {
Expand Down Expand Up @@ -128,7 +129,12 @@
"label": "NativeScript",
"languages": [
"javascript",
"typescript"
"typescript",
"javascriptreact",
"typescriptreact",
"vue",
"svelte",
"tsrx"
],
"initialConfigurations": [
{
Expand Down Expand Up @@ -514,6 +520,21 @@
},
{
"language": "typescript"
},
{
"language": "javascriptreact"
},
{
"language": "typescriptreact"
},
{
"language": "vue"
},
{
"language": "svelte"
},
{
"language": "tsrx"
}
]
}
Expand Down
50 changes: 50 additions & 0 deletions src/debug-adapter/configurationProvider.ts
Original file line number Diff line number Diff line change
@@ -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<vscode.DebugConfiguration | undefined> {
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;
}
}
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions src/project/projectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
100 changes: 100 additions & 0 deletions src/tests/configurationProvider.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
26 changes: 25 additions & 1 deletion src/tests/projectInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading