build(node): migrate from CommonJS to ESM modules

This commit is contained in:
link2xt
2023-11-10 01:41:07 +00:00
parent 94dc65c1a2
commit b607f12b0e
20 changed files with 89 additions and 75 deletions

View File

@@ -1,9 +1,13 @@
import { join } from 'path' import { join } from 'path'
import * as url from 'url'
/** /**
* bindings are not typed yet. * bindings are not typed yet.
* if the available function names are required they can be found inside of `../src/module.c` * if the available function names are required they can be found inside of `../src/module.c`
*/ */
export const bindings: any = require('node-gyp-build')(join(__dirname, '../')) import build from 'node-gyp-build'
export const bindings: any = build(
join(url.fileURLToPath(new URL('.', import.meta.url)), '../')
)
export default bindings export default bindings

View File

@@ -1,11 +1,11 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
import rawDebug from 'debug' import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:chat') const debug = rawDebug('deltachat:node:chat')
import { C } from './constants' import { C } from './constants.js'
import { integerToHexColor } from './util' import { integerToHexColor } from './util.js'
import { ChatJSON } from './types' import { ChatJSON } from './types.js'
interface NativeChat {} interface NativeChat {}
/** /**

View File

@@ -1,9 +1,10 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
import { Lot } from './lot' import { Lot } from './lot.js'
import { Chat } from './chat' import { Chat } from './chat.js'
const debug = require('debug')('deltachat:node:chatlist') import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:chatlist')
interface NativeChatList {} interface NativeChatList {}
/** /**

View File

@@ -1,9 +1,10 @@
import { integerToHexColor } from './util' import { integerToHexColor } from './util.js'
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
const debug = require('debug')('deltachat:node:contact') import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:contact')
interface NativeContact {} interface NativeContact {}
/** /**

View File

@@ -1,17 +1,17 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
import { C, EventId2EventName } from './constants' import { C, EventId2EventName } from './constants.js'
import { Chat } from './chat' import { Chat } from './chat.js'
import { ChatList } from './chatlist' import { ChatList } from './chatlist.js'
import { Contact } from './contact' import { Contact } from './contact.js'
import { Message } from './message' import { Message } from './message.js'
import { Lot } from './lot' import { Lot } from './lot.js'
import { Locations } from './locations' import { Locations } from './locations.js'
import rawDebug from 'debug' import rawDebug from 'debug'
import { AccountManager } from './deltachat' import { AccountManager } from './deltachat.js'
import { join } from 'path' import { join } from 'path'
import { EventEmitter } from 'stream' import { EventEmitter } from 'events'
const debug = rawDebug('deltachat:node:index') const debug = rawDebug('deltachat:node:index')
const noop = function () {} const noop = function () {}

View File

@@ -1,13 +1,13 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
import { EventId2EventName } from './constants' import { EventId2EventName } from './constants.js'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import rawDebug from 'debug' import rawDebug from 'debug'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { join } from 'path' import { join } from 'path'
import { Context } from './context' import { Context } from './context.js'
const debug = rawDebug('deltachat:node:index') const debug = rawDebug('deltachat:node:index')
const noop = function () {} const noop = function () {}

View File

@@ -1,20 +1,20 @@
import { AccountManager } from './deltachat' import { AccountManager } from './deltachat.js'
export default AccountManager export default AccountManager
export { Context } from './context' export { Context } from './context.js'
export { Chat } from './chat' export { Chat } from './chat.js'
export { ChatList } from './chatlist' export { ChatList } from './chatlist.js'
export { C } from './constants' export { C } from './constants.js'
export { Contact } from './contact' export { Contact } from './contact.js'
export { AccountManager as DeltaChat } export { AccountManager as DeltaChat }
export { Locations } from './locations' export { Locations } from './locations.js'
export { Lot } from './lot' export { Lot } from './lot.js'
export { export {
Message, Message,
MessageState, MessageState,
MessageViewType, MessageViewType,
MessageDownloadState, MessageDownloadState,
} from './message' } from './message.js'
export * from './types' export * from './types.js'

View File

@@ -1,7 +1,8 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
const binding = require('../binding') import binding from './binding.js'
const debug = require('debug')('deltachat:node:locations') import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:locations')
interface NativeLocations {} interface NativeLocations {}
/** /**

View File

@@ -1,7 +1,8 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
const binding = require('../binding') import binding from './binding.js'
const debug = require('debug')('deltachat:node:lot') import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:lot')
interface NativeLot {} interface NativeLot {}
/** /**

View File

@@ -1,11 +1,12 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import binding from './binding' import binding from './binding.js'
import { C } from './constants' import { C } from './constants.js'
import { Lot } from './lot' import { Lot } from './lot.js'
import { Chat } from './chat' import { Chat } from './chat.js'
import { WebxdcInfo } from './context' import { WebxdcInfo } from './context.js'
const debug = require('debug')('deltachat:node:message') import rawDebug from 'debug'
const debug = rawDebug('deltachat:node:message')
export enum MessageDownloadState { export enum MessageDownloadState {
Available = C.DC_DOWNLOAD_AVAILABLE, Available = C.DC_DOWNLOAD_AVAILABLE,

1
node/lib/node-gyp-build.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
declare module 'node-gyp-build'

View File

@@ -1,4 +1,4 @@
import { C } from './constants' import { C } from './constants.js'
export type ChatTypes = export type ChatTypes =
| C.DC_CHAT_TYPE_GROUP | C.DC_CHAT_TYPE_GROUP

View File

@@ -1,4 +1,4 @@
const spawnSync = require('child_process').spawnSync import { spawnSync } from 'child_process'
const verbose = isVerbose() const verbose = isVerbose()
@@ -23,4 +23,4 @@ function isVerbose () {
return loglevel === 'verbose' || process.env.CI === 'true' return loglevel === 'verbose' || process.env.CI === 'true'
} }
module.exports = { spawn, log, isVerbose, verbose } export { spawn, log, isVerbose, verbose }

View File

@@ -1,14 +1,16 @@
#!/usr/bin/env node #!/usr/bin/env node
const fs = require('fs') import fs from 'fs'
const path = require('path') import path from 'path'
import * as url from 'url'
const data = [] const data = []
const header = path.resolve(__dirname, '../../deltachat-ffi/deltachat.h') const header = path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../../deltachat-ffi/deltachat.h')
console.log('Generating constants...') console.log('Generating constants...')
const header_data = fs.readFileSync(header, 'UTF-8') const header_data = fs.readFileSync(header, 'UTF-8')
const regex = /^#define\s+(\w+)\s+(\w+)/gm const regex = /^#define\s+(\w+)\s+(\w+)/gm
var match
while (null != (match = regex.exec(header_data))) { while (null != (match = regex.exec(header_data))) {
const key = match[1] const key = match[1]
const value = parseInt(match[2]) const value = parseInt(match[2])
@@ -17,8 +19,6 @@ while (null != (match = regex.exec(header_data))) {
} }
} }
delete header_data
const constants = data const constants = data
.filter( .filter(
({ key }) => key.toUpperCase()[0] === key[0] // check if define name is uppercase ({ key }) => key.toUpperCase()[0] === key[0] // check if define name is uppercase
@@ -49,17 +49,17 @@ const events = data
// backwards compat // backwards compat
fs.writeFileSync( fs.writeFileSync(
path.resolve(__dirname, '../constants.js'), path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../constants.js'),
`// Generated!\n\nmodule.exports = {\n${constants}\n}\n` `// Generated!\n\nmodule.exports = {\n${constants}\n}\n`
) )
// backwards compat // backwards compat
fs.writeFileSync( fs.writeFileSync(
path.resolve(__dirname, '../events.js'), path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../events.js'),
`/* eslint-disable quotes */\n// Generated!\n\nmodule.exports = {\n${events}\n}\n` `/* eslint-disable quotes */\n// Generated!\n\nmodule.exports = {\n${events}\n}\n`
) )
fs.writeFileSync( fs.writeFileSync(
path.resolve(__dirname, '../lib/constants.ts'), path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../lib/constants.js'),
`// Generated!\n\nexport enum C {\n${constants.replace(/:/g, ' =')},\n}\n `// Generated!\n\nexport enum C {\n${constants.replace(/:/g, ' =')},\n}\n
// Generated!\n\nexport const EventId2EventName: { [key: number]: string } = {\n${events},\n}\n` // Generated!\n\nexport const EventId2EventName: { [key: number]: string } = {\n${events},\n}\n`
) )

