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
43 changes: 27 additions & 16 deletions bin/cli.mjs → bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import inquirer from "inquirer";
import autocompletePrompt from "inquirer-autocomplete-prompt";

import { hideBin } from "yargs/helpers";
import { deconstruct } from "./commands/deconstruct.mjs";
import { construct } from "./commands/construct.mjs";
import { add } from "./commands/add.mjs";
import { config } from "./commands/config.mjs";
import { remove } from "./commands/remove.mjs";
import { clone } from "./commands/clone.mjs";
import { awsRegions } from "./utils/constants.mjs";
import { deconstruct } from "./commands/deconstruct.js";
import { construct } from "./commands/construct.js";
import { add } from "./commands/add.js";
import { config } from "./commands/config.js";
import { remove } from "./commands/remove.js";
import { clone } from "./commands/clone.js";
import { awsRegions } from "./utils/constants.js";
import type {
AddArgs,
CloneArgs,
ConfigArgs,
ConstructArgs,
DeconstructArgs,
RemoveArgs,
} from "./types.js";

// Get the original command from process.argv
const command = process.argv.slice(2).join(" ");
Expand All @@ -34,7 +42,7 @@ yargs(hideBin(process.argv))
});
},
(argv) => {
construct(argv, command);
construct(argv as ConstructArgs);
}
)
.command(
Expand All @@ -53,7 +61,7 @@ yargs(hideBin(process.argv))
});
},
(argv) => {
deconstruct(argv, command);
deconstruct(argv as DeconstructArgs);
}
)
.command(
Expand All @@ -74,7 +82,7 @@ yargs(hideBin(process.argv))
});
},
(argv) => {
add(argv, command);
add(argv as unknown as AddArgs);
}
)
.command(
Expand All @@ -89,7 +97,7 @@ yargs(hideBin(process.argv))
});
},
(argv) => {
remove(argv, command);
remove(argv as unknown as RemoveArgs);
}
)
.command(
Expand All @@ -116,7 +124,7 @@ yargs(hideBin(process.argv))
});
},
(argv) => {
clone(argv, command);
clone(argv as unknown as CloneArgs);
}
)
.command("config", "Configure ShelbySAM", {}, () => {
Expand All @@ -126,7 +134,10 @@ yargs(hideBin(process.argv))
type: "autocomplete",
name: "region",
message: "Select your AWS region:",
source: (answers, input) => {
source: (
answers: Record<string, unknown>,
input: string | undefined
) => {
return Promise.resolve(
awsRegions.filter((region) => region.includes(input || ""))
);
Expand All @@ -153,10 +164,10 @@ yargs(hideBin(process.argv))
},
])
.then((answers) => {
config(answers);
config(answers as ConfigArgs);
});
})
.demandCommand(1, "", null)
.demandCommand(1, "You need to specify a command to run")
.version("1.1.5")
.parse()
.parse();

52 changes: 36 additions & 16 deletions bin/commands/add.mjs → bin/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
import { readJson, readYaml, readConfig } from "../utils/helper.mjs";
import { readJson, readYaml, readConfig } from "../utils/helper.js";
import fs from "fs";
import yaml from "js-yaml";
import { markdownSyntax } from "../utils/constants.mjs";
let templateRegistry = {};
import { markdownSyntax } from "../utils/constants.js";
import type { AddArgs, TemplateRegistry, PropertyType } from "../types.js";

