Skip to content

Commit f9b20dd

Browse files
committed
feature: edward: migrate to ESM
1 parent 645041c commit f9b20dd

20 files changed

Lines changed: 163 additions & 187 deletions

.eslintrc.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
File renamed without changes.

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ modules/ace-builds
99
!modules/ace-builds/src-min
1010

1111
yarn-error.log
12+
client
1213

1314

1415
coverage

bin/edward.js

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

3-
'use strict';
3+
import process from 'node:process';
4+
import fs from 'node:fs';
5+
import {fileURLToPath} from 'node:url';
6+
import {dirname} from 'node:path';
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 {edward} from '../server/index.js';
414

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

919
if (!arg)
@@ -21,7 +31,7 @@ else
2131
});
2232

2333
function getPath(name) {
24-
const reg = /^(~|\/)/;
34+
const reg = /^[~/]/;
2535

2636
if (!reg.test(name))
2737
name = process.cwd() + '/' + name;
@@ -32,10 +42,6 @@ function getPath(name) {
3242
function main(name) {
3343
const filename = getPath(name);
3444
const DIR = `${__dirname}/../assets/`;
35-
const edward = require('..');
36-
const http = require('http');
37-
const express = require('express');
38-
const io = require('socket.io');
3945

4046
const app = express();
4147
const server = http.createServer(app);
@@ -49,13 +55,13 @@ function main(name) {
4955
app
5056
.use(express.static(DIR))
5157
.use(edward({
52-
diff: true,
53-
zip: true,
54-
}));
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 = edward.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:');

bower.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "edward",
3-
"homepage": "https://github.com/cloudcmd/edward",
4-
"authors": [
5-
"coderaiser <mnemonic.enemy@gmail.com>"
6-
],
7-
"description": "web editor",
8-
"main": "bin/edward.js",
9-
"keywords": [
10-
"file",
11-
"edit"
12-
],
13-
"license": "MIT",
14-
"private": true,
15-
"ignore": [
16-
"node_modules",
17-
"modules"
18-
],
19-
"dependencies": {
20-
"ace-builds": "^1.43.6"
21-
}
2+
"name": "edward",
3+
"homepage": "https://github.com/cloudcmd/edward",
4+
"authors": [
5+
"coderaiser <mnemonic.enemy@gmail.com>"
6+
],
7+
"description": "web editor",
8+
"main": "bin/edward.js",
9+
"keywords": [
10+
"file",
11+
"edit"
12+
],
13+
"license": "MIT",
14+
"private": true,
15+
"ignore": [
16+
"node_modules",
17+
"modules"
18+
],
19+
"dependencies": {
20+
"ace-builds": "^1.43.6"
21+
}
2222
}

client/_clipboard.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
'use strict';
1+
import clipboard from '@cloudcmd/clipboard';
2+
import createElement from '@cloudcmd/create-element';
3+
import once from 'once';
4+
import showMessage from './show-message.js';
25

3-
const clipboard = require('@cloudcmd/clipboard');
4-
const createElement = require('@cloudcmd/create-element');
5-
6-
const showMessage = require('./show-message');
7-
const once = require('once');
86
const showMessageOnce = once(showMessage);
97

108
const resolve = Promise.resolve.bind(Promise);
119
const reject = Promise.reject.bind(Promise);
1210

13-
module.exports = function(cmd) {
11+
export default function(cmd) {
1412
const NAME = 'editor-clipboard';
1513
const {_Ace, _story} = this;
1614

@@ -37,7 +35,7 @@ module.exports = function(cmd) {
3735

3836
insert(value);
3937
});
40-
};
38+
}
4139

4240
function cut(value) {
4341
const textarea = createElement('textarea', {

client/_init-socket.js

Lines changed: 4 additions & 6 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 edward = this;
1513
const href = getHost();
1614
const FIVE_SECONDS = 5000;
@@ -74,4 +72,4 @@ module.exports = function() {
7472
socket.on('err', (error) => {
7573
alert(this._TITLE, error);
7674
});
77-
};
75+
}

client/_on-save.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
'use strict';
1+
import {confirm} from 'smalltalk';
2+
import {tryToCatch} from 'try-to-catch';
3+
import {promisify} from 'es6-promisify';
4+
import {write} from 'restafary/client';
25

3-
const {confirm} = require('smalltalk');
4-
const {tryToCatch} = require('try-to-catch');
5-
const {promisify} = require('es6-promisify');
6-
const write = promisify(require('restafary/client').write);
6+
const _write = promisify(write);
77

8-
module.exports = async function(error, text) {
8+
export async function _onSave(error, text) {
99
const edward = this;
1010

1111
const {
@@ -25,7 +25,7 @@ module.exports = async function(error, text) {
2525
const [cancel] = await tryToCatch(confirm, _title, msg);
2626

2727
if (!cancel) {
28-
const [error, text] = await tryToCatch(write, _filename, this._value);
28+
const [error, text] = await tryToCatch(_write, _filename, this._value);
2929
return this._onSave(error, text);
3030
}
3131

@@ -41,4 +41,4 @@ module.exports = async function(error, text) {
4141
.setHash(_filename, hash);
4242

4343
this._Emitter.emit('save', _value.length);
44-
};
44+
}

client/_set-emmet.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
2-
3-
const exec = require('execon');
4-
const load = require('load.js');
1+
import exec from 'execon';
2+
import load from 'load.js';
53

64
/* global ace */
75
/* global join */
8-
module.exports = function() {
6+
export default function() {
97
const {_DIR, _PREFIX} = this;
108

119
const dir = `${_DIR}ace-builds/src-min/`;
@@ -32,4 +30,4 @@ module.exports = function() {
3230

3331
callback();
3432
});
35-
};
33+
}

client/edward.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
1-
'use strict';
2-
3-
const isString = (a) => typeof a === 'string';
4-
51
/* global ace */
62
/* global join */
7-
require('../css/edward.css');
8-
9-
const {createPatch} = require('daffy');
10-
const exec = require('execon');
11-
const Emitify = require('emitify');
12-
const load = require('load.js');
13-
const loadremote = require('./loadremote');
14-
const wraptile = require('wraptile');
15-
const smalltalk = require('smalltalk');
16-
const jssha = require('jssha');
17-
const restafary = require('restafary/client');
18-
const {tryToCatch} = require('try-to-catch');
3+
import '../css/edward.css';
4+
import {createPatch} from 'daffy';
5+
import exec from 'execon';
6+
import Emitify from 'emitify';
7+
import load from 'load.js';
8+
import wraptile from 'wraptile';
9+
import * as smalltalk from 'smalltalk';
10+
import jssha from 'jssha';
11+
import restafary from 'restafary/client';
12+
import {tryToCatch} from 'try-to-catch';
13+
import loadremote from './loadremote.js';
14+
import Story from './story.js';
15+
import _clipboard from './_clipboard.js';
16+
import _setEmmet from './_set-emmet.js';
17+
import _initSocket from './_init-socket.js';
18+
import setKeyMap from './set-key-map.js';
19+
import showMessage from './show-message.js';
20+
import save from './save.js';
21+
import {_onSave} from './_on-save.js';
22+
23+
const isString = (a) => typeof a === 'string';
1924

2025
globalThis.load = globalThis.load || load;
2126
globalThis.exec = globalThis.exec || exec;
2227

23-
const Story = require('./story');
24-
const _clipboard = require('./_clipboard');
25-
const _setEmmet = require('./_set-emmet');
26-
const _initSocket = require('./_init-socket');
27-
const setKeyMap = require('./set-key-map');
28-
const showMessage = require('./show-message');
29-
const save = require('./save');
30-
3128
function empty() {}
3229

33-
module.exports = Edward;
34-
35-
function Edward(el, options, callback) {
30+
export default function Edward(el, options, callback) {
3631
if (!(this instanceof Edward))
3732
return new Edward(el, options, callback);
3833

@@ -488,7 +483,7 @@ Edward.prototype._writeHttp = function(path, result) {
488483
restafary.write(path, result, onSave);
489484
};
490485

491-
Edward.prototype._onSave = require('./_on-save');
486+
Edward.prototype._onSave = _onSave;
492487

493488
Edward.prototype._doDiff = async function(path) {
494489
const value = this.getValue();

0 commit comments

Comments
 (0)