Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Prep for npm packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed Nov 21, 2023
1 parent cb79153 commit d4ed3a6
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 142 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
8 changes: 8 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true,
"all": true,
"include": ["src/**/*.[tj]s?(x)"],
"reporter": ["html", "lcov", "text", "text-summary"],
"report-dir": "coverage"
}
2 changes: 0 additions & 2 deletions extension/src/activateAvmDebug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import * as vscode from 'vscode';
import { AvmDebugConfigProvider } from './configuration';

Expand Down
2 changes: 0 additions & 2 deletions extension/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import * as vscode from 'vscode';
import {
WorkspaceFolder,
Expand Down
2 changes: 0 additions & 2 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import * as vscode from 'vscode';
import { activateAvmDebug } from './activateAvmDebug';
import { ServerDebugAdapterFactory } from './serverDescriptorFactory';
Expand Down
4 changes: 1 addition & 3 deletions extension/src/fileAccessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

import * as vscode from 'vscode';
import { FileAccessor } from '../../src';
import { FileAccessor } from '../../src/common';

export const workspaceFileAccessor: FileAccessor = {
isWindows: typeof process !== 'undefined' && process.platform === 'win32',
Expand Down
4 changes: 1 addition & 3 deletions extension/src/internalDescriptorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

import * as vscode from 'vscode';
import { ProviderResult } from 'vscode';
import { AvmDebugSession } from '../../src';
import { AvmDebugSession } from '../../src/common';
import { workspaceFileAccessor } from './fileAccessor';

export class InlineDebugAdapterFactory
Expand Down
4 changes: 1 addition & 3 deletions extension/src/serverDescriptorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

import * as Net from 'net';
import * as vscode from 'vscode';
import { ProviderResult } from 'vscode';
import { AvmDebugSession } from '../../src';
import { AvmDebugSession } from '../../src/common';
import { workspaceFileAccessor } from './fileAccessor';

export class ServerDebugAdapterFactory
Expand Down
51 changes: 24 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "avm-debug",
"name": "avm-debug-adapter",
"version": "0.1.0",
"description": "Algorand AVM transaction and smart contract debugger using the Debugger Adapter Protocol",
"author": {
Expand All @@ -16,25 +16,40 @@
"bugs": {
"url": "https://github.com/algorand/avm-debugger/issues"
},
"exports": {
".": {
"types": "./dist/common/index.d.ts",
"default": "./dist/common/index.js"
},
"./node": {
"types": "./dist/node/index.d.ts",
"default": "./dist/node/index.js"
}
},
"bin": {
"avm-debug": "./dist/cli.js"
},
"files": [
"dist"
],
"scripts": {
"compile": "tsc -p ./",
"compile": "rimraf out && rimraf dist && tsc -p ./ && cp -R out/src dist",
"lint": "eslint src --ext ts",
"typecheck": "tsc -p tsconfig.json --noEmit",
"check-format": "prettier . --check",
"format": "prettier . --write",
"esbuild-base": "esbuild ./extension/src/extension.ts --bundle --tsconfig=./tsconfig.json --external:vscode --format=cjs --platform=node --outfile=extension/dist/extension.js",
"watch": "npm run -S esbuild-base -- --sourcemap --sources-content=false --watch",
"esbuild-web": "esbuild ./extension/src/web-extension.ts --bundle --tsconfig=./tsconfig.json --external:vscode --format=cjs --platform=browser --outfile=extension/dist/web-extension.js",
"watch-web": "npm run -S esbuild-web -- --sourcemap --sources-content=false --watch",
"build": "npm run -S esbuild-base -- --sourcemap --sources-content=false && npm run -S esbuild-web -- --sourcemap --sources-content=false",
"extension:esbuild-base": "esbuild ./extension/src/extension.ts --bundle --tsconfig=./tsconfig.json --external:vscode --format=cjs --platform=node --outfile=extension/dist/extension.js",
"extension:watch": "npm run -S extension:esbuild-base -- --sourcemap --sources-content=false --watch",
"extension:esbuild-web": "esbuild ./extension/src/web-extension.ts --bundle --tsconfig=./tsconfig.json --external:vscode --format=cjs --platform=browser --outfile=extension/dist/web-extension.js",
"extension:watch-web": "npm run -S extension:esbuild-web -- --sourcemap --sources-content=false --watch",
"extension:build": "npm run -S extension:esbuild-base -- --sourcemap --sources-content=false && npm run -S extension:esbuild-web -- --sourcemap --sources-content=false",
"package": "vsce package",
"publish": "vsce publish",
"publish-pre-release": "vsce publish --pre-release",
"vscode:prepublish": "rimraf dist && npm run -S esbuild-base -- --minify && npm run -S esbuild-web -- --minify",
"vscode:prepublish": "rimraf extension/dist && npm run -S esbuild-base -- --minify && npm run -S esbuild-web -- --minify",
"test": "ts-mocha -p tsconfig.json tests/*test.ts --timeout 30s --diff false",
"test:coverage": "nyc npm run test"
},
"main": "./dist/debugAdapter/index.js",
"dependencies": {
"@vscode/debugadapter": "^1.64.0",
"algosdk": "github:jasonpaulos/js-algorand-sdk#teal-source-map-improvements",
Expand Down Expand Up @@ -64,23 +79,5 @@
"ts-mocha": "^10.0.0",
"typescript": "^4.6.3",
"url": "^0.11.3"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true,
"all": true,
"include": [
"src/**/!(*.test.*).[tj]s?(x)"
],
"exclude": [
"src/_tests_/**/*.*"
],
"reporter": [
"html",
"lcov",
"text",
"text-summary"
],
"report-dir": "coverage"
}
}
31 changes: 0 additions & 31 deletions src/basicServer.ts

This file was deleted.

78 changes: 32 additions & 46 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import * as fs from 'fs/promises';
import { basename } from 'path';
import * as Net from 'net';
import { FileAccessor } from './fileAccessor';
import { AvmDebugSession } from './debugSession';
#!/usr/bin/env node

/*
* cli.js is the entrypoint of the debug adapter when it runs as a separate process.
*/
import { AvmDebugSession } from './common';
import { Server, nodeFileAccessor } from './node';

function handleError(err: Error) {
console.error(err);
process.exit(1);
}

/*
* Since here we run the debug adapter as a separate ("external") process, it has no access to VS Code API.
* So we can only use node.js API for accessing files.
* cli.js is the entrypoint of the debug adapter when it runs as a separate process.
*/
const fsAccessor: FileAccessor = {
isWindows: process.platform === 'win32',
readFile(path: string): Promise<Uint8Array> {
return fs.readFile(path);
},
writeFile(path: string, contents: Uint8Array): Promise<void> {
return fs.writeFile(path, contents);
},
basename(path: string): string {
return basename(path);
},
};

async function run() {
/*
Expand All @@ -37,45 +24,44 @@ async function run() {
*/

// first parse command line arguments to see whether the debug adapter should run as a server
let port = 0;
let port: number | undefined;

const args = process.argv.slice(2);
args.forEach(function (val, index, array) {
const portMatch = /^--server=(\d{4,5})$/.exec(val);
args.forEach(function (val) {
const portMatch = /^--port=(\d+)$/.exec(val);
if (portMatch) {
port = parseInt(portMatch[1], 10);
}
});

if (port > 0) {
if (typeof port !== 'undefined') {
console.log('>> running as a server with port ' + port);

// start a server that creates a new session for every connection request
console.error(`waiting for debug protocol on port ${port}`);
const server = Net.createServer((socket) => {
console.log('>> accepted connection from client');
socket.on('error', (err) => {
throw err;
});
socket.on('end', () => {
console.error('>> client connection closed\n');
});
const session = new AvmDebugSession(fsAccessor);
session.setRunAsServer(true);
session.start(socket, socket);
}).listen(port);
server.on('error', (err) => {
throw err;

const server = new Server({
fileAccessor: nodeFileAccessor,
port,
ready: () => {
console.log(`Waiting for debug protocol on ${server.address()}`);
},
onSocketError: (err) => {
console.error('>> client connection error: ', err);
},
onServerError: handleError,
});

process.on('SIGTERM', () => {
server.close();
});
} else {
// start a single session that communicates via stdin/stdout
const session = new AvmDebugSession(fsAccessor);
const session = new AvmDebugSession(nodeFileAccessor);
process.on('SIGTERM', () => {
session.shutdown();
});
session.start(process.stdin, process.stdout);
}
}

run().catch((err) => {
console.error(err);
process.exit(1);
});
run().catch(handleError);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions src/node/fileAccessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { readFile, writeFile } from 'fs/promises';
import { basename } from 'path';
import { FileAccessor } from '../common';

export const nodeFileAccessor: FileAccessor = {
isWindows: process.platform === 'win32',
readFile(path: string): Promise<Uint8Array> {
return readFile(path);
},
writeFile(path: string, contents: Uint8Array): Promise<void> {
return writeFile(path, contents);
},
basename(path: string): string {
return basename(path);
},
};
2 changes: 2 additions & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Server } from './server';
export { nodeFileAccessor } from './fileAccessor';
43 changes: 43 additions & 0 deletions src/node/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as Net from 'net';
import { AvmDebugSession, FileAccessor } from '../common';

export class Server {
private readonly server: Net.Server;

constructor({
fileAccessor,
port,
ready,
onSocketError,
onServerError,
}: {
fileAccessor: FileAccessor;
port: number;
ready?: () => void;
onSocketError: (err: Error) => void;
onServerError: (err: Error) => void;
}) {
this.server = Net.createServer((socket) => {
const session = new AvmDebugSession(fileAccessor);
session.setRunAsServer(true);
session.start(socket, socket);
socket.on('error', onSocketError);
if (ready) {
ready();
}
}).listen(port);
this.server.on('error', onServerError);
}

address(): Net.AddressInfo {
return this.server.address() as Net.AddressInfo;
}

port(): number {
return this.address().port;
}

close() {
this.server.close();
}
}
2 changes: 1 addition & 1 deletion tests/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'assert';
import * as path from 'path';
import * as algosdk from 'algosdk';
import { DebugProtocol } from '@vscode/debugprotocol';
import { ByteArrayMap } from '../src/utils';
import { ByteArrayMap } from '../src/common/utils';
import { TestFixture, assertVariables, advanceTo, DATA_ROOT } from './testing';

describe('Debug Adapter Tests', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'assert';
import { SpawnOptions } from 'child_process';
import { DebugClient as DebugClientBase } from '@vscode/debugadapter-testsupport';
import { DebugProtocol } from '@vscode/debugprotocol';
import { ILaunchRequestArguments } from '../src/debugSession';
import { ILaunchRequestArguments } from '../src/common/debugSession';
import {
ILocation,
IPartialLocation,
Expand Down
Loading

0 comments on commit d4ed3a6

Please sign in to comment.