-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
6 changed files
with
171 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`normal: file content 1`] = ` | ||
"// @index('./*', f => \`export * from '\${f.path}'\`) | ||
// @endindex" | ||
`; | ||
|
||
exports[`normal: generated content 1`] = ` | ||
"// @index('./*', f => \`export * from '\${f.path}'\`) | ||
export * from './_8' | ||
export * from './1' | ||
export * from './x' | ||
export * from './国家' | ||
// @endindex" | ||
`; | ||
|
||
exports[`not found 1`] = `[Error: File not found.]`; | ||
|
||
exports[`replace: file content 1`] = ` | ||
"// @index('./*', f => \`export * from '\${f.path}'\`) | ||
export * from './_8' | ||
export * from './1' | ||
export * from './x' | ||
export * from './国家' | ||
// @endindex" | ||
`; | ||
|
||
exports[`replace: generated content 1`] = ` | ||
"// @index('./*', f => \`export * from '\${f.path}'\`) | ||
export * from './_8' | ||
export * from './1' | ||
export * from './x' | ||
export * from './国家' | ||
// @endindex" | ||
`; |
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,53 @@ | ||
import tmp from 'tempy' | ||
import { dedent } from 'vtils' | ||
import { generateIndex } from './generateIndex' | ||
import { join } from 'path' | ||
import { readFileSync, writeFileSync } from 'fs' | ||
import { sync as rmrf } from 'rimraf' | ||
|
||
let dir: string | ||
|
||
beforeAll(() => { | ||
dir = tmp.directory() | ||
writeFileSync( | ||
join(dir, 'index.ts'), | ||
dedent` | ||
// @index('./*', f => \`export * from '\${f.path}'\`) | ||
// @endindex | ||
`, | ||
) | ||
writeFileSync(join(dir, 'x.ts'), '') | ||
writeFileSync(join(dir, '1.ts'), '') | ||
writeFileSync(join(dir, '国家.ts'), '') | ||
writeFileSync(join(dir, '_8.ts'), '') | ||
}) | ||
|
||
afterAll(() => { | ||
rmrf(dir) | ||
}) | ||
|
||
test('normal', async () => { | ||
const generatedContent = await generateIndex(join(dir, 'index.ts')) | ||
expect(generatedContent).toMatchSnapshot('generated content') | ||
expect(readFileSync(join(dir, 'index.ts')).toString()).toMatchSnapshot( | ||
'file content', | ||
) | ||
}) | ||
|
||
test('replace', async () => { | ||
const generatedContent = await generateIndex(join(dir, 'index.ts'), true) | ||
expect(generatedContent).toMatchSnapshot('generated content') | ||
expect(readFileSync(join(dir, 'index.ts')).toString()).toMatchSnapshot( | ||
'file content', | ||
) | ||
}) | ||
|
||
test('not found', async () => { | ||
let err: any | ||
try { | ||
await generateIndex(join(dir, 'no-file')) | ||
} catch (e) { | ||
err = e | ||
} | ||
expect(err).toMatchSnapshot() | ||
}) |
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,30 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
import { IndexGenerator } from './IndexGenerator' | ||
|
||
export async function generateIndex( | ||
filePath: string, | ||
replaceFile = false, | ||
): Promise<string> { | ||
if (!existsSync(filePath)) { | ||
throw new Error('File not found.') | ||
} | ||
let fileContent = readFileSync(filePath).toString() | ||
const generator = new IndexGenerator({ | ||
filePath: filePath, | ||
fileContent: fileContent, | ||
onWarning: msg => { | ||
console.warn(msg) | ||
}, | ||
onGenerate: ({ code, marker }) => { | ||
fileContent = | ||
fileContent.substr(0, marker.start) + | ||
code + | ||
fileContent.substr(marker.end, fileContent.length) | ||
}, | ||
}) | ||
await generator.generate() | ||
if (replaceFile) { | ||
writeFileSync(filePath, fileContent) | ||
} | ||
return fileContent | ||
} |