-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default pkg
to the current package
#172
Comments
Sure. To be ESM compatible, it should accept a |
Trying to figure out the shape of this. What about: allowing the |
👍
I disagree. CommonJS users can already just use |
I found this after experiencing the same issue and coming to the same conclusion(s)! 😅 I would love if diff --git a/index.ts b/index.ts
#!/usr/bin/env node
+// eslint-disable-next-line import/default
+import readPkgUp from 'read-pkg-up';
import updateNotifier from 'update-notifier';
-import packageJson from './package.json';
+const { packageJson } = readPkgUp.sync() ?? {};
+
+if (packageJson) {
updateNotifier({ pkg: packageJson }).notify();
+}
diff --git a/package.json b/package.json
+ "read-pkg-up": "7.0.1",
diff --git a/tsconfig.json b/tsconfig.json
- "resolveJsonModule": true, // Include modules imported with `.json` extension. Workarounds, plural, because I'm debating with a much simpler alternative at the moment... diff --git a/package.json b/package.json
"scripts": {
+ "build": "tsc --build && ln -sf ../package.json dist/package.json", Not sure which route to go for. Future-and-less-sleep-deprived-me will make the right decision. |
I'm reading the package information on my own import { readFile } from 'fs/promises'
export const getPackageInfo = async () => {
const pkg = await readFile(new URL('../../package.json', import.meta.url))
return JSON.parse(pkg)
} And using to send this information as import updateNotifier from 'update-notifier'
import { getPackageInfo } from './info.js'
export const autoUpdate = async () => {
const { name, version } = await getPackageInfo()
const option = {
pkg: {
name,
version
},
shouldNotifyInNpmScript: true,
updateCheckInterval: 0
}
const notifier = updateNotifier(option)
await notifier.notify()
} And call it from index.js import { autoUpdate } from './helpers/autoUpdate.js'
await autoUpdate() Sadly is using an |
The
pkg
option must be set with the package'spackage.json
.However this does not work well with ES imports. At the moment, importing JSON files with ES imports is still experimental in Node.js.
Also, hardcoding the path to
package.json
can be tricky when transpiling is involved. For example, my binary file is atsrc/bin.js
, but it is transpiled tobuild/src/bin.js
. If I want to run both files, I need to dynamically find the path topackage.json
with a library likeread-pkg-up
.Adding an option to automatically find the package (with
read-pkg-up
for example) would solve this problem. This option would probably need user to pass__dirname
to be able to find the rightpackage.json
.What do you think?
I can submit a PR if needed.
The text was updated successfully, but these errors were encountered: