Skip to content

Commit

Permalink
JS-based command line tool (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauckhart committed Oct 12, 2024
1 parent 881afaa commit 70e973a
Show file tree
Hide file tree
Showing 55 changed files with 1,838 additions and 82 deletions.
3 changes: 0 additions & 3 deletions codegen/bin/repl.js

This file was deleted.

5 changes: 2 additions & 3 deletions codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"clean": "matter-build clean",
"build": "matter-build",
"build-clean": "matter-build --clean",
"console": "matter-run bin/repl.ts",
"generate-spec": "matter-run bin/generate-spec.js",
"generate-chip": "matter-run bin/generate-chip.js",
"generate-model": "matter-run bin/generate-model.js",
Expand All @@ -33,9 +32,9 @@
"homepage": "https://github.com/project-chip/matter.js#readme",
"dependencies": {
"@matter/general": "*",
"@matter/intermediate-models": "*",
"@matter/model": "*",
"@matter/tools": "*",
"@matter/intermediate-models": "*"
"@matter/tools": "*"
},
"devDependencies": {
"@types/jsdom": "^21.1.6",
Expand Down
28 changes: 0 additions & 28 deletions codegen/src/repl.ts

This file was deleted.

4 changes: 2 additions & 2 deletions codegen/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"path": "../../packages/general/src"
},
{
"path": "../../packages/model/src"
"path": "../../models/src"
},
{
"path": "../../models/src"
"path": "../../packages/model/src"
}
]
}
4 changes: 2 additions & 2 deletions models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/project-chip/matter.js#readme",
"devDependencies": {
"@matter/tools": "*",
"@matter/model": "*"
"@matter/model": "*",
"@matter/tools": "*"
}
}
74 changes: 73 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"packages/nodejs-shell",
"packages/examples",
"packages/react-native",
"packages/cli-tool",
"models",
"codegen",
"chip-testing",
Expand All @@ -40,7 +41,7 @@
"matter-multidevice": "matter-run packages/examples/src/examples/MultiDeviceNode.ts",
"matter-controller": "matter-run packages/examples/src/examples/ControllerNode.ts",
"shell": "matter-run packages/nodejs-shell/src/app.ts"
},
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.18.0",
Expand Down
1 change: 1 addition & 0 deletions packages/cli-tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A matter.js-based command line utility for interacting with Matter.
3 changes: 3 additions & 0 deletions packages/cli-tool/bin/matter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env matter-run
import { main } from "../dist/esm/cli.js";
await main(process.argv);
69 changes: 69 additions & 0 deletions packages/cli-tool/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@matter/cli-tool",
"version": "0.0.0-git",
"description": "Command line tool for interacting with Matter",
"keywords": [
"iot",
"home automation",
"matter",
"smart device"
],
"license": "Apache-2.0",
"author": "matter.js authors",
"contributors": [
"Greg Lauckhart <[email protected]>"
],
"bugs": {
"url": "https://github.com/project-chip/matter.js/issues"
},
"homepage": "https://github.com/project-chip/matter.js",
"repository": {
"type": "git",
"url": "git+https://github.com/project-chip/matter.js.git"
},
"scripts": {
"clean": "matter-build clean",
"build": "matter-build",
"build-clean": "matter-build --clean",
"matter": "matter-run bin/matter.js"
},
"bin": {
"matter": "bin/matter.js"
},
"imports": {
"#tools": "@matter/tools",
"#general": "@matter/general",
"#model": "@matter/model",
"#types": "@matter/types",
"#protocol": "@matter/protocol",
"#node": "@matter/node",
"#*": "./src/*"
},
"dependencies": {
"@matter/general": "*",
"@matter/model": "*",
"@matter/node": "*",
"@matter/protocol": "*",
"@matter/tools": "*",
"@matter/types": "*",
"@types/escodegen": "^0.0.10",
"acorn": "^8.12.1",
"ansi-colors": "^4.1.3",
"escodegen": "^2.1.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@matter/tools": "*",
"@types/yargs": "^17.0.33"
},
"files": [
"dist/**/*",
"src/**/*",
"LICENSE",
"README.md"
],
"type": "module",
"publishConfig": {
"access": "public"
}
}
21 changes: 21 additions & 0 deletions packages/cli-tool/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { repl } from "#repl.js";
import colors from "ansi-colors";
import { stdout } from "process";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

export async function main(argv: string[]) {
colors.enabled = stdout.isTTY;

await yargs(hideBin(argv)).usage("Interact with the local Matter environment.").strict().argv;

// TODO - REPL is enough for testing but need proper CLI

await repl();
}
19 changes: 19 additions & 0 deletions packages/cli-tool/src/commands/cat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { bin } from "#globals.js";
import { stdout } from "process";
import { inspect } from "util";

bin.cat = async function (...args: unknown[]) {
const locations = await Promise.all(args.map(arg => this.location.at(`${arg}`)));
for (const location of locations) {
stdout.write(inspect(location.definition, false, 1, stdout.isTTY));
stdout.write("\n");
}
};

bin.inspect = bin.cat;
28 changes: 28 additions & 0 deletions packages/cli-tool/src/commands/cd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { NotADirectoryError, TooManyArgsError } from "#errors.js";
import { bin } from "#globals.js";

bin.cd = async function (path, ...extraArgs: unknown[]) {
if (extraArgs.length) {
throw new TooManyArgsError("cd");
}

if (path === undefined) {
path = "/";
} else {
path = `${path}`;
}

const location = await this.location.at(path as string);

if (location.kind !== "directory") {
throw new NotADirectoryError(path);
}

this.location = location;
};
18 changes: 18 additions & 0 deletions packages/cli-tool/src/commands/cwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { TooManyArgsError } from "#errors.js";
import { bin } from "#globals.js";
import { stdout } from "process";

bin.cwd = function (...args: unknown[]) {
if (args.length) {
throw new TooManyArgsError("cwd");
}

stdout.write(this.location.path);
stdout.write("\n");
};
14 changes: 14 additions & 0 deletions packages/cli-tool/src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { bin } from "#globals.js";

bin.help = function () {
// TODO - be helpful
process.stdout.write("Help is on the way!\n");
};

bin.man = bin.help;
Loading

0 comments on commit 70e973a

Please sign in to comment.