aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-05 18:10:44 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-05 18:10:44 +0530
commitfccd7917e62a3fbe3b0637ee630d09a9bdbf0678 (patch)
tree0488afc6f8b53972872bb7054ebc4ccea8c5151d /src
parent9a1274b41515fd2ad096818c83f34c98cc9c7656 (diff)
downloadts-types-lang-fccd7917e62a3fbe3b0637ee630d09a9bdbf0678.tar.gz
ts-types-lang-fccd7917e62a3fbe3b0637ee630d09a9bdbf0678.zip
feat: adds get env type func
Diffstat (limited to 'src')
-rw-r--r--src/index.ts5
-rw-r--r--src/runtime.ts22
2 files changed, 22 insertions, 5 deletions
diff --git a/src/index.ts b/src/index.ts
index 87eea0b..f7e00b7 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -8,6 +8,8 @@ export interface Print<_ extends any> extends EffectAtom { }
export interface WriteFile<_Path extends string, _Content extends string> extends EffectAtom { }
export interface ReadFile<_Path extends string> extends EffectAtom<string> { }
+export interface GetEnv<_Name extends string> extends EffectAtom<string> { }
+
export interface Program<Effs extends Effect, ExitCode extends number = 0> {
effects: Effs,
exitCode: ExitCode,
@@ -23,12 +25,13 @@ export interface ChainIO<Eff extends EffectAtom, Fn extends Kind1> extends Effec
chainTo: Fn
}
-export interface PrintK extends Kind1<string> {
+export interface PrintK extends Kind1<string | undefined> {
return: Print<this['input']>
}
export type main = Program<[
[1, 2, 3] extends infer Res ? Print<Res> : never,
Print<`wow: ${string} -> ${'helo'}`>,
+ ChainIO<GetEnv<"NODE_ENV">, PrintK>,
ChainIO<ReadFile<"./default.nix">, PrintK>,
]>
diff --git a/src/runtime.ts b/src/runtime.ts
index e0a055b..f664a9c 100644
--- a/src/runtime.ts
+++ b/src/runtime.ts
@@ -42,6 +42,9 @@ const addResult = (name: string, ty: string) => {
}
}
+const createHash = () =>
+ Math.random().toFixed(8).slice(2)
+
const match = <K extends string, R>(k: K | undefined, pattern: { [key in K | '_']: () => R }) =>
k && pattern[k] ? pattern[k]() : pattern._()
@@ -53,7 +56,7 @@ const accumulateResults = (effTyp: Type, node: Node): string[] => {
const [pathTyp] = effTyp.getTypeArguments()
const filePath = JSON.parse(typeToString(pathTyp))
const contents = fs.readFileSync(filePath, 'utf-8')
- const hash = Math.random().toFixed(8).slice(2)
+ const hash = createHash()
addResult(hash, JSON.stringify(contents))
return [hash]
},
@@ -64,8 +67,16 @@ const accumulateResults = (effTyp: Type, node: Node): string[] => {
return [...(inputResults ?? [])]
},
+ GetEnv: () => {
+ const [envTyp] = effTyp.getTypeArguments()
+ const envName = JSON.parse(typeToString(envTyp))
+ const hash = createHash()
+ addResult(hash, `${JSON.stringify(process.env[envName] ?? '')}`)
+ return [hash]
+ },
+
_: () => {
- console.log(`${name} effect is unhandled`)
+ console.log(`${name} result effect is unhandled`)
return []
},
})
@@ -84,24 +95,26 @@ const evalAccumulator = (effNode: Node, node: Node) => {
const [hash] = accumulateResults(effTyp, node)
effNode.replaceWithText(`${RESULT_TYPE_NAME}[${JSON.stringify(hash)}]`)
},
+
WriteFile: () => {
const [pathTyp, contentsTyp] = effTyp.getTypeArguments()
const filePath = JSON.parse(typeToString(pathTyp))
const contents = JSON.parse(typeToString(contentsTyp))
fs.writeFileSync(filePath, contents)
},
+
ChainIO: () => {
const inputTyp = effTyp.getProperty('input')?.getTypeAtLocation(node)
const chainToKind = effTyp.getProperty('chainTo')?.getTypeAtLocation(node)
const [hashRes] = inputTyp ? accumulateResults(inputTyp, node) : []
const chainRes = `(${typeToString(chainToKind)} & { input: ${RESULT_TYPE_NAME}[${JSON.stringify(hashRes)}]['output'] })['return']`
-
const updateEffNode = effNode.replaceWithText(chainRes)
-
evalAccumulator(updateEffNode, node)
},
_: () => {
+ console.log(effNode.print())
+ console.log('TTTT', typeToString(effTyp))
console.log(`${name} effect is unhandled`)
}
})
@@ -131,4 +144,5 @@ if (typeRefNode) {
}
console.log(entryPoint?.print())
+console.log(statement?.print())