Skip to content

Commit

Permalink
Convert to esm/typescript. (#216)
Browse files Browse the repository at this point in the history
* add typescript configuration

* drop 'use strict' from tests

* convert tests to esm

* migrate tests to vitest

* convert implementation to typescript

* drop node 14 support

* Update README.md

* Update package.json

* Update lockfile

---------

Co-authored-by: Simon Boudrias <admin@simonboudrias.com>
  • Loading branch information
mshima and SBoudrias authored Apr 17, 2023
1 parent 5445f42 commit bf832f5
Show file tree
Hide file tree
Showing 75 changed files with 4,097 additions and 9,988 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
coverage
28 changes: 14 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [18.x, 16.x, 14.x]
node-version: [18.x, 16.x]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install and test
run: |
node --version
npm ci
npm test
- name: Upload code coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/coverage-final.json
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install and test
run: |
node --version
npm ci
npm test
- name: Upload code coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/coverage-final.json
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
coverage/
dist/
.settings
.project
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
coverage
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"endOfLine": "auto",
"singleQuote": true,
"printWidth": 90
"printWidth": 120
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ File edition helpers working on top of [mem-fs](https://github.com/SBoudrias/mem
## Usage

```js
import memFs from 'mem-fs';
import { create as createMemFs } from 'mem-fs';
import editor from 'mem-fs-editor';

const store = memFs.create();
const store = createMemFs();
const fs = editor.create(store);

fs.write('somefile.js', 'var a = 1;');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`#commit() should match snapshot 1`] = `
exports[`#commit() > should match snapshot 1`] = `
{
"file-0.txt": {
"contents": "foo",
Expand Down Expand Up @@ -405,7 +405,7 @@ exports[`#commit() should match snapshot 1`] = `
}
`;

exports[`#copy() and #commit() should match snapshot 1`] = `
exports[`#copy() and #commit() > should match snapshot 1`] = `
{
"[file].txt": {
"contents": "foo
Expand Down Expand Up @@ -461,7 +461,7 @@ exports[`#copy() and #commit() should match snapshot 1`] = `
},
"file.json": {
"contents": "{
"foo": "bar"
\\"foo\\": \\"bar\\"
}
",
"stateCleared": "modified",
Expand All @@ -474,7 +474,7 @@ exports[`#copy() and #commit() should match snapshot 1`] = `
}
`;

