Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(typescript): add foundation for incremential TS migration (#…
…13569) * refactor(typescript): add foundation for incremential TS migration Converts the logger interface to TypeScript. Adds eslint rules and TypeScript configuration to allow for a gradual migration to TS. Adds a build script to transpile both TS and JS code while keeping the same API for package usage. Changes imports from test files to use "absolute" paths that point to the directory of the compiled code. * use || instead of ?? * fix source maps breaking tests * fix build script for older versions of node * make some small fixes + add better docs to logger.ts * add ts support to tests * update yarn.lock * disable no-loss-of-percesion rule * fix & cleanup a lil * test.js -> test.[tj]s Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
- Loading branch information
Showing
194 changed files
with
1,157 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ test/binary/tmp/* | |
.vscode/ | ||
esdoc | ||
node_modules | ||
dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
const glob = require('fast-glob'); | ||
const { promisify } = require('util'); | ||
const { build } = require('esbuild'); | ||
const fs = require('fs'); | ||
const copyFiles = promisify( require('copyfiles')); | ||
const path = require('path'); | ||
const exec = promisify(require('child_process').exec); | ||
|
||
const rmdir = promisify(fs.rmdir); | ||
const stat = promisify(fs.stat); | ||
|
||
// if this script is moved, this will need to be adjusted | ||
const rootDir = __dirname; | ||
const outdir = path.join(rootDir, 'dist'); | ||
|
||
const nodeMajorVersion = Number(process.version.match(/(?<=^v)\d+/)); | ||
|
||
async function rmDistDir() { | ||
try { | ||
await stat(outdir); | ||
if (nodeMajorVersion >= 12) { | ||
await rmdir(outdir, { recursive: true }); | ||
} else { | ||
await rmdir(outdir); | ||
} | ||
} catch { | ||
/* no-op */ | ||
} | ||
} | ||
|
||
async function main() { | ||
console.log('Compiling sequelize...'); | ||
const [declarationFiles, filesToCompile] = await Promise.all([ | ||
// Find all .d.ts files from types/ | ||
glob('./types/**/*.d.ts', { onlyFiles: true, absolute: false }), | ||
// Find all .js and .ts files from lib/ | ||
glob('./lib/**/*.[tj]s', { onlyFiles: true, absolute: false }), | ||
// Delete dist/ for a full rebuild. | ||
rmDistDir() | ||
]); | ||
|
||
// copy .d.ts files prior to generating them from the .ts files | ||
// so the .ts files in lib/ will take priority.. | ||
await copyFiles( | ||
// The last path in the list is the output directory | ||
declarationFiles.concat(outdir), | ||
{ up: 1 } | ||
); | ||
|
||
await Promise.all([ | ||
build({ | ||
// Adds source mapping | ||
sourcemap: true, | ||
// The compiled code should be usable in node v10 | ||
target: 'node10', | ||
// The source code's format is commonjs. | ||
format: 'cjs', | ||
|
||
outdir, | ||
entryPoints: filesToCompile | ||
.concat('./index.js') | ||
.map(file => path.resolve(file)), | ||
|
||
// minify the compiled code | ||
minify: true, | ||
// Keep `constructor.name` the same (used for associations) | ||
keepNames: true | ||
}), | ||
|
||
exec('tsc', { | ||
env: { | ||
// binaries installed from modules have symlinks in | ||
// <pkg root>/node_modules/.bin. | ||
PATH: `${process.env.PATH || ''}:${path.join( | ||
rootDir, | ||
'node_modules/.bin' | ||
)}` | ||
}, | ||
cwd: rootDir | ||
}) | ||
]); | ||
} | ||
|
||
main().catch(console.error).finally(process.exit); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @file Sequelize module for debug and deprecation messages. | ||
* It require a `context` for which messages will be printed. | ||
* | ||
* @module logging | ||
* @access package | ||
*/ | ||
import nodeDebug from 'debug'; | ||
import util from 'util'; | ||
|
||
/** | ||
* The configuration for sequelize's logging interface. | ||
* | ||
* @access package | ||
*/ | ||
export interface LoggerConfig { | ||
/** | ||
* The context which the logger should log in. | ||
* | ||
* @default 'sequelize' | ||
*/ | ||
context?: string; | ||
} | ||
|
||
export class Logger { | ||
protected config: LoggerConfig; | ||
|
||
constructor({ context = 'sequelize', ...rest }: Partial<LoggerConfig> = {}) { | ||
this.config = { | ||
context, | ||
...rest | ||
}; | ||
} | ||
|
||
/** | ||
* Logs a warning in the logger's context. | ||
* | ||
* @param message The message of the warning. | ||
*/ | ||
warn(message: string): void { | ||
console.warn(`(${this.config.context}) Warning: ${message}`); | ||
} | ||
|
||
/** | ||
* Uses node's util.inspect to stringify a value. | ||
* | ||
* @param value The value which should be inspected. | ||
* @returns The string of the inspected value. | ||
*/ | ||
inspect(value: unknown): string { | ||
return util.inspect(value, { | ||
showHidden: false, | ||
depth: 1 | ||
}); | ||
} | ||
|
||
/** | ||
* Gets a debugger for a context. | ||
* | ||
* @param name The name of the context. | ||
* @returns A debugger interace which can be used to debug. | ||
*/ | ||
debugContext(name: string): nodeDebug.Debugger { | ||
return nodeDebug(`${this.config.context}:${name}`); | ||
} | ||
} | ||
|
||
export const logger = new Logger(); |
Oops, something went wrong.