Skip to content

Commit

Permalink
Merge pull request #683 from magieno/update-cli-to-use-data-mapper
Browse files Browse the repository at this point in the history
- Supporting multiple parameters in the CLI. Added support for defaul…
  • Loading branch information
etiennenoel authored Mar 9, 2024
2 parents d7e344c + f4d0d1b commit 27c819a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/event-payloads/command.event-payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class CommandEventPayload {
arguments: {[key in string]: string | number | boolean} = {};
arguments: {[key in string]: string | number | boolean | (string)[]} = {};

constructor(public name: string, public scriptPath: string) {
}
Expand Down
18 changes: 17 additions & 1 deletion packages/cli/src/mappers/command-event.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ describe('Command Event Mapper', function () {
expect(commandEventPayload.arguments.parameter).toBe("value")
}
})
it("should properly map multiple '--' arguments with the same name into an array", async () => {
const commandEventMapper = new CommandEventMapper();

const mapped = await commandEventMapper.map(["node", "scriptFilePath", "name", "--parameter=value1", "--parameter=value2"], {keyname: ExecutionContextKeynameEnum.Cli, context: {}});

const commandEventPayload: CommandEventPayload = mapped.events[0].payload;

expect(commandEventPayload instanceof CommandEventPayload).toBeTruthy()
expect(commandEventPayload.name).toBe("name")
expect(commandEventPayload.scriptPath).toBe("scriptFilePath")
expect(Array.isArray(commandEventPayload.arguments.parameter)).toBeTruthy()
expect((commandEventPayload.arguments.parameter as string[]).length).toBe(2)
expect((commandEventPayload.arguments.parameter as string[])[0]).toBe("value1")
expect((commandEventPayload.arguments.parameter as string[])[1]).toBe("value2")
})


it("should properly transform booleans, numbers and strings into their intended representation", async () => {
Expand All @@ -45,13 +60,14 @@ describe('Command Event Mapper', function () {
{args:["--parameter", "FALSE"], expectedValue: false},
{args:["--parameter", "1.05"], expectedValue: 1.05},
{args:["--parameter", "54"], expectedValue: 54},
{args:["--parameter", "allo", "--parameter", "secondAllo"], expectedValue: ["allo", "secondAllo"]},
{args:["--parameter", "A sentence"], expectedValue: "A sentence"},
];

for (let i = 0; i < consoleArguments.length; i++){
const args = consoleArguments[i].args;
const mapped = await commandEventMapper.map(["node", "scriptFilePath", "name"].concat(...args), {keyname: ExecutionContextKeynameEnum.Cli, context: {}});
expect(mapped.events[0].payload.arguments.parameter).toBe(consoleArguments[i].expectedValue)
expect(mapped.events[0].payload.arguments.parameter).toStrictEqual(consoleArguments[i].expectedValue)
}
})
});
23 changes: 22 additions & 1 deletion packages/cli/src/mappers/command-event.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ export class CommandEventMapper implements EventMapperInterface<CommandEventPayl
const indexOfEqualSign = argumentName.indexOf("=");
if(numberOfStartingDashes === 2 && indexOfEqualSign != -1) {
const actualArgumentName = argumentName.slice(0, indexOfEqualSign);
command.arguments[actualArgumentName] = argumentName.slice(indexOfEqualSign+1);
const actualArgumentValue = argumentName.slice(indexOfEqualSign+1)

if(command.arguments[actualArgumentName] !== undefined && Array.isArray(command.arguments[actualArgumentName]) === false) {
command.arguments[actualArgumentName] = [command.arguments[actualArgumentName] as (string)];
}

if(Array.isArray(command.arguments[actualArgumentName])) {
(command.arguments[actualArgumentName] as (string | number | boolean)[]).push(actualArgumentValue);
continue;
}

// Else, directly assign it
command.arguments[actualArgumentName] = actualArgumentValue;
continue;
}

Expand Down Expand Up @@ -83,6 +95,15 @@ export class CommandEventMapper implements EventMapperInterface<CommandEventPayl
continue;
}

if(command.arguments[argumentName] !== undefined && Array.isArray(command.arguments[argumentName]) === false) {
command.arguments[argumentName] = [command.arguments[argumentName] as (string)];
}

if(Array.isArray(command.arguments[argumentName])) {
(command.arguments[argumentName] as (string | number | boolean)[]).push(argumentValue);
continue;
}

command.arguments[argumentName] = argumentValue;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/http/src/commands/file-server.command-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export class FileServerCommandOptions {
port?: number;

address?: string;

header?: string | string[];
}
15 changes: 14 additions & 1 deletion packages/http/src/commands/file-server.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ export class FileServerCommand implements CommandInterface<FileServerCommandOpti
name = "file-server:start";

async run(args: FileServerCommandOptions): Promise<ExitCodeEnum | number> {
const defaultHeaders: {[key in string]: string} = {};

if(Array.isArray(args.header)) {
for(const header of args.header) {
const [key, value] = header.split(":");
defaultHeaders[key] = value;
}
} else if(args.header) {
const [key, value] = args.header.split(":");
defaultHeaders[key] = value;
}

await this.fileHttpServer.start(args.directory ?? "./", args.port, args.address, (port, address) => {
this.consoleManager.writeLine(`Pristine HTTP File server listening on: '${address}:${port}'`);
}, (req) => {
this.consoleManager.writeLine(`Request received: ${req.url}`);
});
},
defaultHeaders);

return ExitCodeEnum.Success;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/http/src/servers/file.http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class FileHttpServer {
return 0;
}

async start(directory: string, port?: number, address?: string, listeningCallback?: (port?: number, address?: string) => void, requestCallback?:(req: IncomingMessage) => void) {
async start(directory: string, port?: number, address?: string, listeningCallback?: (port?: number, address?: string) => void, requestCallback?:(req: IncomingMessage) => void, defaultHeaders: {[id in string]: string} = {}) {
port = port ?? this.getPort();
address = address ?? this.address;

Expand Down Expand Up @@ -146,6 +146,11 @@ export class FileHttpServer {
} else {
// if the file is found, set Content-type and send data
res.setHeader('Content-type', map[ext] ?? 'text/plain' );

for (const key in defaultHeaders) {
res.setHeader(key, defaultHeaders[key]);
}

res.end(data);
}
});
Expand Down

0 comments on commit 27c819a

Please sign in to comment.