From 7cb936fd8e5bbd0fa6a137f9afaf352db882b7c6 Mon Sep 17 00:00:00 2001 From: Ely De La Cruz <603428+elycruz@users.noreply.github.com> Date: Wed, 29 May 2024 08:25:21 -0400 Subject: [PATCH] fix: issue-#107 - js scripts to mjs ones - Converted *.js scripts to *.mjs ones. - Fixed error catch, in rollup.config. --- .puppeteerrc.cjs => .puppeteerrc.mjs | 6 ++-- ...eer.config.js => jest-puppeteer.config.mjs | 4 +-- jest.config.js => jest.config.mjs | 10 +++--- .../{link-packages.js => link-packages.mjs} | 9 +++-- node_scripts/utils/index.js | 3 -- node_scripts/utils/index.mjs | 1 + .../utils/{ioUtils.js => ioUtils.mjs} | 25 +++++--------- package.json | 16 ++++----- .../src/{index.js => index.ts} | 11 +++---- .../tests/{mock-server.js => mock-server.mjs} | 33 +++++++------------ .../tests/test-reCaptchaValidator.ts | 29 ++++++++-------- .../tests/{utils.js => utils.mjs} | 4 +-- rollup.config.mjs | 7 ++-- 13 files changed, 69 insertions(+), 89 deletions(-) rename .puppeteerrc.cjs => .puppeteerrc.mjs (56%) rename jest-puppeteer.config.js => jest-puppeteer.config.mjs (70%) rename jest.config.js => jest.config.mjs (84%) rename node_scripts/tasks/{link-packages.js => link-packages.mjs} (91%) delete mode 100644 node_scripts/utils/index.js create mode 100644 node_scripts/utils/index.mjs rename node_scripts/utils/{ioUtils.js => ioUtils.mjs} (78%) rename packages/fjl-validator-recaptcha/src/{index.js => index.ts} (97%) rename packages/fjl-validator-recaptcha/tests/{mock-server.js => mock-server.mjs} (67%) rename packages/fjl-validator-recaptcha/tests/{utils.js => utils.mjs} (87%) diff --git a/.puppeteerrc.cjs b/.puppeteerrc.mjs similarity index 56% rename from .puppeteerrc.cjs rename to .puppeteerrc.mjs index 87ce17aa..94f9530f 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.mjs @@ -1,6 +1,8 @@ -const path = require('path'); +import path from 'path'; -module.exports = { +const __dirname = path.dirname(new URL(import.meta.url).pathname); + +export default { // args: ['--disable-setuid-sandbox', '--no-sandbox'], headless: true, cacheDirectory: path.join(path.resolve(__dirname), './.cache') diff --git a/jest-puppeteer.config.js b/jest-puppeteer.config.mjs similarity index 70% rename from jest-puppeteer.config.js rename to jest-puppeteer.config.mjs index c2c9701c..6bdc4653 100644 --- a/jest-puppeteer.config.js +++ b/jest-puppeteer.config.mjs @@ -1,6 +1,6 @@ -const packageJson = require('./package.json'); +import packageJson from './package.json'; -module.exports = { +export default { executablePath: process.env?.PUPPETEER_EXE_PATH, // Browser executable path server: { command: packageJson.mockServerCommand, diff --git a/jest.config.js b/jest.config.mjs similarity index 84% rename from jest.config.js rename to jest.config.mjs index 8d8ab914..d57da773 100644 --- a/jest.config.js +++ b/jest.config.mjs @@ -1,14 +1,16 @@ -const path = require('path'), - testMatch = [ +import path from 'path'; + +const testMatch = [ '**/(tests|src)/**/*@(.test|_test|.spec).@(ts|js)', '**/tests/**/test-*.@(ts|js)', ], transform = { '\\.ts$': 'ts-jest' - } + }, + __dirname = path.dirname(new URL(import.meta.url).pathname) ; -module.exports = { +export default { preset: 'ts-jest', collectCoverage: true, testEnvironment: 'node', diff --git a/node_scripts/tasks/link-packages.js b/node_scripts/tasks/link-packages.mjs similarity index 91% rename from node_scripts/tasks/link-packages.js rename to node_scripts/tasks/link-packages.mjs index faca33ad..14b0e102 100644 --- a/node_scripts/tasks/link-packages.js +++ b/node_scripts/tasks/link-packages.mjs @@ -1,15 +1,14 @@ -const +import path from "node:path"; +import {ioSpawn} from '../utils/index.mjs'; - path = require('path'), - {log, error} = console, +const {log, error} = console, - {ioSpawn} = require('../utils'), + __dirname = path.dirname(new URL(import.meta.url).pathname), repoRoot = path.join(__dirname, '../../'), taskGroupSeparator = '---------------------------\n\n' - ; /** diff --git a/node_scripts/utils/index.js b/node_scripts/utils/index.js deleted file mode 100644 index 1f5615bb..00000000 --- a/node_scripts/utils/index.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - ...(require('./ioUtils')), -}; diff --git a/node_scripts/utils/index.mjs b/node_scripts/utils/index.mjs new file mode 100644 index 00000000..d986c43c --- /dev/null +++ b/node_scripts/utils/index.mjs @@ -0,0 +1 @@ +export * from './ioUtils.mjs'; diff --git a/node_scripts/utils/ioUtils.js b/node_scripts/utils/ioUtils.mjs similarity index 78% rename from node_scripts/utils/ioUtils.js rename to node_scripts/utils/ioUtils.mjs index aeb5eb3e..fc56091e 100644 --- a/node_scripts/utils/ioUtils.js +++ b/node_scripts/utils/ioUtils.mjs @@ -1,11 +1,10 @@ -const - fs = require('fs'), - os = require('os'), - child_process = require('child_process'), - {exec, spawn} = child_process, - {log, warn} = console, +import fs from 'fs'; +import os from 'os'; +import { exec, spawn } from 'child_process'; - npmCmd = os.platform().indexOf('win') === 0 ? 'npm.cmd' : 'npm', +const {log, warn} = console; + +export const npmCmd = os.platform().indexOf('win') === 0 ? 'npm.cmd' : 'npm', ioExec = (cmd, options) => new Promise((resolve, reject) => { const p = exec(cmd, options, (err, stdout, stderr) => { @@ -53,14 +52,6 @@ const }) ; -module.exports = { - ioExec, - ioFileExists, - canWriteFileIo, - canReadFileIo, - canReadAndExistsFileIo, - canReadAndWriteFileIo, - ioPassTailThroughAndContinue: ioExecuteAndPassThrough, - ioSpawn, - npmCmd +export { + ioExecuteAndPassThrough as ioPassTailThroughAndContinue, }; diff --git a/package.json b/package.json index fed0e75d..6e373ba9 100644 --- a/package.json +++ b/package.json @@ -31,18 +31,18 @@ "scripts": { "build": "rollup --config rollup.config.mjs", "eslint": "eslint -c .eslintrc.json .", - "link-packages": "node node_scripts/tasks/link-packages.js", + "link-packages": "node node_scripts/tasks/link-packages.mjs", "lint": "pnpm eslint", "lintfix": "pnpm eslint --fix", "lint-staged": "lint-staged", "prepare": "husky", - "test": "jest -c jest.config.js", - "test:fjl": "jest -c jest.config.js --selectProjects fjl", - "test:fjl-filter": "jest --c jest.config.js --selectProjects fjl-filter", - "test:fjl-inputfilter": "jest -c jest.config.js --selectProjects fjl-inputfilter", - "test:fjl-validator": "jest -c jest.config.js --selectProjects fjl-validator", - "test:fjl-validator-recaptcha": "jest -c jest.config.js --selectProjects fjl-validator-recaptcha", - "test:staged": "jest -c jest.config.js --bail --findRelatedTests" + "test": "jest -c jest.config.mjs", + "test:fjl": "jest -c jest.config.mjs --selectProjects fjl", + "test:fjl-filter": "jest --c jest.config.mjs --selectProjects fjl-filter", + "test:fjl-inputfilter": "jest -c jest.config.mjs --selectProjects fjl-inputfilter", + "test:fjl-validator": "jest -c jest.config.mjs --selectProjects fjl-validator", + "test:fjl-validator-recaptcha": "jest -c jest.config.mjs --selectProjects fjl-validator-recaptcha", + "test:staged": "jest -c jest.config.mjs --bail --findRelatedTests" }, "repository": { "type": "git", diff --git a/packages/fjl-validator-recaptcha/src/index.js b/packages/fjl-validator-recaptcha/src/index.ts similarity index 97% rename from packages/fjl-validator-recaptcha/src/index.js rename to packages/fjl-validator-recaptcha/src/index.ts index a2416df5..2a523fc3 100644 --- a/packages/fjl-validator-recaptcha/src/index.js +++ b/packages/fjl-validator-recaptcha/src/index.ts @@ -1,5 +1,4 @@ /** - * Created by elydelacruz on 6/10/16. * @module fjlValidatorReCaptcha * @recaptchaVersion v2 * @reference see below: @@ -7,10 +6,10 @@ * @see https://developers.google.com/recaptcha/docs/verify * @todo Request handlers should be separated out from inlined definitions */ -const https = require('https'), - querystring = require('querystring'), - {getErrorMsgByKey: getErrorMessageByKey, toValidationResult, toValidationOptions} = require('fjl-validator'), - {assign, assignDeep, isEmpty, defineEnumProps} = require('fjl'); +import https from 'https'; +import querystring from 'querystring'; +import { getErrorMsgByKey as getErrorMessageByKey, toValidationResult, toValidationOptions } from 'fjl-validator'; +import { assign, assignDeep, isEmpty, defineEnumProps } from 'fjl'; /** * @memberOf module:fjlValidatorReCaptcha @@ -258,7 +257,7 @@ const MISSING_INPUT_SECRET = 'missing-input-secret', $reCaptchaValidatorV2 = (options) => (value) => reCaptchaIOValidator(options, value); -module.exports = { +export { MISSING_INPUT_SECRET, MISSING_INPUT_RESPONSE, INVALID_INPUT_RESPONSE, diff --git a/packages/fjl-validator-recaptcha/tests/mock-server.js b/packages/fjl-validator-recaptcha/tests/mock-server.mjs similarity index 67% rename from packages/fjl-validator-recaptcha/tests/mock-server.js rename to packages/fjl-validator-recaptcha/tests/mock-server.mjs index 768a308d..850ef63d 100644 --- a/packages/fjl-validator-recaptcha/tests/mock-server.js +++ b/packages/fjl-validator-recaptcha/tests/mock-server.mjs @@ -3,34 +3,25 @@ * @script * @standalone */ +import path from "node:path"; +import express from 'express'; +import helmet from 'helmet'; +import session from 'express-session'; +import compression from 'compression'; +import bodyParser from 'body-parser'; +import { log, jsonClone } from 'fjl'; +import { reCaptchaIOValidator } from '../src'; -// If 'NODE_ENV' not set, set it -if (!process.env.NODE_ENV) { - process.env.NODE_ENV = 'dev'; -} +import { appSessionSecret, mockServerPort as port } from '../../../package.json'; -const isDevEnv = process.env.NODE_ENV.toLowerCase().indexOf('dev') === 0; +const __dirname = path.dirname(new URL(import.meta.url).pathname); -// If dev mode load local environment config -if (isDevEnv) { - require('dotenv').config(); -} +const isDevEnv = (process.env.NODE_ENV ?? 'dev').toLowerCase().includes('dev'); // Preliminaries -const {appSessionSecret = 'Supercalifragilisticexpialidocious'} = process.env, - {mockServerPort: port} = require('../../../package'), - express = require('express'), - helmet = require('helmet'), - session = require('express-session'), - compression = require('compression'), - bodyParser = require('body-parser'), - router = new express.Router(), +const router = new express.Router(), app = express(); -const {log, jsonClone} = require('fjl'); - -const {reCaptchaIOValidator} = require('../src/index'); - // Security features // @see https://expressjs.com/en/advanced/best-practice-security.html#use-helmet app.use(helmet()); diff --git a/packages/fjl-validator-recaptcha/tests/test-reCaptchaValidator.ts b/packages/fjl-validator-recaptcha/tests/test-reCaptchaValidator.ts index 1b83e080..2b22e3ec 100644 --- a/packages/fjl-validator-recaptcha/tests/test-reCaptchaValidator.ts +++ b/packages/fjl-validator-recaptcha/tests/test-reCaptchaValidator.ts @@ -1,20 +1,19 @@ -const { - reCaptchaIOValidator, - toReCaptchaValidatorOptions, - toReCaptchaTestValue, - MISSING_INPUT_SECRET, - MISSING_INPUT_RESPONSE - } = require('../src/index'), - - {log, error, runHasPropTypes} = require('./utils'), - packageJson = require('../package.json'), - puppeteerConfig = require('../../../.puppeteerrc.cjs'), - puppeteer = require('puppeteer'), - {waitFor} = require('./utils'); +import { + reCaptchaIOValidator, + toReCaptchaValidatorOptions, + toReCaptchaTestValue, + MISSING_INPUT_SECRET, + MISSING_INPUT_RESPONSE +} from '../src/index'; + +import { mockServerPort } from '../../../package.json'; +import { log, error, runHasPropTypes } from './utils'; +import packageJson from '../package.json'; +import puppeteerConfig from '../../../.puppeteerrc'; +import puppeteer from 'puppeteer'; +import { waitFor } from './utils'; const {recaptchaKeys} = packageJson, - {mockServerPort} = require('../../../package.json'), - unhandledHandler = e => { console.error(e); process.exit(1); diff --git a/packages/fjl-validator-recaptcha/tests/utils.js b/packages/fjl-validator-recaptcha/tests/utils.mjs similarity index 87% rename from packages/fjl-validator-recaptcha/tests/utils.js rename to packages/fjl-validator-recaptcha/tests/utils.mjs index 5fd22119..b213d3ad 100644 --- a/packages/fjl-validator-recaptcha/tests/utils.js +++ b/packages/fjl-validator-recaptcha/tests/utils.mjs @@ -1,4 +1,4 @@ -const {log, error, peek, range} = require('fjl'); +import { log, error, peek, range } from 'fjl'; const @@ -39,4 +39,4 @@ const ; -module.exports = {log, error, peek, jsonClone, genRan, genRanChar, genRanStr, runHasPropTypes, runHasPropOfType, waitFor}; +export {log, error, peek, jsonClone, genRan, genRanChar, genRanStr, runHasPropTypes, runHasPropOfType, waitFor}; diff --git a/rollup.config.mjs b/rollup.config.mjs index b15e90bb..7465d25a 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -3,9 +3,7 @@ import path from 'node:path'; import typescript from '@rollup/plugin-typescript'; import terser from '@rollup/plugin-terser'; -const - - __dirname = path.dirname(new URL(import.meta.url).pathname), +const __dirname = path.dirname(new URL(import.meta.url).pathname), {log, error} = console, @@ -85,7 +83,8 @@ await Promise.all(projectNames.map(projectName => { // Delete files in 'dist/' paths for each project return fs.readdir(distPath) - .then(files => files.map(file => { + .catch(() => []) + .then(files => files?.map(file => { const filePath = path.join(distPath, file); const projectPath = `./${filePath.split(__dirname)[1]}`; return fs.rm(filePath, {recursive: true})