-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(loader-fs): extract shared loader fs package #5947
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
Open
killagu
wants to merge
2
commits into
next
Choose a base branch
from
agent/egg-dev/03217a77
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,43 +1,2 @@ | ||
| import fs, { type Stats } from 'node:fs'; | ||
|
|
||
| import globby from 'globby'; | ||
| import { readJSONSync } from 'utility'; | ||
|
|
||
| import utils from '../utils/index.ts'; | ||
|
|
||
| export type LoaderFSGlobOptions = globby.GlobbyOptions; | ||
|
|
||
| export interface LoaderFS { | ||
| exists(filepath: string): boolean; | ||
| stat(filepath: string): Stats; | ||
| realpath(filepath: string): string; | ||
| readJSON<T = unknown>(filepath: string): T; | ||
| glob(patterns: string | string[], options?: LoaderFSGlobOptions): string[]; | ||
| loadFile(filepath: string): Promise<unknown>; | ||
| } | ||
|
|
||
| export class RealLoaderFS implements LoaderFS { | ||
| exists(filepath: string): boolean { | ||
| return fs.existsSync(filepath); | ||
| } | ||
|
|
||
| stat(filepath: string): Stats { | ||
| return fs.statSync(filepath); | ||
| } | ||
|
|
||
| realpath(filepath: string): string { | ||
| return fs.realpathSync(filepath); | ||
| } | ||
|
|
||
| readJSON<T = unknown>(filepath: string): T { | ||
| return readJSONSync(filepath) as T; | ||
| } | ||
|
|
||
| glob(patterns: string | string[], options?: LoaderFSGlobOptions): string[] { | ||
| return globby.sync(patterns, options); | ||
| } | ||
|
|
||
| async loadFile(filepath: string): Promise<unknown> { | ||
| return utils.loadFile(filepath); | ||
| } | ||
| } | ||
| export { RealLoaderFS } from '@eggjs/loader-fs'; | ||
| export type { LoaderFS, LoaderFSGlobOptions } from '@eggjs/loader-fs'; |
This file contains hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
| # Changelog | ||
|
|
||
| > [!IMPORTANT] | ||
| > Moving forwards we are using the GitHub releases page at <https://github.com/eggjs/egg/releases> in combination with [release.yml](https://github.com/eggjs/egg/actions/workflows/release.yml) for publishing releases and their changelogs. | ||
|
|
||
| --- | ||
|
|
||
| ## 1.0.0+ | ||
|
|
||
| ### Features | ||
|
|
||
| - Initial shared `LoaderFS` and `RealLoaderFS` package. |
This file contains hidden or 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,14 @@ | ||
| # @eggjs/loader-fs | ||
|
|
||
| Loader-facing filesystem abstraction for Egg loaders and bundled runtimes. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { RealLoaderFS, type LoaderFS } from '@eggjs/loader-fs'; | ||
|
|
||
| const loaderFS: LoaderFS = new RealLoaderFS(); | ||
| ``` | ||
|
|
||
| `LoaderFS` intentionally covers only the operations required by loader code: | ||
| `exists`, `stat`, `realpath`, `readJSON`, `glob`, and `loadFile`. |
This file contains hidden or 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,57 @@ | ||
| { | ||
| "name": "@eggjs/loader-fs", | ||
| "version": "1.0.0-beta.9", | ||
| "description": "Loader-facing filesystem abstraction for Egg", | ||
| "keywords": [ | ||
| "egg", | ||
| "fs", | ||
| "loader" | ||
| ], | ||
| "homepage": "https://github.com/eggjs/egg/tree/next/packages/loader-fs", | ||
| "bugs": { | ||
| "url": "https://github.com/eggjs/egg/issues" | ||
| }, | ||
| "license": "MIT", | ||
| "author": "fengmk2 <fengmk2@gmail.com>", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/eggjs/egg.git", | ||
| "directory": "packages/loader-fs" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "type": "module", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| ".": "./src/index.ts", | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public", | ||
| "exports": { | ||
| ".": "./dist/index.js", | ||
| "./package.json": "./package.json" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "typecheck": "tsgo --noEmit", | ||
| "test": "vitest run" | ||
| }, | ||
| "dependencies": { | ||
| "@eggjs/utils": "workspace:*", | ||
| "globby": "catalog:", | ||
| "utility": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@eggjs/tsconfig": "workspace:*", | ||
| "@types/node": "catalog:", | ||
| "typescript": "catalog:", | ||
| "vitest": "catalog:" | ||
| }, | ||
| "engines": { | ||
| "node": ">=22.18.0" | ||
| } | ||
| } | ||
This file contains hidden or 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,73 @@ | ||
| import fs, { type Stats } from 'node:fs'; | ||
| import BuiltinModule from 'node:module'; | ||
| import path from 'node:path'; | ||
| import { debuglog } from 'node:util'; | ||
|
|
||
| import { importModule } from '@eggjs/utils'; | ||
| import globby from 'globby'; | ||
| import { readJSONSync } from 'utility'; | ||
|
|
||
| const debug = debuglog('egg/core/utils'); | ||
|
|
||
| type CommonJSModuleConstructor = { | ||
| _extensions?: Record<string, unknown>; | ||
| }; | ||
|
|
||
| // Guard against poorly mocked module constructors. | ||
| const Module = typeof module !== 'undefined' && module.constructor.length > 1 ? module.constructor : BuiltinModule; | ||
|
|
||
| const extensions = (Module as unknown as CommonJSModuleConstructor)._extensions ?? {}; | ||
| const extensionNames = Object.keys(extensions).concat(['.cjs', '.mjs']); | ||
|
killagu marked this conversation as resolved.
Outdated
|
||
|
|
||
| export type LoaderFSGlobOptions = globby.GlobbyOptions; | ||
|
|
||
| export interface LoaderFS { | ||
| exists(filepath: string): boolean; | ||
| stat(filepath: string): Stats; | ||
| realpath(filepath: string): string; | ||
| readJSON<T = unknown>(filepath: string): T; | ||
| glob(patterns: string | string[], options?: LoaderFSGlobOptions): string[]; | ||
| loadFile(filepath: string): Promise<unknown>; | ||
| } | ||
|
|
||
| export class RealLoaderFS implements LoaderFS { | ||
| exists(filepath: string): boolean { | ||
| return fs.existsSync(filepath); | ||
| } | ||
|
|
||
| stat(filepath: string): Stats { | ||
| return fs.statSync(filepath); | ||
| } | ||
|
|
||
| realpath(filepath: string): string { | ||
| return fs.realpathSync(filepath); | ||
| } | ||
|
|
||
| readJSON<T = unknown>(filepath: string): T { | ||
| return readJSONSync(filepath) as T; | ||
| } | ||
|
|
||
| glob(patterns: string | string[], options?: LoaderFSGlobOptions): string[] { | ||
| return globby.sync(patterns, options); | ||
| } | ||
|
|
||
| async loadFile(filepath: string): Promise<unknown> { | ||
| debug('[loadFile:start] filepath: %s', filepath); | ||
| try { | ||
| const extname = path.extname(filepath); | ||
| if (extname && !extensionNames.includes(extname) && extname !== '.ts') { | ||
| return fs.readFileSync(filepath); | ||
| } | ||
| return await importModule(filepath, { importDefaultOnly: true }); | ||
| } catch (e) { | ||
| if (!(e instanceof Error)) { | ||
| console.trace(e); | ||
| throw e; | ||
| } | ||
| const err = new Error(`[egg/core] load file: ${filepath}, error: ${e.message}`); | ||
| err.cause = e; | ||
| debug('[loadFile] handle %s error: %s', filepath, e); | ||
| throw err; | ||
|
killagu marked this conversation as resolved.
|
||
| } | ||
|
killagu marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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,3 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = { a: 1 }; |
This file contains hidden or 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,3 @@ | ||
| { | ||
| "type": "commonjs" | ||
| } |
This file contains hidden or 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 @@ | ||
| name: loader-fs |
This file contains hidden or 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,37 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| import globby from 'globby'; | ||
| import { describe, it } from 'vitest'; | ||
|
|
||
| import { RealLoaderFS } from '../src/index.ts'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| describe('test/loader_fs.test.ts', () => { | ||
| const loaderFS = new RealLoaderFS(); | ||
| const baseDir = path.join(__dirname, 'fixtures/loadfile'); | ||
|
|
||
| it('should wrap exists/stat/realpath with node fs behavior', () => { | ||
| const filepath = path.join(baseDir, 'object.js'); | ||
|
|
||
| assert.equal(loaderFS.exists(filepath), fs.existsSync(filepath)); | ||
| assert.equal(loaderFS.exists(path.join(baseDir, 'not-exists.js')), false); | ||
| assert.equal(loaderFS.stat(filepath).isFile(), fs.statSync(filepath).isFile()); | ||
| assert.equal(loaderFS.realpath(baseDir), fs.realpathSync(baseDir)); | ||
| }); | ||
|
|
||
| it('should wrap readJSON/glob/loadFile with current loader behavior', async () => { | ||
| const packagePath = path.join(baseDir, 'package.json'); | ||
| const patterns = ['*.js', '!null.js']; | ||
| const yamlPath = path.join(baseDir, 'plain.yml'); | ||
|
|
||
| assert.deepEqual(loaderFS.readJSON(packagePath), JSON.parse(fs.readFileSync(packagePath, 'utf8'))); | ||
| assert.deepEqual(loaderFS.glob(patterns, { cwd: baseDir }).sort(), globby.sync(patterns, { cwd: baseDir }).sort()); | ||
| assert.deepEqual(await loaderFS.loadFile(path.join(baseDir, 'object.js')), { a: 1 }); | ||
| assert.deepEqual(await loaderFS.loadFile(yamlPath), fs.readFileSync(yamlPath)); | ||
| }); | ||
| }); |
This file contains hidden or 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,3 @@ | ||
| { | ||
| "extends": "../../tsconfig.json" | ||
| } |
This file contains hidden or 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,7 @@ | ||
| import { defineConfig } from 'tsdown'; | ||
|
|
||
| export default defineConfig({ | ||
| entry: { | ||
| index: 'src/index.ts', | ||
| }, | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.