Skip to content

Commit

Permalink
add args type to some block
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Zhe Jiet authored and Chang Zhe Jiet committed Nov 24, 2023
1 parent 252be24 commit f063935
Show file tree
Hide file tree
Showing 46 changed files with 428 additions and 7,861 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@
"selector": "variableLike",
"format": [
"strictCamelCase",
"UPPER_CASE"
"UPPER_CASE",
"snake_case"
],
"leadingUnderscore": "allow"
},
{
"selector": "memberLike",
"format": [
"strictCamelCase",
"UPPER_CASE"
"UPPER_CASE",
"snake_case"
]
},
{
Expand Down
61 changes: 23 additions & 38 deletions src/TerraformGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import shell from 'shelljs';
import { Block, Comment, Resource, Data, Module, Output, Provider, Variable, Backend, Provisioner, ResourceToDataOptions, Locals, Import } from './blocks';
import { Util } from './Util';
import { Block, Comment, Resource, Data, Module, Output, Provider, Variable, Backend, Provisioner, ResourceToDataOptions, Locals, Import, ImportArgs, VariableArgs, ModuleArgs, OutputArgs } from './blocks';
import { BlockArgs, Util } from './utils';

/**
* @category TerraformGenerator
Expand Down Expand Up @@ -42,9 +42,9 @@ export interface WriteOptions {
*/
export class TerraformGenerator {

readonly #arguments?: Record<string, any>;
readonly #arguments?: BlockArgs;
readonly #blocks: Block[] = [];
#variables: Record<string, any> = {};
#variables: BlockArgs = {};

/**
* Construct Terraform generator.
Expand All @@ -53,7 +53,7 @@ export class TerraformGenerator {
*
* @param args arguments
*/
constructor(args?: Record<string, any>) {
constructor(args?: BlockArgs) {
this.#arguments = args;
}

Expand Down Expand Up @@ -170,7 +170,7 @@ export class TerraformGenerator {
* @param type type
* @param args arguments
*/
provider(type: string, args?: Record<string, any>): Provider {
provider(type: string, args: BlockArgs): Provider {
const block = new Provider(type, args);
this.addBlocks(block);
return block;
Expand All @@ -186,7 +186,7 @@ export class TerraformGenerator {
* @param args arguments
* @param provisioners provisioners
*/
resource(type: string, name: string, args?: Record<string, any>, provisioners?: Provisioner[]): Resource {
resource(type: string, name: string, args: BlockArgs, provisioners?: Provisioner[]): Resource {
const block = new Resource(type, name, args, provisioners);
this.addBlocks(block);
return block;
Expand All @@ -201,8 +201,7 @@ export class TerraformGenerator {
* use array for name mapping, position 0 = original resource's argument name, position 1 = mapped data source's argument name
* @param args extra arguments
*/
dataFromResource(resource: Resource, options: ResourceToDataOptions | undefined, argNames: (string | [string, string])[],
args?: Record<string, any>): Data {
dataFromResource(resource: Resource, options: ResourceToDataOptions | undefined, argNames: (string | [string, string])[], args?: BlockArgs): Data {
const block = resource.toData(options, argNames, args);
this.addBlocks(block);
return block;
Expand All @@ -217,7 +216,7 @@ export class TerraformGenerator {
* @param name name
* @param args arguments
*/
data(type: string, name: string, args?: Record<string, any>): Data {
data(type: string, name: string, args: BlockArgs): Data {
const block = new Data(type, name, args);
this.addBlocks(block);
return block;
Expand All @@ -231,7 +230,7 @@ export class TerraformGenerator {
* @param name name
* @param args arguments
*/
module(name: string, args?: Record<string, any>): Module {
module(name: string, args: ModuleArgs): Module {
const block = new Module(name, args);
this.addBlocks(block);
return block;
Expand All @@ -245,7 +244,7 @@ export class TerraformGenerator {
* @param name name
* @param args arguments
*/
output(name: string, args?: Record<string, any>): Output {
output(name: string, args: OutputArgs): Output {
const block = new Output(name, args);
this.addBlocks(block);
return block;
Expand All @@ -258,7 +257,7 @@ export class TerraformGenerator {
*
* @param args arguments
*/
locals(args?: Record<string, any>): Locals {
locals(args: BlockArgs): Locals {
const block = new Locals(args);
this.addBlocks(block);
return block;
Expand All @@ -273,7 +272,7 @@ export class TerraformGenerator {
* @param args arguments
* @param value variable value
*/
variable(name: string, args?: Record<string, any>, value?: any): Variable {
variable(name: string, args: VariableArgs, value?: any): Variable {
const block = new Variable(name, args);
this.addBlocks(block);
if (value != null) {
Expand All @@ -283,42 +282,28 @@ export class TerraformGenerator {
}

/**
* Add backend into Terraform.
* Add import into Terraform.
*
* Refer to Terraform documentation on what can be put as type & arguments.
* Refer to Terraform documentation on what can be put as arguments.
*
* @param type type
* @param args arguments
*/
backend(type: string, args?: Record<string, any>): Backend {
const block = new Backend(type, args);
import(args: ImportArgs): Import {
const block = new Import(args);
this.addBlocks(block);
return block;
}

/**
* Add provisioner into Terraform.
* Add backend into Terraform.
*
* Refer to Terraform documentation on what can be put as type & arguments.
*
* @param type type
* @param args arguments
*/
provisioner(type: string, args?: Record<string, any>): Provisioner {
const block = new Provisioner(type, args);
this.addBlocks(block);
return block;
}

/**
* Add import into Terraform.
*
* Refer to Terraform documentation on what can be put as arguments.
*
* @param args arguments
*/
import(args?: Record<string, any>): Import {
const block = new Import(args);
backend(type: string, args: BlockArgs): Backend {
const block = new Backend(type, args);
this.addBlocks(block);
return block;
}
Expand All @@ -328,7 +313,7 @@ export class TerraformGenerator {
*
* @param variables variables
*/
addVars(variables: Record<string, any>): this {
addVars(variables: BlockArgs): this {
this.#variables = {
...this.#variables,
...variables
Expand All @@ -352,7 +337,7 @@ export class TerraformGenerator {
/**
* Get arguments.
*/
getArguments(): Record<string, any> | undefined {
getArguments(): BlockArgs | undefined {
return this.#arguments;
}

Expand All @@ -366,7 +351,7 @@ export class TerraformGenerator {
/**
* Get variables.
*/
getVars(): Record<string, any> {
getVars(): BlockArgs {
return this.#variables;
}

Expand Down
10 changes: 5 additions & 5 deletions src/arguments/Argument.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Util } from '../Util';
import { Util } from '../utils';

/**
* @category Argument
*/
export class Argument {
export class Argument<T extends string | Argument = string | Argument<string>> {

readonly argument: string | Argument;
readonly argument: T;

/**
* Construct argument.
*
* @param arg argument as string or copy from another argument object
*/
constructor(arg: string | Argument) {
constructor(arg: T) {
if (!arg || (typeof arg === 'string' && !arg.trim())) {
throw new Error('Argument cannot be empty.');
}
Expand Down Expand Up @@ -80,4 +80,4 @@ export class Argument {
*
* @category Argument
*/
export const arg = (arg: string | Argument): Argument => new Argument(arg);
export const arg = <T extends string | Argument = string | Argument<string>>(arg: T): Argument<T> => new Argument(arg);
2 changes: 1 addition & 1 deletion src/arguments/Function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Util } from '../Util';
import { Util } from '../utils';
import { Argument } from '.';

/**
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/Backend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Argument, Attribute } from '../arguments';
import { BlockArgs } from '../utils';
import { Block } from '.';

/**
Expand All @@ -16,7 +17,7 @@ export class Backend extends Block {
* @param type type
* @param args arguments
*/
constructor(type: string, args?: Record<string, any>) {
constructor(type: string, args: BlockArgs) {
super('backend', [type], args, undefined, true);

this.type = type;
Expand Down
25 changes: 13 additions & 12 deletions src/blocks/Block.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Argument, Attribute } from '../arguments';
import { Util } from '../Util';
import { BlockArgs, Util } from '../utils';

/**
* @category Block
*/
export abstract class Block {
export abstract class Block<Args extends BlockArgs = BlockArgs> {

readonly blockType: string;
readonly blockNames: string[];
readonly #arguments: Record<string, any>;
#arguments: Args;
#innerBlocks: Block[];
readonly #insideTerraformBlock: boolean;

Expand All @@ -19,15 +19,15 @@ export abstract class Block {
* @param names names
* @param args arguments
*/
constructor(type: string, names: string[], args?: Record<string, any>, innerBlocks?: Block[], insideTerraformBlock = false) {
constructor(type: string, names: string[], args: Args, innerBlocks?: Block[], insideTerraformBlock = false) {
this.#validateIdentifier(type);
names.forEach(name => {
this.#validateIdentifier(name);
});

this.blockType = type;
this.blockNames = names;
this.#arguments = args ? args : {};
this.#arguments = args;
this.#innerBlocks = innerBlocks ? innerBlocks : [];
this.#insideTerraformBlock = insideTerraformBlock;
}
Expand All @@ -42,7 +42,7 @@ export abstract class Block {
/**
* Get arguments.
*/
getArguments(): Record<string, any> {
getArguments(): Args {
return this.#arguments;
}

Expand All @@ -51,7 +51,7 @@ export abstract class Block {
*
* @param key key
*/
getArgument(key: string): any {
getArgument<K extends keyof Args>(key: K): Args[K] {
return this.#arguments[key];
}

Expand All @@ -61,7 +61,7 @@ export abstract class Block {
* @param key key
* @param value value
*/
setArgument(key: string, value: any): this {
setArgument<K extends keyof Args>(key: K, value: Args[K]): this {
this.#arguments[key] = value;
return this;
}
Expand All @@ -71,10 +71,11 @@ export abstract class Block {
*
* @param args arguments
*/
setArguments(args: Record<string, any>): this {
for (const key in args) {
this.#arguments[key] = args[key];
}
setArguments(args: Partial<Args>): this {
this.#arguments = {
...this.#arguments,
...args
};
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/blocks/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Block } from '.';
/**
* @category Block
*/
export class Comment extends Block {
export class Comment extends Block<Record<string, never>> {

readonly comment: string;

Expand All @@ -14,7 +14,7 @@ export class Comment extends Block {
* @param comment comment
*/
constructor(comment: string) {
super('comment', []);
super('comment', [], {});

this.comment = comment;
}
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/Data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Argument, Attribute } from '../arguments';
import { BlockArgs } from '../utils';
import { Block } from '.';

/**
Expand All @@ -18,7 +19,7 @@ export class Data extends Block {
* @param name name
* @param args arguments
*/
constructor(type: string, name: string, args?: Record<string, any>) {
constructor(type: string, name: string, args: BlockArgs) {
super('data', [type, name], args);

this.type = type;
Expand Down
15 changes: 12 additions & 3 deletions src/blocks/Import.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Argument, Attribute } from '../arguments';
import { Block } from '.';
import { Block, Resource } from '.';

/**
* @category Block
*/
export class Import extends Block {
export interface ImportArgs {
to: Resource;
id: string;
provider?: Argument;
}

/**
* @category Block
*/
export class Import extends Block<ImportArgs> {

/**
* Construct import.
Expand All @@ -13,7 +22,7 @@ export class Import extends Block {
*
* @param args arguments
*/
constructor(args?: Record<string, any>) {
constructor(args: ImportArgs) {
super('import', [], args);
}

Expand Down
Loading

0 comments on commit f063935

Please sign in to comment.