Skip to content

Commit

Permalink
feat: detect the type of egg project (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Dec 29, 2024
1 parent 2851f83 commit 68950de
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![CI](https://github.com/eggjs/utils/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/utils/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/@eggjs/utils.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/@eggjs/utils.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/utils
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"lint": "eslint src test --ext ts",
"pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly",
"test": "npm run test-local",
"posttest": "npm run clean",
"test-local": "egg-bin test",
"preci": "npm run clean && npm run lint && npm run prepublishOnly",
"ci": "egg-bin cov",
"postci": "npm run clean",
"clean": "rimraf dist",
"prepublishOnly": "tshy && tshy-after && attw --pack"
},
Expand Down
41 changes: 41 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { getFrameworkPath } from './framework.js';
import { getPlugins, getConfig, getLoadUnits } from './plugin.js';
import { getFrameworkOrEggPath } from './deprecated.js';
Expand All @@ -14,3 +16,42 @@ export default {
getPlugins, getConfig, getLoadUnits,
getFrameworkOrEggPath,
};

export enum EggType {
framework = 'framework',
plugin = 'plugin',
application = 'application',
unknown = 'unknown',
}

/**
* Detect the type of egg project
*/
export async function detectType(baseDir: string): Promise<EggType> {
const pkgFile = path.join(baseDir, 'package.json');
let pkg: {
egg?: {
framework?: boolean;
};
eggPlugin?: {
name: string;
};
};
try {
await fs.access(pkgFile);
} catch {
return EggType.unknown;
}
try {
pkg = JSON.parse(await fs.readFile(pkgFile, 'utf-8'));
} catch {
return EggType.unknown;
}
if (pkg.egg?.framework) {
return EggType.framework;
}
if (pkg.eggPlugin?.name) {
return EggType.plugin;
}
return EggType.application;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/no-package-json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = 'no-package-json';
31 changes: 31 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { strict as assert } from 'node:assert';
import { detectType, EggType } from '../src/index.js';
import { getFilepath } from './helper.js';

describe('test/index.test.ts', () => {
describe('detectType()', () => {
it('should detect application', async () => {
const baseDir = getFilepath('egg-app');
assert.equal(await detectType(baseDir), EggType.application);
assert.equal(await detectType(baseDir), 'application');
});

it('should detect plugin', async () => {
const baseDir = getFilepath('egg-app/plugin/p');
assert.equal(await detectType(baseDir), EggType.plugin);
assert.equal(await detectType(baseDir), 'plugin');
});

it('should detect framework', async () => {
const baseDir = getFilepath('framework-egg-default/node_modules/egg');
assert.equal(await detectType(baseDir), EggType.framework);
assert.equal(await detectType(baseDir), 'framework');
});

it('should detect unknown', async () => {
const baseDir = getFilepath('no-package-json');
assert.equal(await detectType(baseDir), EggType.unknown);
assert.equal(await detectType(baseDir), 'unknown');
});
});
});

0 comments on commit 68950de

Please sign in to comment.