Skip to content

Commit 7070137

Browse files
authored
Merge pull request #83 from reflector-six/allow-file-postprocessors
Allow file postprocessors like Prettier
2 parents 9bb5cbe + 193f693 commit 7070137

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,18 @@ Returns `DtsContent` instance.
143143
### class DtsContent
144144
DtsContent instance has `*.d.ts` content, final output path, and function to write file.
145145

146-
#### `writeFile() => Promise(dtsContent)`
147-
Writes the DtsContent instance's content to a file.
146+
#### `writeFile(postprocessor) => Promise(dtsContent)`
147+
Writes the DtsContent instance's content to a file. Returns the DtsContent instance.
148148

149-
* `dtsContent`: the DtsContent instance itself.
149+
* `postprocessor` (optional): a function that takes the formatted definition string and returns a modified string that will be the final content written to the file.
150+
151+
You could use this, for example, to pass generated definitions through a formatter like Prettier, or to add a comment to the top of generated files:
152+
153+
```js
154+
dtsContent.writeFile(
155+
definition => `// Generated automatically, do not edit\n${definition}`
156+
)
157+
```
150158

151159
#### `tokens`
152160
An array of tokens retrieved from input CSS file.

src/dts-content.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ export class DtsContent {
6868
return path.join(this.rootDir, this.searchDir, this.rInputPath);
6969
}
7070

71-
public async writeFile(): Promise<void> {
71+
public async writeFile(postprocessor = (formatted: string) => formatted): Promise<void> {
72+
const finalOutput = postprocessor(this.formatted);
73+
7274
const outPathDir = path.dirname(this.outputFilePath);
7375
if(!isThere(outPathDir)) {
7476
mkdirp.sync(outPathDir);
7577
}
7678

77-
await writeFile(this.outputFilePath, this.formatted, 'utf8');
79+
await writeFile(this.outputFilePath, finalOutput, 'utf8');
7880
}
7981
}
8082

test/dts-creator.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ export = styles;
180180
});
181181

182182
describe('#writeFile', () => {
183+
it('accepts a postprocessor function', done => {
184+
new DtsCreator()
185+
.create('test/testStyle.css')
186+
.then(content => {
187+
return content.writeFile(
188+
formatted => `// this banner was added to the .d.ts file automatically.\n${formatted}`
189+
);
190+
})
191+
.then(() => {
192+
done();
193+
});
194+
});
195+
183196
it('writes a file', done => {
184197
new DtsCreator()
185198
.create('test/testStyle.css')

0 commit comments

Comments
 (0)