Skip to content

Commit

Permalink
fix(api): write files only when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 17, 2020
1 parent e20ac71 commit 80e45c3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/IndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ test('ok', async () => {
'',
)
expect(fileContent).toMatchSnapshot(`generated content of ${humanFilePath}`)
expect(msgs).toMatchSnapshot(`messages of ${humanFilePath}`)
expect(msgs.sort()).toMatchSnapshot(`messages of ${humanFilePath}`)
}
})
2 changes: 1 addition & 1 deletion src/__snapshots__/IndexGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Array [

exports[`ok: messages of /invalid/error 1`] = `
Array [
"[SKIP] Invalid patterns or code generator. (// @index(\\"./*', f => \`export * from '\${f.path}'\`))",
"No @index maker found.",
"[SKIP] Invalid patterns or code generator. (// @index(\\"./*', f => \`export * from '\${f.path}'\`))",
]
`;

Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/generateIndex.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ exports[`many: warningMsgs 1`] = `
Array [
"1.ts: No @index maker found.",
"_8.ts: No @index maker found.",
"国家.ts: No @index maker found.",
"x.ts: No @index maker found.",
"国家.ts: No @index maker found.",
]
`;

Expand Down
4 changes: 2 additions & 2 deletions src/generateIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ test('many', async () => {
expect(res[0].generatedFileContent).toMatchSnapshot(
'res[0].generatedFileContent',
)
expect(successMsgs).toMatchSnapshot('successMsgs')
expect(warningMsgs).toMatchSnapshot('warningMsgs')
expect(successMsgs.sort()).toMatchSnapshot('successMsgs')
expect(warningMsgs.sort()).toMatchSnapshot('warningMsgs')
})
19 changes: 12 additions & 7 deletions src/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ export async function generateIndex(
if (!(await pathExists(filePath))) {
throw new Error(`File not found. <${filePath}>`)
}
let fileContent = (await readFile(filePath)).toString()
const fileContent = (await readFile(filePath)).toString()
let generatedFileContent = fileContent
const generator = new IndexGenerator({
filePath: filePath,
fileContent: fileContent,
onWarning: msg => {
onWarning?.(msg)
},
onGenerate: ({ code, marker }) => {
fileContent =
fileContent.substr(0, marker.start) +
generatedFileContent =
generatedFileContent.substr(0, marker.start) +
code +
fileContent.substr(marker.end, fileContent.length)
generatedFileContent.substr(marker.end, generatedFileContent.length)
},
})
const generateResult = await generator.generate()
if (generateResult !== false && replaceFile) {
await writeFile(filePath, fileContent)
if (
generateResult !== false &&
replaceFile &&
generatedFileContent !== fileContent
) {
await writeFile(filePath, generatedFileContent)
}
return generateResult === false ? false : fileContent
return generateResult === false ? false : generatedFileContent
}

export interface GenerateManyIndexPayload {
Expand Down

0 comments on commit 80e45c3

Please sign in to comment.