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: support egg v4 #114

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -13,4 +12,4 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '14, 16, 18, 20'
version: '20, 22'
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,9 @@ JS demo: <https://github.com/whxaxes/egg-boilerplate-d-js>
## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/tracer)](https://github.com/eggjs/tracer/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
6 changes: 6 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,9 @@ JS 项目: <https://github.com/whxaxes/egg-boilerplate-d-js>
## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/tracer)](https://github.com/eggjs/tracer/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"lint": "eslint . --ext .ts",
"check": "npm run tsc && npm run lint",
"test": "npm run check && npm run test-local",
"test-local": "egg-bin test --ts",
"test-local": "egg-bin test",
"prepublishOnly": "del dist && npm run tsc",
"cov": "egg-bin cov --ts",
"cov": "egg-bin cov",
"ci": "npm run check && npm run cov && npm run tsc"
},
"keywords": [
Expand All @@ -48,6 +48,8 @@
"yn": "^3.0.0"
},
"devDependencies": {
"@eggjs/bin": "^7.0.4",
"@eggjs/mock": "^6.0.5",
"@eggjs/tsconfig": "^1.0.0",
"@tsconfig/node14": "^14.1.2",
"@types/commander": "^2.12.2",
Expand All @@ -57,16 +59,13 @@
"@types/node": "^20.4.5",
"del": "^3.0.0",
"del-cli": "^1.1.0",
"egg": "^3.5.0",
"egg-bin": "^6.4.1",
"egg-mock": "^5.2.1",
"egg": "^4.0.7",
"egg-sequelize": "^4.3.1",
"eslint": "^8.28.0",
"eslint-config-egg": "^12.1.0",
"extend2": "^1.0.0",
"runscript": "^1.3.0"
"eslint-config-egg": "14",
"extend2": "^1.0.0"
},
"engines": {
"node": ">= 14.21.3"
"node": ">= 18.19.0"
}
}
4 changes: 2 additions & 2 deletions src/cmd/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { prompt } from 'enquirer';
import * as utils from '../utils';
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
import { createTsHelperInstance } from '../';

const TYPE_TS = 'typescript';
Expand Down
5 changes: 2 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import path from 'path';
import path from 'node:path';
import { Command } from 'commander';
import assert from 'assert';
import assert from 'node:assert';
import TsHelper, { defaultConfig } from './core';
import { loadModules, writeJsConfig, checkMaybeIsJsProj, getPkgInfo } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import { getPkgInfo } from './utils';

const root = path.dirname(__dirname);
Expand Down
10 changes: 5 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chokidar from 'chokidar';
import assert from 'assert';
import { EventEmitter } from 'events';
import fs from 'fs';
import crypto from 'crypto';
import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import fs from 'node:fs';
import crypto from 'node:crypto';
import chalk from 'chalk';
import path from 'path';
import path from 'node:path';
import * as generator from './generator';
import { get as deepGet, set as deepSet } from 'dot-prop';
import { declMapping, dtsComment, dtsCommentRE } from './config';
Expand Down
5 changes: 3 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import ObjectGenerator from './generators/object';
import PluginGenerator from './generators/plugin';
import { BaseGenerator } from './generators/base';
import * as utils from './utils';
import path from 'path';
import assert = require('assert');
import path from 'node:path';
import assert from 'node:assert';

type GeneratorKlass = { new (...args: any[]): BaseGenerator };

export const generators = {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { debuglog } from 'util';
import path from 'path';
import { debuglog } from 'node:util';
import path from 'node:path';
import { TsGenConfig, TsHelperConfig } from '..';
import * as utils from '../utils';

Expand Down
4 changes: 2 additions & 2 deletions src/generators/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
import { TsGenConfig } from '..';
import { declMapping } from '../config';
Expand Down
2 changes: 1 addition & 1 deletion src/generators/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { default as TsHelper, TsGenConfig, TsHelperConfig } from '../core';
import { declMapping } from '../config';
import * as utils from '../utils';
import path from 'path';
import path from 'node:path';

const customWatcherName = 'custom';
const customSpecRef = `${customWatcherName}_spec_ref`;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/egg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TsGenConfig, TsHelperConfig } from '..';
import path from 'path';
import path from 'node:path';

