Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ESM and fix Node 18.13+ compatibility #259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "ipfs"
}
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x]
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
6 changes: 4 additions & 2 deletions md/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
We've been trying to make `ipfs-deploy` more friendly as a library. However, we
have no documentation yet.

This library is only available in ESM format.

```javascript
const { deploy, dnsLinkers, dnsLinkersMap, pinners, pinnersMap} = require('ipfs-deploy')
import { deploy, dnsLinkers, dnsLinkersMap, pinners, pinnersMap } from 'ipfs-deploy'

// Get available dnsLinkers identifiers
dnsLinkersMap.keys()
Expand All @@ -21,7 +23,7 @@ dnsLinkersMap.get('cloudflare')
How we currently use the deploy function:

```javascript
const { deploy } = require('ipfs-deploy')
import { deploy } from 'ipfs-deploy'

const cid = await deploy({
dir: argv.path,
Expand Down
13 changes: 3 additions & 10 deletions md/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ with the name of the pinning service. Let's say it's called `PinningService`:
create a file at `src/pinners/pinning-service.js` with the following contents:

```javascript
'use strict'

class PinningService {
export default class PinningService {
constructor () {
// TODO
}
Expand All @@ -52,8 +50,6 @@ class PinningService {
return 'pinning-service'
}
}

module.exports = PinningService
```

Where `options` in the constructor are the required parameters to connect to
Expand All @@ -69,7 +65,7 @@ the name of the DNS provider. Let's say it's called `DNS Provider`: create a
file at `src/dnslinkers/dns-provider.js` with the following contents:

```javascript
class DNSProvider {
export default class DNSProvider {
constructor () {
// TODO
}
Expand All @@ -94,14 +90,11 @@ class DNSProvider {
return 'dns-provider'
}
}

module.exports = DNSProvider

```

Where `options` in the constructor are the required parameters to connect to
such provider. You will also need to add the provider to the list `dnsLinkers`
at `src/dnslinkers/index.js`, as well as adding the required options in
`src/cli.js`.

Also, do not forget to add documentation!
Also, do not forget to add documentation!
24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
"src",
"dist"
],
"type": "module",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.js"
}
},
"typesVersions": {
"*": {
"src/*": [
Expand All @@ -44,8 +51,8 @@
"url": "https://github.com/ipfs-shipyard/ipfs-deploy.git"
},
"scripts": {
"lint": "aegir lint && aegir ts -p check",
"test": "aegir test",
"lint": "aegir lint",
"test": "aegir test -t node -- --loader=esmock",
"types": "aegir ts -p types",
"release": "aegir release -t node",
"release-minor": "aegir release --type minor",
Expand All @@ -67,8 +74,8 @@
],
"dependencies": {
"@aws-sdk/client-route-53": "^3.53.0",
"@filebase/client": "^0.0.2",
"axios": "^0.26.0",
"@filebase/client": "^0.0.4",
"axios": "^1.2.6",
"byte-size": "^8.1.0",
"chalk": "^4.1.1",
"clipboardy": "^2.3.0",
Expand All @@ -78,7 +85,7 @@
"dreamhost": "^1.0.5",
"files-from-path": "^0.2.6",
"form-data": "^4.0.0",
"ipfs-http-client": "^50.1.0",
"ipfs-http-client": "^60.0.0",
"it-all": "^1.0.6",
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
Expand All @@ -91,12 +98,11 @@
"devDependencies": {
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.isstring": "^4.0.1",
"@types/proxyquire": "^1.3.28",
"aegir": "^33.2.0",
"proxyquire": "^2.1.3"
"aegir": "^38.1.0",
"esmock": "^2.1.0"
},
"engines": {
"node": ">=14.0.0"
"node": ">=16.0.0"
},
"contributors": [
"Henrique Dias <[email protected]>",
Expand Down
17 changes: 10 additions & 7 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
// @ts-nocheck

/* eslint-disable no-console */
'use strict'

const updateNotifier = require('update-notifier')
const yargs = require('yargs')
const dotenv = require('dotenv')
import { readFile } from 'node:fs/promises'
import updateNotifier from 'update-notifier'
import yargs from 'yargs/yargs'
import { hideBin } from 'yargs/helpers'
import dotenv from 'dotenv'

const { deploy, dnsLinkersMap, pinnersMap } = require('.')
const pkg = require('../package.json')
import { deploy, dnsLinkersMap, pinnersMap } from './index.js'

const pkgUrl = new URL('../package.json', import.meta.url)
const pkg = JSON.parse(await readFile(pkgUrl, 'utf8'))

const dnsProviders = [...dnsLinkersMap.keys()]
const pinningServices = [...pinnersMap.keys()]

updateNotifier({ pkg, updateCheckInterval: 0 }).notify()
dotenv.config()

const argv = yargs
const argv = yargs(hideBin(process.argv))
.scriptName('ipfs-deploy')
.usage(
'$0 [path] [options]',
Expand Down
49 changes: 23 additions & 26 deletions src/deploy.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict'
import { fileURLToPath } from 'node:url'
import chalk from 'chalk'
import path from 'node:path'
import fs from 'node:fs'

const chalk = require('chalk')
const path = require('path')
const fs = require('fs')

const { dnsLinkersMap } = require('./dnslinkers')
const { pinnersMap } = require('./pinners')
const { guessPath, getReadableSize, terminalUrl } = require('./utils')
import { dnsLinkersMap } from './dnslinkers/index.js'
import { pinnersMap } from './pinners/index.js'
import { guessPath, getReadableSize, terminalUrl } from './utils.js'

/**
* @typedef {import('./dnslinkers/types').DNSLinker} DNSLinker
* @typedef {import('./pinners/types').PinningService} PinningService
* @typedef {import('./pinners/types').PinDirOptions} PinDirOptions
* @typedef {import('./types').DeployOptions} DeployOptions
* @typedef {import('./types').Logger} Logger
* @typedef {import('./dnslinkers/types.js').DNSLinker} DNSLinker
* @typedef {import('./pinners/types.js').PinningService} PinningService
* @typedef {import('./pinners/types.js').PinDirOptions} PinDirOptions
* @typedef {import('./types.js').DeployOptions} DeployOptions
* @typedef {import('./types.js').Logger} Logger
*/

/**
Expand Down Expand Up @@ -96,7 +95,7 @@ async function dnsLink (services, cid, logger) {
* @param {string[]} gatewayUrls
* @param {Logger} logger
*/
function copyToClipboard (hostnames, gatewayUrls, logger) {
async function copyToClipboard (hostnames, gatewayUrls, logger) {
let toCopy
if (hostnames.length > 0) {
toCopy = hostnames[hostnames.length - 1]
Expand All @@ -111,11 +110,11 @@ function copyToClipboard (hostnames, gatewayUrls, logger) {
}

try {
const clipboardy = require('clipboardy')
clipboardy.writeSync(toCopy)
const { default: clipboardy } = await import('clipboardy')
await clipboardy.write(toCopy)
logger.info('📋 Copied HTTP gateway URL to clipboard:')
logger.info(terminalUrl(toCopy, toCopy))
} catch (e) {
} catch (/** @type {any} */ e) {
logger.info('⚠️ Could not copy URL to clipboard.')
logger.error(e.stack || e.toString())
}
Expand All @@ -130,15 +129,15 @@ function copyToClipboard (hostnames, gatewayUrls, logger) {
* @param {string[]} hostnames
* @param {Logger} logger
*/
function openUrlsBrowser (gatewayUrls, hostnames, logger) {
async function openUrlsBrowser (gatewayUrls, hostnames, logger) {
logger.info('🏄 Opening URLs on web browser...')

try {
const open = require('open')
const { default: open } = await import('open')
gatewayUrls.forEach(url => { open(url) })
hostnames.forEach(hostname => open(`https://${hostname}`))
logger.info('🏄 All URLs opened.')
} catch (e) {
} catch (/** @type {any} */ e) {
logger.info('⚠️ Could not open URLs on web browser.')
logger.error(e.stack || e.toString())
}
Expand Down Expand Up @@ -189,7 +188,7 @@ async function checkDirAndCid (dir, cid, logger) {
* @param {DeployOptions} options
* @returns {Promise<string>}
*/
async function deploy ({
export default async function deploy ({
dir,
cid,
tag,
Expand All @@ -211,7 +210,7 @@ async function deploy ({
dir = res.dir
cid = res.cid

tag = tag || __dirname
tag = tag || fileURLToPath(new URL('.', import.meta.url))

// In the case we only set pinning services and we're deploying a directory,
// then call those upload services.
Expand Down Expand Up @@ -270,15 +269,13 @@ async function deploy ({
const hostnames = await dnsLink(dnsProviders, cid, logger)

if (openUrls) {
openUrlsBrowser(gatewayUrls, hostnames, logger)
await openUrlsBrowser(gatewayUrls, hostnames, logger)
}

if (copyUrl) {
copyToClipboard(hostnames, gatewayUrls, logger)
await copyToClipboard(hostnames, gatewayUrls, logger)
}

logger.out(cid)
return cid
}

module.exports = deploy
14 changes: 5 additions & 9 deletions src/dnslinkers/cloudflare.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

// @ts-ignore
const dnslink = require('dnslink-cloudflare')
const isEmpty = require('lodash.isempty')
import dnslink from 'dnslink-cloudflare'
import isEmpty from 'lodash.isempty'

/**
* @typedef {import('./types').DNSRecord} DNSRecord
* @typedef {import('./types').CloudflareOptions} CloudflareOptions
* @typedef {import('./types.js').DNSRecord} DNSRecord
* @typedef {import('./types.js').CloudflareOptions} CloudflareOptions
*/

class Cloudflare {
export default class Cloudflare {
/**
* @param {CloudflareOptions} options
*/
Expand Down Expand Up @@ -64,5 +62,3 @@ class Cloudflare {
return 'cloudflare'
}
}

module.exports = Cloudflare
14 changes: 5 additions & 9 deletions src/dnslinkers/dnsimple.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

// @ts-ignore
const dnslink = require('dnslink-dnsimple')
const isEmpty = require('lodash.isempty')
import dnslink from 'dnslink-dnsimple'
import isEmpty from 'lodash.isempty'

/**
* @typedef {import('./types').DNSRecord} DNSRecord
* @typedef {import('./types').DNSimpleOptions} DNSimpleOptions
* @typedef {import('./types.js').DNSRecord} DNSRecord
* @typedef {import('./types.js').DNSimpleOptions} DNSimpleOptions
*/

class DNSimple {
export default class DNSimple {
/**
* @param {DNSimpleOptions} options
*/
Expand Down Expand Up @@ -63,5 +61,3 @@ class DNSimple {
return 'dnsimple'
}
}

module.exports = DNSimple
14 changes: 5 additions & 9 deletions src/dnslinkers/dreamhost.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

// @ts-ignore
const DreamHostClient = require('dreamhost')
const isEmpty = require('lodash.isempty')
import DreamHostClient from 'dreamhost'
import isEmpty from 'lodash.isempty'

/**
* @typedef {import('./types').DNSRecord} DNSRecord
* @typedef {import('./types').DreamHostOptions} DreamHostOptions
* @typedef {import('./types.js').DNSRecord} DNSRecord
* @typedef {import('./types.js').DreamHostOptions} DreamHostOptions
*/

class DreamHost {
export default class DreamHost {
/**
* @param {DreamHostOptions} options
*/
Expand Down Expand Up @@ -80,5 +78,3 @@ class DreamHost {
return 'dreamhost'
}
}

module.exports = DreamHost
19 changes: 6 additions & 13 deletions src/dnslinkers/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
'use strict'
import Cloudflare from './cloudflare.js'
import DNSimple from './dnsimple.js'
import DreamHost from './dreamhost.js'
import Route53 from './route53.js'

const Cloudflare = require('./cloudflare')
const DNSimple = require('./dnsimple')
const DreamHost = require('./dreamhost')
const Route53 = require('./route53')
export const dnsLinkers = [Cloudflare, DNSimple, DreamHost, Route53]

const dnsLinkers = [Cloudflare, DNSimple, DreamHost, Route53]

const dnsLinkersMap = dnsLinkers.reduce((map, dnsLinker) => {
export const dnsLinkersMap = dnsLinkers.reduce((map, dnsLinker) => {
map.set(dnsLinker.slug, dnsLinker)
return map
}, new Map())

module.exports = {
dnsLinkers,
dnsLinkersMap
}
Loading