Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gen /lib & /types from /src & drop /dist (main) #14068

Merged
merged 11 commits into from
Feb 9, 2022
10 changes: 9 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ module.exports = {
'babel/no-invalid-this': 'off',
'func-names': 'off',
'import/order': 'off',

ephys marked this conversation as resolved.
Show resolved Hide resolved
'no-invalid-this': 'off',
'no-unused-expressions': 'off',
camelcase: 'off',
WikiRik marked this conversation as resolved.
Show resolved Hide resolved
'no-console': 'off',
'no-prototype-builtins': 'off',
'no-multi-spaces': 'off',
},
}, {
// Disable slow rules that are not important in tests & docs (perf)
Expand Down Expand Up @@ -184,7 +191,8 @@ module.exports = {
ecmaVersion: 2020,
sourceType: 'script',
},
ignorePatterns: ['dist/**/*', 'types/**/*', 'dev/**/*'],
// TODO: un-ignore test/types/**, src/**/*.d.ts, and 'dev/**/*'
ignorePatterns: ['lib/**/*', 'types/**/*', 'test/types/**/*', 'src/**/*.d.ts', 'dev/**/*'],
env: {
node: true,
mocha: true,
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ test/binary/tmp/*
.vscode/
esdoc
node_modules
dist
*.log
/lib
/types
56 changes: 38 additions & 18 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ const path = require('path');
const exec = promisify(require('child_process').exec);

const stat = promisify(fs.stat);
const copyFile = promisify(fs.copyFile);

// if this script is moved, this will need to be adjusted
const rootDir = __dirname;
const outdir = path.join(rootDir, 'dist');
const outdir = path.join(rootDir, 'lib');
const typesDir = path.join(rootDir, 'types');

const nodeMajorVersion = Number(process.version.match(/(?<=^v)\d+/));

async function rmDistDir() {
async function rmDir(dirName) {
try {
await stat(outdir);
await stat(dirName);
if (nodeMajorVersion >= 14) {
const rm = promisify(fs.rm);
await rm(outdir, { recursive: true });
await rm(dirName, { recursive: true });
} else {
const rmdir = promisify(fs.rmdir);
await (nodeMajorVersion >= 12 ? rmdir(outdir, { recursive: true }) : rmdir(outdir));
await (nodeMajorVersion >= 12 ? rmdir(dirName, { recursive: true }) : rmdir(dirName));
}
} catch {
/* no-op */
Expand All @@ -34,23 +34,46 @@ async function rmDistDir() {

async function main() {
console.info('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(),
const [sourceFiles] = await Promise.all([
// Find all .js and .ts files from /src
glob('./src/**/*.{mjs,cjs,js,mts,cts,ts}', { onlyFiles: true, absolute: false }),
// Delete /lib for a full rebuild.
rmDir(outdir),
// Delete /types for a full rebuild.
rmDir(typesDir),
]);

const filesToCompile = [];
const filesToCopyToLib = [];
const declarationFiles = [];

for (const file of sourceFiles) {
// mjs files cannot be built as they would be compiled to commonjs
if (file.endsWith('.mjs')) {
filesToCopyToLib.push(file);
} else if (file.endsWith('.d.ts')) {
declarationFiles.push(file);
} else {
filesToCompile.push(file);
}
}

// 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, outdir],
[...declarationFiles, typesDir],
{ up: 1 },
);

if (filesToCopyToLib.length > 0) {
await copyFiles(
// The last path in the list is the output directory
filesToCopyToLib.concat(outdir),
{ up: 1 },
);
}

await Promise.all([
build({
// Adds source mapping
Expand All @@ -61,13 +84,10 @@ async function main() {
format: 'cjs',

outdir,
entryPoints: [...filesToCompile, './index.js']
entryPoints: filesToCompile
.map(file => path.resolve(file)),
}),

// not passed to "build" because we need this file to stay as ESM instead of CJS
copyFile('./index.mjs', path.resolve(outdir, './index.mjs')),

exec('tsc', {
env: {
// binaries installed from modules have symlinks in
Expand Down
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,25 @@
"url": "https://github.com/sequelize/sequelize/issues"
},
"homepage": "https://sequelize.org/",
"main": "./dist/index.js",
"types": "./dist",
"main": "./lib/index.js",
"types": "./types",
"type": "commonjs",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
"import": "./lib/index.mjs",
"require": "./lib/index.js"
},
"./lib/*": "./dist/lib/*.js",
"./lib/errors": "./dist/lib/errors/index.js",
"./lib/*": "./lib/*.js",
"./lib/errors": "./lib/errors/index.js",
"./*": "./*"
},
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"files": [
"dist",
"lib",
"index.js",
"types/index.d.ts",
"types/lib",
"types/type-helpers"
"types",
"index.js"
],
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -210,7 +207,7 @@
"lint": "eslint . --fix --report-unused-disable-directives",
"lint-no-fix": "eslint . --quiet --report-unused-disable-directives",
"lint-docs": "markdownlint docs",
"test-typings": "tsc -b types/tsconfig.json && tsc -b types/test/tsconfig.json && tsc --noEmit --emitDeclarationOnly false && tsc -b test/tsconfig.json",
"test-typings": "tsc --noEmit --emitDeclarationOnly false && tsc -b test/tsconfig.json",
"----------------------------------------- documentation -------------------------------------------": "",
"docs": "rimraf esdoc && esdoc -c docs/esdoc-config.js && cp docs/favicon.ico esdoc/favicon.ico && cp docs/ROUTER.txt esdoc/ROUTER && node docs/run-docs-transforms.js && node docs/redirects/create-redirects.js && rimraf esdoc/file esdoc/source.html",
"----------------------------------------- tests ---------------------------------------------------": "",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataType } from './data-types';
import { DataType } from '../../data-types';
import {
Logging,
Model,
Expand All @@ -8,17 +8,17 @@ import {
WhereOptions,
Filterable,
Poolable,
ModelCtor,
ModelStatic,
ModelType,
CreationAttributes,
Attributes,
} from './model';
import { QueryTypes, Transaction } from '..';
import { Sequelize, RetryOptions } from './sequelize';
import { SetRequired } from './../type-helpers/set-required';
import { Fn, Literal } from './utils';
import { Deferrable } from './deferrable';
} from '../../model';
import { QueryTypes } from '../../query-types';
import { Sequelize, RetryOptions } from '../../sequelize';
import { Transaction } from '../../transaction';
import { SetRequired } from '../../utils/set-required';
import { Fn, Literal } from '../../utils';
import { Deferrable } from '../../deferrable';

type BindOrReplacements = { [key: string]: unknown } | unknown[];
type FieldMap = { [key: string]: string };
Expand Down
24 changes: 18 additions & 6 deletions types/lib/query.d.ts → src/dialects/abstract/query.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { IncludeOptions } from '..';
import { Connection } from './connection-manager';
import {
Model, ModelType
} from './model';
import { Sequelize } from './sequelize';
import { QueryTypes } from '..';
import { QueryTypes } from '../../query-types';
import { Model, ModelType, IncludeOptions } from '../../model';
import { Sequelize } from '../../sequelize';

type BindOrReplacements = { [key: string]: unknown } | unknown[];
type FieldMap = { [key: string]: string };
Expand Down Expand Up @@ -161,24 +158,28 @@ export class AbstractQuery {

/**
* Checks if the query type is RAW
*
* @returns {boolean}
*/
public isRawQuery(): boolean;

/**
* Checks if the query type is VERSION
*
* @returns {boolean}
*/
public isVersionQuery(): boolean;

/**
* Checks if the query type is UPSERT
*
* @returns {boolean}
*/
public isUpsertQuery(): boolean;

/**
* Checks if the query type is INSERT
*
* @returns {boolean}
*/
public isInsertQuery(results?: unknown[], metaData?: unknown): boolean;
Expand All @@ -194,6 +195,7 @@ export class AbstractQuery {

/**
* Checks if the query type is SHOWTABLES
*
* @returns {boolean}
*/
public isShowTablesQuery(): boolean;
Expand All @@ -208,48 +210,56 @@ export class AbstractQuery {

/**
* Checks if the query type is SHOWINDEXES
*
* @returns {boolean}
*/
public isShowIndexesQuery(): boolean;

/**
* Checks if the query type is SHOWCONSTRAINTS
*
* @returns {boolean}
*/
public isShowConstraintsQuery(): boolean;

/**
* Checks if the query type is DESCRIBE
*
* @returns {boolean}
*/
public isDescribeQuery(): boolean;

/**
* Checks if the query type is SELECT
*
* @returns {boolean}
*/
public isSelectQuery(): boolean;

/**
* Checks if the query type is BULKUPDATE
*
* @returns {boolean}
*/
public isBulkUpdateQuery(): boolean;

/**
* Checks if the query type is BULKDELETE
*
* @returns {boolean}
*/
public isBulkDeleteQuery(): boolean;

/**
* Checks if the query type is FOREIGNKEYS
*
* @returns {boolean}
*/
public isForeignKeysQuery(): boolean;

/**
* Checks if the query type is UPDATE
*
* @returns {boolean}
*/
public isUpdateQuery(): boolean;
Expand All @@ -264,12 +274,14 @@ export class AbstractQuery {

/**
* Checks if the query starts with 'show' or 'describe'
*
* @returns {boolean}
*/
public isShowOrDescribeQuery(): boolean;

/**
* Checks if the query starts with 'call'
*
* @returns {boolean}
*/
public isCallQuery(): boolean;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Model } from '../..';
import type { Model } from '..';
import BaseError from './base-error';

/**
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Model } from '../..';
import type { Model } from '..';
import type { ErrorOptions } from './base-error';
import BaseError from './base-error';

Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions types/lib/hooks.d.ts → src/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Attributes, CreationAttributes, ModelType } from '../index';
import { ValidationOptions } from './instance-validator';
import Model, {
BulkCreateOptions,
Expand All @@ -9,10 +8,11 @@ import Model, {
InstanceRestoreOptions,
InstanceUpdateOptions,
ModelAttributes,
ModelOptions, RestoreOptions, UpdateOptions, UpsertOptions
ModelOptions, RestoreOptions, UpdateOptions, UpsertOptions,
Attributes, CreationAttributes, ModelType
} from './model';
import { AbstractQuery } from './query';
import { QueryOptions } from './query-interface';
import { AbstractQuery } from './dialects/abstract/query';
import { QueryOptions } from './dialects/abstract/query-interface';
import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { DeepWriteable } from './utils';

Expand All @@ -34,7 +34,7 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> {
beforeUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
afterUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
beforeUpsert(attributes: M, options: UpsertOptions<TAttributes>): HookReturn;
afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions<TAttributes>): HookReturn;
afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions<TAttributes>): HookReturn;
beforeSave(
instance: M,
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>
Expand Down
File renamed without changes.
File renamed without changes.