// declare global namespace Egg
export default function EggGenerator(config: TsGenConfig, baseConfig: TsHelperConfig) {
Expand Down
6 changes: 3 additions & 3 deletions src/generators/extend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debuglog } from 'util';
import fs from 'fs';
import path from 'path';
import { debuglog } from 'node:util';
import fs from 'node:fs';
import path from 'node:path';
import * as utils from '../utils';
import { declMapping } from '../config';
import { GeneratorResult, TsGenConfig, TsHelperConfig } from '..';
Expand Down
2 changes: 1 addition & 1 deletion src/generators/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import { declMapping } from '../config';
import { TsGenConfig, TsHelperConfig } from '..';
import * as utils from '../utils';
Expand Down
4 changes: 2 additions & 2 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cluster from 'cluster';
import { debuglog } from 'util';
import cluster from 'node:cluster';
import { debuglog } from 'node:util';
import TsHelper, { TsHelperOption } from './core';
import {
convertString, checkMaybeIsJsProj, writeJsConfig, cleanJs,
Expand Down
80 changes: 42 additions & 38 deletions src/scripts/eggInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Getting plugin info in child_process to prevent effecting egg application( splitting scopes ).
*/

import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import { eggInfoPath } from '../config';
import * as utils from '../utils';

Expand All @@ -21,43 +21,45 @@ if (utils.checkMaybeIsTsProj(cwd)) {
}
}

