Skip to content

Commit

Permalink
fix(aws): resolved inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Aug 9, 2023
1 parent 7ace355 commit f1658ea
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/aws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class AWSSourceModule extends SourceModule {
get originalKeyInfos(): KeyInfo[] {
return [
{
name: 'AWS_ACCESS_KEY_ID',
name: prefix(this.options.prefix, 'AWS_ACCESS_KEY_ID'),
value: this.options.key,
},
{
name: 'AWS_SECRET_ACCESS_KEY',
name: prefix(this.options.prefix, 'AWS_SECRET_ACCESS_KEY'),
value: this.options.secretKey,
},
];
Expand All @@ -49,6 +49,10 @@ class AWSSourceModule extends SourceModule {
})
);

// Why the hell do I have to arbitrarily wait?
// What is propagating on Amazon's backend that results in the token not being immediately usable?
await new Promise((resolve) => setTimeout(resolve, 7000));

this.accessKey = AccessKey;

return [
Expand Down
2 changes: 1 addition & 1 deletion packages/core/@types/source-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class SourceModule {
await Promise.all(
this.options.targets.map(async (target) => {
Logger.silly(`(${target.name}) Reverting...`);
await target.revert(this.originalKeyInfos);
await target.target(this.originalKeyInfos);
Logger.silly(`(${target.name}) Successfully reverted!`);
})
);
Expand Down
1 change: 0 additions & 1 deletion packages/core/@types/target-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export interface ITargetModule {
get name(): string;

target(keyInfos: KeyInfo[]): Promise<void>;
revert(keyInfos: KeyInfo[]): Promise<void>;
}
7 changes: 7 additions & 0 deletions packages/core/fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FSSourceModule } from './source';
import { FSTargetModule } from './target';

export const FS = {
Source: FSSourceModule,
Target: FSTargetModule,
};
42 changes: 42 additions & 0 deletions packages/core/fs/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'node:path';
import { SourceModule, KeyInfo } from '../@types';
import { read, readSync } from './utils/dotenv';

export class FSSourceModule extends SourceModule {
protected declare options: FSSourceModule.Options;
private keyInfos: KeyInfo[];

constructor({ targets, file, ...options }: FSSourceModule.Options) {
super({ targets });

this.options = {
...this.options,
...options,
file: path.join(process.cwd(), file),
};

this.keyInfos = readSync(this.options.file);
}

get name(): string {
return 'fs';
}

get originalKeyInfos(): KeyInfo[] {
return this.keyInfos;
}

async source(): Promise<KeyInfo[]> {
return await read(this.options.file);
}

async revert(): Promise<void> {}

async cleanup(): Promise<void> {}
}

namespace FSSourceModule {
export type Options = {
file: string;
} & SourceModule.Options;
}
28 changes: 28 additions & 0 deletions packages/core/fs/target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ITargetModule, KeyInfo } from '../@types';
import path from 'node:path';
import { merge } from './utils/dotenv';

export class FSTargetModule implements ITargetModule {
private options: FSTargetModule.Options;

constructor({ file, ...options }: FSTargetModule.Options) {
this.options = {
...options,
file: path.join(process.cwd(), file),
};
}

get name(): string {
return 'fs';
}

async target(keyInfos: KeyInfo[]): Promise<void> {
return await merge(this.options.file, keyInfos);
}
}

export namespace FSTargetModule {
export interface Options {
file: string;
}
}
39 changes: 39 additions & 0 deletions packages/core/fs/utils/dotenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { KeyInfo } from '../..';
import * as fs from 'node:fs/promises';
import { readFileSync } from 'node:fs';
import { keyInfoToKeyValue, keyValueToKeyInfo } from './keyinfo';

export async function read(path: string): Promise<KeyInfo[]> {
const content = await fs.readFile(path, {
encoding: 'utf-8',
});

return content.split(/\r?\n/).filter(Boolean).map<KeyInfo>(keyValueToKeyInfo);
}

export function readSync(path: string): KeyInfo[] {
const content = readFileSync(path, {
encoding: 'utf-8',
});

return content.split(/\r?\n/).filter(Boolean).map<KeyInfo>(keyValueToKeyInfo);
}

export async function write(path: string, keyInfos: KeyInfo[]): Promise<void> {
await fs.writeFile(path, keyInfos.map(keyInfoToKeyValue).join('\n'), {
encoding: 'utf-8',
});
}

export async function merge(path: string, keyInfos: KeyInfo[]): Promise<void> {
const existingKeyInfos = await read(path);
const mergedKeyInfos: KeyInfo[] = [...keyInfos];

for (const existingKeyInfo of existingKeyInfos) {
if (mergedKeyInfos.find((keyInfo) => keyInfo.name === existingKeyInfo.name)) continue;

mergedKeyInfos.push(existingKeyInfo);
}

await write(path, mergedKeyInfos);
}
14 changes: 14 additions & 0 deletions packages/core/fs/utils/keyinfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { KeyInfo } from '../../@types/key-info';

export function keyInfoToKeyValue(keyInfo: KeyInfo): string {
return `${keyInfo.name}=${keyInfo.value}`;
}

export function keyValueToKeyInfo(keyValue: string): KeyInfo {
const [name, value] = keyValue.split('=');

return {
name,
value,
};
}
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export function prefix(...values: string[]): string {
}

export * from './@types';
export * from './fs';
export { LogLevel, Logger };
2 changes: 1 addition & 1 deletion packages/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GitHubTargetModule implements ITargetModule {
}

get name(): string {
return 'github';
return `github:${this.options.org}`;
}

private async getOrgPublicKey(org: string): Promise<{ key_id: string; key: string }> {
Expand Down

0 comments on commit f1658ea

Please sign in to comment.