From f3c4c746cc3ac283d0f8e493682af34086831f51 Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Tue, 30 Jul 2024 15:34:04 +1200 Subject: [PATCH 1/5] chore: convert codebase to ESM BREAKING CHANGE: JavaScript API is now ESM only and no longer supports CJS --- .eslintrc | 3 +- classes/alias.js | 16 ++++----- classes/index.js | 20 +++++------ classes/integrity.js | 15 ++++---- classes/login.js | 10 +++--- classes/meta.js | 13 +++---- classes/ping.js | 10 +++--- classes/publish/map.js | 16 ++++----- classes/publish/package/index.js | 30 ++++++++-------- .../package/tasks/check-bundle-sizes.js | 14 +++----- .../tasks/check-if-already-published.js | 16 ++++----- classes/publish/package/tasks/cleanup.js | 12 +++---- .../package/tasks/create-temp-directory.js | 9 ++--- .../publish/package/tasks/create-zip-file.js | 13 +++---- classes/publish/package/tasks/dry-run.js | 9 ++--- .../publish/package/tasks/save-metafile.js | 10 +++--- classes/publish/package/tasks/task.js | 6 ++-- classes/publish/package/tasks/upload-files.js | 15 ++++---- .../publish/package/tasks/validate-input.js | 9 ++--- classes/version.js | 25 ++++++-------- commands/index.js | 13 +++++++ commands/init.js | 22 ++++++------ commands/integrity.js | 27 +++++++-------- commands/login.js | 24 ++++++------- commands/map-alias.js | 18 +++++----- commands/map.js | 22 ++++++------ commands/meta.js | 21 +++++------- commands/npm-alias.js | 20 +++++------ commands/package-alias.js | 22 ++++++------ commands/ping.js | 18 +++++----- commands/publish.js | 34 +++++++++---------- commands/version.js | 28 +++++++-------- formatters/alias.js | 11 +++--- formatters/artifact.js | 11 +++--- formatters/file.js | 9 ++--- formatters/index.js | 10 +++--- formatters/version.js | 13 +++---- index.js | 27 +++++++++------ package.json | 1 + test/integration/init.test.mjs | 7 ++-- utils/get-cwd.js | 6 ++-- utils/hash/compare.js | 4 +-- utils/hash/file.js | 8 ++--- utils/hash/files.js | 8 ++--- utils/hash/index.js | 8 ++--- utils/http/index.js | 11 +++--- utils/http/integrity.js | 8 ++--- utils/http/latest-version.js | 8 ++--- utils/http/request.js | 10 +++--- utils/http/versions.js | 8 ++--- utils/index.js | 16 ++++----- utils/json/index.js | 8 ++--- utils/json/read.js | 10 +++--- utils/json/write-eik.js | 8 ++--- utils/json/write.js | 10 +++--- utils/logger.js | 4 +-- utils/type-slug.js | 2 +- utils/type-title.js | 2 +- 58 files changed, 346 insertions(+), 422 deletions(-) create mode 100644 commands/index.js diff --git a/.eslintrc b/.eslintrc index 41b7cbeb..4df79209 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,6 +5,7 @@ "strict": [0, "global"], "class-methods-use-this": [0], "no-restricted-syntax": [0], - "default-param-last": [0] + "default-param-last": [0], + "import/extensions": [0] } } diff --git a/classes/alias.js b/classes/alias.js index 071a116f..335d2f28 100644 --- a/classes/alias.js +++ b/classes/alias.js @@ -1,13 +1,11 @@ -'use strict'; +import assert from 'assert'; +import abslog from 'abslog'; +import { join } from 'path'; +import { schemas, validators } from '@eik/common'; +import { request } from '../utils/http/index.js'; +import { typeSlug } from '../utils/index.js'; -const assert = require('assert'); -const abslog = require('abslog'); -const { join } = require('path'); -const { schemas, validators } = require('@eik/common'); -const { request } = require('../utils/http'); -const { typeSlug } = require('../utils'); - -module.exports = class Alias { +export default class Alias { constructor({ logger, server, token, type, name, version, alias } = {}) { this.log = abslog(logger); this.server = server; diff --git a/classes/index.js b/classes/index.js index 631d3ffe..e55bbe7f 100644 --- a/classes/index.js +++ b/classes/index.js @@ -1,15 +1,15 @@ 'use strict'; -const Ping = require('./ping'); -const Alias = require('./alias'); -const Meta = require('./meta'); -const Login = require('./login'); -const PublishMap = require('./publish/map'); -const PublishPackage = require('./publish/package/index'); -const Integrity = require('./integrity'); -const Version = require('./version'); - -module.exports = { +import Ping from './ping.js'; +import Alias from './alias.js'; +import Meta from './meta.js'; +import Login from './login.js'; +import PublishMap from './publish/map.js'; +import PublishPackage from './publish/package/index.js'; +import Integrity from './integrity.js'; +import Version from './version.js'; + +export default { ping(opts) { return new Ping(opts).run(); }, diff --git a/classes/integrity.js b/classes/integrity.js index 784018c3..26de1fbf 100644 --- a/classes/integrity.js +++ b/classes/integrity.js @@ -1,15 +1,14 @@ /* eslint-disable no-await-in-loop */ /* eslint-disable no-plusplus */ +import abslog from 'abslog'; +import { join } from 'path'; +import eik from '@eik/common'; +import fetch from 'node-fetch'; +import { typeSlug } from '../utils/index.js'; -'use strict'; +const { schemas, ValidationError } = eik; -const abslog = require('abslog'); -const { join } = require('path'); -const { schemas, ValidationError } = require('@eik/common'); -const fetch = require('node-fetch'); -const { typeSlug } = require('../utils'); - -module.exports = class Integrity { +export default class Integrity { constructor({ logger, name, diff --git a/classes/login.js b/classes/login.js index 0dabe73a..78061cc5 100644 --- a/classes/login.js +++ b/classes/login.js @@ -1,10 +1,12 @@ 'use strict'; -const abslog = require('abslog'); -const { schemas, ValidationError } = require('@eik/common'); -const { request } = require('../utils/http'); +import abslog from 'abslog'; +import eik from '@eik/common'; +import { request } from '../utils/http/index.js'; -module.exports = class Login { +const { schemas, ValidationError } = eik; + +export default class Login { constructor({ logger, server, key } = {}) { this.log = abslog(logger); this.server = server; diff --git a/classes/meta.js b/classes/meta.js index 8a2f8d97..f009aa87 100644 --- a/classes/meta.js +++ b/classes/meta.js @@ -1,16 +1,13 @@ /* eslint-disable no-await-in-loop */ /* eslint-disable no-plusplus */ - -'use strict'; - -const abslog = require('abslog'); -const { join } = require('path'); -const { schemas } = require('@eik/common'); -const fetch = require('node-fetch'); +import abslog from 'abslog'; +import { join } from 'path'; +import { schemas } from '@eik/common'; +import fetch from 'node-fetch'; const types = ['pkg', 'map', 'npm']; -module.exports = class Meta { +export default class Meta { constructor({ logger, server, name, version } = {}) { this.log = abslog(logger); this.server = server; diff --git a/classes/ping.js b/classes/ping.js index 01a9cd9c..f2472027 100644 --- a/classes/ping.js +++ b/classes/ping.js @@ -1,10 +1,8 @@ -'use strict'; +import fetch from 'node-fetch'; +import abslog from 'abslog'; +import { schemas } from '@eik/common'; -const fetch = require('node-fetch'); -const abslog = require('abslog'); -const { schemas } = require('@eik/common'); - -module.exports = class Ping { +export default class Ping { constructor({ logger, server } = {}) { this.log = abslog(logger); this.server = server; diff --git a/classes/publish/map.js b/classes/publish/map.js index b43e122b..14eaafb9 100644 --- a/classes/publish/map.js +++ b/classes/publish/map.js @@ -1,13 +1,11 @@ -'use strict'; +import assert from 'assert'; +import abslog from 'abslog'; +import { join, parse, isAbsolute } from 'path'; +import { existsSync } from 'fs'; +import { schemas } from '@eik/common'; +import { request } from '../../utils/http/index.js'; -const assert = require('assert'); -const abslog = require('abslog'); -const { join, parse, isAbsolute } = require('path'); -const { existsSync } = require('fs'); -const { schemas } = require('@eik/common'); -const { request } = require('../../utils/http'); - -module.exports = class PublishMap { +export default class PublishMap { constructor({ logger, cwd = process.cwd(), diff --git a/classes/publish/package/index.js b/classes/publish/package/index.js index c92ad5fe..e2076af9 100644 --- a/classes/publish/package/index.js +++ b/classes/publish/package/index.js @@ -1,20 +1,18 @@ -'use strict'; +import abslog from 'abslog'; +import { join, isAbsolute } from 'path'; +import { EikConfig } from '@eik/common'; +import { typeSlug } from '../../../utils/index.js'; +import ValidateInput from './tasks/validate-input.js'; +import CreateTempDirectory from './tasks/create-temp-directory.js'; +import CreateZipFile from './tasks/create-zip-file.js'; +import CheckBundleSizes from './tasks/check-bundle-sizes.js'; +import DryRun from './tasks/dry-run.js'; +import CheckIfAlreadyPublished from './tasks/check-if-already-published.js'; +import UploadFiles from './tasks/upload-files.js'; +import SaveMetafile from './tasks/save-metafile.js'; +import Cleanup from './tasks/cleanup.js'; -const abslog = require('abslog'); -const { join, isAbsolute } = require('path'); -const { EikConfig } = require('@eik/common'); -const { typeSlug } = require('../../../utils'); -const ValidateInput = require('./tasks/validate-input'); -const CreateTempDirectory = require('./tasks/create-temp-directory'); -const CreateZipFile = require('./tasks/create-zip-file'); -const CheckBundleSizes = require('./tasks/check-bundle-sizes'); -const DryRun = require('./tasks/dry-run'); -const CheckIfAlreadyPublished = require('./tasks/check-if-already-published'); -const UploadFiles = require('./tasks/upload-files'); -const SaveMetafile = require('./tasks/save-metafile'); -const Cleanup = require('./tasks/cleanup'); - -module.exports = class Publish { +export default class Publish { constructor({ logger, cwd = process.cwd(), diff --git a/classes/publish/package/tasks/check-bundle-sizes.js b/classes/publish/package/tasks/check-bundle-sizes.js index e7a5edf3..9118fb3d 100644 --- a/classes/publish/package/tasks/check-bundle-sizes.js +++ b/classes/publish/package/tasks/check-bundle-sizes.js @@ -1,13 +1,9 @@ -/* eslint-disable no-await-in-loop */ +import bytes from 'bytes'; +import fs from 'fs'; +import gzipSize from 'gzip-size'; +import Task from './task.js'; -'use strict'; - -const bytes = require('bytes'); -const fs = require('fs'); -const gzipSize = require('gzip-size'); -const Task = require('./task'); - -module.exports = class CheckBundleSizes extends Task { +export default class CheckBundleSizes extends Task { async process() { this.log.debug('Checking bundle file sizes'); try { diff --git a/classes/publish/package/tasks/check-if-already-published.js b/classes/publish/package/tasks/check-if-already-published.js index 09c78262..92138149 100644 --- a/classes/publish/package/tasks/check-if-already-published.js +++ b/classes/publish/package/tasks/check-if-already-published.js @@ -1,14 +1,10 @@ -/* eslint-disable no-param-reassign */ +import { join } from 'path'; +import { integrity, versions } from '../../../../utils/http/index.js'; +import hash from '../../../../utils/hash/index.js'; +import { typeSlug } from '../../../../utils/index.js'; +import Task from './task.js'; -'use strict'; - -const { join } = require('path'); -const { integrity, versions } = require('../../../../utils/http'); -const hash = require('../../../../utils/hash'); -const { typeSlug } = require('../../../../utils'); -const Task = require('./task'); - -module.exports = class CheckIfAlreadyPublished extends Task { +export default class CheckIfAlreadyPublished extends Task { async process() { const { log, path } = this; const { server, name, version, files, type } = this.config; diff --git a/classes/publish/package/tasks/cleanup.js b/classes/publish/package/tasks/cleanup.js index 5ebe4243..4366b39a 100644 --- a/classes/publish/package/tasks/cleanup.js +++ b/classes/publish/package/tasks/cleanup.js @@ -1,11 +1,9 @@ -'use strict'; +import { join } from 'path'; +import fs from 'fs'; +import rimraf from 'rimraf'; +import Task from './task.js'; -const { join } = require('path'); -const fs = require('fs'); -const rimraf = require('rimraf'); -const Task = require('./task'); - -module.exports = class Cleanup extends Task { +export default class Cleanup extends Task { async process() { const { log, path } = this; log.debug('Cleaning up'); diff --git a/classes/publish/package/tasks/create-temp-directory.js b/classes/publish/package/tasks/create-temp-directory.js index df28cf32..69a4776a 100644 --- a/classes/publish/package/tasks/create-temp-directory.js +++ b/classes/publish/package/tasks/create-temp-directory.js @@ -1,9 +1,6 @@ /* eslint-disable max-classes-per-file */ - -'use strict'; - -const mkdir = require('make-dir'); -const Task = require('./task'); +import mkdir from 'make-dir'; +import Task from './task.js'; class IOError extends Error { constructor(message, err) { @@ -13,7 +10,7 @@ class IOError extends Error { } } -module.exports = class CreateTempDir extends Task { +export default class CreateTempDir extends Task { async process() { const { log, path } = this; diff --git a/classes/publish/package/tasks/create-zip-file.js b/classes/publish/package/tasks/create-zip-file.js index e5b85526..8d2ce851 100644 --- a/classes/publish/package/tasks/create-zip-file.js +++ b/classes/publish/package/tasks/create-zip-file.js @@ -1,16 +1,13 @@ /* eslint-disable no-plusplus */ /* eslint-disable no-await-in-loop */ - -'use strict'; - -const fs = require('fs'); -const { join, resolve, basename, dirname } = require('path'); -const tar = require('tar'); -const Task = require('./task'); +import fs from 'fs'; +import { join, resolve, basename, dirname } from 'path'; +import tar from 'tar'; +import Task from './task.js'; const { copyFileSync, writeFileSync } = fs; -module.exports = class CreateZipFile extends Task { +export default class CreateZipFile extends Task { async process() { const { log, path } = this; const { name, map, server, out, files } = this.config; diff --git a/classes/publish/package/tasks/dry-run.js b/classes/publish/package/tasks/dry-run.js index 23e83494..987a234b 100644 --- a/classes/publish/package/tasks/dry-run.js +++ b/classes/publish/package/tasks/dry-run.js @@ -1,11 +1,8 @@ /* eslint-disable no-param-reassign */ +import { join } from 'path'; +import Task from './task.js'; -'use strict'; - -const { join } = require('path'); -const Task = require('./task'); - -module.exports = class DryRun extends Task { +export default class DryRun extends Task { async process(zipFile) { const { path } = this; diff --git a/classes/publish/package/tasks/save-metafile.js b/classes/publish/package/tasks/save-metafile.js index f5184be2..3159e015 100644 --- a/classes/publish/package/tasks/save-metafile.js +++ b/classes/publish/package/tasks/save-metafile.js @@ -1,10 +1,8 @@ -'use strict'; +import { join } from 'path'; +import json from '../../../../utils/json/index.js'; +import Task from './task.js'; -const { join } = require('path'); -const json = require('../../../../utils/json'); -const Task = require('./task'); - -module.exports = class SaveMetaFile extends Task { +export default class SaveMetaFile extends Task { async process(response) { const { log, cwd } = this; const { out } = this.config; diff --git a/classes/publish/package/tasks/task.js b/classes/publish/package/tasks/task.js index 7188fcad..daedc60a 100644 --- a/classes/publish/package/tasks/task.js +++ b/classes/publish/package/tasks/task.js @@ -1,8 +1,6 @@ -'use strict'; +import abslog from 'abslog'; -const abslog = require('abslog'); - -module.exports = class Task { +export default class Task { constructor(options) { this.cwd = options.cwd; this.log = abslog(options.logger); diff --git a/classes/publish/package/tasks/upload-files.js b/classes/publish/package/tasks/upload-files.js index d3c5459f..8e7dfad0 100644 --- a/classes/publish/package/tasks/upload-files.js +++ b/classes/publish/package/tasks/upload-files.js @@ -1,13 +1,10 @@ /* eslint-disable no-param-reassign */ +import { join } from 'path'; +import { request } from '../../../../utils/http/index.js'; +import { typeSlug } from '../../../../utils/index.js'; +import Task from './task.js'; -'use strict'; - -const { join } = require('path'); -const { request } = require('../../../../utils/http'); -const { typeSlug } = require('../../../../utils'); -const Task = require('./task'); - -module.exports = class UploadFiles extends Task { +export default class UploadFiles extends Task { async process(zipFile) { const { log } = this; const { server, name, version, type, token } = this.config; @@ -62,4 +59,4 @@ module.exports = class UploadFiles extends Task { } } } -}; +} diff --git a/classes/publish/package/tasks/validate-input.js b/classes/publish/package/tasks/validate-input.js index 1f8accc4..7ed4331a 100644 --- a/classes/publish/package/tasks/validate-input.js +++ b/classes/publish/package/tasks/validate-input.js @@ -1,9 +1,6 @@ /* eslint-disable max-classes-per-file */ - -'use strict'; - -const { parse } = require('path'); -const Task = require('./task'); +import { parse } from 'path'; +import Task from './task.js'; class ValidationError extends Error { constructor(message, err) { @@ -15,7 +12,7 @@ class ValidationError extends Error { } } -module.exports = class ValidateInput extends Task { +export default class ValidateInput extends Task { process() { const { log } = this; const { cwd, dryRun } = this.config; diff --git a/classes/version.js b/classes/version.js index b7f77e5e..a16022f8 100644 --- a/classes/version.js +++ b/classes/version.js @@ -1,18 +1,15 @@ /* eslint-disable max-classes-per-file */ - -'use strict'; - -const { copyFileSync, writeFileSync } = require('fs'); -const { join, isAbsolute, parse } = require('path'); -const abslog = require('abslog'); -const semver = require('semver'); -const mkdir = require('make-dir'); -const { schemas, EikConfig } = require('@eik/common'); -const { integrity } = require('../utils/http'); -const hash = require('../utils/hash'); -const { typeSlug } = require('../utils'); - -module.exports = class Version { +import { copyFileSync, writeFileSync } from 'fs'; +import { join, isAbsolute, parse } from 'path'; +import abslog from 'abslog'; +import semver from 'semver'; +import mkdir from 'make-dir'; +import { schemas, EikConfig } from '@eik/common'; +import { integrity } from '../utils/http/index.js'; +import hash from '../utils/hash/index.js'; +import { typeSlug } from '../utils/index.js'; + +export default class Version { constructor({ logger, server, diff --git a/commands/index.js b/commands/index.js new file mode 100644 index 00000000..8e403262 --- /dev/null +++ b/commands/index.js @@ -0,0 +1,13 @@ +import * as init from './init.js'; +import * as integrity from './integrity.js'; +import * as login from './login.js'; +import * as mapAlias from './map-alias.js'; +import * as map from './map.js'; +import * as meta from './meta.js'; +import * as npmAlias from './npm-alias.js'; +import * as packageAlias from './package-alias.js'; +import * as ping from './ping.js'; +import * as publish from './publish.js'; +import * as version from './version.js'; + +export const commands = [ init, integrity, login, mapAlias, map, meta, npmAlias, packageAlias, ping, publish, version ]; \ No newline at end of file diff --git a/commands/init.js b/commands/init.js index 3fd26dfe..20b8eeb9 100644 --- a/commands/init.js +++ b/commands/init.js @@ -1,18 +1,16 @@ -'use strict'; +import { join } from 'path'; +import fs from 'fs'; +import ora from 'ora'; +import { logger } from '../utils/index.js'; -const { join } = require('path'); -const fs = require('fs'); -const ora = require('ora'); -const { logger } = require('../utils'); +const command = 'init'; -exports.command = 'init'; +const aliases = ['i']; -exports.aliases = ['i']; - -exports.describe = `Creates a new default "eik.json" file and saves it to the current working directory +const describe = `Creates a new default "eik.json" file and saves it to the current working directory Override default "eik.json" fields using command line flags --server, --name, --major, --js and --css`; -exports.builder = (yargs) => { +const builder = (yargs) => { yargs.example('eik init'); yargs.example('eik init --cwd /path/to/dir'); yargs.example( @@ -55,7 +53,7 @@ exports.builder = (yargs) => { }); }; -exports.handler = async (argv) => { +const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); const { name, version, server, cwd, debug } = argv; const pathname = join(cwd, './eik.json'); @@ -103,3 +101,5 @@ exports.handler = async (argv) => { spinner.text = ''; spinner.stopAndPersist(); }; + +export { command, aliases, describe, builder, handler }; \ No newline at end of file diff --git a/commands/integrity.js b/commands/integrity.js index 209e5981..c0ade3b5 100644 --- a/commands/integrity.js +++ b/commands/integrity.js @@ -1,25 +1,22 @@ /* eslint-disable prefer-template */ /* eslint-disable no-restricted-properties */ /* eslint-disable one-var */ +import { join } from 'path'; +import ora from 'ora'; +import { helpers } from '@eik/common'; +import Integrity from '../classes/integrity.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import json from '../utils/json/index.js'; -'use strict'; +const { configStore } = helpers; -const { join } = require('path'); -const ora = require('ora'); -const { - helpers: { configStore }, -} = require('@eik/common'); -const Integrity = require('../classes/integrity'); -const { logger, getDefaults, getCWD } = require('../utils'); -const json = require('../utils/json'); +export const command = 'integrity [name] [version]'; -exports.command = 'integrity [name] [version]'; +export const aliases = ['int']; -exports.aliases = ['int']; +export const describe = `Retrieve file integrity information for package name and version defined in eik.json, then populate integrity.json file with this information`; -exports.describe = `Retrieve file integrity information for package name and version defined in eik.json, then populate integrity.json file with this information`; - -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -45,7 +42,7 @@ exports.builder = (yargs) => { yargs.example(`eik integrity --debug`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); let integrity = false; const { debug, cwd } = argv; diff --git a/commands/login.js b/commands/login.js index f09422dc..732600b1 100644 --- a/commands/login.js +++ b/commands/login.js @@ -1,21 +1,21 @@ -'use strict'; +import os from 'os'; +import readline from 'readline'; +import ora from 'ora'; +import Login from '../classes/login.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import json from '../utils/json/index.js'; -const homedir = require('os').homedir(); -const readline = require('readline'); -const ora = require('ora'); -const Login = require('../classes/login'); -const { logger, getDefaults, getCWD } = require('../utils'); -const json = require('../utils/json'); +const homedir = os.homedir(); -exports.command = 'login'; +export const command = 'login'; -exports.aliases = []; +export const aliases = []; -exports.describe = `Authenticate against an Eik server and save the returned token to an .eikrc file in the users home directory. +export const describe = `Authenticate against an Eik server and save the returned token to an .eikrc file in the users home directory. You can specify key and server values to authenticate against using the --key and --server flags which will then bypass login prompts It is possible to be authenticated against multiple asset servers simultaneously. Simply call "eik login" multiple times.`; -exports.builder = (yargs) => { +export const builder = (yargs) => { yargs.example('eik login --server https://assets.myserver.com'); yargs.example( 'eik login --server https://assets.myserver.com --key ######', @@ -53,7 +53,7 @@ exports.builder = (yargs) => { }); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { let success = false; const { debug, key, server } = argv; let k = key; diff --git a/commands/map-alias.js b/commands/map-alias.js index 55938582..644f3aa8 100644 --- a/commands/map-alias.js +++ b/commands/map-alias.js @@ -1,20 +1,20 @@ 'use strict'; -const ora = require('ora'); -const Alias = require('../classes/alias'); -const { logger, getDefaults, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); +import ora from 'ora'; +import Alias from '../classes/alias.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import { Alias as AliasFormatter } from '../formatters/index.js'; -exports.command = 'map-alias '; +export const command = 'map-alias '; -exports.aliases = ['ma']; +export const aliases = ['ma']; -exports.describe = `Create a semver major alias for an import map as identified by its name and version. +export const describe = `Create a semver major alias for an import map as identified by its name and version. An import map with the given name and version must already exist on asset server Alias should be the semver major part of the import map version. Eg. For an import map of version 5.4.3, you should use 5 as the alias`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -67,7 +67,7 @@ exports.builder = (yargs) => { yargs.example(`eik map-alias my-map 4.2.2 4 --debug`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); let success = false; const { debug, name, version, server } = argv; diff --git a/commands/map.js b/commands/map.js index 03a22bc8..6608ae9a 100644 --- a/commands/map.js +++ b/commands/map.js @@ -1,21 +1,21 @@ 'use strict'; -const { join } = require('path'); -const fetch = require('node-fetch'); -const ora = require('ora'); -const PublishMap = require('../classes/publish/map'); -const { logger, getDefaults, getCWD } = require('../utils'); -const { Artifact } = require('../formatters'); +import { join } from 'path'; +import fetch from 'node-fetch'; +import ora from 'ora'; +import PublishMap from '../classes/publish/map.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import { Artifact } from '../formatters/index.js'; -exports.command = 'map '; +export const command = 'map '; -exports.aliases = ['m']; +export const aliases = ['m']; -exports.describe = `Upload an import map file to the server under a given name and version. +export const describe = `Upload an import map file to the server under a given name and version. A name/version combination must be unique and a version must be semver compliant. Subsquent published versions must increase. Eg. 1.0.0 1.0.1, 1.1.0, 2.0.0 etc.`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -68,7 +68,7 @@ exports.builder = (yargs) => { ); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); const { debug, server, name, version } = argv; diff --git a/commands/meta.js b/commands/meta.js index c103ffe5..07c2cc58 100644 --- a/commands/meta.js +++ b/commands/meta.js @@ -1,22 +1,19 @@ /* eslint-disable prefer-template */ /* eslint-disable no-restricted-properties */ /* eslint-disable one-var */ +import ora from 'ora'; +import Meta from '../classes/meta.js'; +import { Artifact } from '../formatters/index.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; -'use strict'; +export const command = 'meta '; -const ora = require('ora'); -const Meta = require('../classes/meta'); -const { Artifact } = require('../formatters'); -const { logger, getDefaults, getCWD } = require('../utils'); +export const aliases = ['show']; -exports.command = 'meta '; - -exports.aliases = ['show']; - -exports.describe = `Retrieve meta information by package, map or npm name +export const describe = `Retrieve meta information by package, map or npm name If a given name exists in several types (package and map for example), results will be returned and displayed from all matching types`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -49,7 +46,7 @@ exports.builder = (yargs) => { yargs.example(`eik meta my-app --server https://assets.myeikserver.com`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); let meta = false; const { debug, server } = argv; diff --git a/commands/npm-alias.js b/commands/npm-alias.js index a5b7dc05..f65f9105 100644 --- a/commands/npm-alias.js +++ b/commands/npm-alias.js @@ -1,20 +1,18 @@ -'use strict'; +import ora from 'ora'; +import Alias from '../classes/alias.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import { Alias as AliasFormatter } from '../formatters/index.js'; -const ora = require('ora'); -const Alias = require('../classes/alias'); -const { logger, getDefaults, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); +export const command = 'npm-alias '; -exports.command = 'npm-alias '; +export const aliases = ['na', 'dep-alias', 'dependency-alias']; -exports.aliases = ['na', 'dep-alias', 'dependency-alias']; - -exports.describe = `Create a semver major alias for an NPM package as identified by its name and version. +export const describe = `Create a semver major alias for an NPM package as identified by its name and version. An NPM package with the given name and version must already exist on the asset server Alias should be the semver major part of the NPM package version. Eg. For an NPM package of version 5.4.3, you should use 5 as the alias`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -66,7 +64,7 @@ exports.builder = (yargs) => { ); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); let success = false; const { debug, server } = argv; diff --git a/commands/package-alias.js b/commands/package-alias.js index 4e35486e..e8a9d26b 100644 --- a/commands/package-alias.js +++ b/commands/package-alias.js @@ -1,21 +1,19 @@ -'use strict'; +import ora from 'ora'; +import semver from 'semver'; +import Alias from '../classes/alias.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import { Alias as AliasFormatter } from '../formatters/index.js'; -const ora = require('ora'); -const semver = require('semver'); -const Alias = require('../classes/alias'); -const { logger, getDefaults, getCWD } = require('../utils'); -const { Alias: AliasFormatter } = require('../formatters'); +export const command = 'package-alias [name] [version] [alias]'; -exports.command = 'package-alias [name] [version] [alias]'; +export const aliases = ['pkg-alias', 'pa']; -exports.aliases = ['pkg-alias', 'pa']; - -exports.describe = `Create a semver major alias for a package as identified by its name and version. +export const describe = `Create a semver major alias for a package as identified by its name and version. A package with the given name and version must already exist on asset server Alias should be the semver major part of the package version. Eg. For a package of version 5.4.3, you should use 5 as the alias`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -73,7 +71,7 @@ exports.builder = (yargs) => { yargs.example(`eik package-alias my-app 4.2.2 4 --debug`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); let success = false; const { debug, server } = argv; diff --git a/commands/ping.js b/commands/ping.js index 49e099b4..a02447b1 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -1,16 +1,14 @@ -'use strict'; +import ora from 'ora'; +import Ping from '../classes/ping.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; -const ora = require('ora'); -const Ping = require('../classes/ping'); -const { logger, getDefaults, getCWD } = require('../utils'); +export const command = 'ping [server]'; -exports.command = 'ping [server]'; +export const aliases = []; -exports.aliases = []; +export const describe = `Ping an Eik server to check that it is responding.`; -exports.describe = `Ping an Eik server to check that it is responding.`; - -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -32,7 +30,7 @@ exports.builder = (yargs) => { yargs.example(`eik ping http://assets.myeikserver.com --debug`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); const { debug, server } = argv; diff --git a/commands/publish.js b/commands/publish.js index 6ffa82da..b0e755d6 100644 --- a/commands/publish.js +++ b/commands/publish.js @@ -1,29 +1,27 @@ -'use strict'; - -const { join } = require('path'); -const fetch = require('node-fetch'); -const ora = require('ora'); -const chalk = require('chalk'); -const { - helpers: { configStore }, -} = require('@eik/common'); -const PublishPackage = require('../classes/publish/package/index'); -const { +import { join } from 'path'; +import fetch from 'node-fetch'; +import ora from 'ora'; +import chalk from 'chalk'; +import { helpers } from '@eik/common'; +import PublishPackage from '../classes/publish/package/index.js'; +import { logger, getDefaults, getCWD, typeSlug, typeTitle, -} = require('../utils'); -const { Artifact } = require('../formatters'); +} from '../utils/index.js'; +import { Artifact } from '../formatters/index.js'; -exports.command = 'publish'; +const { configStore } = helpers; -exports.aliases = ['pkg', 'package', 'pub']; +export const command = 'publish'; -exports.describe = `Publish an app package to an Eik server. Reads configuration from eik.json or package.json files. See https://eik.dev for more details.`; +export const aliases = ['pkg', 'package', 'pub']; -exports.builder = (yargs) => { +export const describe = `Publish an app package to an Eik server. Reads configuration from eik.json or package.json files. See https://eik.dev for more details.`; + +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -63,7 +61,7 @@ exports.builder = (yargs) => { yargs.example(`eik pkg --debug`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); const { debug, dryRun, cwd, token } = argv; const config = configStore.findInDirectory(cwd); diff --git a/commands/version.js b/commands/version.js index 6544f699..d3c016ed 100644 --- a/commands/version.js +++ b/commands/version.js @@ -1,22 +1,20 @@ -'use strict'; +import { execSync } from 'child_process'; +import { join } from 'path'; +import ora from 'ora'; +import { helpers } from '@eik/common'; +import VersionPackage from '../classes/version.js'; +import { logger, getDefaults, getCWD } from '../utils/index.js'; +import json from '../utils/json/index.js'; -const { execSync } = require('child_process'); -const { join } = require('path'); -const ora = require('ora'); -const { - helpers: { configStore }, -} = require('@eik/common'); -const VersionPackage = require('../classes/version'); -const { logger, getDefaults, getCWD } = require('../utils'); -const json = require('../utils/json'); +const { configStore } = helpers; -exports.command = 'version [level]'; +export const command = 'version [level]'; -exports.aliases = ['v']; +export const aliases = ['v']; -exports.describe = `Compares local files with files on server and increments "version" field in eik.json if necessary.`; +export const describe = `Compares local files with files on server and increments "version" field in eik.json if necessary.`; -exports.builder = (yargs) => { +export const builder = (yargs) => { const cwd = getCWD(); const defaults = getDefaults(cwd); @@ -53,7 +51,7 @@ exports.builder = (yargs) => { yargs.example(`eik v`); }; -exports.handler = async (argv) => { +export const handler = async (argv) => { const spinner = ora({ stream: process.stdout }).start('working...'); const { level, debug, dryRun, cwd } = argv; const config = configStore.findInDirectory(cwd); diff --git a/formatters/alias.js b/formatters/alias.js index bd5a7fa7..1673f205 100644 --- a/formatters/alias.js +++ b/formatters/alias.js @@ -1,10 +1,7 @@ /* eslint-disable no-underscore-dangle */ - -'use strict'; - -const { join } = require('path'); -const chalk = require('chalk'); -const File = require('./file'); +import { join } from 'path'; +import chalk from 'chalk'; +import File from './file.js'; function colorType(type) { if (type === 'npm') { @@ -79,4 +76,4 @@ class Alias { } } -module.exports = Alias; +export default Alias; diff --git a/formatters/artifact.js b/formatters/artifact.js index fb892fca..0c4d1a5a 100644 --- a/formatters/artifact.js +++ b/formatters/artifact.js @@ -1,10 +1,7 @@ /* eslint-disable no-underscore-dangle */ - -'use strict'; - -const { join } = require('path'); -const chalk = require('chalk'); -const Version = require('./version'); +import { join } from 'path'; +import chalk from 'chalk'; +import Version from './version.js'; const _name = Symbol('name'); const _type = Symbol('type'); @@ -86,4 +83,4 @@ class Artifact { } } -module.exports = Artifact; +export default Artifact; diff --git a/formatters/file.js b/formatters/file.js index 1db62bc6..43b0a902 100644 --- a/formatters/file.js +++ b/formatters/file.js @@ -2,11 +2,8 @@ /* eslint-disable prefer-template */ /* eslint-disable one-var */ /* eslint-disable no-underscore-dangle */ - -'use strict'; - -const { join } = require('path'); -const chalk = require('chalk'); +import { join } from 'path'; +import chalk from 'chalk'; function readableBytes(bytes) { const i = Math.floor(Math.log(bytes) / Math.log(1024)), @@ -39,4 +36,4 @@ class File { } } -module.exports = File; +export default File; diff --git a/formatters/index.js b/formatters/index.js index edf3ecb1..32fba67c 100644 --- a/formatters/index.js +++ b/formatters/index.js @@ -1,6 +1,6 @@ -const Artifact = require('./artifact'); -const Alias = require('./alias'); -const File = require('./file'); -const Version = require('./version'); +import Artifact from './artifact.js'; +import Alias from './alias.js'; +import File from './file.js'; +import Version from './version.js'; -module.exports = { Artifact, Alias, Version, File }; +export { Artifact, Alias, Version, File }; diff --git a/formatters/version.js b/formatters/version.js index 8c1132b2..03bdd945 100644 --- a/formatters/version.js +++ b/formatters/version.js @@ -2,13 +2,10 @@ /* eslint-disable prefer-template */ /* eslint-disable one-var */ /* eslint-disable no-underscore-dangle */ - -'use strict'; - -const { join } = require('path'); -const chalk = require('chalk'); -const formatDistance = require('date-fns/formatDistance'); -const File = require('./file'); +import { join } from 'path'; +import chalk from 'chalk'; +import formatDistance from 'date-fns/formatDistance/index.js'; +import File from './file.js'; class Version { constructor({ @@ -65,4 +62,4 @@ class Version { } } -module.exports = Version; +export default Version; diff --git a/index.js b/index.js index 66bb4197..9896f41b 100755 --- a/index.js +++ b/index.js @@ -1,14 +1,18 @@ #!/usr/bin/env node +import chalk from 'chalk'; +import yargs from 'yargs'; +import { hideBin } from 'yargs/helpers'; +import boxen from 'boxen'; +import { join } from 'path'; +import { readFileSync } from 'fs'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import { commands } from './commands/index.js'; -'use strict'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); -const chalk = require('chalk'); -const yargs = require('yargs'); -const boxen = require('boxen'); -const { join } = require('path'); -const { readFileSync } = require('fs'); - -const { version } = JSON.parse(readFileSync(join(__dirname, './package.json'))); +const { version } = JSON.parse(readFileSync(join(__dirname, './package.json'), { encoding: 'utf-8' })); const greeting = chalk.white.bold(`Eik CLI (v${version})`); const boxenOptions = { @@ -24,7 +28,7 @@ const msgBox = boxen(greeting, boxenOptions); console.log(msgBox); // eslint-disable-next-line no-unused-expressions -yargs +yargs(hideBin(process.argv)) .example('eik init') .example('eik login --server https://assets.myserver.com --key ######') .example('eik publish') @@ -36,8 +40,9 @@ yargs 'eik map my-map 1.0.0 ./import-map.json --server https://assets.myserver.com --token ######', ) .example('eik map-alias my-map 1.0.0 1') - .commandDir('commands') + .command(commands) .demandCommand() .wrap(150) .version(false) - .help().argv; + .help() + .parse(); diff --git a/package.json b/package.json index a0e16a0b..0375306c 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.0.39", "description": "CLI tool for publishing assets to an Eik server", "main": "./classes/index.js", + "type": "module", "bin": { "eik": "index.js" }, diff --git a/test/integration/init.test.mjs b/test/integration/init.test.mjs index 4276ae41..a93b38c5 100644 --- a/test/integration/init.test.mjs +++ b/test/integration/init.test.mjs @@ -3,7 +3,7 @@ import os from 'os'; import { test } from 'tap'; import { join, basename } from 'path'; import { readFileSync } from 'fs'; -import { exec as execCallback } from 'child_process'; +import { exec as execCallback } from 'node:child_process'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; @@ -21,12 +21,9 @@ function exec(cmd) { test('Initializing a new eik.json file', async (t) => { const eik = join(__dirname, '../../index.js'); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); - const publishCmd = `${eik} init --cwd ${folder}`; - await exec(publishCmd); - + const res = await exec(publishCmd); const assets = JSON.parse(readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' })); - t.equal(assets.name, '', 'eik.json "name" field should be empty'); t.equal( assets.version, diff --git a/utils/get-cwd.js b/utils/get-cwd.js index aacf8ade..1cc0146a 100644 --- a/utils/get-cwd.js +++ b/utils/get-cwd.js @@ -1,6 +1,6 @@ -'use strict'; +import yargs from 'yargs-parser'; -const av = require('yargs-parser')(process.argv.slice(2)); +const av = yargs(process.argv.slice(2)); /** * Returns the current working directory path @@ -9,6 +9,6 @@ const av = require('yargs-parser')(process.argv.slice(2)); * * @returns {string} */ -module.exports = function getCWD() { +export default function getCWD() { return av.cwd || av.c || process.cwd(); }; diff --git a/utils/hash/compare.js b/utils/hash/compare.js index aca384b6..bd32fd49 100644 --- a/utils/hash/compare.js +++ b/utils/hash/compare.js @@ -1,5 +1,3 @@ -'use strict'; - /** * Compares 2 hash strings for comparison. Returns `true` if hashes are identical, false otherwise. * @@ -10,4 +8,4 @@ * * @example hash.compare('a1b22c23d24e25f4g33a123b23c34', 'a1b22c23d24e25f4g33a123b23c34'); */ -module.exports = (hash1, hash2) => hash1 === hash2; +export default (hash1, hash2) => hash1 === hash2; diff --git a/utils/hash/file.js b/utils/hash/file.js index ffb9c6fe..14564253 100644 --- a/utils/hash/file.js +++ b/utils/hash/file.js @@ -1,7 +1,5 @@ -'use strict'; - -const ssri = require('ssri'); -const fs = require('fs'); +import ssri from 'ssri'; +import fs from 'fs'; /** * Reads a file from a given path and produces and returns an integrity hash from its contents @@ -12,7 +10,7 @@ const fs = require('fs'); * * @example hash.file('/path/to/file.js'); */ -module.exports = async (path) => { +export default async (path) => { const integrity = await ssri.fromStream(fs.createReadStream(path)); return integrity.toString(); }; diff --git a/utils/hash/files.js b/utils/hash/files.js index fbff6d93..c856a58f 100644 --- a/utils/hash/files.js +++ b/utils/hash/files.js @@ -1,7 +1,5 @@ -'use strict'; - -const ssri = require('ssri'); -const fileHash = require('./file'); +import ssri from 'ssri'; +import fileHash from './file.js'; /** * Reads files from given paths and produces and returns an integrity hash from all files contents @@ -12,7 +10,7 @@ const fileHash = require('./file'); * * @example hash.files(['/path/to/file1.js', '/path/to/file2.js']); */ -module.exports = async (files) => { +export default async (files) => { const hashes = await Promise.all(files.map(fileHash)); const hasher = ssri.create(); for (const hash of hashes.sort()) { diff --git a/utils/hash/index.js b/utils/hash/index.js index 079c5c25..bf18b795 100644 --- a/utils/hash/index.js +++ b/utils/hash/index.js @@ -1,5 +1,5 @@ -const file = require('./file'); -const files = require('./files'); -const compare = require('./compare'); +import file from './file.js'; +import files from './files.js'; +import compare from './compare.js'; -module.exports = { file, files, compare }; +export default { file, files, compare }; diff --git a/utils/http/index.js b/utils/http/index.js index 28fa5c81..7559924b 100644 --- a/utils/http/index.js +++ b/utils/http/index.js @@ -1,6 +1,7 @@ -const latestVersion = require('./latest-version'); -const versions = require('./versions'); -const integrity = require('./integrity'); -const request = require('./request'); +import latestVersion from './latest-version.js'; +import versions from './versions.js'; +import integrity from './integrity.js'; +import request from './request.js'; -module.exports = { latestVersion, versions, integrity, request }; +export default { latestVersion, versions, integrity, request }; +export { latestVersion, versions, integrity, request }; diff --git a/utils/http/integrity.js b/utils/http/integrity.js index 349d742a..2754342a 100644 --- a/utils/http/integrity.js +++ b/utils/http/integrity.js @@ -1,7 +1,5 @@ -'use strict'; - -const { join } = require('path'); -const fetch = require('node-fetch'); +import { join } from 'path'; +import fetch from 'node-fetch'; /** * Fetches package integrity string by name and version from a given Eik asset server. @@ -14,7 +12,7 @@ const fetch = require('node-fetch'); * * @throws Error */ -module.exports = async (server, type, name, version) => { +export default async (server, type, name, version) => { const url = new URL(join(type, name, version), server); url.search = `?t=${Date.now()}`; diff --git a/utils/http/latest-version.js b/utils/http/latest-version.js index 159bfe9a..426a7004 100644 --- a/utils/http/latest-version.js +++ b/utils/http/latest-version.js @@ -1,7 +1,5 @@ -'use strict'; - -const { join } = require('path'); -const fetch = require('node-fetch'); +import { join } from 'path'; +import fetch from 'node-fetch'; /** * Fetches the latest version from an Eik server of a package by name, optionally restricting the lookup to a specified semver major version @@ -14,7 +12,7 @@ const fetch = require('node-fetch'); * * @throws Error */ -module.exports = async (server, type, name, major) => { +export default async (server, type, name, major) => { const url = new URL(`${join(type, name)}?t=${Date.now()}`, server); const res = await fetch(url); if (!res.ok) { diff --git a/utils/http/request.js b/utils/http/request.js index e61e1749..a28d7874 100644 --- a/utils/http/request.js +++ b/utils/http/request.js @@ -1,8 +1,6 @@ -'use strict'; - -const fetch = require('node-fetch'); -const FormData = require('form-data'); -const { createReadStream } = require('fs'); +import fetch from 'node-fetch'; +import FormData from 'form-data'; +import { createReadStream } from 'fs'; /** * HTTP Utility for making requests against an Eik server @@ -65,4 +63,4 @@ async function request(options) { } } -module.exports = request; +export default request; diff --git a/utils/http/versions.js b/utils/http/versions.js index 03c4dd97..8fd79c5e 100644 --- a/utils/http/versions.js +++ b/utils/http/versions.js @@ -1,7 +1,5 @@ -'use strict'; - -const { join } = require('path'); -const fetch = require('node-fetch'); +import { join } from 'path'; +import fetch from 'node-fetch'; /** * Fetches package versions by name from a given Eik asset server. @@ -13,7 +11,7 @@ const fetch = require('node-fetch'); * * @throws Error */ -module.exports = async (server, type, name) => { +export default async (server, type, name) => { const pkg = join(type, name); const url = new URL(pkg, server); url.search = `?t=${Date.now()}`; diff --git a/utils/index.js b/utils/index.js index 26d39f7b..17703d14 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,11 +1,9 @@ -'use strict'; +import { helpers } from '@eik/common'; +import logger from './logger.js'; +import getCWD from './get-cwd.js'; +import typeSlug from './type-slug.js'; +import typeTitle from './type-title.js'; -const { - helpers: { getDefaults, files }, -} = require('@eik/common'); -const logger = require('./logger'); -const getCWD = require('./get-cwd'); -const typeSlug = require('./type-slug'); -const typeTitle = require('./type-title'); +const { getDefaults, files } = helpers; -module.exports = { logger, getDefaults, getCWD, files, typeSlug, typeTitle }; +export { logger, getDefaults, getCWD, files, typeSlug, typeTitle }; diff --git a/utils/json/index.js b/utils/json/index.js index 5bedead7..2e7f62bd 100644 --- a/utils/json/index.js +++ b/utils/json/index.js @@ -1,5 +1,5 @@ -const read = require('./read'); -const write = require('./write'); -const writeEik = require('./write-eik'); +import read from './read.js'; +import write from './write.js'; +import writeEik from './write-eik.js'; -module.exports = { read, write, writeEik }; +export default { read, write, writeEik }; diff --git a/utils/json/read.js b/utils/json/read.js index 0a092338..9bc1522a 100644 --- a/utils/json/read.js +++ b/utils/json/read.js @@ -1,8 +1,6 @@ -'use strict'; - -const assert = require('assert'); -const fs = require('fs').promises; -const { join, isAbsolute } = require('path'); +import assert from 'assert'; +import fs from 'node:fs/promises'; +import { join, isAbsolute } from 'path'; /** * Reads a file at a given location, assumes the contents to be JSON and then deserializes into a JavaScript object @@ -18,7 +16,7 @@ const { join, isAbsolute } = require('path'); * @example json.read({ filename: './relative/path/to/file.json' }); * @example json.read({ filename: './relative/path/to/file.json', cwd: '/path/to/cwd }); */ -module.exports = async (location) => { +export default async (location) => { if (typeof location !== 'string') { assert( location.filename, diff --git a/utils/json/write-eik.js b/utils/json/write-eik.js index cf602247..1fcd1b9c 100644 --- a/utils/json/write-eik.js +++ b/utils/json/write-eik.js @@ -1,7 +1,5 @@ -'use strict'; - -const fs = require('fs').promises; -const { join } = require('path'); +import fs from 'node:fs/promises'; +import { join } from 'path'; /** * Reads, updates and then writes data to given eik.json file (defaults to file in current directory) @@ -15,7 +13,7 @@ const { join } = require('path'); * @example json.writeEik({ key: 'value' }, { cwd: '/path/to/cwd' }); * @example json.writeEik({ key: 'value' }, { cwd: '/path/to/cwd', filename: 'eik.json' }); */ -module.exports = async (data = {}, options) => { +export default async (data = {}, options) => { const { cwd = process.cwd(), filename = 'eik.json' } = options; const eikpath = join(cwd, filename); const eik = await fs.readFile(eikpath); diff --git a/utils/json/write.js b/utils/json/write.js index ce1977dc..d7ea886c 100644 --- a/utils/json/write.js +++ b/utils/json/write.js @@ -1,8 +1,6 @@ -'use strict'; - -const assert = require('assert'); -const fs = require('fs').promises; -const { join, isAbsolute, dirname } = require('path'); +import assert from 'assert'; +import fs from 'node:fs/promises'; +import { join, isAbsolute, dirname } from 'path'; /** * Utility function that can be used to write a JavaScript object to a file at a given location. @@ -21,7 +19,7 @@ const { join, isAbsolute, dirname } = require('path'); * * @throws Error */ -module.exports = async (meta = {}, location) => { +export default async (meta = {}, location) => { if (typeof location !== 'string') { assert( location.filename, diff --git a/utils/logger.js b/utils/logger.js index a5cb389d..48060402 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -1,5 +1,3 @@ -'use strict'; - /** * Creates a logger object that wraps an instance of the "ora" module in order to provide consistent command line logging that includes a spinner * @@ -54,4 +52,4 @@ const logger = (spinner, debug = false) => ({ }, }); -module.exports = logger; +export default logger; diff --git a/utils/type-slug.js b/utils/type-slug.js index e0266111..6d5e72cc 100644 --- a/utils/type-slug.js +++ b/utils/type-slug.js @@ -1,4 +1,4 @@ -module.exports = (type) => { +export default (type) => { if (type === 'package') return 'pkg'; return type; }; diff --git a/utils/type-title.js b/utils/type-title.js index ffe0973b..fa39162c 100644 --- a/utils/type-title.js +++ b/utils/type-title.js @@ -1,4 +1,4 @@ -module.exports = (type) => { +export default (type) => { if (type === 'package') return 'PACKAGE'; if (type === 'npm') return 'NPM'; return 'MAP'; From 36830b1d9e4588e0c6b4f06caeef867760c70eb2 Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Tue, 30 Jul 2024 15:49:06 +1200 Subject: [PATCH 2/5] chore: update and fix linting --- .eslintrc | 18 +++++++++++------- classes/alias.js | 2 +- classes/integrity.js | 2 +- classes/login.js | 2 +- classes/meta.js | 2 +- classes/ping.js | 2 +- classes/publish/map.js | 2 +- classes/publish/package/index.js | 2 +- .../package/tasks/check-bundle-sizes.js | 2 +- .../tasks/check-if-already-published.js | 2 +- classes/publish/package/tasks/cleanup.js | 2 +- .../package/tasks/create-temp-directory.js | 2 +- .../publish/package/tasks/create-zip-file.js | 2 +- classes/publish/package/tasks/dry-run.js | 2 +- classes/publish/package/tasks/save-metafile.js | 2 +- classes/publish/package/tasks/task.js | 2 +- .../publish/package/tasks/validate-input.js | 2 +- classes/version.js | 2 +- commands/index.js | 14 +++++++++++++- commands/init.js | 2 +- index.js | 6 ++++-- package.json | 3 ++- test/fixtures/client-with-bare-imports.js | 9 ++------- utils/get-cwd.js | 2 +- 24 files changed, 51 insertions(+), 37 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4df79209..d76d0460 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,11 +1,15 @@ { - "extends": ["airbnb-base", "prettier"], + "extends": ["eslint:recommended", "plugin:prettier/recommended"], "plugins": ["prettier"], - "rules": { - "strict": [0, "global"], - "class-methods-use-this": [0], - "no-restricted-syntax": [0], - "default-param-last": [0], - "import/extensions": [0] + "env": { "es6": true, "node": true }, + "parser": "@babel/eslint-parser", + "ignorePatterns": [ + "build", + "public" + ], + "parserOptions": { + "requireConfigFile": false, + "ecmaVersion": 2020, + "sourceType": "module" } } diff --git a/classes/alias.js b/classes/alias.js index 335d2f28..88a1f2fc 100644 --- a/classes/alias.js +++ b/classes/alias.js @@ -119,4 +119,4 @@ export default class Alias { } } } -}; +} diff --git a/classes/integrity.js b/classes/integrity.js index 26de1fbf..e3c2636b 100644 --- a/classes/integrity.js +++ b/classes/integrity.js @@ -92,4 +92,4 @@ export default class Integrity { ); } } -}; +} diff --git a/classes/login.js b/classes/login.js index 78061cc5..fc945307 100644 --- a/classes/login.js +++ b/classes/login.js @@ -48,4 +48,4 @@ export default class Login { } } } -}; +} diff --git a/classes/meta.js b/classes/meta.js index f009aa87..0644ea1e 100644 --- a/classes/meta.js +++ b/classes/meta.js @@ -87,4 +87,4 @@ export default class Meta { return false; } } -}; +} diff --git a/classes/ping.js b/classes/ping.js index f2472027..b0d94534 100644 --- a/classes/ping.js +++ b/classes/ping.js @@ -46,4 +46,4 @@ export default class Ping { } } } -}; +} diff --git a/classes/publish/map.js b/classes/publish/map.js index 14eaafb9..f20f1d86 100644 --- a/classes/publish/map.js +++ b/classes/publish/map.js @@ -91,4 +91,4 @@ export default class PublishMap { } } } -}; +} diff --git a/classes/publish/package/index.js b/classes/publish/package/index.js index e2076af9..f907c076 100644 --- a/classes/publish/package/index.js +++ b/classes/publish/package/index.js @@ -129,4 +129,4 @@ export default class Publish { ...response, }; } -}; +} diff --git a/classes/publish/package/tasks/check-bundle-sizes.js b/classes/publish/package/tasks/check-bundle-sizes.js index 9118fb3d..29a22133 100644 --- a/classes/publish/package/tasks/check-bundle-sizes.js +++ b/classes/publish/package/tasks/check-bundle-sizes.js @@ -21,4 +21,4 @@ export default class CheckBundleSizes extends Task { throw new Error(`Failed to check bundle sizes: ${err.message}`); } } -}; +} diff --git a/classes/publish/package/tasks/check-if-already-published.js b/classes/publish/package/tasks/check-if-already-published.js index 92138149..08cf1390 100644 --- a/classes/publish/package/tasks/check-if-already-published.js +++ b/classes/publish/package/tasks/check-if-already-published.js @@ -80,4 +80,4 @@ export default class CheckIfAlreadyPublished extends Task { return localHash; } -}; +} diff --git a/classes/publish/package/tasks/cleanup.js b/classes/publish/package/tasks/cleanup.js index 4366b39a..4f478eeb 100644 --- a/classes/publish/package/tasks/cleanup.js +++ b/classes/publish/package/tasks/cleanup.js @@ -14,4 +14,4 @@ export default class Cleanup extends Task { .forEach((file) => rimraf.sync(join(path, file))); } } -}; +} diff --git a/classes/publish/package/tasks/create-temp-directory.js b/classes/publish/package/tasks/create-temp-directory.js index 69a4776a..e4827fbb 100644 --- a/classes/publish/package/tasks/create-temp-directory.js +++ b/classes/publish/package/tasks/create-temp-directory.js @@ -23,4 +23,4 @@ export default class CreateTempDir extends Task { throw new IOError('Unable to create temp dir', err); } } -}; +} diff --git a/classes/publish/package/tasks/create-zip-file.js b/classes/publish/package/tasks/create-zip-file.js index 8d2ce851..30c7bdc6 100644 --- a/classes/publish/package/tasks/create-zip-file.js +++ b/classes/publish/package/tasks/create-zip-file.js @@ -77,4 +77,4 @@ export default class CreateZipFile extends Task { throw new Error(`Unable to create zip file: ${err.message}`); } } -}; +} diff --git a/classes/publish/package/tasks/dry-run.js b/classes/publish/package/tasks/dry-run.js index 987a234b..eb34adba 100644 --- a/classes/publish/package/tasks/dry-run.js +++ b/classes/publish/package/tasks/dry-run.js @@ -20,4 +20,4 @@ export default class DryRun extends Task { return files; } -}; +} diff --git a/classes/publish/package/tasks/save-metafile.js b/classes/publish/package/tasks/save-metafile.js index 3159e015..6a7db422 100644 --- a/classes/publish/package/tasks/save-metafile.js +++ b/classes/publish/package/tasks/save-metafile.js @@ -17,4 +17,4 @@ export default class SaveMetaFile extends Task { ); } } -}; +} diff --git a/classes/publish/package/tasks/task.js b/classes/publish/package/tasks/task.js index daedc60a..904bcfa3 100644 --- a/classes/publish/package/tasks/task.js +++ b/classes/publish/package/tasks/task.js @@ -7,4 +7,4 @@ export default class Task { this.path = options.path; this.config = options.config; } -}; +} diff --git a/classes/publish/package/tasks/validate-input.js b/classes/publish/package/tasks/validate-input.js index 7ed4331a..71f635db 100644 --- a/classes/publish/package/tasks/validate-input.js +++ b/classes/publish/package/tasks/validate-input.js @@ -31,4 +31,4 @@ export default class ValidateInput extends Task { throw new ValidationError('Parameter "dryRun" is not valid'); } } -}; +} diff --git a/classes/version.js b/classes/version.js index a16022f8..047de4e0 100644 --- a/classes/version.js +++ b/classes/version.js @@ -154,4 +154,4 @@ export default class Version { log.debug(` ==> ${newVersion}`); return newVersion; } -}; +} diff --git a/commands/index.js b/commands/index.js index 8e403262..8e06465a 100644 --- a/commands/index.js +++ b/commands/index.js @@ -10,4 +10,16 @@ import * as ping from './ping.js'; import * as publish from './publish.js'; import * as version from './version.js'; -export const commands = [ init, integrity, login, mapAlias, map, meta, npmAlias, packageAlias, ping, publish, version ]; \ No newline at end of file +export const commands = [ + init, + integrity, + login, + mapAlias, + map, + meta, + npmAlias, + packageAlias, + ping, + publish, + version, +]; diff --git a/commands/init.js b/commands/init.js index 20b8eeb9..79a4b14a 100644 --- a/commands/init.js +++ b/commands/init.js @@ -102,4 +102,4 @@ const handler = async (argv) => { spinner.stopAndPersist(); }; -export { command, aliases, describe, builder, handler }; \ No newline at end of file +export { command, aliases, describe, builder, handler }; diff --git a/index.js b/index.js index 9896f41b..b8f1e5ab 100755 --- a/index.js +++ b/index.js @@ -12,7 +12,9 @@ import { commands } from './commands/index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); -const { version } = JSON.parse(readFileSync(join(__dirname, './package.json'), { encoding: 'utf-8' })); +const { version } = JSON.parse( + readFileSync(join(__dirname, './package.json'), { encoding: 'utf-8' }), +); const greeting = chalk.white.bold(`Eik CLI (v${version})`); const boxenOptions = { @@ -45,4 +47,4 @@ yargs(hideBin(process.argv)) .wrap(150) .version(false) .help() - .parse(); + .parse(); diff --git a/package.json b/package.json index 0375306c..2feddecf 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.0.39", "description": "CLI tool for publishing assets to an Eik server", "main": "./classes/index.js", - "type": "module", + "type": "module", "bin": { "eik": "index.js" }, @@ -55,6 +55,7 @@ "yargs-parser": "21.1.1" }, "devDependencies": { + "@babel/eslint-parser": "^7.25.1", "@eik/core": "1.2.28", "@eik/service": "1.2.98", "@semantic-release/changelog": "6.0.3", diff --git a/test/fixtures/client-with-bare-imports.js b/test/fixtures/client-with-bare-imports.js index aa817d5c..24d1a480 100644 --- a/test/fixtures/client-with-bare-imports.js +++ b/test/fixtures/client-with-bare-imports.js @@ -1,13 +1,8 @@ -/* eslint-disable import/no-unresolved */ - -'use strict'; - +/* eslint-disable no-undef */ +// @ts-nocheck import scrollIntoView from 'scroll-into-view-if-needed'; const main = () => { - // eslint-disable-next-line no-undef - const node = document.getElementById('hero'); - scrollIntoView(node, { scrollMode: 'if-needed', block: 'nearest', diff --git a/utils/get-cwd.js b/utils/get-cwd.js index 1cc0146a..7f6a14cf 100644 --- a/utils/get-cwd.js +++ b/utils/get-cwd.js @@ -11,4 +11,4 @@ const av = yargs(process.argv.slice(2)); */ export default function getCWD() { return av.cwd || av.c || process.cwd(); -}; +} From 68aec2b176eb17cc5f2de389477de0f590b29d23 Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Tue, 30 Jul 2024 15:53:28 +1200 Subject: [PATCH 3/5] chore: file formatting --- .eslintrc | 5 +---- test/integration/init.test.mjs | 10 ++++++--- test/integration/integrity.test.mjs | 2 +- test/integration/meta.test.mjs | 2 +- test/utils.test.mjs | 34 +++++++++++------------------ 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/.eslintrc b/.eslintrc index d76d0460..56e20755 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,10 +3,7 @@ "plugins": ["prettier"], "env": { "es6": true, "node": true }, "parser": "@babel/eslint-parser", - "ignorePatterns": [ - "build", - "public" - ], + "ignorePatterns": ["build", "public"], "parserOptions": { "requireConfigFile": false, "ecmaVersion": 2020, diff --git a/test/integration/init.test.mjs b/test/integration/init.test.mjs index a93b38c5..da4b8435 100644 --- a/test/integration/init.test.mjs +++ b/test/integration/init.test.mjs @@ -12,7 +12,7 @@ const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - execCallback(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); @@ -23,7 +23,9 @@ test('Initializing a new eik.json file', async (t) => { const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const publishCmd = `${eik} init --cwd ${folder}`; const res = await exec(publishCmd); - const assets = JSON.parse(readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' })); + const assets = JSON.parse( + readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' }), + ); t.equal(assets.name, '', 'eik.json "name" field should be empty'); t.equal( assets.version, @@ -45,7 +47,9 @@ test('Initializing a new eik.json file passing custom values', async (t) => { --server http://localhost:4001`; await exec(publishCmd.split('\n').join(' ')); - const assets = JSON.parse(readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' })); + const assets = JSON.parse( + readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' }), + ); t.equal( assets.name, diff --git a/test/integration/integrity.test.mjs b/test/integration/integrity.test.mjs index dd1fc1e1..21be6153 100644 --- a/test/integration/integrity.test.mjs +++ b/test/integration/integrity.test.mjs @@ -15,7 +15,7 @@ const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - execCallback(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/meta.test.mjs b/test/integration/meta.test.mjs index aa1a2f4e..125147fe 100644 --- a/test/integration/meta.test.mjs +++ b/test/integration/meta.test.mjs @@ -15,7 +15,7 @@ const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - execCallback(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/utils.test.mjs b/test/utils.test.mjs index 1fdbfe80..c5cc3601 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -238,9 +238,7 @@ test('fetch remote hash for a given version', async (t) => { }); test('write JSON file - object - file relative to cwd', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await j.write( { version: '1.0.0', integrity: [] }, { cwd, filename: '.eikrc' }, @@ -253,11 +251,11 @@ test('write JSON file - object - file relative to cwd', async (t) => { }); test('write JSON file - object - file absolute path', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await j.write({ prop: 'val' }, { filename: join(cwd, 'test.json') }); - const eikrc = await fs.readFile(join(cwd, 'test.json'), { encoding: 'utf8' }); + const eikrc = await fs.readFile(join(cwd, 'test.json'), { + encoding: 'utf8', + }); const { prop } = JSON.parse(eikrc); t.equal(prop, 'val', 'Prop should equal val'); @@ -267,7 +265,7 @@ test('write JSON file - string - file relative path', async (t) => { await j.write({ prop: 'val' }, './test-using-relative.json'); const eikrc = await fs.readFile( join(__dirname, '../test-using-relative.json'), - { encoding: 'utf8' }, + { encoding: 'utf8' }, ); const { prop } = JSON.parse(eikrc); await fs.unlink(join(__dirname, '../test-using-relative.json')); @@ -276,20 +274,18 @@ test('write JSON file - string - file relative path', async (t) => { }); test('write JSON file - string - file absolute path', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await j.write({ prop: 'val' }, join(cwd, 'test3.json')); - const eikrc = await fs.readFile(join(cwd, 'test3.json'), { encoding: 'utf8' }); + const eikrc = await fs.readFile(join(cwd, 'test3.json'), { + encoding: 'utf8', + }); const { prop } = JSON.parse(eikrc); t.equal(prop, 'val', 'Prop should equal val'); }); test('read JSON file - object - file relative path', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await fs.writeFile(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); const json = await j.read({ cwd, filename: './test3.json' }); @@ -297,9 +293,7 @@ test('read JSON file - object - file relative path', async (t) => { }); test('read JSON file - object - file absolute path', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await fs.writeFile(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); const json = await j.read({ filename: join(cwd, './test3.json') }); @@ -317,9 +311,7 @@ test('read JSON file - string - file relative path', async (t) => { }); test('read JSON file - string - file absolute path', async (t) => { - const cwd = await fs.mkdtemp( - join(os.tmpdir(), basename(__filename)), - ); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); await fs.writeFile( join(cwd, './test-read-json-2.json'), JSON.stringify({ key: 'val' }), From ce3774461cdd4f26b3bfd5c07b8bafe853504dee Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Wed, 31 Jul 2024 14:12:25 +1200 Subject: [PATCH 4/5] chore: remove node-fetch in favour of global fetch --- classes/integrity.js | 1 - classes/meta.js | 1 - classes/ping.js | 1 - commands/map.js | 1 - commands/publish.js | 1 - package.json | 1 - test/integration/map.test.mjs | 1 - utils/http/integrity.js | 1 - utils/http/latest-version.js | 1 - utils/http/request.js | 27 ++++++++++++--------------- utils/http/versions.js | 1 - 11 files changed, 12 insertions(+), 25 deletions(-) diff --git a/classes/integrity.js b/classes/integrity.js index e3c2636b..2e085c76 100644 --- a/classes/integrity.js +++ b/classes/integrity.js @@ -3,7 +3,6 @@ import abslog from 'abslog'; import { join } from 'path'; import eik from '@eik/common'; -import fetch from 'node-fetch'; import { typeSlug } from '../utils/index.js'; const { schemas, ValidationError } = eik; diff --git a/classes/meta.js b/classes/meta.js index 0644ea1e..8d868aad 100644 --- a/classes/meta.js +++ b/classes/meta.js @@ -3,7 +3,6 @@ import abslog from 'abslog'; import { join } from 'path'; import { schemas } from '@eik/common'; -import fetch from 'node-fetch'; const types = ['pkg', 'map', 'npm']; diff --git a/classes/ping.js b/classes/ping.js index b0d94534..a326146b 100644 --- a/classes/ping.js +++ b/classes/ping.js @@ -1,4 +1,3 @@ -import fetch from 'node-fetch'; import abslog from 'abslog'; import { schemas } from '@eik/common'; diff --git a/commands/map.js b/commands/map.js index 6608ae9a..b55e4f30 100644 --- a/commands/map.js +++ b/commands/map.js @@ -1,7 +1,6 @@ 'use strict'; import { join } from 'path'; -import fetch from 'node-fetch'; import ora from 'ora'; import PublishMap from '../classes/publish/map.js'; import { logger, getDefaults, getCWD } from '../utils/index.js'; diff --git a/commands/publish.js b/commands/publish.js index b0e755d6..77c0832d 100644 --- a/commands/publish.js +++ b/commands/publish.js @@ -1,5 +1,4 @@ import { join } from 'path'; -import fetch from 'node-fetch'; import ora from 'ora'; import chalk from 'chalk'; import { helpers } from '@eik/common'; diff --git a/package.json b/package.json index 2feddecf..2fb83a7d 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "form-data": "4.0.0", "gzip-size": "6.0.0", "make-dir": "3.1.0", - "node-fetch": "2.7.0", "ora": "5.4.1", "rimraf": "3.0.2", "semver": "7.6.3", diff --git a/test/integration/map.test.mjs b/test/integration/map.test.mjs index 64d6c882..32d4c467 100644 --- a/test/integration/map.test.mjs +++ b/test/integration/map.test.mjs @@ -4,7 +4,6 @@ import os from 'os'; import { exec as execCallback } from 'child_process'; import { join, basename } from 'path'; import { test, beforeEach, afterEach } from 'tap'; -import fetch from 'node-fetch'; import EikService from '@eik/service'; import { sink } from '@eik/core'; import cli from '../../classes/index.js'; diff --git a/utils/http/integrity.js b/utils/http/integrity.js index 2754342a..53c35a1d 100644 --- a/utils/http/integrity.js +++ b/utils/http/integrity.js @@ -1,5 +1,4 @@ import { join } from 'path'; -import fetch from 'node-fetch'; /** * Fetches package integrity string by name and version from a given Eik asset server. diff --git a/utils/http/latest-version.js b/utils/http/latest-version.js index 426a7004..35282a47 100644 --- a/utils/http/latest-version.js +++ b/utils/http/latest-version.js @@ -1,5 +1,4 @@ import { join } from 'path'; -import fetch from 'node-fetch'; /** * Fetches the latest version from an Eik server of a package by name, optionally restricting the lookup to a specified semver major version diff --git a/utils/http/request.js b/utils/http/request.js index a28d7874..0e13e691 100644 --- a/utils/http/request.js +++ b/utils/http/request.js @@ -1,6 +1,4 @@ -import fetch from 'node-fetch'; -import FormData from 'form-data'; -import { createReadStream } from 'fs'; +import { readFile } from 'node:fs/promises'; /** * HTTP Utility for making requests against an Eik server @@ -13,45 +11,44 @@ import { createReadStream } from 'fs'; */ async function request(options) { const { method = 'POST', host, pathname, data, file, map, token } = options; - const form = new FormData(); - const headers = {}; + const body = new FormData(); + const headers = new Headers(); if (data) { for (const [key, value] of Object.entries(data)) { - form.append(key, value); + body.set(key, value); } } if (file) { - form.append('package', createReadStream(file)); + const fileData = await readFile(file); + body.set('package', new Blob([fileData])); } if (map) { - form.append('map', createReadStream(map)); + const mapData = await readFile(map); + body.set('map', new Blob([mapData])); } if (token) { - headers.Authorization = `Bearer ${token}`; + headers.set('Authorization', `Bearer ${token}`); } try { const url = new URL(pathname, host); url.search = `?t=${Date.now()}`; - const res = await fetch(url, { - method, - body: form, - headers: { ...headers, ...form.getHeaders() }, - }); + const res = await fetch(url, { method, body, headers }); if (!res.ok) { const err = new Error( `Server responded with a non 200 ok status code. Response: ${res.status}`, ); + // @ts-ignore err.statusCode = res.status; throw err; } - if (res.headers.get('content-type').includes('application/json')) { + if (res?.headers?.get('content-type')?.includes('application/json')) { return { message: await res.json(), status: res.status }; } return { message: await res.text(), status: res.status }; diff --git a/utils/http/versions.js b/utils/http/versions.js index 8fd79c5e..3468954e 100644 --- a/utils/http/versions.js +++ b/utils/http/versions.js @@ -1,5 +1,4 @@ import { join } from 'path'; -import fetch from 'node-fetch'; /** * Fetches package versions by name from a given Eik asset server. From 94924bc1373c64eedda32eca5008583bb722fb3d Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Wed, 31 Jul 2024 14:17:07 +1200 Subject: [PATCH 5/5] chore: remove 'use strict' statements from codebase --- classes/index.js | 2 -- classes/login.js | 2 -- commands/map-alias.js | 2 -- commands/map.js | 2 -- test/fixtures/client.js | 2 -- test/utils.test.mjs | 2 +- 6 files changed, 1 insertion(+), 11 deletions(-) diff --git a/classes/index.js b/classes/index.js index e55bbe7f..c38bff4d 100644 --- a/classes/index.js +++ b/classes/index.js @@ -1,5 +1,3 @@ -'use strict'; - import Ping from './ping.js'; import Alias from './alias.js'; import Meta from './meta.js'; diff --git a/classes/login.js b/classes/login.js index fc945307..bce05250 100644 --- a/classes/login.js +++ b/classes/login.js @@ -1,5 +1,3 @@ -'use strict'; - import abslog from 'abslog'; import eik from '@eik/common'; import { request } from '../utils/http/index.js'; diff --git a/commands/map-alias.js b/commands/map-alias.js index 644f3aa8..69a19657 100644 --- a/commands/map-alias.js +++ b/commands/map-alias.js @@ -1,5 +1,3 @@ -'use strict'; - import ora from 'ora'; import Alias from '../classes/alias.js'; import { logger, getDefaults, getCWD } from '../utils/index.js'; diff --git a/commands/map.js b/commands/map.js index b55e4f30..f6d79a91 100644 --- a/commands/map.js +++ b/commands/map.js @@ -1,5 +1,3 @@ -'use strict'; - import { join } from 'path'; import ora from 'ora'; import PublishMap from '../classes/publish/map.js'; diff --git a/test/fixtures/client.js b/test/fixtures/client.js index 6ae893a3..201c57ea 100644 --- a/test/fixtures/client.js +++ b/test/fixtures/client.js @@ -1,5 +1,3 @@ -'use strict'; - const main = () => { const thing = 'this is a test fixture'; return thing; diff --git a/test/utils.test.mjs b/test/utils.test.mjs index c5cc3601..33e51cd9 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -17,7 +17,7 @@ test('calculate file hash', async (t) => { const hash = await h.file(join(__dirname, 'fixtures', 'client.js')); t.equal( hash, - 'sha512-7y37q0qk5mDqzHrGvJAR9J8kPX+orJhuO+KrCTKw11ZKRI/5udUuKt2Zb/thH5H39OQYrvnHTLbZS9ShG/lGCg==', + 'sha512-AzZUEv6TzJOlb7MOJSkAtFDihZnjCqOjgWqQmRlQj+/9CsWGQKGJzOT1CPp2R9PQlA0dd3B1+xrrgLsDX9OFtQ==', 'returned hash should match', ); });