Skip to content

Commit

Permalink
Convert Node scripts to module scripts
Browse files Browse the repository at this point in the history
This reduces the need for variation in eg. linting rules by making all JS files
modules where possible. Note that Karma does not support this. See
karma-runner/karma#3677.
  • Loading branch information
robertknight committed Apr 15, 2024
1 parent 7d0260e commit 595d1f0
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 24 deletions.
5 changes: 2 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"overrides": [
{
"files": [
"gulpfile.js",
"tests/karma.config.js",
"tests/karma.config.cjs",

// Entry points which currently get loaded as non-module scripts.
"src/help/index.js",
Expand All @@ -46,7 +45,7 @@
}
},
{
"files": ["gulpfile.js", "tests/karma.config.js"],
"files": ["tests/karma.config.cjs"],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.yalc/
.yarn/
build/
coverage/
src/vendor/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ lint: node_modules/.uptodate build/settings.json

.PHONY: checkformatting
checkformatting: node_modules/.uptodate
$(PRETTIER) --check '**/*.{ts,js}'
$(PRETTIER) --check '**/*.{ts,js,cjs}'

.PHONY: format
format: node_modules/.uptodate
$(PRETTIER) --list-different --write '**/*.{ts,js}'
$(PRETTIER) --list-different --write '**/*.{ts,js,cjs}'

.PHONY: test
test: node_modules/.uptodate
Expand Down
12 changes: 5 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
/* eslint-env node */
// @ts-nocheck

'use strict';
import { spawn } from 'node:child_process';

const { spawn } = require('child_process');

const { parallel, watch } = require('gulp');
import * as gulp from 'gulp';

function build(cb) {
const make = spawn('make', ['build'], { stdio: 'inherit' });
Expand All @@ -20,11 +18,11 @@ function build(cb) {
}

function watchClient() {
watch('node_modules/hypothesis', { events: 'all' }, build);
gulp.watch('node_modules/hypothesis', { events: 'all' }, build);
}

function watchSrc() {
watch('src', { events: 'all' }, build);
gulp.watch('src', { events: 'all' }, build);
}

exports.watch = parallel(build, watchClient, watchSrc);
export const watch = gulp.parallel(build, watchClient, watchSrc);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "hypothesis-browser-extension",
"version": "1.1483.0",
"private": true,
"type": "module",
"description": "Annotate with anyone, anywhere.",
"license": "BSD-2-Clause",
"homepage": "https://hypothes.is",
Expand Down Expand Up @@ -50,7 +51,7 @@
},
"browserslist": "chrome 85, firefox 75",
"scripts": {
"test": "rollup -c rollup-tests.config.mjs && karma start tests/karma.config.js --single-run",
"test": "rollup -c rollup-tests.config.mjs && karma start tests/karma.config.cjs --single-run",
"typecheck": "tsc --build src/tsconfig.json"
},
"packageManager": "[email protected]"
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions tools/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"env": {
"node": true
},
"parserOptions": {
"sourceType": "script"
},
"rules": {
"@typescript-eslint/no-var-requires": "off",
"no-console": "off"
Expand Down
11 changes: 7 additions & 4 deletions tools/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* file and the package environment.
*/

'use strict';
import * as fs from 'node:fs';
import * as path from 'node:path';

const path = require('path');
const gitDescribeSync = require('git-describe').gitDescribeSync;
import gitDescribe from 'git-describe';
const { gitDescribeSync } = gitDescribe;

// Suppress (expected) EPIPE errors on STDOUT
process.stdout.on('error', err => {
Expand Down Expand Up @@ -50,7 +51,9 @@ if (process.argv.length !== 3) {
process.exit(1);
}

const settings = require(path.join(process.cwd(), process.argv[2]));
const settings = JSON.parse(
fs.readFileSync(path.join(process.cwd(), process.argv[2])),
);
const settingsOut = {
...settings,
...getVersion(settings.buildType),
Expand Down
9 changes: 5 additions & 4 deletions tools/template-context-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* `app.html` file.
*/

'use strict';

const path = require('path');
import * as fs from 'node:fs';
import * as path from 'node:path';

function appSettings(settings) {
let result = {};
Expand Down Expand Up @@ -37,7 +36,9 @@ if (process.argv.length !== 3) {
process.exit(1);
}

const settings = require(path.join(process.cwd(), process.argv[2]));
const settings = JSON.parse(
fs.readFileSync(path.join(process.cwd(), process.argv[2])),
);

console.log(
JSON.stringify({
Expand Down

0 comments on commit 595d1f0

Please sign in to comment.