const identifyType = (key, properties, resource) => {

let templateRegistry: TemplateRegistry = { ResourceTypes: {}, PropertyTypes: {} };

const identifyType = (key: string, properties: PropertyType, resource: string): unknown => {
// Level 1 processing
if (Object.keys(properties).includes("PrimitiveType")) {
// directly return the types, no further processing required
Expand All @@ -22,9 +25,9 @@ const identifyType = (key, properties, resource) => {

// Level 2 processing - flex
if (Object.keys(properties).includes("Type")) {
let outputTemplate = {};
let outputTemplate: Record<string, unknown> = {};
let outputType = "json";
let propertyTemplate = {};
let propertyTemplate: PropertyType | undefined;
// sub template definition --> "PropertyTypes"
if (Object.keys(properties).includes("ItemType")) {
propertyTemplate =
Expand All @@ -36,10 +39,13 @@ const identifyType = (key, properties, resource) => {
outputType = "json";
}

for (const [k, v] of Object.entries(propertyTemplate)) {
if (k === "Properties") {
for (const [kk, vv] of Object.entries(v)) {
outputTemplate[kk] = identifyType(kk, vv, resource);
if (propertyTemplate) {
for (const [k, v] of Object.entries(propertyTemplate)) {
if (k === "Properties") {
const properties = v as Record<string, PropertyType>;
for (const [kk, vv] of Object.entries(properties)) {
outputTemplate[kk] = identifyType(kk, vv, resource);
}
}
}
}
Expand All @@ -49,34 +55,45 @@ const identifyType = (key, properties, resource) => {
};

// Main Function
const add = async (args, command) => {

const add = async (args: AddArgs): Promise<object | undefined> => {
try {
// read config file
const shelbysamConfig = await readConfig();
if (!shelbysamConfig) {
throw new Error("ShelbySAM configuration not found. Please run 'shelbysam config' first.");
}

// get cloudformation registry
templateRegistry = await readJson(
`.shelbysam/${shelbysamConfig.region}.json`
);
) as TemplateRegistry;

// read template file
const shelbysamTemplate = await readYaml(
shelbysamConfig.shelbysam_template_file
);
if (!shelbysamTemplate) {
throw new Error(`Unable to read template file: ${shelbysamConfig.shelbysam_template_file}`);
}

// main template definition--> "ResourceTypes"
let outputTemplate = {};
let outputTemplate: Record<string, unknown> = {};
const resourceTemplate = templateRegistry.ResourceTypes[args.type];

if (!resourceTemplate) {
throw new Error(`Resource type '${args.type}' not found in template registry`);
}

// log document link
console.log(
`\n For more information on ${args.type}, visit ${resourceTemplate.Documentation}\n`
`\n For more information on ${args.type}, visit ${resourceTemplate.Documentation || 'N/A'}\n`
);

//start processing
for (const [k, v] of Object.entries(resourceTemplate)) {
if (k === "Properties") {
for (const [kk, vv] of Object.entries(v)) {
for (const [kk, vv] of Object.entries(v as Record<string, PropertyType>)) {
outputTemplate[kk] = identifyType(kk, vv, args.type);
}
}
Expand All @@ -90,8 +107,11 @@ const add = async (args, command) => {
".yaml";

// add resource to template
if (!shelbysamTemplate.Resources) {
shelbysamTemplate.Resources = {};
}
shelbysamTemplate.Resources[args.lid] =
"${file:" + shelbysamResourcePath + "}";
"${file:" + shelbysamResourcePath + "}" as string;

// write the resource file
fs.writeFileSync(
Expand Down
71 changes: 0 additions & 71 deletions bin/commands/clone.mjs

This file was deleted.

99 changes: 99 additions & 0 deletions bin/commands/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env node
import {
readYaml,
readConfig,
matchRegex,
writeYaml,
} from "../utils/helper.js";
import fs from "fs";
import type { CloneArgs } from "../types.js";


// Main Function
const clone = async (args:CloneArgs): Promise<void> => {
// read config file
const shelbysamConfig = await readConfig();
if (!shelbysamConfig) {
throw new Error("ShelbySAM configuration not found. Please run 'shelbysam config' first.");
}

// read template
const shelbysamTemplate = await readYaml(
shelbysamConfig.shelbysam_template_file!
);
if (!shelbysamTemplate) {
throw new Error(`Unable to read template file: ${shelbysamConfig.shelbysam_template_file}`);
}

// match regex for the available declarations
if (!shelbysamTemplate.Resources) {
throw new Error("No resources found in template");
}
const resourceValue = shelbysamTemplate.Resources[args.slid];
if (!resourceValue || typeof resourceValue !== 'string') {
throw new Error(`Source resource '${args.slid}' not found or invalid format`);
}
const resource = matchRegex(resourceValue);

const shelbysamSourceResourcePath = (resource[1] as RegExpMatchArray)[1]!;
const shelbysamDestinationResourcePath =
shelbysamConfig.shelbysam_template_folder! +
"/Resources/" +
args.dlid +
".yaml";

// Direct File References
if (resource[0] === "File") {
if (!shelbysamTemplate.Resources) {
shelbysamTemplate.Resources = {};
}
shelbysamTemplate.Resources[args.dlid] =
"${file:" + shelbysamDestinationResourcePath + "}" as string;
fs.cpSync(shelbysamSourceResourcePath, shelbysamDestinationResourcePath);
}
// File Object / Group References
else if (resource[0] === "FileObject") {
const sourceResources = await readYaml(shelbysamSourceResourcePath);
if (!sourceResources) {
throw new Error(`Unable to read source resource file: ${shelbysamSourceResourcePath}`);
}

// if group flag is set to true, re-create all resources in the file
if (args.group) {
const destResources: Record<string, unknown> = {};
for (const [k, v] of Object.entries(sourceResources)) {
if (!shelbysamTemplate.Resources) {
shelbysamTemplate.Resources = {};
}
shelbysamTemplate.Resources[args.dlid + k] =
"${file:" + shelbysamDestinationResourcePath + ":" + k + "}" as string;
destResources[k] = v;
}
await writeYaml(shelbysamDestinationResourcePath, destResources);
}
// if group flag is set to false, re-create only the specified resource
else {
if (!shelbysamTemplate.Resources) {
shelbysamTemplate.Resources = {};
}
shelbysamTemplate.Resources[args.dlid] =
"${file:" + shelbysamDestinationResourcePath + "}" as string;

const resourceKey = (resource[1] as RegExpMatchArray)[2] as string;
console.log((sourceResources as Record<string, unknown>)[resourceKey]);
await writeYaml(
shelbysamDestinationResourcePath,
(sourceResources as Record<string, unknown>)[resourceKey]
);
}
}

// write the final template
await writeYaml(shelbysamConfig.shelbysam_template_file!, shelbysamTemplate);

return;
};

// add support for nested file objects

export { clone };
Loading