Skip to content

Commit 64a1e1f

Browse files
committed
Convert CLI to ES Module
1 parent 12768ce commit 64a1e1f

18 files changed

+46
-44
lines changed

cli/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "@dodona/dolos",
33
"version": "2.3.0",
44
"description": "Code similarity detection based on the Winnowing algorithm",
5-
"main": "dist/index.js",
5+
"type": "module",
6+
"export": "./dist/index.js",
67
"engines": {
7-
"node": ">12"
8+
"node": ">=16"
89
},
910
"bin": {
1011
"dolos": "dist/cli.js"

cli/src/cli.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
#!/usr/bin/env node
2-
import * as Utils from "./cli/util/utils";
2+
import * as Utils from "./cli/util/utils.js";
33
import { Command } from "commander";
4-
import { runCommand } from "./cli/commands/run";
5-
import { serveCommand } from "./cli/commands/serve";
6-
import * as path from "path";
4+
import { runCommand } from "./cli/commands/run.js";
5+
import { serveCommand } from "./cli/commands/serve.js";
6+
import { readFileSync } from "fs";
77

8-
// eslint-disable-next-line @typescript-eslint/no-var-requires
9-
const pkg = require("../package.json");
10-
// eslint-disable-next-line @typescript-eslint/no-var-requires
11-
const treeSitterPkg = require(path.dirname(require.resolve("tree-sitter")) + "/package.json");
8+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf-8"));
129

1310
const versions = [
1411
`Dolos v${pkg.version}`,
1512
`Node ${process.version}`,
16-
`Tree-sitter v${treeSitterPkg.version}`
13+
`Tree-sitter ${pkg.dependencies["tree-sitter"]}`
1714
];
1815

1916
const program = new Command();

cli/src/cli/commands/run.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
setLogging,
44
tryCatch,
55
warning
6-
} from "../util/utils";
6+
} from "../util/utils.js";
77

8-
import { DEFAULT_HOST, DEFAULT_PORT } from "../server";
9-
import { TerminalView } from "../views/terminalView";
10-
import { FileView } from "../views/fileView";
11-
import { WebView } from "../views/webView";
8+
import { DEFAULT_HOST, DEFAULT_PORT } from "../server.js";
9+
import { TerminalView } from "../views/terminalView.js";
10+
import { FileView } from "../views/fileView.js";
11+
import { WebView } from "../views/webView.js";
1212
import { Command } from "commander";
13-
import * as Utils from "../util/utils";
13+
import * as Utils from "../util/utils.js";
1414
import { Dolos, Options } from "@dodona/dolos-lib";
1515

1616
export function runCommand(program: Command): Command {

cli/src/cli/commands/serve.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Command } from "commander";
2-
import { promises as fs, constants } from "fs";
3-
import * as path from "path";
4-
import runServer, { DEFAULT_HOST, DEFAULT_PORT } from "../server";
5-
import * as Utils from "../util/utils";
6-
import { tryCatch, error, setLogging } from "../util/utils";
2+
import { constants } from "node:fs";
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import runServer, { DEFAULT_HOST, DEFAULT_PORT } from "../server.js";
6+
import * as Utils from "../util/utils.js";
7+
import { tryCatch, error, setLogging } from "../util/utils.js";
78

