Skip to content

Commit 44394e2

Browse files
segayuuyoshinorinSukkaW
authored
Replace typescript (#60)
* Add tsc * Replace js to ts * chore(deps): update devDependencies * refactor: use import syntax * chore: format `.tsconfig.json` * chore: update `tsconfig.json` * Revert only under the `test` dir: "refactor: use import syntax" This reverts commit b1a4c57. * chore: built to dist * chore: update `.eslint.json` extends from `hexo/ts.js` * chore(deps): update dependencies * fix; `rfdc` import * fix: exports of types * test: `js` to `ts` * fix: allow empty argument for Database constructor and some methods * refactor: `export` to `export default` * refactor: util export * fix: import `isPlainObject` instead of const * chore: once ignore eslint for test * chore: add .tsconfig.json for test * chore: .tsconfig.json to tsconfig.json * chore: delete tsconfig.json for test * update each type functions number of argument * test(eslint): fix and ignore eslint some rules * chore: specify node 18.4 in ci.yml * chore: workaround for `Error [ERR_STREAM_PREMATURE_CLOSE]` in node 18.6 * chore: delete require nodejs`<18.5` * migrate jsonstream from js to ts * fix lint * Update src/database.ts Co-authored-by: Sukka <[email protected]> * chore(deps): add `@types/through2` * chore: add ts-ignore * chore: allow ts-ignore Co-authored-by: yoshinorin <[email protected]> Co-authored-by: Sukka <[email protected]>
1 parent e0d4503 commit 44394e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+629
-567
lines changed

Diff for: .eslintrc.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
22
"root": true,
3-
"extends": "hexo"
4-
}
3+
"extends": "hexo/ts.js",
4+
"parserOptions": {
5+
"sourceType": "module",
6+
"ecmaVersion": 2020
7+
},
8+
"rules": {
9+
"@typescript-eslint/no-unused-vars": 0,
10+
"@typescript-eslint/no-explicit-any": 0,
11+
"@typescript-eslint/no-var-requires": 0,
12+
"@typescript-eslint/ban-ts-comment": 0,
13+
"no-use-before-define": 0
14+
}
15+
}

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules/
44
.nyc_output/
55
*.log
66
docs/
7-
coverage/
7+
coverage/
8+
dist

Diff for: lib/types/index.js

-14
This file was deleted.

Diff for: package.json

