Skip to content

Commit c189463

Browse files
committed
feature: dword: migrate to ESM
1 parent 7dd2705 commit c189463

28 files changed

Lines changed: 333 additions & 2530 deletions

.eslintrc.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/workflows/nodejs.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ jobs:
88
strategy:
99
matrix:
1010
node-version:
11-
- 18.x
12-
- 20.x
13-
- 21.x
11+
- 22.x
12+
- 24.x
13+
- 25.x
1414
steps:
15-
- uses: actions/checkout@v4
16-
- uses: oven-sh/setup-bun@v1
15+
- uses: actions/checkout@v5
16+
- uses: oven-sh/setup-bun@v2
1717
with:
1818
bun-version: latest
1919
- name: Use Node.js ${{ matrix.node-version }}
20-
uses: actions/setup-node@v4
20+
uses: actions/setup-node@v6
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323
- name: Install Redrun
File renamed without changes.

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ modules/jshint
1515
!modules/jshint/README.md
1616

1717
coverage
18+
*.config.*

.putout.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
{
22
"match": {
3+
"client": {
4+
"nodejs/declare": "off"
5+
},
36
"client/dword.js": {
47
"remove-console": "off"
5-
},
6-
"*.md": {
7-
"remove-unused-variables": "off"
88
}
99
},
10-
"rules": {
11-
"nodejs/declare": "off"
12-
},
1310
"ignore": [
1411
"modules",
1512
"codemirror"

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ For this purpuse API could be used.
6060
Middleware of `dword`. Options could be omitted.
6161

6262
```js
63-
const dword = require('dword');
64-
const express = require('express');
63+
import {dword} from 'dword';
64+
import express from 'express';
65+
6566
const app = express();
6667

6768
app.use(dword({
@@ -81,8 +82,8 @@ app.listen(31_337);
8182
Could be used with [socket.io](http://socket.io "Socket.io") to handle editor events with.
8283

8384
```js
84-
const io = require('socket.io');
85-
const socket = io.listen(server);
85+
import {Server} from 'socket.io';
86+
const socket = new Server(server);
8687

8788
dword.listen(socket, {
8889
// optional

bin/dword.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#!/usr/bin/env node
22

3-
'use strict';
3+
import fs from 'node:fs';
4+
import {fileURLToPath} from 'node:url';
5+
import {dirname} from 'node:path';
6+
import process from 'node:process';
7+
import http from 'node:http';
8+
import express from 'express';
9+
import {Server} from 'socket.io';
10+
import info from '../package.json' with {
11+
type: 'json',
12+
};
13+
import {dword} from '../server/index.js';
414

5-
const fs = require('fs');
15+
const __filename = fileURLToPath(import.meta.url);
16+
const __dirname = dirname(__filename);
617
const [arg] = process.argv.slice(2);
718

819
if (!arg)
@@ -20,7 +31,7 @@ else
2031
});
2132

2233
function getPath(name) {
23-
const reg = /^(~|\/)/;
34+
const reg = /^[~/]/;
2435

2536
if (!reg.test(name))
2637
name = process.cwd() + '/' + name;
@@ -31,31 +42,26 @@ function getPath(name) {
3142
function main(name) {
3243
const filename = getPath(name);
3344
const DIR = `${__dirname}/../assets/`;
34-
const dword = require('..');
35-
const http = require('http');
36-
const express = require('express');
37-
const io = require('socket.io');
3845

3946
const app = express();
4047
const server = http.createServer(app);
4148

4249
const {env} = process;
4350

44-
const port = env.PORT /* c9 */ || env.VCAP_APP_PORT /* cloudfoundry */ || 1337;
45-
46-
const ip = env.IP /* c9 */ || '0.0.0.0';
51+
const port = env.PORT || 1337;
52+
const ip = env.IP || '0.0.0.0';
4753

4854
app
4955
.use(express.static(DIR))
5056
.use(dword({
51-
online: false,
52-
diff: true,
53-
zip: true,
54-
}));
57+
online: false,
58+
diff: true,
59+
zip: true,
60+
}));
5561

5662
server.listen(port, ip);
5763

58-
const socket = io(server);
64+
const socket = new Server(server);
5965
const edSocket = dword.listen(socket);
6066

6167
edSocket.on('connection', () => {
@@ -84,19 +90,15 @@ function checkFile(name, callback) {
8490
}
8591

8692
function version() {
87-
console.log(`v${info().version}`);
88-
}
89-
90-
function info() {
91-
return require('../package');
93+
console.log(`v${info.version}`);
9294
}
9395

9496
function usage() {
95-
console.log(`Usage: ${info().name} [filename]`);
97+
console.log(`Usage: ${info.name} [filename]`);
9698
}
9799

98-
function help() {
99-
const bin = require('../json/bin');
100+
async function help() {
101+
const bin = await import('../json/bin');
100102

101103
usage();
102104
console.log('Options:');

client/_clipboard.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
'use strict';
2-
3-
const clipboard = require('@cloudcmd/clipboard');
4-
const createElement = require('@cloudcmd/create-element');
5-
const once = require('once');
1+
import clipboard from '@cloudcmd/clipboard';
2+
import createElement from '@cloudcmd/create-element';
3+
import once from 'once';
4+
import _showMessageOnce from './show-message.js';
65

76
const resolve = Promise.resolve.bind(Promise);
87
const reject = Promise.reject.bind(Promise);
8+
const showMessageOnce = once(_showMessageOnce);
99

10-
const showMessageOnce = once(require('./show-message'));
11-
12-
module.exports = (cmd) => {
10+
export default (cmd) => {
1311
const NAME = 'editor-clipboard';
1412
const {_Ace, _story} = this;
1513

1614
const value = _Ace.getSelection('\n');
17-
const insert = (a) => _Ace.getDoc().replaceSelection(a);
15+
const insert = (a) => _Ace
16+
.getDoc()
17+
.replaceSelection(a);
1818

1919
if (cmd === 'copy') {
2020
_story.setData(NAME, value);
@@ -23,7 +23,7 @@ module.exports = (cmd) => {
2323

2424
if (cmd === 'cut') {
2525
_story.setData(NAME, value);
26-
return cut(_story, value) ? resolve() : reject();
26+
return cut(value) ? resolve() : reject();
2727
}
2828

2929
return clipboard
@@ -38,7 +38,7 @@ module.exports = (cmd) => {
3838
});
3939
};
4040

41-
function cut(story, value) {
41+
function cut(value) {
4242
const textarea = createElement('textarea', {
4343
value,
4444
});

client/_init-socket.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
'use strict';
2-
31
/* global io */
4-
const {applyPatch} = require('daffy');
5-
const {alert} = require('smalltalk');
2+
import {applyPatch} from 'daffy';
3+
import {alert} from 'smalltalk';
64

75
const getHost = () => {
86
const l = location;
97

108
return l.origin || l.protocol + '//' + l.host;
119
};
1210

13-
module.exports = function() {
11+
export default function() {
1412
const dword = this;
1513
const href = getHost();
1614
const FIVE_SECONDS = 5000;
@@ -70,10 +68,10 @@ module.exports = function() {
7068
});
7169

7270
socket.on('disconnect', () => {
73-
dword.save.patch = self._patchHttp;
71+
dword.save.patch = globalThis._patchHttp;
7472
});
7573

7674
socket.on('err', (error) => {
7775
alert(this._TITLE, error);
7876
});
79-
};
77+
}

0 commit comments

Comments
 (0)