@pnpm/exec.prepare-package 1000.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2026 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @pnpm/prepare-package
2
+
3
+ > Prepares a Git-hosted package
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@pnpm/prepare-package.svg)](https://www.npmjs.com/package/@pnpm/prepare-package)
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ pnpm add @pnpm/prepare-package
11
+ ```
12
+
13
+ ## License
14
+
15
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { AllowBuild } from '@pnpm/types';
2
+ export interface PreparePackageOptions {
3
+ allowBuild?: AllowBuild;
4
+ ignoreScripts?: boolean;
5
+ rawConfig: Record<string, unknown>;
6
+ unsafePerm?: boolean;
7
+ }
8
+ export declare function preparePackage(opts: PreparePackageOptions, gitRootDir: string, subDir: string): Promise<{
9
+ shouldBeBuilt: boolean;
10
+ pkgDir: string;
11
+ }>;
package/lib/index.js ADDED
@@ -0,0 +1,98 @@
1
+ import assert from 'node:assert';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import util from 'node:util';
5
+ import { PnpmError } from '@pnpm/error';
6
+ import { runLifecycleHook } from '@pnpm/exec.lifecycle';
7
+ import { safeReadPackageJsonFromDir } from '@pnpm/pkg-manifest.reader';
8
+ import { rimraf } from '@zkochan/rimraf';
9
+ import { preferredPM } from 'preferred-pm';
10
+ import { omit } from 'ramda';
11
+ // We don't run prepublishOnly to prepare the dependency.
12
+ // This might be counterintuitive as prepublishOnly is where a lot of packages put their build scripts.
13
+ // However, neither npm nor Yarn run prepublishOnly of git-hosted dependencies (checked on npm v10 and Yarn v3).
14
+ const PREPUBLISH_SCRIPTS = [
15
+ 'prepublish',
16
+ 'prepack',
17
+ 'publish',
18
+ ];
19
+ export async function preparePackage(opts, gitRootDir, subDir) {
20
+ const pkgDir = safeJoinPath(gitRootDir, subDir);
21
+ const manifest = await safeReadPackageJsonFromDir(pkgDir);
22
+ if (manifest?.scripts == null || !packageShouldBeBuilt(manifest, pkgDir))
23
+ return { shouldBeBuilt: false, pkgDir };
24
+ if (opts.ignoreScripts)
25
+ return { shouldBeBuilt: true, pkgDir };
26
+ // Check if the package is allowed to run build scripts
27
+ // If allowBuild is undefined or returns false, block the build
28
+ if (!opts.allowBuild?.(manifest.name, manifest.version)) {
29
+ throw new PnpmError('GIT_DEP_PREPARE_NOT_ALLOWED', `The git-hosted package "${manifest.name}@${manifest.version}" needs to execute build scripts but is not in the "allowBuilds" allowlist.`, {
30
+ hint: `Add the package to "allowBuilds" in your project's pnpm-workspace.yaml to allow it to run scripts. For example:
31
+ allowBuilds:
32
+ ${manifest.name}: true`,
33
+ });
34
+ }
35
+ const pm = (await preferredPM(gitRootDir))?.name ?? 'npm';
36
+ const execOpts = {
37
+ depPath: `${manifest.name}@${manifest.version}`,
38
+ pkgRoot: pkgDir,
39
+ // We can't prepare a package without running its lifecycle scripts.
40
+ // An alternative solution could be to throw an exception.
41
+ rawConfig: omit(['ignore-scripts'], opts.rawConfig),
42
+ rootModulesDir: pkgDir, // We don't need this property but there is currently no way to not set it.
43
+ unsafePerm: Boolean(opts.unsafePerm),
44
+ };
45
+ try {
46
+ const installScriptName = `${pm}-install`;
47
+ manifest.scripts[installScriptName] = `${pm} install`;
48
+ await runLifecycleHook(installScriptName, manifest, execOpts);
49
+ for (const scriptName of PREPUBLISH_SCRIPTS) {
50
+ if (manifest.scripts[scriptName] == null || manifest.scripts[scriptName] === '')
51
+ continue;
52
+ let newScriptName;
53
+ if (pm !== 'pnpm') {
54
+ newScriptName = `${pm}-run-${scriptName}`;
55
+ manifest.scripts[newScriptName] = `${pm} run ${scriptName}`;
56
+ }
57
+ else {
58
+ newScriptName = scriptName;
59
+ }
60
+ // eslint-disable-next-line no-await-in-loop
61
+ await runLifecycleHook(newScriptName, manifest, execOpts);
62
+ }
63
+ }
64
+ catch (err) {
65
+ assert(util.types.isNativeError(err));
66
+ Object.assign(err, {
67
+ code: 'ERR_PNPM_PREPARE_PACKAGE',
68
+ });
69
+ throw err;
70
+ }
71
+ await rimraf(path.join(pkgDir, 'node_modules'));
72
+ return { shouldBeBuilt: true, pkgDir };
73
+ }
74
+ function packageShouldBeBuilt(manifest, pkgDir) {
75
+ if (manifest.scripts == null)
76
+ return false;
77
+ const scripts = manifest.scripts;
78
+ if (scripts.prepare != null && scripts.prepare !== '')
79
+ return true;
80
+ const hasPrepublishScript = PREPUBLISH_SCRIPTS.some((scriptName) => scripts[scriptName] != null && scripts[scriptName] !== '');
81
+ if (!hasPrepublishScript)
82
+ return false;
83
+ const mainFile = manifest.main ?? 'index.js';
84
+ return !fs.existsSync(path.join(pkgDir, mainFile));
85
+ }
86
+ function safeJoinPath(root, sub) {
87
+ const joined = path.join(root, sub);
88
+ // prevent the dir traversal attack
89
+ const relative = path.relative(root, joined);
90
+ if (relative.startsWith('..')) {
91
+ throw new PnpmError('INVALID_PATH', `Path "${sub}" should be a sub directory`);
92
+ }
93
+ if (!fs.existsSync(joined) || !fs.lstatSync(joined).isDirectory()) {
94
+ throw new PnpmError('INVALID_PATH', `Path "${sub}" is not a directory`);
95
+ }
96
+ return joined;
97
+ }
98
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@pnpm/exec.prepare-package",
3
+ "version": "1000.0.26",
4
+ "description": "Prepares a Git-hosted package",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11"
8
+ ],
9
+ "license": "MIT",
10
+ "funding": "https://opencollective.com/pnpm",
11
+ "repository": "https://github.com/pnpm/pnpm/tree/main/exec/prepare-package",
12
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/exec/prepare-package#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pnpm/pnpm/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "lib/index.js",
18
+ "types": "lib/index.d.ts",
19
+ "exports": {
20
+ ".": "./lib/index.js"
21
+ },
22
+ "files": [
23
+ "lib",
24
+ "!*.map"
25
+ ],
26
+ "dependencies": {
27
+ "@zkochan/rimraf": "^4.0.0",
28
+ "execa": "npm:safe-execa@0.3.0",
29
+ "preferred-pm": "^5.0.0",
30
+ "ramda": "npm:@pnpm/ramda@0.28.1",
31
+ "@pnpm/error": "1000.0.5",
32
+ "@pnpm/exec.lifecycle": "1001.0.25",
33
+ "@pnpm/types": "1000.9.0",
34
+ "@pnpm/pkg-manifest.reader": "1000.1.2"
35
+ },
36
+ "devDependencies": {
37
+ "@types/ramda": "0.29.12",
38
+ "load-json-file": "^7.0.1",
39
+ "@pnpm/exec.prepare-package": "1000.0.26",
40
+ "@pnpm/prepare": "1000.0.4",
41
+ "@pnpm/test-fixtures": "1000.0.0",
42
+ "@pnpm/test-ipc-server": "1000.0.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=22.13"
46
+ },
47
+ "jest": {
48
+ "preset": "@pnpm/jest-config"
49
+ },
50
+ "scripts": {
51
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
52
+ "test": "pnpm run compile && pnpm run _test",
53
+ "compile": "tsgo --build && pnpm run lint --fix",
54
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest"
55
+ }
56
+ }