generated from seb-cr/ts-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
133 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import { EOL } from 'os'; | ||
|
||
import { Text } from './text'; | ||
|
||
/** | ||
* Work with a text file. | ||
* | ||
* Opens the file, passes the content to `callback`, then saves it back if any | ||
* changes were made. | ||
* | ||
* Line endings in the content are normalised to `\n`, and returned to the | ||
* OS-specific line ending on save (as defined by `os.EOL`). | ||
* | ||
* @param path File path. | ||
* @param callback Function that does something with the file content. | ||
*/ | ||
export async function withFile( | ||
path: string, | ||
callback: (f: Text) => void | Promise<void>, | ||
): Promise<void> { | ||
const rawContent = (await readFile(path)).toString(); | ||
const originalContent = rawContent | ||
.replace(/\r\n/g, '\r') | ||
.replace(/\r/g, '\n'); | ||
|
||
const f = new Text(originalContent); | ||
await callback(f); | ||
|
||
if (f.content !== originalContent) { | ||
await writeFile(path, f.content.replace(/\n/g, EOL)); | ||
} | ||
} |
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 +1,3 @@ | ||
export * from './file'; | ||
export * from './shell'; | ||
export * from './text'; |
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,20 @@ | ||
/** | ||
* Class for manipulating a block of text. | ||
*/ | ||
export class Text { | ||
/** | ||
* The text content. | ||
*/ | ||
content: string; | ||
|
||
constructor(content: string) { | ||
this.content = content; | ||
} | ||
|
||
/** | ||
* Returns the text split into lines. | ||
*/ | ||
lines(): string[] { | ||
return this.content.split('\n'); | ||
} | ||
} |
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,2 +1,5 @@ | ||
extends: | ||
- '@comicrelief/eslint-config/mixins/mocha' | ||
|
||
rules: | ||
no-param-reassign: off |
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,37 @@ | ||
import { readFile, unlink, writeFile } from 'fs/promises'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { withFile } from '@/src'; | ||
|
||
const TEST_FILE = 'test.tmp'; | ||
|
||
describe('withFile', () => { | ||
before('create test file', async () => { | ||
await writeFile(TEST_FILE, 'hello world'); | ||
}); | ||
|
||
after('delete test file', async () => { | ||
await unlink(TEST_FILE); | ||
}); | ||
|
||
it('should pass file content to callback', async () => { | ||
let wasCalled = false; | ||
|
||
await withFile(TEST_FILE, (f) => { | ||
expect(f.content).to.equal('hello world'); | ||
wasCalled = true; | ||
}); | ||
|
||
expect(wasCalled, 'callback was not called').to.be.true; | ||
}); | ||
|
||
it('should write back any changes made', async () => { | ||
await withFile(TEST_FILE, (f) => { | ||
f.content = 'goodbye'; | ||
}); | ||
|
||
const newContent = (await readFile(TEST_FILE)).toString(); | ||
expect(newContent).to.equal('goodbye'); | ||
}); | ||
}); |
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,25 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { Text } from '@/src'; | ||
|
||
describe('Text', () => { | ||
describe('content', () => { | ||
it('should get content', () => { | ||
const text = new Text('test'); | ||
expect(text.content).to.equal('test'); | ||
}); | ||
|
||
it('should set content', () => { | ||
const text = new Text('foo'); | ||
text.content = 'bar'; | ||
expect(text.content).to.equal('bar'); | ||
}); | ||
}); | ||
|
||
describe('lines', () => { | ||
it('should return an array of lines', () => { | ||
const text = new Text('one\ntwo\nthree'); | ||
expect(text.lines()).to.deep.equal(['one', 'two', 'three']); | ||
}); | ||
}); | ||
}); |