Skip to content

Commit

Permalink
fix: cli
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 28, 2024
1 parent 582ae09 commit b339f77
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 148 deletions.
124 changes: 66 additions & 58 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,72 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.build = build;
const fs_1 = require("fs");
const types_1 = require("./types");
const glob_1 = require("glob");
const chalk_1 = require("chalk");
const child_process_1 = require("child_process");
const util_1 = require("./util");
async function build(args, flags) {
const config = Object.assign(new types_1.Config(), JSON.parse((0, fs_1.readFileSync)("./as-test.config.json").toString()));
const pkg = JSON.parse((0, fs_1.readFileSync)("./package.json").toString());
let buildCommands = [];
if (config.buildOptions.wasi) {
if (!(0, fs_1.existsSync)("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
console.log(chalk_1.default.bgRed(" ERROR ") + chalk_1.default.dim(":") + " " + "could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!");
process.exit(1);
}
if ((pkg.dependencies && !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim"))
|| (pkg.devDependencies && !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim"))
|| (pkg.peerDependencies && !Object.keys(pkg.peerDependencies).includes("@assemblyscript/wasi-shim"))) {
if ((0, fs_1.existsSync)("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
console.log(chalk_1.default.bold.bgMagentaBright(" WARN ") + chalk_1.default.dim(": @assemblyscript/wasi-shim") + " is not included in project dependencies!");
}
}
}
const inputFiles = await (0, glob_1.glob)(config.input);
for (const file of inputFiles) {
console.log(chalk_1.default.dim("Including " + file));
let command = `npx asc ${file}${args.length ? " " + args.join(" ") : ""}`;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { existsSync, readFileSync } from "fs";
import { Config } from "./types.js";
import { glob } from "glob";
import chalk from "chalk";
import { exec } from "child_process";
import { formatTime } from "./util.js";
export function build(args, flags) {
return __awaiter(this, void 0, void 0, function* () {
const config = Object.assign(new Config(), JSON.parse(readFileSync("./as-test.config.json").toString()));
const pkg = JSON.parse(readFileSync("./package.json").toString());
let buildCommands = [];
if (config.buildOptions.wasi) {
command += " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
if (!existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
console.log(chalk.bgRed(" ERROR ") + chalk.dim(":") + " " + "could not find @assemblyscript/wasi-shim! Add it to your dependencies to run with WASI!");
process.exit(1);
}
if ((pkg.dependencies && !Object.keys(pkg.dependencies).includes("@assemblyscript/wasi-shim"))
|| (pkg.devDependencies && !Object.keys(pkg.devDependencies).includes("@assemblyscript/wasi-shim"))
|| (pkg.peerDependencies && !Object.keys(pkg.peerDependencies).includes("@assemblyscript/wasi-shim"))) {
if (existsSync("./node_modules/@assemblyscript/wasi-shim/asconfig.json")) {
console.log(chalk.bold.bgMagentaBright(" WARN ") + chalk.dim(": @assemblyscript/wasi-shim") + " is not included in project dependencies!");
}
}
}
const outFile = config.outDir + "/" + file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
if (config.outDir) {
command += " -o " + outFile;
const inputFiles = yield glob(config.input);
for (const file of inputFiles) {
console.log(chalk.dim("Including " + file));
let command = `npx asc ${file}${args.length ? " " + args.join(" ") : ""}`;
if (config.buildOptions.wasi) {
command += " --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json";
}
const outFile = config.outDir + "/" + file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
if (config.outDir) {
command += " -o " + outFile;
}
buildCommands.push(command);
}
buildCommands.push(command);
}
const build = (command) => {
return new Promise((resolve, _) => {
(0, child_process_1.exec)(command, (err, stdout, stderr) => {
if (config.buildOptions.verbose) {
process.stdout.write(stdout);
}
if (err) {
process.stderr.write(stderr + "\n");
process.exit(1);
}
resolve();
const build = (command) => {
return new Promise((resolve, _) => {
exec(command, (err, stdout, stderr) => {
if (config.buildOptions.verbose) {
process.stdout.write(stdout);
}
if (err) {
process.stderr.write(stderr + "\n");
process.exit(1);
}
resolve();
});
});
});
};
if (config.buildOptions.parallel) {
console.log(chalk_1.default.dim("Building sources in parallel..."));
const start = performance.now();
let builders = [];
for (const command of buildCommands) {
builders.push(build(command));
};
if (config.buildOptions.parallel) {
console.log(chalk.dim("Building sources in parallel..."));
const start = performance.now();
let builders = [];
for (const command of buildCommands) {
builders.push(build(command));
}
yield Promise.all(builders);
console.log(chalk.dim("Compiled in " + formatTime(performance.now() - start)) + "\n");
}
await Promise.all(builders);
console.log(chalk_1.default.dim("Compiled in " + (0, util_1.formatTime)(performance.now() - start)) + "\n");
}
});
}
60 changes: 29 additions & 31 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const package_json_1 = require("../package.json");
const build_1 = require("./build");
const run_1 = require("./run");
import chalk from "chalk";
import { build } from "./build.js";
import { run } from "./run.js";
const _args = process.argv.slice(2);
const flags = [];
const args = [];
Expand All @@ -12,6 +9,7 @@ const COMMANDS = [
"build",
"test"
];
const version = "0.1.4";
for (const arg of _args) {
if (arg.startsWith("-"))
flags.push(arg);
Expand All @@ -20,44 +18,44 @@ for (const arg of _args) {
}
if (!args.length) {
if (flags.includes("--version") || flags.includes("-v")) {
console.log("as-test" + " " + package_json_1.version.toString());
console.log("as-test" + " " + version.toString());
}
else {
console.log(chalk_1.default.bold.blueBright("as-test") + " is a testing framework for AssemblyScript. " + chalk_1.default.dim("(v" + package_json_1.version + ")") + "\n");
console.log(chalk_1.default.bold("Usage: as-test") + " " + chalk_1.default.dim("<command>") + " " + chalk_1.default.bold.blueBright("[...flags]") + " " + chalk_1.default.bold("[...args]") + " " + chalk_1.default.dim("(alias: ast)") + "\n");
console.log(chalk_1.default.bold("Commands:"));
console.log(" " + chalk_1.default.bold.blueBright("run") + " " + chalk_1.default.dim("<my-test.spec.ts>") + " " + "Run unit tests with selected runtime");
console.log(" " + chalk_1.default.bold.blueBright("build") + " " + chalk_1.default.dim("<my-test.spec.ts>") + " " + "Build unit tests and compile");
console.log(" " + chalk_1.default.bold.blueBright("test") + " " + chalk_1.default.dim("<my-test.spec.ts>") + " " + "Build and run unit tests with selected runtime" + "\n");
console.log(" " + chalk_1.default.bold.magentaBright("init") + " " + chalk_1.default.dim("") + " " + "Initialize an empty testing template");
console.log(" " + chalk_1.default.bold.magentaBright("config") + " " + chalk_1.default.dim("as-test.config.json") + " " + "Specify the configuration file");
console.log(" " + chalk_1.default.bold.magentaBright("reporter") + " " + chalk_1.default.dim("<tap>") + " " + "Specify the test reporter to use");
console.log(" " + chalk_1.default.bold.magentaBright("use") + " " + chalk_1.default.dim("wasmtime") + " " + "Specify the runtime to use" + "\n");
console.log(chalk_1.default.bold("Flags:"));
console.log(" " + chalk_1.default.dim("run") + " " + chalk_1.default.bold.blue("--coverage") + " " + "Use code coverage");
console.log(" " + chalk_1.default.dim("run") + " " + chalk_1.default.bold.blue("--snapshot") + " " + "Take a snapshot of the tests");
console.log(" " + chalk_1.default.dim("use") + " " + chalk_1.default.bold.blue("--list") + " " + "List supported runtimes");
console.log(" " + chalk_1.default.dim("reporter") + " " + chalk_1.default.bold.blue("--list") + " " + "List supported reporters");
console.log(" " + chalk_1.default.dim("<command>") + " " + chalk_1.default.bold.blue("--help") + " " + "Print info about command" + "\n");
console.log(chalk_1.default.dim("If your using this, consider dropping a star, it would help a lot!") + "\n");
console.log("View the repo: " + chalk_1.default.magenta("https://github.com/JairusSW/as-test"));
console.log("View the docs: " + chalk_1.default.blue("https://docs.jairus.dev/as-test"));
console.log(chalk.bold.blueBright("as-test") + " is a testing framework for AssemblyScript. " + chalk.dim("(v" + version + ")") + "\n");
console.log(chalk.bold("Usage: as-test") + " " + chalk.dim("<command>") + " " + chalk.bold.blueBright("[...flags]") + " " + chalk.bold("[...args]") + " " + chalk.dim("(alias: ast)") + "\n");
console.log(chalk.bold("Commands:"));
console.log(" " + chalk.bold.blueBright("run") + " " + chalk.dim("<my-test.spec.ts>") + " " + "Run unit tests with selected runtime");
console.log(" " + chalk.bold.blueBright("build") + " " + chalk.dim("<my-test.spec.ts>") + " " + "Build unit tests and compile");
console.log(" " + chalk.bold.blueBright("test") + " " + chalk.dim("<my-test.spec.ts>") + " " + "Build and run unit tests with selected runtime" + "\n");
console.log(" " + chalk.bold.magentaBright("init") + " " + chalk.dim("") + " " + "Initialize an empty testing template");
console.log(" " + chalk.bold.magentaBright("config") + " " + chalk.dim("as-test.config.json") + " " + "Specify the configuration file");
console.log(" " + chalk.bold.magentaBright("reporter") + " " + chalk.dim("<tap>") + " " + "Specify the test reporter to use");
console.log(" " + chalk.bold.magentaBright("use") + " " + chalk.dim("wasmtime") + " " + "Specify the runtime to use" + "\n");
console.log(chalk.bold("Flags:"));
console.log(" " + chalk.dim("run") + " " + chalk.bold.blue("--coverage") + " " + "Use code coverage");
console.log(" " + chalk.dim("run") + " " + chalk.bold.blue("--snapshot") + " " + "Take a snapshot of the tests");
console.log(" " + chalk.dim("use") + " " + chalk.bold.blue("--list") + " " + "List supported runtimes");
console.log(" " + chalk.dim("reporter") + " " + chalk.bold.blue("--list") + " " + "List supported reporters");
console.log(" " + chalk.dim("<command>") + " " + chalk.bold.blue("--help") + " " + "Print info about command" + "\n");
console.log(chalk.dim("If your using this, consider dropping a star, it would help a lot!") + "\n");
console.log("View the repo: " + chalk.magenta("https://github.com/JairusSW/as-test"));
console.log("View the docs: " + chalk.blue("https://docs.jairus.dev/as-test"));
}
}
else if (COMMANDS.includes(args[0])) {
const command = args.shift();
if (command === "build") {
(0, build_1.build)(args, flags);
build(args, flags);
}
else if (command === "run") {
(0, run_1.run)();
run();
}
else if (command === "test") {
(0, build_1.build)(args, flags).then(() => {
(0, run_1.run)();
build(args, flags).then(() => {
run();
});
}
}
else {
console.log(chalk_1.default.bgRed(" ERROR ") + chalk_1.default.dim(":") + " " + chalk_1.default.bold("Unknown command: ") + args[0]);
console.log(chalk.bgRed(" ERROR ") + chalk.dim(":") + " " + chalk.bold("Unknown command: ") + args[0]);
}
77 changes: 43 additions & 34 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const fs_1 = require("fs");
const types_1 = require("./types");
const chalk_1 = require("chalk");
const child_process_1 = require("child_process");
const glob_1 = require("glob");
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { readFileSync, readdirSync } from "fs";
import { Config } from "./types.js";
import chalk from "chalk";
import { exec } from "child_process";
import { glob } from "glob";
const installScripts = new Map([
["wasmtime", "curl https://wasmtime.dev/install.sh -sSf | bash"]
]);
async function run() {
const config = Object.assign(new types_1.Config(), JSON.parse((0, fs_1.readFileSync)("./as-test.config.json").toString()));
const inputFiles = await (0, glob_1.glob)(config.input);
console.log(chalk_1.default.dim("Running tests using " + config.runOptions.runtime.name + ""));
let execPath = "";
const PATH = process.env["PATH"]?.split(":");
for (const bin of PATH) {
if (bin.startsWith("/mnt/"))
continue;
for (const file of (0, fs_1.readdirSync)(bin)) {
if (file == config.runOptions.runtime.name || file == config.runOptions.runtime.name + ".exe") {
execPath = bin + "/" + file;
export function run() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const config = Object.assign(new Config(), JSON.parse(readFileSync("./as-test.config.json").toString()));
const inputFiles = yield glob(config.input);
console.log(chalk.dim("Running tests using " + config.runOptions.runtime.name + ""));
let execPath = "";
const PATH = (_a = process.env["PATH"]) === null || _a === void 0 ? void 0 : _a.split(":");
for (const bin of PATH) {
if (bin.startsWith("/mnt/"))
continue;
for (const file of readdirSync(bin)) {
if (file == config.runOptions.runtime.name || file == config.runOptions.runtime.name + ".exe") {
execPath = bin + "/" + file;
}
}
}
}
if (!execPath) {
console.log(chalk_1.default.bgRed(" ERROR ") + chalk_1.default.dim(":") + " could not locate " + config.runOptions.runtime.name + " in your PATH variable. Either set it, or install it" + (config.runOptions.runtime.name ? "using " + chalk_1.default.dim(installScripts.get(config.runOptions.runtime.name)) : "."));
}
for (const file of inputFiles) {
const outFile = config.outDir + "/" + file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
(0, child_process_1.exec)(config.runOptions.runtime.run.replace(config.runOptions.runtime.name, execPath).replace("<file>", outFile), (err, stdout, stderr) => {
process.stdout.write(stdout);
process.stderr.write(stderr);
if (err) {
process.exit(err.code);
}
});
}
if (!execPath) {
console.log(chalk.bgRed(" ERROR ") + chalk.dim(":") + " could not locate " + config.runOptions.runtime.name + " in your PATH variable. Either set it, or install it" + (config.runOptions.runtime.name ? "using " + chalk.dim(installScripts.get(config.runOptions.runtime.name)) : "."));
}
for (const file of inputFiles) {
const outFile = config.outDir + "/" + file.slice(file.lastIndexOf("/") + 1).replace(".ts", ".wasm");
exec(config.runOptions.runtime.run.replace(config.runOptions.runtime.name, execPath).replace("<file>", outFile), (err, stdout, stderr) => {
process.stdout.write(stdout);
process.stderr.write(stderr);
if (err) {
process.exit(err.code);
}
});
}
});
}
18 changes: 5 additions & 13 deletions bin/types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Runtime = exports.RunOptions = exports.BuildOptions = exports.Suite = exports.Config = void 0;
class Config {
export class Config {
constructor() {
this.input = [];
this.outDir = "./build";
Expand All @@ -10,32 +7,27 @@ class Config {
this.runOptions = new RunOptions();
}
}
exports.Config = Config;
class Suite {
export class Suite {
constructor() {
this.name = "";
}
}
exports.Suite = Suite;
class BuildOptions {
export class BuildOptions {
constructor() {
this.args = [];
this.wasi = true;
this.parallel = true;
this.verbose = true;
}
}
exports.BuildOptions = BuildOptions;
class RunOptions {
export class RunOptions {
constructor() {
this.runtime = new Runtime();
}
}
exports.RunOptions = RunOptions;
class Runtime {
export class Runtime {
constructor() {
this.name = "wasmtime";
this.run = "wasmtime <file>";
}
}
exports.Runtime = Runtime;
5 changes: 1 addition & 4 deletions bin/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatTime = formatTime;
function formatTime(ms) {
export function formatTime(ms) {
if (ms < 0) {
throw new Error("Time should be a non-negative number.");
}
Expand Down
Loading

0 comments on commit b339f77

Please sign in to comment.