Skip to content

Commit 904bc52

Browse files
fix: get appPath only once
Instead of calculating the app path on each path transform, set it only once - when the debugger is attaching.
1 parent 9b8b004 commit 904bc52

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

src/debug-adapter/nativeScriptDebugAdapter.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { existsSync, readFileSync } from 'fs';
2+
import { join } from 'path';
13
import { ChromeDebugAdapter, IRestartRequestArgs } from 'vscode-chrome-debug-core';
24
import { Event, TerminatedEvent } from 'vscode-debugadapter';
35
import { DebugProtocol } from 'vscode-debugprotocol';
@@ -108,14 +110,31 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
108110
this._session.sendEvent(new TerminatedEvent());
109111
}
110112

111-
(this.pathTransformer as any).setTargetPlatform(args.platform);
113+
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);
114+
115+
(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath);
112116
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;
113117

114118
this.isLiveSync = args.watch;
115119

116120
return super.attach(transformedArgs);
117121
}
118122

123+
private getAppDirPath(webRoot: string): string {
124+
const pathToNsconfig = join(webRoot, 'nsconfig.json');
125+
126+
if (existsSync(pathToNsconfig)) {
127+
try {
128+
const content = readFileSync(pathToNsconfig).toString();
129+
const jsonContent = JSON.parse(content);
130+
131+
return jsonContent.appPath;
132+
} catch (err) {
133+
// Ignore the error for the moment
134+
}
135+
}
136+
}
137+
119138
private translateArgs(args): any {
120139
if (args.diagnosticLogging) {
121140
args.trace = args.diagnosticLogging;

src/debug-adapter/nativeScriptPathTransformer.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {
1010
};
1111

1212
private targetPlatform: string;
13+
private appDirPath: string;
1314

14-
public setTargetPlatform(targetPlatform: string) {
15+
public setTransformOptions(targetPlatform: string, appDirPath: string) {
1516
this.targetPlatform = targetPlatform.toLowerCase();
17+
this.appDirPath = appDirPath;
1618
}
1719

1820
protected async targetUrlToClientPath(webRoot: string, scriptUrl: string): Promise<string> {
@@ -42,19 +44,8 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {
4244

4345
relativePath = relativePath.replace('tns_modules', nodePath);
4446

45-
const pathToNsconfig = path.join(webRoot, 'nsconfig.json');
46-
47-
if (fs.existsSync(pathToNsconfig)) {
48-
try {
49-
const content = fs.readFileSync(pathToNsconfig).toString();
50-
const jsonContent = JSON.parse(content);
51-
52-
if (jsonContent.appPath) {
53-
relativePath = relativePath.replace('app', jsonContent.appPath);
54-
}
55-
} catch (err) {
56-
// Ignore the error for the moment
57-
}
47+
if (this.appDirPath) {
48+
relativePath = relativePath.replace('app', this.appDirPath);
5849
}
5950

6051
const absolutePath = path.resolve(path.join(webRoot, relativePath));

src/tests/nativeScriptDebugAdapter.tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('NativeScriptDebugAdapter', () => {
5858
pathTransformerMock = {
5959
attach: () => ({}),
6060
clearTargetContext: () => ({}),
61-
setTargetPlatform: () => ({}),
61+
setTransformOptions: () => ({}),
6262
};
6363

6464
nativeScriptDebugAdapter = new NativeScriptDebugAdapter(
@@ -84,8 +84,8 @@ describe('NativeScriptDebugAdapter', () => {
8484
sinon.assert.calledWith(spy, sinon.match({ event: extProtocol.BEFORE_DEBUG_START }));
8585
});
8686

87-
it(`${method} for ${platform} should call project setTargetPlatform`, async () => {
88-
const spy = sinon.spy(pathTransformerMock, 'setTargetPlatform');
87+
it(`${method} for ${platform} should call project setTransformOptions`, async () => {
88+
const spy = sinon.spy(pathTransformerMock, 'setTransformOptions');
8989

9090
await nativeScriptDebugAdapter[method](argsMock);
9191

src/tests/nativeScriptPathTransformer.tests.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as tests from './pathTransformData';
88
describe('NativeScriptPathTransformer', () => {
99
let nativeScriptPathTransformer: any;
1010
let existsSyncStub;
11-
let readFileSyncStub;
1211

1312
before(() => {
1413
nativeScriptPathTransformer = new NativeScriptPathTransformer();
@@ -23,17 +22,11 @@ describe('NativeScriptPathTransformer', () => {
2322
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}${nsConfigPartInTestName}`, async () => {
2423
(path as any).join = path.win32.join;
2524
(path as any).resolve = path.win32.resolve;
26-
(path as any).basename = path.win32.basename;
27-
nativeScriptPathTransformer.setTargetPlatform(test.platform);
28-
const isNsconfigFilePath = (filePath: string) => path.basename(filePath) === 'nsconfig.json';
25+
nativeScriptPathTransformer.setTransformOptions(test.platform, test.nsconfig ? test.nsconfig.appPath : null);
2926

3027
existsSyncStub = sinon
3128
.stub(fs, 'existsSync')
32-
.callsFake((arg: string) => arg === test.existingPath || (test.nsconfig && isNsconfigFilePath(arg)));
33-
readFileSyncStub = sinon
34-
.stub(fs, 'readFileSync')
35-
.callsFake((filePath: string) => (isNsconfigFilePath(filePath) && test.nsconfig) ? JSON.stringify(test.nsconfig) : null);
36-
29+
.callsFake((arg: string) => arg === test.existingPath);
3730
const result = await nativeScriptPathTransformer.targetUrlToClientPath(webRoot, test.scriptUrl);
3831

3932
assert.equal(result, test.expectedResult);
@@ -42,7 +35,6 @@ describe('NativeScriptPathTransformer', () => {
4235

4336
afterEach(() => {
4437
existsSyncStub.restore();
45-
readFileSyncStub.restore();
4638
});
4739
});
4840

0 commit comments

Comments
 (0)