Initial commit
This commit is contained in:
commit
f1548f10cc
7585 files changed
+1411120
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
All packages under `src/` are licensed according to the terms in
|
||||
their respective `LICENSE` or `LICENSE.md` files.
|
||||
|
||||
The remainder of this project is licensed under the Blue Oak
|
||||
Model License, as follows:
|
||||
|
||||
-----
|
||||
|
||||
# Blue Oak Model License
|
||||
|
||||
Version 1.0.0
|
||||
|
||||
## Purpose
|
||||
|
||||
This license gives everyone as much permission to work with
|
||||
this software as possible, while protecting contributors
|
||||
from liability.
|
||||
|
||||
## Acceptance
|
||||
|
||||
In order to receive this license, you must agree to its
|
||||
rules. The rules of this license are both obligations
|
||||
under that agreement and conditions to your license.
|
||||
You must not do anything with this software that triggers
|
||||
a rule that you cannot or will not follow.
|
||||
|
||||
## Copyright
|
||||
|
||||
Each contributor licenses you to do everything with this
|
||||
software that would otherwise infringe that contributor's
|
||||
copyright in it.
|
||||
|
||||
## Notices
|
||||
|
||||
You must ensure that everyone who gets a copy of
|
||||
any part of this software from you, with or without
|
||||
changes, also gets the text of this license or a link to
|
||||
<https://blueoakcouncil.org/license/1.0.0>.
|
||||
|
||||
## Excuse
|
||||
|
||||
If anyone notifies you in writing that you have not
|
||||
complied with [Notices](#notices), you can keep your
|
||||
license by taking all practical steps to comply within 30
|
||||
days after the notice. If you do not do so, your license
|
||||
ends immediately.
|
||||
|
||||
## Patent
|
||||
|
||||
Each contributor licenses you to do everything with this
|
||||
software that would otherwise infringe any patent claims
|
||||
they can license or become able to license.
|
||||
|
||||
## Reliability
|
||||
|
||||
No contributor can revoke this license.
|
||||
|
||||
## No Liability
|
||||
|
||||
***As far as the law allows, this software comes as is,
|
||||
without any warranty or condition, and no contributor
|
||||
will be liable to anyone for any damages related to this
|
||||
software or this license, under any kind of legal claim.***
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# package-json-from-dist
|
||||
|
||||
Sometimes you want to load the `package.json` into your
|
||||
TypeScript program, and it's tempting to just `import
|
||||
'../package.json'`, since that seems to work.
|
||||
|
||||
However, this requires `tsc` to make an entire copy of your
|
||||
`package.json` file into the `dist` folder, which is a problem if
|
||||
you're using something like
|
||||
[tshy](https://github.com/isaacs/tshy), which uses the
|
||||
`package.json` file in dist for another purpose. Even when that
|
||||
does work, it's asking the module system to do a bunch of extra
|
||||
fs system calls, just to load a version number or something. (See
|
||||
[this issue](https://github.com/isaacs/tshy/issues/61).)
|
||||
|
||||
This module helps by just finding the package.json file
|
||||
appropriately, and reading and parsing it in the most normal
|
||||
fashion.
|
||||
|
||||
## Caveats
|
||||
|
||||
This _only_ works if your code builds into a target folder called
|
||||
`dist`, which is in the root of the package. It also requires
|
||||
that you do not have a folder named `node_modules` anywhere
|
||||
within your dev environment, or else it'll get the wrong answers
|
||||
there. (But, at least, that'll be in dev, so you're pretty likely
|
||||
to notice.)
|
||||
|
||||
If you build to some other location, then you'll need a different
|
||||
approach. (Feel free to fork this module and make it your own, or
|
||||
just put the code right inline, there's not much of it.)
|
||||
|
||||
## USAGE
|
||||
|
||||
```js
|
||||
// src/index.ts
|
||||
import {
|
||||
findPackageJson,
|
||||
loadPackageJson,
|
||||
} from 'package-json-from-dist'
|
||||
|
||||
const pj = findPackageJson(import.meta.url)
|
||||
console.log(`package.json found at ${pj}`)
|
||||
|
||||
const pkg = loadPackageJson(import.meta.url)
|
||||
console.log(`Hello from ${pkg.name}@${pkg.version}`)
|
||||
```
|
||||
|
||||
If your module is not directly in the `./src` folder, then you need
|
||||
to specify the path that you would expect to find the
|
||||
`package.json` when it's _not_ built to the `dist` folder.
|
||||
|
||||
```js
|
||||
// src/components/something.ts
|
||||
import {
|
||||
findPackageJson,
|
||||
loadPackageJson,
|
||||
} from 'package-json-from-dist'
|
||||
|
||||
const pj = findPackageJson(import.meta.url, '../../package.json')
|
||||
console.log(`package.json found at ${pj}`)
|
||||
|
||||
const pkg = loadPackageJson(import.meta.url, '../../package.json')
|
||||
console.log(`Hello from ${pkg.name}@${pkg.version}`)
|
||||
```
|
||||
|
||||
When running from CommmonJS, use `__filename` instead of
|
||||
`import.meta.url`.
|
||||
|
||||
```js
|
||||
// src/index.cts
|
||||
import {
|
||||
findPackageJson,
|
||||
loadPackageJson,
|
||||
} from 'package-json-from-dist'
|
||||
|
||||
const pj = findPackageJson(__filename)
|
||||
console.log(`package.json found at ${pj}`)
|
||||
|
||||
const pkg = loadPackageJson(__filename)
|
||||
console.log(`Hello from ${pkg.name}@${pkg.version}`)
|
||||
```
|
||||
|
||||
Since [tshy](https://github.com/isaacs/tshy) builds _both_
|
||||
CommonJS and ESM by default, you may find that you need a
|
||||
CommonJS override and some `//@ts-ignore` magic to make it work.
|
||||
|
||||
`src/pkg.ts`:
|
||||
|
||||
```js
|
||||
import {
|
||||
findPackageJson,
|
||||
loadPackageJson,
|
||||
} from 'package-json-from-dist'
|
||||
//@ts-ignore
|
||||
export const pkg = loadPackageJson(import.meta.url)
|
||||
//@ts-ignore
|
||||
export const pj = findPackageJson(import.meta.url)
|
||||
```
|
||||
|
||||
`src/pkg-cjs.cts`:
|
||||
|
||||
```js
|
||||
import {
|
||||
findPackageJson,
|
||||
loadPackageJson,
|
||||
} from 'package-json-from-dist'
|
||||
export const pkg = loadPackageJson(__filename)
|
||||
export const pj = findPackageJson(__filename)
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Find the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(__filename)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*/
|
||||
export declare const findPackageJson: (from: string | URL, pathFromSrc?: string) => string;
|
||||
/**
|
||||
* Load the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(__filename)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*/
|
||||
export declare const loadPackageJson: (from: string | URL, pathFromSrc?: string) => any;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,eAAe,SACpB,MAAM,GAAG,GAAG,gBACL,MAAM,KAClB,MAsCF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,eAAe,SACpB,MAAM,GAAG,GAAG,gBACL,MAAM,QAEiD,CAAA"}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.loadPackageJson = exports.findPackageJson = void 0;
|
||||
const node_fs_1 = require("node:fs");
|
||||
const node_path_1 = require("node:path");
|
||||
const node_url_1 = require("node:url");
|
||||
const NM = `${node_path_1.sep}node_modules${node_path_1.sep}`;
|
||||
const STORE = `.store${node_path_1.sep}`;
|
||||
const PKG = `${node_path_1.sep}package${node_path_1.sep}`;
|
||||
const DIST = `${node_path_1.sep}dist${node_path_1.sep}`;
|
||||
/**
|
||||
* Find the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(__filename)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*/
|
||||
const findPackageJson = (from, pathFromSrc = '../package.json') => {
|
||||
const f = typeof from === 'object' || from.startsWith('file://') ?
|
||||
(0, node_url_1.fileURLToPath)(from)
|
||||
: from;
|
||||
const __dirname = (0, node_path_1.dirname)(f);
|
||||
const nms = __dirname.lastIndexOf(NM);
|
||||
if (nms !== -1) {
|
||||
// inside of node_modules. find the dist directly under package name.
|
||||
const nm = __dirname.substring(0, nms + NM.length);
|
||||
const pkgDir = __dirname.substring(nms + NM.length);
|
||||
// affordance for yarn berry, which puts package contents in
|
||||
// '.../node_modules/.store/${id}-${hash}/package/...'
|
||||
if (pkgDir.startsWith(STORE)) {
|
||||
const pkg = pkgDir.indexOf(PKG, STORE.length);
|
||||
if (pkg) {
|
||||
return (0, node_path_1.resolve)(nm, pkgDir.substring(0, pkg + PKG.length), 'package.json');
|
||||
}
|
||||
}
|
||||
const pkgName = pkgDir.startsWith('@') ?
|
||||
pkgDir.split(node_path_1.sep, 2).join(node_path_1.sep)
|
||||
: String(pkgDir.split(node_path_1.sep)[0]);
|
||||
return (0, node_path_1.resolve)(nm, pkgName, 'package.json');
|
||||
}
|
||||
else {
|
||||
// see if we are in a dist folder.
|
||||
const d = __dirname.lastIndexOf(DIST);
|
||||
if (d !== -1) {
|
||||
return (0, node_path_1.resolve)(__dirname.substring(0, d), 'package.json');
|
||||
}
|
||||
else {
|
||||
return (0, node_path_1.resolve)(__dirname, pathFromSrc);
|
||||
}
|
||||
}
|
||||
};
|
||||
exports.findPackageJson = findPackageJson;
|
||||
/**
|
||||
* Load the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(__filename)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*/
|
||||
const loadPackageJson = (from, pathFromSrc = '../package.json') => JSON.parse((0, node_fs_1.readFileSync)((0, exports.findPackageJson)(from, pathFromSrc), 'utf8'));
|
||||
exports.loadPackageJson = loadPackageJson;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AACtC,yCAAiD;AACjD,uCAAwC;AAExC,MAAM,EAAE,GAAG,GAAG,eAAG,eAAe,eAAG,EAAE,CAAA;AACrC,MAAM,KAAK,GAAG,SAAS,eAAG,EAAE,CAAA;AAC5B,MAAM,GAAG,GAAG,GAAG,eAAG,UAAU,eAAG,EAAE,CAAA;AACjC,MAAM,IAAI,GAAG,GAAG,eAAG,OAAO,eAAG,EAAE,CAAA;AAE/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACI,MAAM,eAAe,GAAG,CAC7B,IAAkB,EAClB,cAAsB,iBAAiB,EAC/B,EAAE;IACV,MAAM,CAAC,GACL,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC;QACtD,IAAA,wBAAa,EAAC,IAAI,CAAC;QACrB,CAAC,CAAC,IAAI,CAAA;IACR,MAAM,SAAS,GAAG,IAAA,mBAAO,EAAC,CAAC,CAAC,CAAA;IAE5B,MAAM,GAAG,GAAG,SAAS,CAAC,WAAW,CAAC,EAAE,CAAC,CAAA;IACrC,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;QACf,qEAAqE;QACrE,MAAM,EAAE,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,CAAA;QAClD,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;;;;;;;;;GA0CG;AACI,MAAM,eAAe,GAAG,CAC7B,IAAkB,EAClB,cAAsB,iBAAiB,EACvC,EAAE,CACF,IAAI,CAAC,KAAK,CAAC,IAAA,sBAAY,EAAC,IAAA,uBAAe,EAAC,IAAI,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,CAAC,CAAA;AAJzD,QAAA,eAAe,mBAI0C","sourcesContent":["import { readFileSync } from 'node:fs'\nimport { dirname, resolve, sep } from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst NM = `${sep}node_modules${sep}`\nconst STORE = `.store${sep}`\nconst PKG = `${sep}package${sep}`\nconst DIST = `${sep}dist${sep}`\n\n/**\n * Find the package.json file, either from a TypeScript file somewhere not\n * in a 'dist' folder, or a built and/or installed 'dist' folder.\n *\n * Note: this *only* works if you build your code into `'./dist'`, and that the\n * source path does not also contain `'dist'`! If you don't build into\n * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will\n * not work properly!\n *\n * The default `pathFromSrc` option assumes that the calling code lives one\n * folder below the root of the package. Otherwise, it must be specified.\n *\n * Example:\n *\n * ```ts\n * // src/index.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url)\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * If the caller is deeper within the project source, then you must provide\n * the appropriate fallback path:\n *\n * ```ts\n * // src/components/something.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url, '../../package.json')\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * When running from CommmonJS, use `__filename` instead of `import.meta.url`\n *\n * ```ts\n * // src/index.cts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(__filename)\n * console.log(`package.json found at ${pj}`)\n * ```\n */\nexport const findPackageJson = (\n from: string | URL,\n pathFromSrc: string = '../package.json',\n): string => {\n const f =\n typeof from === 'object' || from.startsWith('file://') ?\n fileURLToPath(from)\n : from\n const __dirname = dirname(f)\n\n const nms = __dirname.lastIndexOf(NM)\n if (nms !== -1) {\n // inside of node_modules. find the dist directly under package name.\n const nm = __dirname.substring(0, nms + NM.length)\n const pkgDir = __dirname.substring(nms + NM.length)\n // affordance for yarn berry, which puts package contents in\n // '.../node_modules/.store/${id}-${hash}/package/...'\n if (pkgDir.startsWith(STORE)) {\n const pkg = pkgDir.indexOf(PKG, STORE.length)\n if (pkg) {\n return resolve(\n nm,\n pkgDir.substring(0, pkg + PKG.length),\n 'package.json',\n )\n }\n }\n const pkgName =\n pkgDir.startsWith('@') ?\n pkgDir.split(sep, 2).join(sep)\n : String(pkgDir.split(sep)[0])\n return resolve(nm, pkgName, 'package.json')\n } else {\n // see if we are in a dist folder.\n const d = __dirname.lastIndexOf(DIST)\n if (d !== -1) {\Line truncated
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "commonjs"
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Find the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(__filename)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*/
|
||||
export declare const findPackageJson: (from: string | URL, pathFromSrc?: string) => string;
|
||||
/**
|
||||
* Load the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(__filename)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*/
|
||||
export declare const loadPackageJson: (from: string | URL, pathFromSrc?: string) => any;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,eAAe,SACpB,MAAM,GAAG,GAAG,gBACL,MAAM,KAClB,MAsCF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,eAAe,SACpB,MAAM,GAAG,GAAG,gBACL,MAAM,QAEiD,CAAA"}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, resolve, sep } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
const NM = `${sep}node_modules${sep}`;
|
||||
const STORE = `.store${sep}`;
|
||||
const PKG = `${sep}package${sep}`;
|
||||
const DIST = `${sep}dist${sep}`;
|
||||
/**
|
||||
* Find the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { findPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = findPackageJson(__filename)
|
||||
* console.log(`package.json found at ${pj}`)
|
||||
* ```
|
||||
*/
|
||||
export const findPackageJson = (from, pathFromSrc = '../package.json') => {
|
||||
const f = typeof from === 'object' || from.startsWith('file://') ?
|
||||
fileURLToPath(from)
|
||||
: from;
|
||||
const __dirname = dirname(f);
|
||||
const nms = __dirname.lastIndexOf(NM);
|
||||
if (nms !== -1) {
|
||||
// inside of node_modules. find the dist directly under package name.
|
||||
const nm = __dirname.substring(0, nms + NM.length);
|
||||
const pkgDir = __dirname.substring(nms + NM.length);
|
||||
// affordance for yarn berry, which puts package contents in
|
||||
// '.../node_modules/.store/${id}-${hash}/package/...'
|
||||
if (pkgDir.startsWith(STORE)) {
|
||||
const pkg = pkgDir.indexOf(PKG, STORE.length);
|
||||
if (pkg) {
|
||||
return resolve(nm, pkgDir.substring(0, pkg + PKG.length), 'package.json');
|
||||
}
|
||||
}
|
||||
const pkgName = pkgDir.startsWith('@') ?
|
||||
pkgDir.split(sep, 2).join(sep)
|
||||
: String(pkgDir.split(sep)[0]);
|
||||
return resolve(nm, pkgName, 'package.json');
|
||||
}
|
||||
else {
|
||||
// see if we are in a dist folder.
|
||||
const d = __dirname.lastIndexOf(DIST);
|
||||
if (d !== -1) {
|
||||
return resolve(__dirname.substring(0, d), 'package.json');
|
||||
}
|
||||
else {
|
||||
return resolve(__dirname, pathFromSrc);
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Load the package.json file, either from a TypeScript file somewhere not
|
||||
* in a 'dist' folder, or a built and/or installed 'dist' folder.
|
||||
*
|
||||
* Note: this *only* works if you build your code into `'./dist'`, and that the
|
||||
* source path does not also contain `'dist'`! If you don't build into
|
||||
* `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will
|
||||
* not work properly!
|
||||
*
|
||||
* The default `pathFromSrc` option assumes that the calling code lives one
|
||||
* folder below the root of the package. Otherwise, it must be specified.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* If the caller is deeper within the project source, then you must provide
|
||||
* the appropriate fallback path:
|
||||
*
|
||||
* ```ts
|
||||
* // src/components/something.ts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(import.meta.url, '../../package.json')
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*
|
||||
* When running from CommmonJS, use `__filename` instead of `import.meta.url`
|
||||
*
|
||||
* ```ts
|
||||
* // src/index.cts
|
||||
* import { loadPackageJson } from 'package-json-from-dist'
|
||||
*
|
||||
* const pj = loadPackageJson(__filename)
|
||||
* console.log(`Hello from ${pj.name}@${pj.version}`)
|
||||
* ```
|
||||
*/
|
||||
export const loadPackageJson = (from, pathFromSrc = '../package.json') => JSON.parse(readFileSync(findPackageJson(from, pathFromSrc), 'utf8'));
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,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,cAAc,CACf,CAAA;YACH,CAAC;QACH,CAAC;QACD,MAAM,OAAO,GACX,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;YACtB,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;YAChC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAChC,OAAO,OAAO,CAAC,EAAE,EAAE,OAAO,EAAE,cAAc,CAAC,CAAA;IAC7C,CAAC;SAAM,CAAC;QACN,kCAAkC;QAClC,MAAM,CAAC,GAAG,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;QACrC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACb,OAAO,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,cAAc,CAAC,CAAA;QAC3D,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;AACH,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,IAAkB,EAClB,cAAsB,iBAAiB,EACvC,EAAE,CACF,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,CAAC,CAAA","sourcesContent":["import { readFileSync } from 'node:fs'\nimport { dirname, resolve, sep } from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst NM = `${sep}node_modules${sep}`\nconst STORE = `.store${sep}`\nconst PKG = `${sep}package${sep}`\nconst DIST = `${sep}dist${sep}`\n\n/**\n * Find the package.json file, either from a TypeScript file somewhere not\n * in a 'dist' folder, or a built and/or installed 'dist' folder.\n *\n * Note: this *only* works if you build your code into `'./dist'`, and that the\n * source path does not also contain `'dist'`! If you don't build into\n * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will\n * not work properly!\n *\n * The default `pathFromSrc` option assumes that the calling code lives one\n * folder below the root of the package. Otherwise, it must be specified.\n *\n * Example:\n *\n * ```ts\n * // src/index.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url)\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * If the caller is deeper within the project source, then you must provide\n * the appropriate fallback path:\n *\n * ```ts\n * // src/components/something.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url, '../../package.json')\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * When running from CommmonJS, use `__filename` instead of `import.meta.url`\n *\n * ```ts\n * // src/index.cts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(__filename)\n * console.log(`package.json found at ${pj}`)\n * ```\n */\nexport const findPackageJson = (\n from: string | URL,\n pathFromSrc: string = '../package.json',\n): string => {\n const f =\n typeof from === 'object' || from.startsWith('file://') ?\n fileURLToPath(from)\n : from\n const __dirname = dirname(f)\n\n const nms = __dirname.lastIndexOf(NM)\n if (nms !== -1) {\n // inside of node_modules. find the dist directly under package name.\n const nm = __dirname.substring(0, nms + NM.length)\n const pkgDir = __dirname.substring(nms + NM.length)\n // affordance for yarn berry, which puts package contents in\n // '.../node_modules/.store/${id}-${hash}/package/...'\n if (pkgDir.startsWith(STORE)) {\n const pkg = pkgDir.indexOf(PKG, STORE.length)\n if (pkg) {\n return resolve(\n nm,\n pkgDir.substring(0, pkg + PKG.length),\n 'package.json',\n )\n }\n }\n const pkgName =\n pkgDir.startsWith('@') ?\n pkgDir.split(sep, 2).join(sep)\n : String(pkgDir.split(sep)[0])\n return resolve(nm, pkgName, 'package.json')\n } else {\n // see if we are in a dist folder.\n const d = __dirname.lastIndexOf(DLine truncated
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"name": "package-json-from-dist",
|
||||
"version": "1.0.1",
|
||||
"description": "Load the local package.json from either src or dist folder",
|
||||
"main": "./dist/commonjs/index.js",
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"default": "./dist/esm/index.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"default": "./dist/commonjs/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"preversion": "npm test",
|
||||
"postversion": "npm publish",
|
||||
"prepublishOnly": "git push origin --follow-tags",
|
||||
"prepare": "tshy",
|
||||
"pretest": "npm run prepare",
|
||||
"presnap": "npm run prepare",
|
||||
"test": "tap",
|
||||
"snap": "tap",
|
||||
"format": "prettier --write . --log-level warn",
|
||||
"typedoc": "typedoc"
|
||||
},
|
||||
"author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)",
|
||||
"license": "BlueOak-1.0.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/isaacs/package-json-from-dist.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.12.12",
|
||||
"prettier": "^3.2.5",
|
||||
"tap": "^18.5.3",
|
||||
"typedoc": "^0.24.8",
|
||||
"typescript": "^5.1.6",
|
||||
"tshy": "^1.14.0"
|
||||
},
|
||||
"prettier": {
|
||||
"semi": false,
|
||||
"printWidth": 70,
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": false,
|
||||
"bracketSameLine": true,
|
||||
"arrowParens": "avoid",
|
||||
"endOfLine": "lf",
|
||||
"experimentalTernaries": true
|
||||
},
|
||||
"tshy": {
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"type": "module"
|
||||
}
|
||||
Reference in new issue
Block a user