-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for --import with register()
- Loading branch information
Showing
8 changed files
with
59 additions
and
29 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import {readFile} from 'fs/promises' | ||
import {dirname, isAbsolute, join} from 'path' | ||
import {argv, cwd} from 'process' | ||
import {cwd} from 'process' | ||
|
||
let pkgJson = await (async () => { | ||
let curDir, upDir = isAbsolute(argv[1] ?? '') ? argv[1] : cwd() | ||
let warn = (field, desc) => console.warn('⚠️ \x1b[33m%s\x1b[0m', | ||
`Warning: The package.json field 'extensionless.${field}' must be ${desc}! Using the default value instead...` | ||
) | ||
|
||
let getPkgJson = async argv1 => { | ||
let path = isAbsolute(argv1 ?? '') ? argv1 : cwd() | ||
|
||
do { | ||
try { | ||
return JSON.parse(await readFile(join(curDir = upDir, 'package.json'), 'utf8')) | ||
return JSON.parse(await readFile(join(path, 'package.json'), 'utf8')) | ||
} catch (e) { | ||
if (!['ENOTDIR', 'ENOENT', 'EISDIR'].includes(e.code)) { | ||
throw new Error('Cannot retrieve package.json', {cause: e}) | ||
} | ||
} | ||
} while (curDir !== (upDir = dirname(curDir))) | ||
})() | ||
|
||
let warn = (field, desc) => console.warn('⚠️ \x1b[33m%s\x1b[0m', `Warning: The package.json field 'extensionless.${field}' must be ${desc}! Using the default value instead...`) | ||
} while (path !== (path = dirname(path))) | ||
} | ||
|
||
let defaults = { | ||
lookFor: ['js'] | ||
}, { | ||
lookFor | ||
} = {...defaults, ...pkgJson?.extensionless} | ||
export async function getConfig({argv1} = {}) { | ||
let defaults = { | ||
lookFor: ['js'] | ||
}, { | ||
lookFor | ||
} = {...defaults, ...(await getPkgJson(argv1))?.extensionless} | ||
|
||
Array.isArray(lookFor) && lookFor.length && lookFor.every(a => typeof a === 'string' && /^[a-z]+\w*$/i.test(a)) || ( | ||
lookFor = defaults.lookFor, warn('lookFor', 'an array of alphanumeric strings') | ||
) | ||
Array.isArray(lookFor) && lookFor.length && lookFor.every(a => typeof a === 'string' && /^[a-z]+\w*$/i.test(a)) || ( | ||
lookFor = defaults.lookFor, warn('lookFor', 'an array of alphanumeric strings') | ||
) | ||
|
||
export { | ||
lookFor | ||
return { | ||
lookFor | ||
} | ||
} |
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,4 @@ | ||
import {register} from 'module' | ||
import {argv} from 'process' | ||
|
||
register('./index.js', import.meta.url, {data: {argv1: argv[1]}}) |
File renamed without changes.