halstead-metrics-core 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { main } = require("./src/cli");
5
+
6
+ const code = main(process.argv.slice(2));
7
+ if (code !== 0) {
8
+ process.exit(code);
9
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
- "name": "halstead-metrics-core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "halstead-metrics-core",
3
+ "version": "1.0.0",
4
+ "description": "Command-line Halstead metrics for all major programming languages",
5
+ "bin": {
6
+ "halstead": "index.js"
7
+ },
8
+ "main": "src/cli.js",
9
+ "files": [
10
+ "index.js",
11
+ "src"
12
+ ],
13
+ "scripts": {
14
+ "start": "node index.js",
15
+ "test:unit": "node --test test/unit/*.js",
16
+ "test:integration": "node --test test/integration/*.js",
17
+ "test": "npm run test:unit && npm run test:integration"
18
+ },
19
+ "engines": {
20
+ "node": ">=18"
21
+ },
22
+ "dependencies": {
23
+ "halstead-metrics": "1.0.0"
24
+ },
25
+ "author": {
26
+ "name": "Harish Ramasubramanian",
27
+ "email": "rharish100193@gmail.com",
28
+ "url": "https://github.com/rharish100193"
29
+ },
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/rharish100193/halstead-metrics.git",
34
+ "directory": "packages/halstead-metrics-core"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }
package/src/cli.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ const { SUPPORTED_EXTENSIONS, reportSource } = require("halstead-metrics");
7
+
8
+ const USAGE = "Usage:\n"
9
+ + "\tTo use in CLI mode: halstead <path name>\n"
10
+ + "\tIn CLI mode the results will be printed to stdout in Json format.\n";
11
+
12
+ /**
13
+ * @param {string[]} args
14
+ * @param {{log?: Function, error?: Function}} [io]
15
+ * @returns {number}
16
+ */
17
+ function main(args, io) {
18
+ const log = (io && io.log) || console.log;
19
+ const error = (io && io.error) || console.error;
20
+
21
+ if (args.length === 0 || args[0] === "-h" || args[0] === "--help" || args[0] === "-help") {
22
+ log(USAGE.trimEnd());
23
+ return 1;
24
+ }
25
+
26
+ const filePath = args[0];
27
+ const absolute = path.resolve(filePath);
28
+ let stat = null;
29
+ try {
30
+ stat = fs.statSync(filePath);
31
+ } catch (err) {
32
+ stat = null;
33
+ }
34
+ if (!stat || !stat.isFile()) {
35
+ log("Cannot read file: " + absolute);
36
+ return 2;
37
+ }
38
+ try {
39
+ fs.accessSync(filePath, fs.constants.R_OK);
40
+ } catch (err) {
41
+ log("Cannot read file: " + absolute);
42
+ return 2;
43
+ }
44
+
45
+ const extension = path.extname(filePath).slice(1).toLowerCase();
46
+ if (!SUPPORTED_EXTENSIONS.has(extension)) {
47
+ log("Check the path you inserted: " + absolute);
48
+ return 3;
49
+ }
50
+
51
+ try {
52
+ const source = fs.readFileSync(filePath, "utf8");
53
+ const report = reportSource(source, extension, filePath);
54
+ log(JSON.stringify(report));
55
+ return 0;
56
+ } catch (err) {
57
+ error("MetricsEvaluator exception.");
58
+ error(err);
59
+ return 4;
60
+ }
61
+ }
62
+
63
+ module.exports = {
64
+ main,
65
+ USAGE,
66
+ };
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.