exports[`#copyTpl() and #commit() should match snapshot 1`] = `
exports[`#copyTpl() and #commit() > should match snapshot 1`] = `
{
"[file].txt": {
"contents": "foo
Expand Down Expand Up @@ -531,7 +531,7 @@ exports[`#copyTpl() and #commit() should match snapshot 1`] = `
},
"file.json": {
"contents": "{
"foo": "bar"
\\"foo\\": \\"bar\\"
}
",
"stateCleared": "modified",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`#dump() should match snapshot 1`] = `
exports[`#dump() > should match snapshot 1`] = `
{
"foo/committed": {
"contents": "committed",
Expand Down Expand Up @@ -31,7 +31,7 @@ exports[`#dump() should match snapshot 1`] = `
}
`;

exports[`#dump() with a glob pattern should return files that matches the pattern and have state or stateCleared 1`] = `
exports[`#dump() > with a glob pattern > should return files that matches the pattern and have state or stateCleared 1`] = `
{
"foo/committed": {
"contents": "committed",
Expand Down
47 changes: 0 additions & 47 deletions __tests__/append-tpl.js

This file was deleted.

44 changes: 44 additions & 0 deletions __tests__/append-tpl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, beforeEach, it, expect } from 'vitest';
import os from 'os';
import { type MemFsEditor, create } from '../lib/index.js';
import { create as createMemFs } from 'mem-fs';
import { getFixture } from './fixtures.js';

describe('#appendTpl()', () => {
let fs: MemFsEditor;

beforeEach(() => {
fs = create(createMemFs());
});

it('appends to file and processes contents as underscore template', () => {
const filepath = getFixture('file-a.txt');
const orginalContent = fs.read(filepath)!.toString();
const contentPath = getFixture('file-tpl.txt');
const contents = fs.read(contentPath)!;
fs.appendTpl(filepath, contents, {
name: 'bar',
});
expect(fs.read(filepath)).toBe(orginalContent + 'bar' + os.EOL);
});

it('allows setting custom template delimiters', () => {
const filepath = getFixture('file-a.txt');
const orginalContent = fs.read(filepath)!.toString();
const contentPath = getFixture('file-tpl-custom-delimiter.txt');
const contents = fs.read(contentPath)!;
fs.appendTpl(filepath, contents, { name: 'bar' }, { delimiter: '?' });
expect(fs.read(filepath)).toBe(orginalContent + 'bar' + os.EOL);
});

it('throws an exception when no template data passed', () => {
const f = () => {
const filepath = getFixture('file-a.txt');
const contentPath = getFixture('file-tpl.txt');
const contents = fs.read(contentPath)!;
fs.appendTpl(filepath, contents);
};

expect(f).toThrow(ReferenceError);
});
});
15 changes: 7 additions & 8 deletions __tests__/append.js → __tests__/append.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

const os = require('os');
const editor = require('..');
const memFs = require('mem-fs');
import { describe, beforeEach, it, expect } from 'vitest';
import os from 'os';
import { MemFsEditor, create } from '../lib/index.js';
import { create as createMemFs } from 'mem-fs';

describe('#write()', () => {
let store;
let fs;
let fs: MemFsEditor;

beforeEach(() => {
store = memFs.create();
fs = editor.create(store);
store = createMemFs();
fs = create(store);
});

it('appends new content to new file', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

const filesystem = require('fs');
const os = require('os');
const path = require('path');
const memFs = require('mem-fs');
const sinon = require('sinon');
const editor = require('..');
const { STATE, STATE_MODIFIED, STATE_DELETED } = require('../lib/state');
import { describe, beforeEach, it, expect, afterEach } from 'vitest';
import filesystem from 'fs';
import os from 'os';
import path from 'path';
import { create as createMemFs } from 'mem-fs';
import sinon from 'sinon';
import { type MemFsEditor, type MemFsEditorFile, create } from '../lib/index.js';

const rmSync = filesystem.rmSync || filesystem.rmdirSync;

Expand All @@ -22,22 +20,22 @@ describe('#commitFileAsync()', () => {
const outputDir = path.join(outputRoot, 'output');
const filename = path.join(outputDir, 'file.txt');
const filenameNew = path.join(outputDir, 'file-new.txt');
let newFile;
let newFile: MemFsEditorFile;

let store;
let fs;
let fs: MemFsEditor;

beforeEach(() => {
store = memFs.create();
store = createMemFs();
sinon.spy(store, 'add');

fs = editor.create(store);
fs = create(store);
fs.write(filename, 'foo');

newFile = {
path: filenameNew,
contents: Buffer.from('bar'),
[STATE]: STATE_MODIFIED,
state: 'modified',
};

expect(store.add.callCount).toEqual(1);
Expand Down Expand Up @@ -67,7 +65,7 @@ describe('#commitFileAsync()', () => {
it("doesn't commit an unmodified file", async () => {
await fs.commitFileAsync({
...newFile,
[STATE]: undefined,
state: undefined,
});
expect(filesystem.existsSync(filenameNew)).toBe(false);
});
Expand All @@ -88,7 +86,7 @@ describe('#commitFileAsync()', () => {
filesystem.writeFileSync(filenameNew, 'foo');
await fs.commitFileAsync({
...newFile,
[STATE]: STATE_DELETED,
state: 'deleted',
});
expect(filesystem.existsSync(filenameNew)).toBe(false);
});
Expand Down
Loading

0 comments on commit bf832f5

Please sign in to comment.