// try to read postinstall script env.ETS_SCRIPT_FRAMEWORK, let egg-bin can auto set the default framework
const framework = (utils.getPkgInfo(cwd).egg || {}).framework || process.env.ETS_SCRIPT_FRAMEWORK || 'egg';
const loader = getLoader(cwd, framework);
if (loader) {
try {
loader.loadPlugin();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadPlugin() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}
async function main() {
// try to read postinstall script env.ETS_SCRIPT_FRAMEWORK, let egg-bin can auto set the default framework
const framework = (utils.getPkgInfo(cwd).egg || {}).framework || process.env.ETS_SCRIPT_FRAMEWORK || 'egg';
const loader = getLoader(cwd, framework);
if (loader) {
try {
await loader.loadPlugin();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadPlugin() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}

// hack loadFile, ignore config file without customLoader for faster booting
mockFn(loader, 'loadFile', (filepath: string) => {
if (filepath && filepath.substring(filepath.lastIndexOf(path.sep) + 1).startsWith('config.')) {
const fileContent = fs.readFileSync(filepath, 'utf-8');
if (!fileContent.includes('customLoader')) return;
}
return true;
});

// hack loadFile, ignore config file without customLoader for faster booting
mockFn(loader, 'loadFile', filepath => {
if (filepath && filepath.substring(filepath.lastIndexOf(path.sep) + 1).startsWith('config.')) {
const fileContent = fs.readFileSync(filepath, 'utf-8');
if (!fileContent.includes('customLoader')) return;
try {
await loader.loadConfig();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadConfig() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}
return true;
});

try {
loader.loadConfig();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadConfig() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
eggInfo.plugins = loader.allPlugins;
eggInfo.config = loader.config;
eggInfo.eggPaths = loader.eggPaths;
eggInfo.timing = Date.now() - startTime;
}

eggInfo.plugins = loader.allPlugins;
eggInfo.config = loader.config;
eggInfo.eggPaths = loader.eggPaths;
eggInfo.timing = Date.now() - startTime;
utils.writeFileSync(eggInfoPath, JSON.stringify(eggInfo));
}

utils.writeFileSync(eggInfoPath, JSON.stringify(eggInfo));

/* istanbul ignore next */
function noop() {}

Expand All @@ -81,7 +83,7 @@ function getLoader(baseDir: string, framework: string) {
}
const eggCore = findEggCore(baseDir, frameworkPath);
if (!eggCore) {
console.warn('[egg-ts-helper] WARN cannot find egg core from frameworkPath: %s', frameworkPath);
console.warn('[egg-ts-helper] WARN cannot find @eggjs/core from frameworkPath: %s', frameworkPath);
return;
}
const EggLoader = eggCore.EggLoader;
Expand All @@ -100,24 +102,26 @@ function getLoader(baseDir: string, framework: string) {
});
}

function findEggCore(baseDir: string, frameworkPath: string) {
function findEggCore(baseDir: string, frameworkPath: string, eggCorePkgName = '@eggjs/core') {
let eggCorePath = '';
try {
eggCorePath = require.resolve('egg-core', { paths: [ frameworkPath ] });
eggCorePath = require.resolve(eggCorePkgName, { paths: [ frameworkPath ] });
} catch (_) {
// ignore error
}
if (!eggCorePath) {
eggCorePath = path.join(baseDir, 'node_modules/egg-core');
eggCorePath = path.join(baseDir, 'node_modules', eggCorePkgName);
if (!fs.existsSync(eggCorePath)) {
eggCorePath = path.join(frameworkPath, 'node_modules/egg-core');
eggCorePath = path.join(frameworkPath, 'node_modules', eggCorePkgName);
}
}
// try to load egg-core in cwd
const eggCore = utils.requireFile(eggCorePath);
if (!eggCore) {
// try to resolve egg-core
return utils.requireFile('egg-core');
return utils.requireFile(eggCorePkgName);
}
return eggCore;
}

main();
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import fs from 'node:fs';
import glob from 'globby';
import path from 'path';
import path from 'node:path';
import ts from 'typescript';
import yn from 'yn';
import { execFileSync, execFile, ExecFileOptions } from 'child_process';
import { execFileSync, execFile, ExecFileOptions } from 'node:child_process';
import JSON5 from 'json5';
import { eggInfoPath, tmpDir } from './config';

Expand Down
8 changes: 4 additions & 4 deletions src/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import path from 'node:path';
import chokidar from 'chokidar';
import assert from 'assert';
import { EventEmitter } from 'events';
import { debuglog } from 'util';
import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import { debuglog } from 'node:util';
import { TsGenerator, TsGenConfig, TsHelperConfig, default as TsHelper } from './core';
import * as utils from './utils';
import { loadGenerator } from './generator';
Expand Down
6 changes: 3 additions & 3 deletions test/bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import del from 'del';
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import TsHelper from '../dist';
import Command from '../dist/command';
import assert = require('assert');
import assert from 'node:assert';
import { triggerBinSync, triggerBin, getOutput, sleep, spawn, getStd } from './utils';

describe('bin.test.ts', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/cmd/clean.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import { triggerBin, getOutput, tsc } from '../utils';
import assert = require('assert');
import assert from 'node:assert';

describe('cmd/clean.test.ts', () => {
it('should works with clean command correctly', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/cmd/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import del from 'del';
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import { triggerBin, getOutput, sleep } from '../utils';
import assert = require('assert');
import assert from 'node:assert';

describe('cmd/init.test.ts', () => {
const appPath = path.resolve(__dirname, '../fixtures/init');
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/real-unittest/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


import mm from 'egg-mock';
import { app } from 'egg-mock/bootstrap';
import { mm } from '@eggjs/mock';
import { app } from '@eggjs/mock/bootstrap';

describe('index.test.js', () => {
beforeEach(() => {
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/real/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import path from 'node:path';
import { Application } from 'egg';

export default (app: Egg.Application) => {
export default (app: Application) => {
let directory = path.resolve(app.baseDir, './app/model');
app.loader.loadToApp(directory, 'model', {
caseStyle: 'upper',
Expand Down
Loading