Skip to content

Commit

Permalink
Migrate to use of ES Modules (#667)
Browse files Browse the repository at this point in the history
- Moves minimum node version to v18 (latest LTS at time of PR)
- Changes gauge-js to an ES module, removing all use of CommonJS requires
- Prefers use of node: prefixed imports
- Upgrades and de-duplicates dependencies to latest

----
Signed-off-by: Chad Wilson <[email protected]>
Signed-off-by: Zabil Cheriya Maliackal <[email protected]>
Co-authored-by: Zabil Cheriya Maliackal <[email protected]>
  • Loading branch information
chadlwilson authored Jun 25, 2024
1 parent a315148 commit 2237e9f
Show file tree
Hide file tree
Showing 50 changed files with 979 additions and 730 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ The plugin is authored in [Javascript](https://en.wikipedia.org/wiki/JavaScript)
Gauge is authored in golang. These are independent processes talking to each other over TCP on port GAUGE_INTERNAL_PORT (env variable) using [Protobuf](https://github.com/getgauge/gauge-proto).

##### Pre-Requisites
* [Node.js](https://nodejs.org/en/) - Version >= 16
* [Npm](https://www.npmjs.com/get-npm)
* [Node.js](https://nodejs.org/en/) - Version >= 18

##### Compiling
```
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs → eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default [
mocha
},
languageOptions: {
ecmaVersion: 8,
ecmaVersion: 11,
globals: {
node: true,
es6: true,
Expand Down
5 changes: 2 additions & 3 deletions examples/tests/step_implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
/**
* Loads the `assert` module provided by NodeJS
*/
var assert = require("assert");

import assert from "node:assert";

/**
* Loads the local `vowels.js` module present in this directory
*/
var vowels = require("./vowels");
import vowels from "./vowels.js";


// --------------------------
Expand Down
14 changes: 6 additions & 8 deletions examples/tests/vowels.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* Use `module.exports` to export an object which can be used with a `require()` on this file.
*/
const vowelList = ["a", "e", "i", "o", "u"];

var vowelList = ["a", "e", "i", "o", "u"];

var numVowels = function (word) {
var vowelArr = word.split("").filter(function (elem) { return vowelList.indexOf(elem) > -1; });
const numVowels = function (word) {
const vowelArr = word.split("").filter(function (elem) {
return vowelList.indexOf(elem) > -1;
});
return vowelArr.length;
};

module.exports = {
export default {
vowelList: vowelList,
numVowels: numVowels
};
66 changes: 39 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
#! /usr/bin/env node

var version = process.versions.node.split(".");
if (parseInt(version[0]) < 16) {
throw new Error("gauge-js requires Node.js version 16+. Current version: " + process.versions.node);
const minNodeVersion = 18;
const version = process.versions.node.split(".");
if (Number.parseInt(version[0]) < minNodeVersion) {
throw new Error(
`gauge-js requires Node.js version ${minNodeVersion}+. Current version: ${process.versions.node}`,
);
}

var fs = require("fs"),
path = require("path"),
child_process = require("child_process");
import child_process from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

var skeldir = path.join(__dirname, "skel"),
srcdir = path.join(process.env.GAUGE_PROJECT_ROOT, "tests"),
envdir = path.join(process.env.GAUGE_PROJECT_ROOT, "env", "default"),
testCode = "step_implementation.js",
jsPropertyFile = "js.properties";

if (process.argv[2] === "--init") {
const skeldir = path.join(__dirname, "skel");
const srcdir = path.join(process.env.GAUGE_PROJECT_ROOT, "tests");
const envdir = path.join(process.env.GAUGE_PROJECT_ROOT, "env", "default");
const testCode = "step_implementation.js";
const jsPropertyFile = "js.properties";

if (process.argv[2] === "--init") {
console.log("Initialising Gauge JavaScript project");
fs.mkdir(srcdir, 484, function (err) {
fs.mkdir(srcdir, 484, (err) => {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
fs.createReadStream(path.join(skeldir, testCode))
.pipe(fs.createWriteStream(path.join(srcdir, testCode)));
fs.createReadStream(path.join(skeldir, testCode)).pipe(
fs.createWriteStream(path.join(srcdir, testCode)),
);
}
});

fs.mkdir(path.dirname(envdir), 484, function (err) {
fs.mkdir(path.dirname(envdir), 484, (err) => {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
fs.mkdir(envdir, 484, function (err) {
fs.mkdir(envdir, 484, (err) => {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
fs.createReadStream(path.join(skeldir, jsPropertyFile))
.pipe(fs.createWriteStream(path.join(envdir, jsPropertyFile)));
fs.createReadStream(path.join(skeldir, jsPropertyFile)).pipe(
fs.createWriteStream(path.join(envdir, jsPropertyFile)),
);
}
});
}
});
}

else if (process.argv[2] === "--start") {
var args = ["./src/gauge.js", "--run"];
} else if (process.argv[2] === "--start") {
let args = ["./src/gauge.js", "--run"];
if (process.env.gauge_nodejs_args) {
args = process.env.gauge_nodejs_args.split(" ").concat(args);
}
var cmd = process.env.gauge_nodejs_bin || "node";
var runner = child_process.spawn(cmd, args, { env: process.env, silent: false, stdio: "inherit" });
const cmd = process.env.gauge_nodejs_bin || "node";
const runner = child_process.spawn(cmd, args, {
env: process.env,
silent: false,
stdio: "inherit",
});
process.on("beforeExit", (code) => {
try {
if (!runner.killed) { runner.kill("SIGINT"); }
if (!runner.killed) {
runner.kill("SIGINT");
}
} finally {
process.exit(code);
}
});
runner.on("error", function (err) {
runner.on("error", (err) => {
console.trace(err.stack);
});
}
2 changes: 1 addition & 1 deletion js.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"--init"
]
},
"version": "4.0.1",
"version": "5.0.0",
"gaugeVersionSupport": {
"minimum": "1.0.7",
"maximum": ""
Expand Down
Loading

0 comments on commit 2237e9f

Please sign in to comment.