Skip to content

Commit

Permalink
feat: add generateIndex api
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 16, 2020
1 parent 898d30e commit f7e0312
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "^10.17.19",
"@types/rimraf": "^3.0.0",
"eslint": "^6",
"haoma": "^1.7.0",
"husky": "^4",
Expand All @@ -88,6 +89,7 @@
"prettier": "^2",
"rimraf": "^3.0.2",
"standard-version": "^6.0.1",
"tempy": "^0.5.0",
"ts-jest": "^25.3.1",
"typescript": "^3",
"vscode": "^1.1.28"
Expand Down
51 changes: 48 additions & 3 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/__snapshots__/IndexGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ exports[`ok: generated content of /cross/index.txt 1`] = `
../scss/b.scss
../scss/c.scss
../scss/index.scss
../sort/1.ts
../sort/001.ts
../sort/1.ts
../sort/2.ts
../sort/11.ts
../sort/884.ts
Expand Down Expand Up @@ -56,8 +56,8 @@ exports[`ok: generated content of /scss/index.scss 1`] = `

exports[`ok: generated content of /sort/index.ts 1`] = `
"// @index('./*.ts', f => \`export * from '\${f.path}'\`)
export * from './1'
export * from './001'
export * from './1'
export * from './2'
export * from './11'
export * from './884'
Expand All @@ -73,8 +73,8 @@ export * from './国家'

exports[`ok: generated content of /sort/index2.ts 1`] = `
"// @index('./*.ts', f => \`export * from '\${f.path}'\`)
export * from './1'
export * from './001'
export * from './1'
export * from './2'
export * from './11'
export * from './884'
Expand Down
35 changes: 35 additions & 0 deletions src/__snapshots__/generateIndex.test.ts.snap
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"
`;
53 changes: 53 additions & 0 deletions src/generateIndex.test.ts
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()
})
30 changes: 30 additions & 0 deletions src/generateIndex.ts
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
}

0 comments on commit f7e0312

Please sign in to comment.