-
Notifications
You must be signed in to change notification settings - Fork 1
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
c6e4053
commit 118fa78
Showing
9 changed files
with
142 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import colors from 'colors'; | ||
import { register } from './register'; | ||
|
||
const getConfig = (configFile: string) => { | ||
let config: any = null; | ||
const { unregister } = register({ key: 'utils-getConfig' }); | ||
let errInfo: any = null; | ||
try { | ||
config = require(configFile).default; | ||
} catch (err: any) { | ||
if ((err.details || err.message).indexOf('Cannot find') > -1) { | ||
console.info(colors.red('配置文件找不到,请检查:'), configFile); | ||
} else { | ||
throw err; | ||
} | ||
errInfo = err; | ||
} finally { | ||
unregister(); | ||
} | ||
if (errInfo !== null) { | ||
throw errInfo; | ||
} | ||
return config; | ||
}; | ||
export default getConfig; |
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,12 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { getAbsolutePath } from '../src'; | ||
import getConfig from '../src/config'; | ||
|
||
describe('config', () => { | ||
test('getConfig', () => { | ||
expect(getConfig(getAbsolutePath('./test/example/a.ts'))).toEqual({ a: 1 }); | ||
expect(() => | ||
getConfig(getAbsolutePath('./test/example/not-exist.ts')), | ||
).toThrow(/Cannot find module/); | ||
}); | ||
}); |
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,4 @@ | ||
const config = { | ||
a: 1, | ||
}; | ||
export default config; |
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,19 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { register } from '../src'; | ||
|
||
describe('register', () => { | ||
test('register', () => { | ||
register.register({ key: '1' }); | ||
register.register({ key: '2' }); | ||
expect(() => register.register({ key: '2' })).toThrow( | ||
'register key have exist!', | ||
); | ||
register.unregister('2'); | ||
register.register({ key: '2' }); | ||
register.restore(); | ||
const register1 = register.register({ key: '1' }); | ||
register.register({ key: '2' }); | ||
register1.unregister(); | ||
register.restore(); | ||
}); | ||
}); |
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,81 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { | ||
copyOptions, | ||
getAbsolutePath, | ||
getRelativePath, | ||
prettierData, | ||
removeFilesSync, | ||
winPath, | ||
} from '../src'; | ||
|
||
describe('utils', () => { | ||
test('getAbsolutePath', () => { | ||
expect(getAbsolutePath('/a/b')).toBe('/a/b'); | ||
expect(winPath(getAbsolutePath('./a/b'))).toContain('/a/b'); | ||
}); | ||
test('getRelativePath', () => { | ||
expect(getRelativePath('/a/b', '/a/b/c')).toBe('c'); | ||
expect(getRelativePath('/a/b', '/a')).toBe('..'); | ||
expect(winPath(getRelativePath('/a/b/c/d', '/a/b/e'))).toBe('../../e'); | ||
}); | ||
test('winPath', () => { | ||
expect(winPath('\\a\\b')).toEqual('/a/b'); | ||
expect(winPath('\\你好\\欢迎')).toEqual('/你好/欢迎'); | ||
expect(winPath('\\$\\%')).toEqual('/$/%'); | ||
// not convert extended-length paths | ||
const path = '\\\\?\\c:\\aaaa\\bbbb'; | ||
expect(winPath(path)).toEqual(path); | ||
}); | ||
test('prettierData', () => { | ||
expect(prettierData('const a=1')).toEqual('const a = 1;\n'); | ||
expect(prettierData('const a=\'1\'', { singleQuote: false })).toEqual( | ||
'const a = "1";\n', | ||
); | ||
}); | ||
test('copyOptions', () => { | ||
expect(copyOptions(undefined)).toEqual(undefined); | ||
const obj1 = { a: 1 }; | ||
const obj2 = copyOptions(obj1); | ||
expect(obj1).toEqual({ a: 1 }); | ||
expect(obj1).toEqual({ a: 1 }); | ||
obj2.a = 2; | ||
expect(obj1).toEqual({ a: 1 }); | ||
expect(obj2).toEqual({ a: 2 }); | ||
}); | ||
test('removeFilesSync, not dir and custom-data dir', () => { | ||
const tempDir = './test/temp1'; | ||
expect(fs.existsSync(tempDir)).toBeFalsy(); | ||
removeFilesSync(tempDir); | ||
expect(fs.existsSync(tempDir)).toBeTruthy(); | ||
fs.removeSync(tempDir); | ||
}); | ||
test('removeFilesSync, have dir, not custom-data dir', () => { | ||
const tempDir = './test/temp1'; | ||
const otherDir = path.join(tempDir, 'other'); | ||
const otherFile = path.join(tempDir, 'a.txt'); | ||
fs.mkdirsSync(tempDir); | ||
fs.mkdirsSync(otherDir); | ||
fs.writeFileSync(otherFile, 'otherFile'); | ||
removeFilesSync(tempDir); | ||
expect(fs.existsSync(otherDir)).toBeFalsy(); | ||
expect(fs.existsSync(otherFile)).toBeFalsy(); | ||
fs.removeSync(tempDir); | ||
}); | ||
test('removeFilesSync, have custom-data dir', () => { | ||
const tempDir = './test/temp2'; | ||
const customDataDir = path.join(tempDir, 'custom-data'); | ||
const otherDir = path.join(tempDir, 'other'); | ||
const otherFile = path.join(tempDir, 'a.txt'); | ||
fs.mkdirsSync(tempDir); | ||
fs.mkdirsSync(customDataDir); | ||
fs.mkdirsSync(otherDir); | ||
fs.writeFileSync(otherFile, 'otherFile'); | ||
removeFilesSync(tempDir); | ||
expect(fs.existsSync(tempDir)).toBeTruthy(); | ||
expect(fs.existsSync(customDataDir)).toBeTruthy(); | ||
expect(fs.existsSync(otherFile)).toBeFalsy(); | ||
fs.removeSync(tempDir); | ||
}); | ||
}); |
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,10 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "../", | ||
"moduleResolution": "node", | ||
}, | ||
"include": [ | ||
"../test/**/*.ts", | ||
] | ||
} |
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,8 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
export default defineConfig({ | ||
test: { | ||
include: ['./**/*.test.{ts,js}'], | ||
environment: 'node', | ||
testTimeout: 1000 * 30, | ||
}, | ||
}); |