89
export function serveCommand(program: Command): Command {
910
return new Command("serve")

cli/src/cli/server.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { error } from "./util/utils";
1+
import { error } from "./util/utils.js";
22
import { default as express, Express } from "express";
33
import http from "http";
44
import path from "path";
55
import open from "open";
6+
// @ts-ignore
7+
import * as web from "@dodona/dolos-web";
8+
69

710
function assets(): string {
811
try {
9-
return require.resolve("@dodona/dolos-web");
12+
console.debug(web);
13+
return web;
1014
} catch (e) {
1115
if (e.code === "MODULE_NOT_FOUND") {
1216
error("Module '@dodona/dolos-web' was not found on your system, " +

cli/src/cli/views/fileView.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { View } from "./view";
1+
import { View } from "./view.js";
22
import { stringify } from "csv-stringify";
33
import { Writable } from "stream";
44
import { createWriteStream, promises as fs } from "fs";

cli/src/cli/views/terminalView.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { View } from "./view";
1+
import { View } from "./view.js";
22

33
/// <reference types="../../../typings/cliui" />
44
import UI from "cliui";
5-
import { Chalk, Instance as ChalkInstance, bold, red } from "chalk";
65
import { Writable } from "stream";
7-
import { closestMatch } from "../util/utils";
6+
import { closestMatch } from "../util/utils.js";
87
import { Report, Fragment, Region, Pair } from "@dodona/dolos-lib";
8+
import chalk from "chalk";
9+
const { Instance: ChalkInstance, bold, red } = chalk;
910

1011
/**
1112
* This {@link View} will print the results of an analysis to the terminal.
@@ -17,7 +18,7 @@ export class TerminalView extends View {
1718
private readonly fragmentSortBy?: string;
1819
private readonly compare: boolean;
1920
private readonly width: number;
20-
private readonly c: Chalk;
21+
private readonly c: chalk.Chalk;
2122
private readonly context: number = 3;
2223

2324
constructor(

cli/src/cli/views/webView.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { FileView, Options as FVOptions } from "./fileView";
2-
import runServer, { Options as ServerOptions } from "../server";
1+
import { FileView, Options as FVOptions } from "./fileView.js";
2+
import runServer, { Options as ServerOptions } from "../server.js";
33
import { Report } from "@dodona/dolos-lib";
44

55
/**

cli/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
//export { FileView } from "./cli/views/fileView";
1+
//export { FileView } from "./cli/views/fileView.js";

cli/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"rootDir": "src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
66
"composite": true /* Enable project compilation */
77
},
8-
"exclude": [ "web/", "dist/" ],
8+
"exclude": [ "dist/" ],
99
"references": [{ "path": "../lib"}]
1010
}

lib/src/lib/analyze/fragment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "node:assert";
1+
import assert from "assert";
22
import { PairedOccurrence } from "./pairedOccurrence.js";
33
import { Region } from "../util/region.js";
44
import { Range } from "../util/range.js";

lib/src/lib/analyze/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "node:assert";
1+
import assert from "assert";
22
import { HashFilter } from "../hashing/hashFilter.js";
33
import { Options } from "../util/options.js";
44
import { Range } from "../util/range.js";

lib/src/lib/file/file.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Result } from "../util/result.js";
22
import Identifiable from "../util/identifiable.js";
3-
import fs from "node:fs/promises";
4-
import path from "node:path";
3+
import fs from "fs/promises";
4+
import path from "path";
55

66
export interface ExtraInfo {
77
filename: string;

lib/src/lib/tokenizer/codeTokenizer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { default as Parser, SyntaxNode } from "tree-sitter";
22
import { Region } from "../util/region.js";
33
import { Token, Tokenizer } from "./tokenizer.js";
44
import { ProgrammingLanguage } from "../util/language.js";
5-
import assert from "node:assert";
5+
import assert from "assert";
66

77
export class CodeTokenizer extends Tokenizer {
88

lib/src/lib/util/range.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "node:assert";
1+
import assert from "assert";
22

33
/**
44
* A range of whole numbers starting at `from` (inclusive) and ending at `to`

lib/src/lib/util/region.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "node:assert";
1+
import assert from "assert";
22

33
/**
44
* Defines a selection in a file.

lib/tsconfig.json

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"outDir": "dist", /* Redirect output structure to the directory. */
55
"rootDir": "src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
66
"composite": true, /* Enable project compilation */
7-
"module": "node16",
8-
"moduleResolution": "node16"
97
},
108
"exclude": [ "dist/" ]
119
}

tsconfig.global.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "commonjs", /* Specify what module code is generated. */
28+
"module": "node16", /* Specify what module code is generated. */
2929
// "rootDir": "./", /* Specify the root folder within your source files. */
30-
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30+
"moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3232
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3333
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */

0 commit comments

Comments
 (0)