View File

@@ -1,6 +1,7 @@
const {execSync} = require('child_process') import {execSync} from 'child_process'
const {existsSync} = require('fs') import {existsSync} from 'fs'
const {join} = require('path') import {join} from 'path'
import * as url from 'url'
const run = (cmd) => { const run = (cmd) => {
console.log('[i] running `' + cmd + '`') console.log('[i] running `' + cmd + '`')
@@ -16,7 +17,7 @@ if (process.env.USE_SYSTEM_LIBDELTACHAT === 'true') {
run('npm run install:prebuilds') run('npm run install:prebuilds')
} }
if (!existsSync(join(__dirname, '..', 'dist'))) { if (!existsSync(join(url.fileURLToPath(new URL('.', import.meta.url)), '..', 'dist'))) {
console.log('[i] Didn\'t find already built typescript bindings. Trying to transpile them. If this fail, make sure typescript is installed ;)') console.log('[i] Didn\'t find already built typescript bindings. Trying to transpile them. If this fail, make sure typescript is installed ;)')
run('npm run build:bindings:ts') run('npm run build:bindings:ts')
} }

View File

@@ -1,4 +1,5 @@
const { readFileSync } = require('fs') import { readFileSync } from 'fs'
import { request } from 'https'
const sha = JSON.parse( const sha = JSON.parse(
readFileSync(process.env['GITHUB_EVENT_PATH'], 'utf8') readFileSync(process.env['GITHUB_EVENT_PATH'], 'utf8')
@@ -21,8 +22,6 @@ const STATUS_DATA = {
target_url: base_url + file_url, target_url: base_url + file_url,
} }
const http = require('https')
const options = { const options = {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -32,7 +31,7 @@ const options = {
}, },
} }
const req = http.request(GITHUB_API_URL, options, function(res) { const req = request(GITHUB_API_URL, options, function (res) {
var chunks = [] var chunks = []
res.on('data', function(chunk) { res.on('data', function(chunk) {
chunks.push(chunk) chunks.push(chunk)

View File

@@ -1,5 +1,6 @@
const fs = require('fs') import fs from 'fs'
const path = require('path') import path from 'path'
import * as url from 'url'
if (process.platform !== 'win32') { if (process.platform !== 'win32') {
console.log('postinstall: not windows, so skipping!') console.log('postinstall: not windows, so skipping!')
@@ -7,7 +8,7 @@ if (process.platform !== 'win32') {
} }
const from = path.resolve( const from = path.resolve(
__dirname, url.fileURLToPath(new URL('.', import.meta.url)),
'..', '..',
'..', '..',
'target', 'target',
@@ -19,7 +20,7 @@ const getDestination = () => {
const argv = process.argv const argv = process.argv
if (argv.length === 3 && argv[2] === '--prebuild') { if (argv.length === 3 && argv[2] === '--prebuild') {
return path.resolve( return path.resolve(
__dirname, url.fileURLToPath(new URL('.', import.meta.url)),
'..', '..',
'prebuilds', 'prebuilds',
'win32-x64', 'win32-x64',
@@ -27,7 +28,7 @@ const getDestination = () => {
) )
} else { } else {
return path.resolve( return path.resolve(
__dirname, url.fileURLToPath(new URL('.', import.meta.url)),
'..', '..',
'build', 'build',
'Release', 'Release',

View File

@@ -1,7 +1,8 @@
const path = require('path') import path from 'path'
const { spawn } = require('./common') import { spawn } from './common.js'
import * as url from 'url'
const opts = { const opts = {
cwd: path.resolve(__dirname, '../..'), cwd: path.resolve(url.fileURLToPath(new URL('.', import.meta.url)), '../..'),
stdio: 'inherit' stdio: 'inherit'
} }

View File

@@ -3,7 +3,8 @@
"outDir": "dist", "outDir": "dist",
"rootDir": "./lib", "rootDir": "./lib",
"sourceMap": true, "sourceMap": true,
"module": "commonjs", "module": "nodenext",
"moduleResolution": "nodenext",
"target": "es5", "target": "es5",
"esModuleInterop": true, "esModuleInterop": true,
"declaration": true, "declaration": true,

View File

@@ -1,4 +1,5 @@
{ {
"type": "module",
"dependencies": { "dependencies": {
"debug": "^4.1.1", "debug": "^4.1.1",
"napi-macros": "^2.0.0", "napi-macros": "^2.0.0",
@@ -10,7 +11,7 @@
"@types/node": "^20.8.10", "@types/node": "^20.8.10",
"chai": "~4.3.10", "chai": "~4.3.10",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"mocha": "^8.2.1", "mocha": "^10.2.0",
"node-gyp": "^10.0.0", "node-gyp": "^10.0.0",
"prebuildify": "^5.0.1", "prebuildify": "^5.0.1",
"prebuildify-ci": "^1.0.5", "prebuildify-ci": "^1.0.5",
@@ -27,7 +28,7 @@
], ],
"homepage": "https://github.com/deltachat/deltachat-core-rust/tree/master/node", "homepage": "https://github.com/deltachat/deltachat-core-rust/tree/master/node",
"license": "GPL-3.0-or-later", "license": "GPL-3.0-or-later",
"main": "node/dist/index.js", "exports": "node/dist/index.js",
"name": "deltachat-node", "name": "deltachat-node",
"repository": { "repository": {
"type": "git", "type": "git",