Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 22, 2024
1 parent 1fe76d9 commit b90307b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
10 changes: 10 additions & 0 deletions examples/format-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as fs from "node:fs/promises";
import * as prettier from "prettier";
import makeSynchronized from "make-synchronized";

export default makeSynchronized(import.meta, async function formatFile(file) {
const config = await prettier.resolveConfig(file);
const content = await fs.readFile(file, "utf8");
const formatted = await prettier.format(content, {...config,filepath: file});
await fs.writeFile(file, formatted);
});
9 changes: 9 additions & 0 deletions examples/run-format-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as url from "node:url";
import formatFile from './format-file.js'

console.log(
formatFile(
url.fileURLToPath(new URL('../readme.md', import.meta.url))

)
)
4 changes: 2 additions & 2 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

const { makeSynchronizedModule } = require("make-synchronized");
const { makeModuleSynchronized } = require("make-synchronized");

function createSynchronizedPrettier({ prettierEntry }) {
return makeSynchronizedModule(prettierEntry);
return makeModuleSynchronized(prettierEntry);
}

module.exports = createSynchronizedPrettier({ prettierEntry: "prettier" });
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@
]
},
"peerDependencies": {
"prettier": "^3.0.0"
"prettier": "*"
},
"devDependencies": {
"@types/node": "20.4.1",
"c8": "8.0.0",
"@types/node": "20.11.5",
"c8": "9.1.0",
"npm-run-all": "4.1.5",
"prettier": "3.0.0"
"prettier": "3.2.4"
},
"packageManager": "[email protected]",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"make-synchronized": "^0.0.3"
"make-synchronized": "^0.2.1"
}
}
21 changes: 8 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,19 @@ This package is a simple wrapper of [`make-synchronized`](https://github.com/fis
For more complex use cases, it more reasonable to extract into a separate file, and run with [`make-synchronized`](https://github.com/fisker/make-synchronized), example

```js
// runs-in-worker.js
import * as fs from "node:fs/promises";
import * as prettier from "prettier";
import makeSynchronized from "make-synchronized";

export async function formatFile(file) {
const config = await prettier.resolveConfig(filepath);
export default makeSynchronized(import.meta, async function formatFile(file) {
const config = await prettier.resolveConfig(file);
const content = await fs.readFile(file, "utf8");
const formatted = await prettier.format(content, config);
const formatted = await prettier.format(content, {
...config,
filepath: file,
});
await fs.writeFile(file, formatted);
}
```

```js
import makeSynchronized from "make-synchronized";

const {
formatFile, // Synchronize version of `formatFile` in `runs-in-worker.js`
} = makeSynchronized(new URL("./runs-in-worker.js", import.meta.url));
});
```

### `createSynchronizedPrettier(options)`
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"checkJs": true,
"noEmit": true,
"target": "esnext",
"module": "NodeNext"
"module": "NodeNext",
},
"files": ["index.cjs"]
"files": ["index.cjs"],
}

0 comments on commit b90307b

Please sign in to comment.