-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add sync resource detector api and service and deployment detectors #129
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Not much to say other than LGTM!
|
||
loadJsonFile(path: string): any { | ||
try { | ||
return JSON.parse(fs.readFileSync(path).toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return require(path);
does the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why, but it gives "Error: Cannot find module 'package.json'" when I try to require('package.json')
.
Probably the require
base path is different somehow vs the fs.readFileSync
alternative (which I believe can also bump into some edge cases where the process is not started from the service root)
If you have experience on how to write it so it works, that would be great :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because it is looking for a package named package.json
. ./package.json
tells require
to look for the file.
Both solutions are prone to errors from running the process from the outside of the project root folder.
I don't think there's a "canonical" way to get that without letting the user specify the root OR the path to the main package.json
.
Best way I could come up with to more or less consistently find what you are looking for is this:
const path = require('path');
const findRootPackageJson = () => {
const paths = require.main.paths.map((p) => {
return path.resolve(p, '..', 'package.json');
});
for (const path of paths) {
try {
return {
path,
contents: require(path),
};
} catch (e) {}
}
return {};
}
console.log(require.main);
console.log(findRootPackageJson());
EDIT: Changed the catch syntax a tiny bit to support node 8.
No description provided.