-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ace355
commit f1658ea
Showing
10 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters