Skip to content

Commit

Permalink
fixes from testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Jul 19, 2023
1 parent 7f145fe commit eb42f21
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 24 deletions.
59 changes: 54 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "samp-cli",
"version": "1.0.11",
"version": "1.0.12",
"description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -44,6 +44,7 @@
"cli-spinner": "^0.2.10",
"clipboardy": "^2.2.0",
"commander": "^7.2.0",
"comment-json": "^4.2.3",
"flat": "^5.0.2",
"fs-extra": "^10.0.0",
"getmac": "^5.20.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/local/cdk-construct-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function findFilesWithExtension(rootDir, fileExtension) {

if (itemStats.isDirectory()) {
traverseDir(itemPath);
} else if (path.extname(itemPath) === fileExtension && !itemPath.includes('node_modules')) {
} else if (path.extname(itemPath) === fileExtension && !itemPath.includes('node_modules') && !itemPath.includes('.samp-out')) {
files.push(itemPath);
}
});
Expand Down
41 changes: 37 additions & 4 deletions src/commands/local/cdk-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const cdk = require('aws-cdk-lib');
const fs = require('fs');
const { yamlDump } = require('yaml-cfn');
let stackFile = fs.readFileSync(`${process.cwd()}/${process.argv[2]}`, 'utf8');
stackFile = stackFile.replace(/\.ts"/g, '.js"').replace(/\.ts'/g, ".js'").replace(/\.ts`/g, ".js`");
fs.writeFileSync(`${process.cwd()}/${process.argv[2]}`, stackFile);
const path = require('path');

const baseDir = `${process.cwd()}/.samp-out`;

for (const file of getAllJsFiles(baseDir)) {
if (file.endsWith('.js')) {
let fileContents = fs.readFileSync(file, 'utf8');
if (fileContents.includes('aws-cdk-lib')) {
fileContents = fileContents.replace(/\.ts"/g, '.js"').replace(/\.ts'/g, ".js'").replace(/\.ts`/g, ".js`");
fs.writeFileSync(file, fileContents);
}
}
}


const TargetStack = require(`${process.cwd()}/${process.argv[2]}`);
const className = Object.keys(TargetStack)[0];

Expand Down Expand Up @@ -58,4 +70,25 @@ function getCircularReplacer() {
}
return value;
};
}
}

function getAllJsFiles(directory) {
let fileArray = [];

const files = fs.readdirSync(directory);

for (const file of files) {
const filePath = path.join(directory, file);
const fileStats = fs.statSync(filePath);

if (fileStats.isDirectory()) {
const nestedFiles = getAllJsFiles(filePath); // Recursively get files in subdirectory
fileArray = fileArray.concat(nestedFiles);
} else {
if (filePath.endsWith('.js'))
fileArray.push(filePath);
}
}

return fileArray;
}
2 changes: 1 addition & 1 deletion src/commands/local/lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (fs.existsSync(process.cwd() + "/.samp-out")) {
console.log("Removing .samp-out directory");
fs.rmSync(process.cwd() + "/.samp-out", { recursive: true, force: true });
}

if (!conf.envConfig) process.exit(0);
const stackName = conf.envConfig.stack_name;
const region = conf.envConfig.region;
const profile = conf.envConfig.profile;
Expand Down
21 changes: 9 additions & 12 deletions src/commands/local/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const inputUtil = require('../../shared/inputUtil');
const settingsUtil = require('../../shared/settingsUtil');
const glob = require('glob');
const { findConstructs } = require('./cdk-construct-finder');

const commentJson = require('comment-json')
function setEnvVars(cmd) {
process.env.SAMP_PROFILE = cmd.profile || process.env.AWS_PROFILE;
process.env.SAMP_REGION = cmd.region || process.env.AWS_REGION;
Expand Down Expand Up @@ -123,7 +123,7 @@ function print(data) {

async function setupDebug() {
let args = ["local"];
let stack = null;
let stack = null;
if (fs.existsSync(`cdk.json`)) {
const constructs = findConstructs();
constructs.push("Enter manually");
Expand All @@ -133,7 +133,7 @@ async function setupDebug() {
}
const cdkTree = JSON.parse(fs.readFileSync("cdk.out/tree.json", "utf8"));
const stacks = Object.keys(cdkTree.tree.children).filter(c => c !== "Tree");
stacks.push("Enter manually");
stacks.push("Enter manually");
stack = await inputUtil.autocomplete("What's the name of the deployed stack?", stacks);
if (stack === "Enter manually") {
stack = await inputUtil.autocomplete("What's the name of the deployed stack?");
Expand All @@ -148,8 +148,7 @@ async function setupDebug() {
if (fs.existsSync(`${pwd}/.vscode/launch.json`)) {
console.log("launch.json already exists");
let fileContent = fs.readFileSync(`${pwd}/.vscode/launch.json`, "utf8");
fileContent = fileContent.replace(/\/\/.*/g, '');
launchJson = JSON.parse(fileContent);
launchJson = commentJson.parse(fileContent);
} else {
launchJson = {
"version": "0.2.0",
Expand Down Expand Up @@ -186,11 +185,10 @@ async function setupDebug() {
let tasksJson;
if (fs.existsSync(`${pwd}/.vscode/tasks.json`)) {
console.log("tasks.json already exists");
tasksJson = JSON.parse(fs.readFileSync(`${pwd}/.vscode/tasks.json`, "utf8"));
const existingTask = tasksJson.tasks.find(t => t.label === "lambda-local-cleanup");
tasksJson = commentJson.parse(fs.readFileSync(`${pwd}/.vscode/tasks.json`, "utf8"));
const existingTask = tasksJson.tasks.find(t => t.label === "samp-local-cleanup");
if (existingTask) {
console.log("Task already exists");
return;
} else {
tasksJson.tasks.push(task);
}
Expand All @@ -203,9 +201,8 @@ async function setupDebug() {
if (!fs.existsSync(`${pwd}/.vscode`)) {
fs.mkdirSync(`${pwd}/.vscode`);
}

fs.writeFileSync(`${pwd}/.vscode/launch.json`, JSON.stringify(launchJson, null, 2));
fs.writeFileSync(`${pwd}/.vscode/tasks.json`, JSON.stringify(tasksJson, null, 2));
fs.writeFileSync(`${pwd}/.vscode/launch.json`, commentJson.stringify(launchJson, null, 2));
fs.writeFileSync(`${pwd}/.vscode/tasks.json`, commentJson.stringify(tasksJson, null, 2));
}

async function warn() {
Expand All @@ -214,7 +211,7 @@ async function warn() {
console.log("Warning: This command will make changes to your deployed function configuration in AWS for the duration of your debugging session.\n\nPlease ONLY run this against a development environment.\n\nTo learn more about the changes made, please visit https://github.com/ljacobsson/samp-cli#how-does-it-work\n");
const answer = await inputUtil.prompt("Warn again next time?");
if (!answer) {
settings.sampLocalWarned = true;
settings.sampLocalWarned = true;
settingsUtil.saveConfigSource(settings)
}
}
Expand Down

0 comments on commit eb42f21

Please sign in to comment.