Skip to content

Commit

Permalink
Merge main into release
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jan 3, 2024
2 parents 4c475bb + 3463bc0 commit a4249e3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 39 deletions.
28 changes: 14 additions & 14 deletions docs/code/classes/types_config.UpdatableConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Updatable AlgoKit config

#### Defined in

[src/types/config.ts:77](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L77)
[src/types/config.ts:75](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L75)

## Properties

Expand All @@ -58,7 +58,7 @@ Updatable AlgoKit config

#### Defined in

[src/types/config.ts:24](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L24)
[src/types/config.ts:22](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L22)

## Accessors

Expand All @@ -76,7 +76,7 @@ Readonly.debug

#### Defined in

[src/types/config.ts:30](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L30)
[src/types/config.ts:28](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L28)

___

Expand All @@ -94,7 +94,7 @@ Readonly.logger

#### Defined in

[src/types/config.ts:26](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L26)
[src/types/config.ts:24](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L24)

___

Expand All @@ -112,7 +112,7 @@ Readonly.maxSearchDepth

#### Defined in

[src/types/config.ts:46](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L46)
[src/types/config.ts:44](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L44)

___

Expand All @@ -130,7 +130,7 @@ Readonly.projectRoot

#### Defined in

[src/types/config.ts:34](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L34)
[src/types/config.ts:32](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L32)

___

Expand All @@ -148,7 +148,7 @@ Readonly.traceAll

#### Defined in

[src/types/config.ts:38](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L38)
[src/types/config.ts:36](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L36)

___

Expand All @@ -166,7 +166,7 @@ Readonly.traceBufferSizeMb

#### Defined in

[src/types/config.ts:42](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L42)
[src/types/config.ts:40](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L40)

## Methods

Expand All @@ -188,24 +188,24 @@ Update the AlgoKit configuration with your own configuration settings

#### Defined in

[src/types/config.ts:118](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L118)
[src/types/config.ts:127](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L127)

___

### configureProjectRoot

**configureProjectRoot**(): `void`
**configureProjectRoot**(): `Promise`\<`void`\>

Configures the project root by searching for a specific file within a depth limit.
This is only supported in a Node environment.

#### Returns

`void`
`Promise`\<`void`\>

#### Defined in

[src/types/config.ts:93](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L93)
[src/types/config.ts:94](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L94)

___

Expand All @@ -229,7 +229,7 @@ The requested logger

#### Defined in

[src/types/config.ts:55](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L55)
[src/types/config.ts:53](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L53)

___

Expand All @@ -251,4 +251,4 @@ Temporarily run with debug set to true.

#### Defined in

[src/types/config.ts:67](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L67)
[src/types/config.ts:65](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L65)
12 changes: 6 additions & 6 deletions docs/code/interfaces/types_config.Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Whether or not debug mode is enabled

#### Defined in

[src/types/config.ts:11](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L11)
[src/types/config.ts:9](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L9)

___

Expand All @@ -39,7 +39,7 @@ Logger

#### Defined in

[src/types/config.ts:9](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L9)
[src/types/config.ts:7](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L7)

___

Expand All @@ -51,7 +51,7 @@ The maximum depth to search for a specific file

#### Defined in

[src/types/config.ts:19](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L19)
[src/types/config.ts:17](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L17)

___

Expand All @@ -63,7 +63,7 @@ The path to the project root directory

#### Defined in

[src/types/config.ts:13](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L13)
[src/types/config.ts:11](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L11)

___

Expand All @@ -75,7 +75,7 @@ Indicates whether to trace all operations

#### Defined in

[src/types/config.ts:15](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L15)
[src/types/config.ts:13](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L13)

___

Expand All @@ -87,4 +87,4 @@ The size of the trace buffer in megabytes

#### Defined in

