Skip to content

Commit 9b8b004

Browse files
fix: debugging is not working when app path is changed
Debugging is not working when project has `nsconfig.json` and the path to `app` directory is changed. The problem is that on device the directory is always called `app`, but NativeScript projects can rename it locally to `src` for example. In order to fix the issue, check if there's nsconfig.json file and replace the `app` from the device path with the local name of the app directory. This fixes debugging for new Angular templates.
1 parent 315503a commit 9b8b004

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/debug-adapter/nativeScriptPathTransformer.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {
4242

4343
relativePath = relativePath.replace('tns_modules', nodePath);
4444

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+
}
58+
}
59+
4560
const absolutePath = path.resolve(path.join(webRoot, relativePath));
4661

4762
if (fs.existsSync(absolutePath)) {

src/tests/nativeScriptPathTransformer.tests.ts

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

1213
before(() => {
1314
nativeScriptPathTransformer = new NativeScriptPathTransformer();
@@ -17,11 +18,21 @@ describe('NativeScriptPathTransformer', () => {
1718
const webRoot = 'C:\\projectpath';
1819

1920
for (const test of tests as any) {
20-
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}`, async () => {
21+
const nsConfigPartInTestName = test.nsconfig ? " when there's nsconfig" : '';
22+
23+
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}${nsConfigPartInTestName}`, async () => {
2124
(path as any).join = path.win32.join;
2225
(path as any).resolve = path.win32.resolve;
26+
(path as any).basename = path.win32.basename;
2327
nativeScriptPathTransformer.setTargetPlatform(test.platform);
24-
existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((arg: string) => arg === test.existingPath);
28+
const isNsconfigFilePath = (filePath: string) => path.basename(filePath) === 'nsconfig.json';
29+
30+
existsSyncStub = sinon
31+
.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);
2536

2637
const result = await nativeScriptPathTransformer.targetUrlToClientPath(webRoot, test.scriptUrl);
2738

@@ -31,6 +42,7 @@ describe('NativeScriptPathTransformer', () => {
3142

3243
afterEach(() => {
3344
existsSyncStub.restore();
45+
readFileSyncStub.restore();
3446
});
3547
});
3648

src/tests/pathTransformData.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const tests = [
1515
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/tns_modules/tns-core-modules/ui/page/page.js', expectedResult: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\page\\page.android.js', existingPath: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\page\\page.android.js' },
1616
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/tns_modules/tns-core-modules/ui/layouts/layout-base.js', expectedResult: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\layouts\\layout-base.android.js', existingPath: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\layouts\\layout-base.android.js' },
1717
{ platform: 'android', scriptUrl: 'ng:///css/0/data/data/org.nativescript.TabNavigation/files/app/tabs/tabs.component.scss.ngstyle.js', expectedResult: 'ng:///css/0/data/data/org.nativescript.TabNavigation/files/app/tabs/tabs.component.scss.ngstyle.js' },
18+
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/main.js', expectedResult: 'C:\\projectpath\\src\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\main.js' },
19+
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/app/main.js', expectedResult: 'C:\\projectpath\\src\\app\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\app\\main.js' },
20+
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.app1/files/app/app/main.js', expectedResult: 'C:\\projectpath\\src\\app\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\app\\main.js' },
1821
];
1922

2023
export = tests;

0 commit comments

Comments
 (0)