Skip to content

#49 Add partial replace .vscode/launch.json #64

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Resources:
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"MyLambda\\",
\\"type\\": \\"node\\",
Expand All @@ -62,6 +63,7 @@ Resources:
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
},
Expand Down Expand Up @@ -89,6 +91,7 @@ Resources:
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"MyLambda\\",
\\"type\\": \\"node\\",
Expand All @@ -108,6 +111,7 @@ Resources:
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
},
Expand Down Expand Up @@ -135,6 +139,7 @@ Resources:
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"MyLambda\\",
\\"type\\": \\"node\\",
Expand All @@ -154,6 +159,7 @@ Resources:
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
},
Expand Down Expand Up @@ -181,6 +187,7 @@ Resources:
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"MyLambda\\",
\\"type\\": \\"node\\",
Expand All @@ -200,6 +207,7 @@ Resources:
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
},
Expand All @@ -216,6 +224,7 @@ Object {
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"a:MyLambda\\",
\\"type\\": \\"node\\",
Expand Down Expand Up @@ -254,6 +263,7 @@ Object {
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
"project-a/.aws-sam/build/template.yaml": "AWSTemplateFormatVersion: 2010-09-09
Expand Down Expand Up @@ -294,6 +304,7 @@ Object {
".vscode/launch.json": "{
\\"version\\": \\"0.2.0\\",
\\"configurations\\": [
// BEGIN AwsSamPlugin
{
\\"name\\": \\"a:MyLambda\\",
\\"type\\": \\"node\\",
Expand Down Expand Up @@ -332,6 +343,7 @@ Object {
\\"<node_internals>/**/*.js\\"
]
}
// END AwsSamPlugin
]
}",
"project-a/.aws-sam/build/template.yaml": "AWSTemplateFormatVersion: 2010-09-09
Expand Down
159 changes: 159 additions & 0 deletions src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,165 @@ test("Happy path with VS Code debugging disabled", () => {
expect({ entryPoints, files: fs.__getMockWrittenFiles() }).toMatchSnapshot();
});

const vscodeLaunchJson1 = `{
"version": "0.2.0",
"configurations": [
{
"name": "CustomLambda",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "\${workspaceFolder}/.aws-sam/build/CustomLambda",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"outFiles": [
"\${workspaceFolder}/.aws-sam/build/CustomLambda/**/*.js"
],
"sourceMaps": true,
"skipFiles": [
"/var/runtime/**/*.js",
"<node_internals>/**/*.js"
]
}
]
}`;
const vscodeLaunchJson2 = `{
"version": "0.2.0",
"configurations": [
{
"name": "CustomLambda",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "\${workspaceFolder}/.aws-sam/build/CustomLambda",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"outFiles": [
"\${workspaceFolder}/.aws-sam/build/CustomLambda/**/*.js"
],
"sourceMaps": true,
"skipFiles": [
"/var/runtime/**/*.js",
"<node_internals>/**/*.js"
]
},
// BEGIN AwsSamPlugin
{
"name": "OldLambda",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "\${workspaceFolder}/.aws-sam/build/OldLambda",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"outFiles": [
"\${workspaceFolder}/.aws-sam/build/OldLambda/**/*.js"
],
"sourceMaps": true,
"skipFiles": [
"/var/runtime/**/*.js",
"<node_internals>/**/*.js"
]
}
// END AwsSamPlugin
]
}`;
const vscodeLaunchJsonTest = `{
"version": "0.2.0",
"configurations": [
{
"name": "CustomLambda",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "\${workspaceFolder}/.aws-sam/build/CustomLambda",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"outFiles": [
"\${workspaceFolder}/.aws-sam/build/CustomLambda/**/*.js"
],
"sourceMaps": true,
"skipFiles": [
"/var/runtime/**/*.js",
"<node_internals>/**/*.js"
]
},
// BEGIN AwsSamPlugin
{
"name": "MyLambda",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "\${workspaceFolder}/.aws-sam/build/MyLambda",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false,
"outFiles": [
"\${workspaceFolder}/.aws-sam/build/MyLambda/**/*.js"
],
"sourceMaps": true,
"skipFiles": [
"/var/runtime/**/*.js",
"<node_internals>/**/*.js"
]
}
// END AwsSamPlugin
]
}`;

test.each([
[vscodeLaunchJson1, vscodeLaunchJsonTest],
[vscodeLaunchJson2, vscodeLaunchJsonTest],
])("Happy build launch.json with replace old content", (srcData, testData) => {
const plugin = new SamPlugin({ vscodeDebug: true });

// @ts-ignore
fs.__clearMocks();
// @ts-ignore
fs.__setMockDirs(["."]);
// @ts-ignore
fs.__setMockFiles({ "./template.yaml": samTemplate, ".vscode/launch.json": srcData });

// @ts-ignore
path.__clearMocks();
// @ts-ignore
path.__setMockBasenames({ "./template.yaml": "template.yaml" });
// @ts-ignore
path.__setMockDirnames({ "./template.yaml": "." });
// @ts-ignore
path.__setMockRelatives({ ".#.": "" });

const entryPoints = plugin.entry();

let afterEmit: (_compilation: any) => void;

plugin.apply({
hooks: {
afterEmit: {
tap: (n: string, f: (_compilation: any) => void) => {
afterEmit = f;
},
},
},
});
// @ts-ignore
afterEmit(null);

// @ts-ignore
const vscodeLaunchJsonContent = fs.__getMockWrittenFiles()[".vscode/launch.json"];

expect(vscodeLaunchJsonContent).toEqual(testData);
});

test("Happy path with multiple projects works", () => {
const plugin = new SamPlugin({ projects: { a: "project-a", b: "project-b" } });

Expand Down
30 changes: 29 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,35 @@ class AwsSamPlugin {
if (!fs.existsSync(".vscode")) {
fs.mkdirSync(".vscode");
}
fs.writeFileSync(".vscode/launch.json", JSON.stringify(this.launchConfig, null, 2));

const launchPath = ".vscode/launch.json";

const launchContent = JSON.stringify(this.launchConfig, null, 2)
.replace(/^(.*"configurations": \[\s*)$/m, "$1\n // BEGIN AwsSamPlugin")
.replace(/(\n \s*\][\r\n]+\})$/m, "\n // END AwsSamPlugin$1");
const regexBlock = /\s+\/\/ BEGIN AwsSamPlugin(\r|\n|.)+\/\/ END AwsSamPlugin/m;

// get new "configurations" content
const matches = launchContent.match(regexBlock);
if (!matches) {
throw new Error(launchPath + " new content does not match");
}
const launchConfigurations = matches[0];

if (fs.existsSync(launchPath)) {
const launchContentOld = fs.readFileSync(launchPath).toString("utf8");
if (launchContentOld.match(regexBlock)) {
// partial rewrite contents
const newContent = launchContentOld.replace(regexBlock, () => launchConfigurations);
fs.writeFileSync(launchPath, newContent);
} else {
// add configurations
const newContent = launchContentOld.replace(/(\n \]\n\})$/m, (p0, p1) => `,${launchConfigurations}${p1}`);
fs.writeFileSync(launchPath, newContent);
}
} else {
fs.writeFileSync(launchPath, launchContent);
}
}
});
}
Expand Down