-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
183d20a
commit 51566c0
Showing
15 changed files
with
163 additions
and
33 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
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
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
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
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,48 @@ | ||
import { IInformationService } from '../interface'; | ||
import { | ||
getCurrentEnvironment, | ||
getUserHome, | ||
isDevelopmentEnvironment, | ||
safeRequire, | ||
} from '../util'; | ||
import { dirname, join } from 'path'; | ||
|
||
export class MidwayInformationService implements IInformationService { | ||
protected pkg: Record<string, unknown>; | ||
private readonly appDir: string; | ||
private readonly baseDir: string; | ||
|
||
constructor(options: { baseDir?: string; appDir?: string }) { | ||
this.baseDir = options.baseDir; | ||
this.appDir = options.appDir; | ||
if (!this.appDir) { | ||
this.appDir = dirname(this.baseDir); | ||
} | ||
this.pkg = safeRequire(join(this.appDir, 'package.json')) || {}; | ||
} | ||
|
||
getAppDir(): string { | ||
return this.appDir; | ||
} | ||
|
||
getBaseDir(): string { | ||
return this.baseDir; | ||
} | ||
|
||
getHome(): string { | ||
return getUserHome(); | ||
} | ||
|
||
getPkg(): any { | ||
return this.pkg; | ||
} | ||
|
||
getProjectName(): string { | ||
return (this.pkg?.['name'] as string) || ''; | ||
} | ||
|
||
getRoot(): string { | ||
const isDevelopmentEnv = isDevelopmentEnvironment(getCurrentEnvironment()); | ||
return isDevelopmentEnv ? this.getAppDir() : this.getHome(); | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
packages/core/test/services/fixtures/default_case/package.json
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,3 @@ | ||
{ | ||
"name": "test-demo" | ||
} |
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,33 @@ | ||
import { join } from 'path'; | ||
import { MidwayInformationService } from '../../src'; | ||
import { getUserHome } from '../../src/util'; | ||
|
||
describe('/test/services/informationService.test.ts', () => { | ||
it('informationService should be ok', () => { | ||
process.env.NODE_ENV = 'local'; | ||
const informationService = new MidwayInformationService({ | ||
baseDir: join(__dirname, 'fixtures/default_case'), | ||
appDir: join(__dirname, 'fixtures/default_case'), | ||
}); | ||
expect(informationService.getAppDir()).toEqual(join(__dirname, 'fixtures/default_case')); | ||
expect(informationService.getBaseDir()).toEqual(join(__dirname, 'fixtures/default_case')); | ||
expect(informationService.getHome()).toEqual(getUserHome()); | ||
expect(informationService.getProjectName()).toEqual('test-demo'); | ||
expect(informationService.getPkg()).toEqual({ | ||
name: 'test-demo' | ||
}); | ||
expect(informationService.getRoot()).toEqual(informationService.getAppDir()); | ||
process.env.NODE_ENV = undefined; | ||
}); | ||
|
||
it('should find appDir with dirname', () => { | ||
process.env.NODE_ENV = 'prod'; | ||
const informationService = new MidwayInformationService({ | ||
baseDir: join(__dirname, 'fixtures/default_case/src'), | ||
}); | ||
expect(informationService.getAppDir()).toEqual(join(__dirname, 'fixtures/default_case')); | ||
expect(informationService.getBaseDir()).toEqual(join(__dirname, 'fixtures/default_case/src')); | ||
expect(informationService.getRoot()).toEqual(informationService.getHome()); | ||
process.env.NODE_ENV = undefined; | ||
}); | ||
}); |
Oops, something went wrong.