diff --git a/.gitignore b/.gitignore
index 2ccbe46..3d2bc62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
+/dist/
/node_modules/
diff --git a/index.d.ts b/index.d.ts
deleted file mode 100644
index a5db288..0000000
--- a/index.d.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-///
-import { EventEmitter } from "events";
-declare type PortNumber = number;
-declare type PortName = string;
-declare type PinName = string;
-declare type DirectionMode = "in" | "out";
-declare type GPIOValue = 0 | 1;
-interface GPIOChangeEvent {
- readonly value: GPIOValue;
- readonly port: GPIOPort;
-}
-interface GPIOChangeEventHandler {
- (event: GPIOChangeEvent): void;
-}
-export declare class GPIOAccess extends EventEmitter {
- private readonly _ports;
- onchange: GPIOChangeEventHandler | undefined;
- constructor(ports?: GPIOPortMap);
- get ports(): GPIOPortMap;
- /**
- * Unexport all exported GPIO ports.
- */
- unexportAll(): Promise;
-}
-/**
- * Different from Web GPIO API specification.
- */
-export declare class GPIOPortMap extends Map {
-}
-export declare class GPIOPort extends EventEmitter {
- private readonly _portNumber;
- private readonly _pollingInterval;
- private _direction;
- private _exported;
- private _value;
- private _timeout;
- onchange: GPIOChangeEventHandler | undefined;
- constructor(portNumber: PortNumber);
- get portNumber(): PortNumber;
- get portName(): PortName;
- get pinName(): PinName;
- get direction(): DirectionMode;
- get exported(): boolean;
- export(direction: DirectionMode): Promise;
- unexport(): Promise;
- read(): Promise;
- write(value: GPIOValue): Promise;
-}
-export declare class InvalidAccessError extends Error {
- constructor(message: string);
-}
-export declare class OperationError extends Error {
- constructor(message: string);
-}
-export declare function requestGPIOAccess(): Promise;
-export {};
diff --git a/index.js b/index.js
deleted file mode 100644
index ef4fb1a..0000000
--- a/index.js
+++ /dev/null
@@ -1,169 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-const events_1 = require("events");
-const fs_1 = require("fs");
-const path = require("path");
-/**
- * Interval of file system polling, in milliseconds.
- */
-const PollingInterval = 100;
-const SysfsGPIOPath = "/sys/class/gpio";
-const GPIOPortMapSizeMax = 1024;
-const Uint16Max = 65535;
-function parseUint16(string) {
- const n = Number.parseInt(string, 10);
- if (0 <= n && n <= Uint16Max)
- return n;
- else
- throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
-}
-class GPIOAccess extends events_1.EventEmitter {
- constructor(ports) {
- super();
- this._ports = ports == null ? new GPIOPortMap() : ports;
- this._ports.forEach(port => port.on("change", event => {
- this.emit("change", event);
- }));
- this.on("change", (event) => {
- if (this.onchange !== undefined)
- this.onchange(event);
- });
- }
- get ports() {
- return this._ports;
- }
- /**
- * Unexport all exported GPIO ports.
- */
- async unexportAll() {
- await Promise.all([...this.ports.values()].map(port => port.exported ? port.unexport() : undefined));
- }
-}
-exports.GPIOAccess = GPIOAccess;
-/**
- * Different from Web GPIO API specification.
- */
-class GPIOPortMap extends Map {
-}
-exports.GPIOPortMap = GPIOPortMap;
-class GPIOPort extends events_1.EventEmitter {
- constructor(portNumber) {
- super();
- this._portNumber = parseUint16(portNumber.toString());
- this._pollingInterval = PollingInterval;
- this._direction = new OperationError("Unknown direction.");
- this._exported = new OperationError("Unknown export.");
- this.on("change", (event) => {
- if (this.onchange !== undefined)
- this.onchange(event);
- });
- }
- get portNumber() {
- return this._portNumber;
- }
- get portName() {
- return `gpio${this.portNumber}`;
- }
- get pinName() {
- // NOTE: Unknown pinName.
- return "";
- }
- get direction() {
- if (this._direction instanceof OperationError)
- throw this._direction;
- return this._direction;
- }
- get exported() {
- if (this._exported instanceof OperationError)
- throw this._exported;
- return this._exported;
- }
- async export(direction) {
- if (!/^(in|out)$/.test(direction)) {
- throw new InvalidAccessError(`Must be "in" or "out".`);
- }
- try {
- await fs_1.promises.access(path.join(SysfsGPIOPath, this.portName));
- this._exported = true;
- }
- catch {
- this._exported = false;
- }
- try {
- clearInterval(this._timeout);
- if (!this.exported) {
- await fs_1.promises.writeFile(path.join(SysfsGPIOPath, "export"), String(this.portNumber));
- }
- await fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "direction"), direction);
- if (direction === "in") {
- this._timeout = setInterval(this.read.bind(this), this._pollingInterval);
- }
- }
- catch (error) {
- throw new OperationError(error);
- }
- this._direction = direction;
- this._exported = true;
- }
- async unexport() {
- clearInterval(this._timeout);
- try {
- await fs_1.promises.writeFile(path.join(SysfsGPIOPath, "unexport"), String(this.portNumber));
- }
- catch (error) {
- throw new OperationError(error);
- }
- this._exported = false;
- }
- async read() {
- if (!(this.exported && this.direction === "in")) {
- throw new InvalidAccessError(`The exported must be true and value of direction must be "in".`);
- }
- try {
- const buffer = await fs_1.promises.readFile(path.join(SysfsGPIOPath, this.portName, "value"));
- const value = parseUint16(buffer.toString());
- if (this._value !== value) {
- this._value = value;
- this.emit("change", { value, port: this });
- }
- return value;
- }
- catch (error) {
- throw new OperationError(error);
- }
- }
- async write(value) {
- if (!(this.exported && this.direction === "out")) {
- throw new InvalidAccessError(`The exported must be true and value of direction must be "out".`);
- }
- try {
- await fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "value"), parseUint16(value.toString()).toString());
- }
- catch (error) {
- throw new OperationError(error);
- }
- }
-}
-exports.GPIOPort = GPIOPort;
-class InvalidAccessError extends Error {
- constructor(message) {
- super(message);
- this.name = this.constructor.name;
- }
-}
-exports.InvalidAccessError = InvalidAccessError;
-class OperationError extends Error {
- constructor(message) {
- super(message);
- this.name = this.constructor.name;
- }
-}
-exports.OperationError = OperationError;
-async function requestGPIOAccess() {
- const ports = new GPIOPortMap([...Array(GPIOPortMapSizeMax).keys()].map(portNumber => [
- portNumber,
- new GPIOPort(portNumber)
- ]));
- return new GPIOAccess(ports);
-}
-exports.requestGPIOAccess = requestGPIOAccess;
diff --git a/package.json b/package.json
index 2007bd0..18b8c9c 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,11 @@
"name": "node-web-gpio",
"version": "1.0.0",
"description": "GPIO access with Node.js",
- "main": "index.js",
+ "main": "dist/index.js",
+ "types": "dist/index.d.ts",
+ "files": [
+ "dist/*"
+ ],
"repository": {
"type": "git",
"url": "https://github.com/chirimen-oh/node-web-gpio.git"
@@ -14,7 +18,8 @@
"typescript": "^4.0.2"
},
"scripts": {
- "build": "tsc"
+ "build": "tsc",
+ "prepublish": "rm -rf dist && yarn build"
},
"keywords": [
"gpio",
diff --git a/tsconfig.json b/tsconfig.json
index 71ed46f..2c499bc 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,5 +1,6 @@
{
"compilerOptions": {
+ "outDir": "dist",
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",