+31-16
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
"name": "warehouse",
33
"version": "4.0.1",
44
"description": "Simple JSON-based database",
5-
"main": "lib/database",
5+
"main": "dist/database",
66
"directories": {
7-
"lib": "./lib"
7+
"lib": "./dist"
88
},
99
"files": [
10-
"lib/"
10+
"dist/"
1111
],
1212
"scripts": {
13-
"eslint": "eslint lib test",
14-
"test": "mocha \"test/scripts/**/*.js\"",
13+
"pretest": "npm run typescript",
14+
"typescript": "tsc --build tsconfig.json",
15+
"eslint": "eslint src test",
16+
"test": "mocha -r ts-node/register 'test/scripts/**/*.ts'",
1517
"test-cov": "nyc --reporter=lcovonly --reporter=text-summary npm test",
16-
"jsdoc": "jsdoc --configure .jsdoc.json"
18+
"jsdoc": "jsdoc --configure .jsdoc.json",
19+
"prepublish": "npm run typescript"
1720
},
1821
"repository": "hexojs/warehouse",
1922
"keywords": [
@@ -24,26 +27,38 @@
2427
"author": "Tommy Chen <[email protected]> (http://zespia.tw)",
2528
"license": "MIT",
2629
"dependencies": {
27-
"bluebird": "^3.2.2",
28-
"cuid": "^2.1.4",
29-
"graceful-fs": "^4.1.3",
30+
"@types/through2": "^2.0.36",
31+
"bluebird": "^3.7.2",
32+
"cuid": "^2.1.8",
33+
"graceful-fs": "^4.2.10",
3034
"hexo-log": "^3.0.0",
3135
"is-plain-object": "^5.0.0",
3236
"jsonparse": "^1.3.1",
33-
"rfdc": "^1.1.4",
37+
"rfdc": "^1.3.0",
3438
"through2": "^4.0.2"
3539
},
3640
"devDependencies": {
37-
"chai": "^4.2.0",
41+
"@types/bluebird": "^3.5.36",
42+
"@types/chai": "^4.3.1",
43+
"@types/graceful-fs": "^4.1.5",
44+
"@types/jsonstream": "^0.8.30",
45+
"@types/mocha": "^9.1.1",
46+
"@types/node": "^18.0.0",
47+
"@types/sinon": "^10.0.11",
48+
"@typescript-eslint/eslint-plugin": "^5.29.0",
49+
"@typescript-eslint/parser": "^5.29.0",
50+
"chai": "^4.3.6",
3851
"chai-as-promised": "^7.1.1",
39-
"eslint": "^8.1.0",
52+
"eslint": "^8.18.0",
4053
"eslint-config-hexo": "^5.0.0",
41-
"jsdoc": "^3.4.0",
42-
"lodash": "^4.17.15",
43-
"minami": "^1.1.1",
54+
"jsdoc": "^3.6.10",
55+
"lodash": "^4.17.21",
56+
"minami": "^1.2.3",
4457
"mocha": "^10.0.0",
4558
"nyc": "^15.0.0",
46-
"sinon": "^14.0.0"
59+
"sinon": "^14.0.0",
60+
"ts-node": "^10.8.1",
61+
"typescript": "^4.7.4"
4762
},
4863
"engines": {
4964
"node": ">=14"

Diff for: lib/database.js renamed to src/database.ts

+44-29
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
'use strict';
2-
3-
const { parse: createJsonParseStream } = require('./jsonstream');
4-
const Promise = require('bluebird');
5-
const fs = require('graceful-fs');
6-
const Model = require('./model');
7-
const Schema = require('./schema');
8-
const SchemaType = require('./schematype');
9-
const WarehouseError = require('./error');
1+
import { parse as createJsonParseStream } from './lib/jsonstream';
2+
import Bluebird from 'bluebird';
3+
import { writev, promises as fsPromises, createReadStream } from 'graceful-fs';
4+
import { pipeline, Stream } from 'stream';
5+
import Model from './model';
6+
import Schema from './schema';
7+
import SchemaType from './schematype';
8+
import WarehouseError from './error';
9+
import log from 'hexo-log';
10+
1011
const pkg = require('../package.json');
11-
const { open } = fs.promises;
12-
const pipeline = Promise.promisify(require('stream').pipeline);
13-
const log = require('hexo-log')();
12+
const { open } = fsPromises;
13+
const pipelineAsync = Bluebird.promisify(pipeline) as (...args: Stream[]) => Bluebird<unknown>;
1414

15-
let _writev;
15+
let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise<unknown>;
1616

17-
if (typeof fs.writev === 'function') {
17+
if (typeof writev === 'function') {
1818
_writev = (handle, buffers) => handle.writev(buffers);
1919
} else {
2020
_writev = async (handle, buffers) => {
2121
for (const buffer of buffers) await handle.write(buffer);
2222
};
2323
}
2424

25-
async function exportAsync(database, path) {
25+
async function exportAsync(database: Database, path: string) {
2626
const handle = await open(path, 'w');
2727

2828
try {
@@ -69,7 +69,17 @@ async function exportAsync(database, path) {
6969
}
7070
}
7171

72+
interface DatabaseOptions {
73+
version: number,
74+
path: string,
75+
onUpgrade: (...args: any[]) => any,
76+
onDowngrade: (...args: any[]) => any
77+
}
78+
7279
class Database {
80+
options: DatabaseOptions;
81+
_models: any;
82+
Model: typeof Model;
7383

7484
/**
7585
* Database constructor.
@@ -80,13 +90,15 @@ class Database {
8090
* @param {function} [options.onUpgrade] Triggered when the database is upgraded
8191
* @param {function} [options.onDowngrade] Triggered when the database is downgraded
8292
*/
83-
constructor(options) {
84-
this.options = Object.assign({
93+
constructor(options?: { path: string } & Partial<DatabaseOptions>) {
94+
this.options = {
8595
version: 0,
96+
// eslint-disable-next-line @typescript-eslint/no-empty-function
8697
onUpgrade() {},
87-
88-
onDowngrade() {}
89-
}, options);
98+
// eslint-disable-next-line @typescript-eslint/no-empty-function
99+
onDowngrade() {},
100+
...options
101+
};
90102

91103
this._models = {};
92104

@@ -104,7 +116,7 @@ class Database {
104116
* @param {Schema|object} [schema]
105117
* @return {Model}
106118
*/
107-
model(name, schema) {
119+
model(name: string, schema?: any) {
108120
if (this._models[name]) {
109121
return this._models[name];
110122
}
@@ -120,7 +132,7 @@ class Database {
120132
* @param {function} [callback]
121133
* @return {Promise}
122134
*/
123-
load(callback) {
135+
load(callback?) {
124136
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;
125137

126138
if (!path) throw new WarehouseError('options.path is required');
@@ -143,9 +155,9 @@ class Database {
143155
this.model(data.key)._import(data.value);
144156
});
145157

146-
const rs = fs.createReadStream(path, 'utf8');
158+
const rs = createReadStream(path, 'utf8');
147159

148-
return pipeline(rs, parseStream).then(() => {
160+
return pipelineAsync(rs, parseStream).then(() => {
149161
if (newVersion > oldVersion) {
150162
return onUpgrade(oldVersion, newVersion);
151163
} else if (newVersion < oldVersion) {
@@ -160,11 +172,11 @@ class Database {
160172
* @param {function} [callback]
161173
* @return {Promise}
162174
*/
163-
save(callback) {
175+
save(callback?) {
164176
const { path } = this.options;
165177

166178
if (!path) throw new WarehouseError('options.path is required');
167-
return Promise.resolve(exportAsync(this, path)).asCallback(callback);
179+
return Bluebird.resolve(exportAsync(this, path)).asCallback(callback);
168180
}
169181

170182
toJSON() {
@@ -182,12 +194,15 @@ class Database {
182194
}, models
183195
};
184196
}
197+
static Schema = Schema;
198+
Schema: typeof Schema;
199+
static SchemaType = SchemaType;
200+
SchemaType: typeof SchemaType;
201+
static version: number;
185202
}
186203

187204
Database.prototype.Schema = Schema;
188-
Database.Schema = Database.prototype.Schema;
189205
Database.prototype.SchemaType = SchemaType;
190-
Database.SchemaType = Database.prototype.SchemaType;
191206
Database.version = pkg.version;
192207

193-
module.exports = Database;
208+
export default Database;

Diff for: lib/document.js renamed to src/document.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
'use strict';
1+
import rfdc from 'rfdc';
2+
const cloneDeep = rfdc();
23

3-
const cloneDeep = require('rfdc')();
4-
5-
class Document {
4+
abstract class Document {
5+
abstract _model;
6+
_id!: any;
7+
abstract _schema;
68

79
/**
810
* Document constructor.
@@ -101,4 +103,4 @@ function isGetter(obj, key) {
101103
return Object.getOwnPropertyDescriptor(obj, key).get;
102104
}
103105

104-
module.exports = Document;
106+
export default Document;

Diff for: lib/error.js renamed to src/error.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
'use strict';
2-
31
class WarehouseError extends Error {
2+
code?: string;
43

54
/**
65
* WarehouseError constructor
76
*
87
* @param {string} msg
98
* @param {string} code
109
*/
11-
constructor(msg, code) {
10+
constructor(msg: string, code?: string) {
1211
super(msg);
1312

1413
Error.captureStackTrace(this);
1514

1615
this.code = code;
1716
}
17+
static ID_EXIST = 'ID_EXIST';
18+
static ID_NOT_EXIST = 'ID_NOT_EXIST';
19+
static ID_UNDEFINED = 'ID_UNDEFINED';
1820
}
1921

2022
WarehouseError.prototype.name = 'WarehouseError';
21-
WarehouseError.ID_EXIST = 'ID_EXIST';
22-
WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST';
23-
WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED';
2423

25-
module.exports = WarehouseError;
24+
export default WarehouseError;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const WarehouseError = require('../error');
1+
import WarehouseError from '../error';
42

53
class PopulationError extends WarehouseError {}
64

75
PopulationError.prototype.name = 'PopulationError';
86

9-
module.exports = PopulationError;
7+
export default PopulationError;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const WarehouseError = require('../error');
1+
import WarehouseError from '../error';
42

53
class ValidationError extends WarehouseError {}
64

75
ValidationError.prototype.name = 'ValidationError';
86

9-
module.exports = ValidationError;
7+
export default ValidationError;

Diff for: lib/jsonstream/index.js renamed to src/lib/jsonstream/index.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const through2 = require('through2');
4-
const Parser = require('jsonparse');
1+
import through2 from 'through2';
2+
import Parser from 'jsonparse';
53

64
/**
75
* Check whether a x and y are equal, or x matches y, or x(y) is truthy.
@@ -29,7 +27,7 @@ const check = (x, y) => {
2927
return false;
3028
};
3129

32-
module.exports.parse = function(path, map) {
30+
export function parse(path, map = null) {
3331
let header, footer;
3432

3533
const parser = new Parser();
@@ -50,6 +48,7 @@ module.exports.parse = function(path, map) {
5048
}
5149

5250
if (parser.tState !== Parser.C.START || parser.stack.length > 0) {
51+
// @ts-ignore
5352
cb(new Error('Incomplete JSON'));
5453
return;
5554
}
@@ -80,6 +79,7 @@ module.exports.parse = function(path, map) {
8079
}
8180

8281
parser.onValue = function(value) {
82+
// @ts-ignore
8383
if (!this.root) { stream.root = value; }
8484

8585
if (!path) return;
@@ -169,9 +169,12 @@ module.exports.parse = function(path, map) {
169169
parser.onToken = function(token, value) {
170170
parser._onToken(token, value);
171171
if (this.stack.length === 0) {
172+
// @ts-ignore
172173
if (stream.root) {
174+
// @ts-ignore
173175
if (!path) { stream.push(stream.root); }
174176

177+
// @ts-ignore
175178
stream.root = null;
176179
}
177180
}
@@ -199,4 +202,4 @@ module.exports.parse = function(path, map) {
199202
footer[key] = value;
200203
}
201204
}
202-
};
205+
}

0 commit comments

Comments
 (0)