[src/types/config.ts:17](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L17)
[src/types/config.ts:15](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/types/config.ts#L15)
31 changes: 22 additions & 9 deletions src/debugging.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import algosdk from 'algosdk'
import * as crypto from 'crypto'
import * as fs from 'fs'
import * as path from 'path'
import { Config, compileTeal } from '.'
import { performAtomicTransactionComposerSimulate } from './transaction'
import { CompiledTeal } from './types/app'
Expand All @@ -11,6 +9,7 @@ import {
PersistSourceMapsParams,
SimulateAndPersistResponseParams,
} from './types/debugging'
import { isNode } from './util'

const ALGOKIT_DIR = '.algokit'
const SOURCES_DIR = 'sources'
Expand All @@ -31,6 +30,7 @@ interface ErrnoException extends Error {

async function loadOrCreateSources(sourcesPath: string): Promise<AVMDebuggerSourceMap> {
try {
const fs = await import('fs')
const data = JSON.parse(await fs.promises.readFile(sourcesPath, 'utf8'))
return AVMDebuggerSourceMap.fromDict(data)
} catch (error: unknown) {
Expand All @@ -45,6 +45,9 @@ async function loadOrCreateSources(sourcesPath: string): Promise<AVMDebuggerSour
}

async function upsertDebugSourcemaps(sourceMaps: AVMDebuggerSourceMapEntry[], projectRoot: string): Promise<void> {
const path = await import('path')
const fs = await import('fs')

const sourcesPath = path.join(projectRoot, ALGOKIT_DIR, SOURCES_DIR, SOURCES_FILE)
const sources = await loadOrCreateSources(sourcesPath)

Expand Down Expand Up @@ -76,6 +79,9 @@ async function upsertDebugSourcemaps(sourceMaps: AVMDebuggerSourceMapEntry[], pr
}

async function writeToFile(filePath: string, content: string): Promise<void> {
const path = await import('path')
const fs = await import('fs')

await fs.promises.mkdir(path.dirname(filePath), { recursive: true })
await fs.promises.writeFile(filePath, content, 'utf8')
}
Expand All @@ -100,6 +106,7 @@ async function buildAVMSourcemap({
if (!rawTeal && !compiledTeal) {
throw new Error('Either rawTeal or compiledTeal must be provided.')
}
const path = await import('path')

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const result = rawTeal ? await compileTeal(rawTeal, client) : compiledTeal!
Expand All @@ -119,11 +126,6 @@ async function buildAVMSourcemap({
return new AVMDebuggerSourceMapEntry(sourceMapOutputPath, programHash)
}

// simple function checking whether this is running in node or browser environment
function isNode(): boolean {
return typeof window === 'undefined'
}

// === Public facing methods ===

/**
Expand Down Expand Up @@ -189,6 +191,9 @@ export async function simulateAndPersistResponse({ atc, projectRoot, algod, buff
throw new Error('Sourcemaps can only be persisted in Node.js environment.')
}

const fs = await import('fs')
const path = await import('path')

try {
const atcToSimulate = atc.clone()
const simulateResult = await performAtomicTransactionComposerSimulate(atcToSimulate, algod)
Expand All @@ -209,8 +214,16 @@ export async function simulateAndPersistResponse({ atc, projectRoot, algod, buff
const outputFileName = `${timestamp}_lr${simulateResult.lastRound}_${txnTypesStr}${TRACES_FILE_EXT}`
const outputFilePath = path.join(outputRootDir, outputFileName)

if (!fs.existsSync(path.dirname(outputFilePath))) {
await fs.promises.mkdir(path.dirname(outputFilePath), { recursive: true })
try {
await fs.promises.access(path.dirname(outputFilePath))
} catch (error: unknown) {
const err = error as ErrnoException

if (err.code === 'ENOENT') {
await fs.promises.mkdir(path.dirname(outputFilePath), { recursive: true })
} else {
throw err
}
}

// cleanup old files if buffer size is exceeded
Expand Down
29 changes: 19 additions & 10 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { existsSync } from 'fs'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { isNode } from '../util'
import { Logger, consoleLogger, nullLogger } from './logging'

/** The AlgoKit configuration type */
Expand Down Expand Up @@ -83,31 +81,42 @@ export class UpdatableConfig implements Readonly<Config> {
traceBufferSizeMb: 256,
maxSearchDepth: 10,
}
this.configureProjectRoot()

if (isNode()) {
this.configureProjectRoot()
}
}

/**
* Configures the project root by searching for a specific file within a depth limit.
* This is only supported in a Node environment.
*/
private configureProjectRoot() {
// fileURLToPath and dirname is only available in Node, hence the check
private async configureProjectRoot() {
if (!isNode()) {
throw new Error('`configureProjectRoot` can only be called in Node.js environment.')
}

const fs = await import('fs')
const path = await import('path')
const url = await import('url')

const _dirname =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unreachable code error
// eslint-disable-next-line no-restricted-syntax
typeof __dirname !== 'undefined' ? __dirname : fileURLToPath && dirname ? dirname(fileURLToPath(import.meta.url)) : undefined
typeof __dirname !== 'undefined' ? __dirname : path.dirname(url.fileURLToPath(import.meta.url))

if (!_dirname) {
return
}

let currentPath = resolve(_dirname)
let currentPath = path.resolve(_dirname)
for (let i = 0; i < this.config.maxSearchDepth; i++) {
if (existsSync(`${currentPath}/.algokit.toml`)) {
if (fs.existsSync(`${currentPath}/.algokit.toml`)) {
this.config.projectRoot = currentPath
break
}
currentPath = dirname(currentPath)
currentPath = path.dirname(currentPath)
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ export const calculateFundAmount = (
return null
}
}

/**
* Checks if the current environment is Node.js
*
* @returns A boolean indicating whether the current environment is Node.js
*/
export const isNode = () => {
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null
}

0 comments on commit a4249e3

Please sign in to comment.