Skip to content

Commit 24f78da

Browse files
authored
Merge pull request #63 from matzkoh/sort-package-json-v2
2 parents 319fc41 + 0dfc9f5 commit 24f78da

File tree

8 files changed

+369
-141
lines changed

8 files changed

+369
-141
lines changed

cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": "0.2",
33
"dictionaries": [],
4-
"words": ["packagejson"]
4+
"words": ["packagejson", "synckit"]
55
}

lib/esm-proxy/chain.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function chain(fn, path = []) {
2+
return new Proxy(() => {}, {
3+
get: (_, prop) => chain(fn, [...path, prop]),
4+
apply: (_, __, args) => fn(path, args),
5+
})
6+
}
7+
8+
module.exports = chain

lib/esm-proxy/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { createSyncFn } = require('synckit')
2+
const chain = require('./chain')
3+
4+
const exec = createSyncFn(require.resolve('./worker'))
5+
6+
function esmProxy(id) {
7+
return chain(exec.bind(null, id))
8+
}
9+
10+
module.exports = esmProxy

lib/esm-proxy/worker.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { runAsWorker } = require('synckit')
2+
3+
runAsWorker(async (id, path, args) => {
4+
const root = await import(id)
5+
const method = path.pop()
6+
const receiver = path.reduce((acc, cur) => acc[cur], root)
7+
return receiver[method](...args)
8+
})

lib/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
const requireSafe = require('./require')
2-
const { parsers } = requireSafe('prettier/parser-babel') || requireSafe('prettier/parser-babylon')
3-
const sortPackageJson = require('sort-package-json')
2+
const sortPackageJson = require('./sort-package-json')
3+
4+
const { parsers } =
5+
requireSafe('prettier/parser-babel') ||
6+
// istanbul ignore next
7+
requireSafe('prettier/parser-babylon')
8+
49
const parser = parsers['json-stringify']
510

611
/** @type {import('prettier').Plugin['parsers']} */
712
exports.parsers = {
813
'json-stringify': {
914
...parser,
1015
preprocess(text, options) {
16+
// istanbul ignore next
1117
if (parser.preprocess) {
1218
text = parser.preprocess(text, options)
1319
}
1420

1521
return options.filepath && /(^|\\|\/)package\.json$/.test(options.filepath)
16-
? sortPackageJson(text)
22+
? sortPackageJson.default(text)
1723
: text
1824
},
1925
},

lib/sort-package-json.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const esmProxy = require('./esm-proxy')
2+
3+
/** @type {import('sort-package-json')} */
4+
module.exports = esmProxy('sort-package-json')

0 commit comments

Comments
 (0)