-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
38 lines (31 loc) · 1.44 KB
/
function.js
File metadata and controls
38 lines (31 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { resolve, basename } from 'node:path'
import { writeJSONPrettySync } from '@dr-js/core/module/node/fs/File.js'
import { getFileList } from '@dr-js/core/module/node/fs/Directory.js'
import { modulePathHack } from '@dr-js/core/bin/function.js'
// HACK: add `@dr-js/dev` to internal `modulePaths` to allow require
// `.../npm/node_modules/@dr-js/*/bin/function.js` + `../../../../` = `.../npm/node_modules/` // allow this and related module to resolve
// `.../.npm/_npx/####/lib/node_modules/@dr-js/*/bin/function.js` + `../../../../` = `.../.npm/_npx/####/lib/node_modules/` // allow this and related module to resolve
const patchModulePath = () => modulePathHack(resolve(module.filename, '../../../../'))
const NAME_PACK_EXPORT = 'EXPORT'
const NAME_PACK_EXPORT_INIT_JSON = 'INIT.json'
const getFromPackExport = (pathPackage) => (...args) => resolve(pathPackage, NAME_PACK_EXPORT, ...args)
const writePackExportInitJSON = async ({
pathPackage,
fromPackExport = getFromPackExport(pathPackage)
}) => {
const initFilePrefix = fromPackExport('INIT#')
writeJSONPrettySync(
fromPackExport(NAME_PACK_EXPORT_INIT_JSON),
(await getFileList(fromPackExport()))
.filter((path) => path.startsWith(initFilePrefix))
.map((path) => [
basename(path), // relative source
path.slice(initFilePrefix.length).replace(/#/g, '/') // relative output
])
)
}
export {
patchModulePath,
getFromPackExport,
writePackExportInitJSON
}