Skip to content

Commit

Permalink
feat: added support for gitlab
Browse files Browse the repository at this point in the history
- renamed fs to dotenv
- added better methods of testing locally
- migrated source files to a 'src' subdirectory to make microbundle happier
- fixed an issue that would cause errors to get obfuscated when logged
  • Loading branch information
cecilia-sanare committed Aug 11, 2023
1 parent f97c5f5 commit f90436e
Show file tree
Hide file tree
Showing 38 changed files with 788 additions and 153 deletions.
30 changes: 30 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,33 @@ $ yarn
```sh
$ yarn test
```

## Manually testing locally

- Create a `.env` file

```properties
# For GitLab.Source & GitLab.Target
GITLAB_TOKEN=glpat-A6_D157s9cnYzFaZx5ec

# For GitHub.Target
GH_TOKEN=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

# For AWS.Source
AWS_ACCESS_KEY_ID=<aws-access-key>
AWS_SECRET_ACCESS_KEY=<aws-secret-access-key>
```

- In one terminal start the watch script

```sh
# This will recompile the modules anytime the code changes
$ yarn watch
```

- In another terminal start the demo

```sh
# This runs the demo only once
$ yarn start
```
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
"packages/*"
],
"scripts": {
"start": "yarn workspace @refreshly/demo start",
"watch": "watch 'yarn build' ./packages/*/src -du",
"test": "jest",
"test:coverage": "jest --coverage",
"lint": "eslint 'packages/**/*.ts'",
"build": "yarn workspaces foreach run build"
"build": "yarn workspaces foreach -pt run build"
},
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/node": "^20.4.8",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"concurrently": "^8.2.0",
"eslint": "^8.46.0",
"eslint-plugin-unused-imports": "^3.0.0",
"jest": "^29.6.2",
"microbundle": "^0.15.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"watch": "^1.0.2"
}
}
2 changes: 1 addition & 1 deletion packages/aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packageManager": "[email protected]",
"description": "Simply a key rotation tool!~ ❤️",
"main": "dist/index.js",
"source": "index.ts",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"scripts": {
"build": "microbundle --target node -f cjs"
Expand Down
19 changes: 15 additions & 4 deletions packages/aws/index.ts → packages/aws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SourceModule, KeyInfo, getEnv, prefix, Logger, PartiallyRequired } from
class AWSSourceModule extends SourceModule {
protected declare options: PartiallyRequired<AWSSourceModule.Options, 'key' | 'secretKey'>;

private accessKey?: AccessKey;
private accessKey?: PartiallyRequired<AccessKey, 'AccessKeyId' | 'SecretAccessKey'>;

constructor({ targets, key, secretKey, ...options }: AWSSourceModule.Options) {
super({ targets });
Expand Down Expand Up @@ -53,16 +53,27 @@ class AWSSourceModule extends SourceModule {
// 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;
// ... why are these all specified as potentially being undefined?...
// In what scenario would I create an access token, and not get one back that doesn't result in an error...
if (!AccessKey || !AccessKey.AccessKeyId || !AccessKey.SecretAccessKey) {
throw new Error('Access key was unexpectedly undefined after creating it!');
}

// This is stupid, but for some reason I have to destruct it in order to get typescript to realize they are in-fact... defined
this.accessKey = {
...AccessKey,
AccessKeyId: AccessKey.AccessKeyId,
SecretAccessKey: AccessKey.SecretAccessKey,
};

return [
{
name: prefix(this.options.prefix, 'AWS_ACCESS_KEY_ID'),
value: this.accessKey.AccessKeyId,
value: AccessKey.AccessKeyId,
},
{
name: prefix(this.options.prefix, 'AWS_SECRET_ACCESS_KEY'),
value: this.accessKey.SecretAccessKey,
value: AccessKey.SecretAccessKey,
},
];
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/@types/required.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyInfo, Refreshly, SourceModule, getEnv, prefix } from '../index';
import { KeyInfo, Refreshly, SourceModule, getEnv, prefix } from '../src/index';

describe('@refreshly/core', () => {
describe('func(Refreshly)', () => {
Expand Down
7 changes: 0 additions & 7 deletions packages/core/fs/index.ts

This file was deleted.

42 changes: 0 additions & 42 deletions packages/core/fs/source.ts

This file was deleted.

28 changes: 0 additions & 28 deletions packages/core/fs/target.ts

This file was deleted.

39 changes: 0 additions & 39 deletions packages/core/fs/utils/dotenv.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"packageManager": "[email protected]",
"description": "Simply a key rotation tool!~ ❤️",
"main": "dist/index.js",
"source": "index.ts",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"scripts": {
"build": "microbundle --target node -f cjs"
},
"dependencies": {
"@rain-cafe/logger": "^1.0.2"
"@rain-cafe/logger": "^1.0.3"
},
"publishConfig": {
"access": "public"
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/dotenv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DotEnvSourceModule } from './source';
import { DotEnvTargetModule } from './target';

/**
* @deprecated Please use {@link DotEnv} instead! :3
*/
export const FS = {
Source: DotEnvSourceModule,
Target: DotEnvTargetModule,
};

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

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

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

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

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

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

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

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

async revert(): Promise<void> {}

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

namespace DotEnvSourceModule {
export type Options = {
file: string;
properties?: string[];
} & SourceModule.Options;
}
28 changes: 28 additions & 0 deletions packages/core/src/dotenv/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 DotEnvTargetModule implements ITargetModule {
private options: DotEnvTargetModule.Options;

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

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

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

export namespace DotEnvTargetModule {
export type Options = {
file: string;
};
}
Loading

0 comments on commit f90436e

Please sign in to comment.