Skip to content

Commit 4c4a26a

Browse files
committed
feat(package): select native packing through get-package-manager
1 parent b2474f9 commit 4c4a26a

8 files changed

Lines changed: 97 additions & 45 deletions

File tree

‎.github/workflows/__test-action-get-package-manager.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,31 @@ jobs:
2020
cache-dependency-path: "tests/npm/**/package-lock.json"
2121
install-command: npm ci
2222
run-script-command: npm run
23+
pack-command: 'npm pack --ignore-scripts --pack-destination "${PACK_DESTINATION:?PACK_DESTINATION is required}"'
2324

2425
- working-directory: tests/pnpm
2526
package-manager: pnpm
2627
lock-file: pnpm-lock.yaml
2728
cache-dependency-path: "tests/pnpm/**/pnpm-lock.yaml"
2829
install-command: pnpm install --frozen-lockfile
2930
run-script-command: pnpm
31+
pack-command: 'pnpm pack --config.ignore-scripts=true --pack-destination "${PACK_DESTINATION:?PACK_DESTINATION is required}"'
3032

3133
- working-directory: tests/pnpm-package-manager
3234
package-manager: pnpm
3335
lock-file: pnpm-lock.yaml
3436
cache-dependency-path: "tests/pnpm-package-manager/**/pnpm-lock.yaml"
3537
install-command: pnpm install --frozen-lockfile
3638
run-script-command: pnpm
39+
pack-command: 'pnpm pack --config.ignore-scripts=true --pack-destination "${PACK_DESTINATION:?PACK_DESTINATION is required}"'
3740

3841
- working-directory: tests/yarn
3942
package-manager: yarn
4043
lock-file: yarn.lock
4144
cache-dependency-path: "tests/yarn/**/yarn.lock"
4245
install-command: yarn install --frozen-lockfile
4346
run-script-command: yarn
47+
pack-command: 'yarn pack --ignore-scripts --filename "${PACK_DESTINATION:?PACK_DESTINATION is required}/package.tgz"'
4448
steps:
4549
- name: Arrange - Checkout sources
4650
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -63,6 +67,8 @@ jobs:
6367
EXPECTED_INSTALL_COMMAND: ${{ matrix.install-command }}
6468
ACTUAL_RUN_SCRIPT_COMMAND: ${{ steps.act-get-package-manager.outputs.run-script-command }}
6569
EXPECTED_RUN_SCRIPT_COMMAND: ${{ matrix.run-script-command }}
70+
ACTUAL_PACK_COMMAND: ${{ steps.act-get-package-manager.outputs.pack-command }}
71+
EXPECTED_PACK_COMMAND: ${{ matrix.pack-command }}
6672
run: |
6773
if [ "$ACTUAL_PACKAGE_MANAGER" != "$EXPECTED_PACKAGE_MANAGER" ]; then
6874
echo "get-package-manager outputs result is not valid"
@@ -83,3 +89,8 @@ jobs:
8389
echo "get-package-manager outputs run-script-command is not valid"
8490
exit 1
8591
fi
92+
93+
if [ "$ACTUAL_PACK_COMMAND" != "$EXPECTED_PACK_COMMAND" ]; then
94+
echo "get-package-manager output pack-command is not valid"
95+
exit 1
96+
fi

‎.github/workflows/__test-action-package-create.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ jobs:
2121
include:
2222
- working-directory: tests/npm
2323
artifact-suffix: npm
24+
package-manager: npm
2425
- working-directory: tests/pnpm
2526
artifact-suffix: pnpm
27+
package-manager: pnpm
2628
- working-directory: tests/pnpm-package-manager
2729
artifact-suffix: pnpm-package-manager
30+
package-manager: pnpm
2831
- working-directory: tests/yarn
2932
artifact-suffix: yarn
33+
package-manager: yarn
3034
steps:
3135
- name: Arrange - Checkout sources
3236
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -39,6 +43,22 @@ jobs:
3943
working-directory: ${{ matrix.working-directory }}
4044
install-dependencies: "false"
4145

46+
- name: Arrange - Record native package manager calls
47+
env:
48+
PACKAGE_MANAGER: ${{ matrix.package-manager }}
49+
run: |
50+
wrapper_directory="$(mktemp -d)"
51+
echo "NATIVE_PACKAGE_MANAGER=$(command -v "$PACKAGE_MANAGER")" >> "$GITHUB_ENV"
52+
echo "PACK_COMMAND_LOG=$wrapper_directory/pack.log" >> "$GITHUB_ENV"
53+
cat > "$wrapper_directory/$PACKAGE_MANAGER" <<'SH'
54+
#!/usr/bin/env bash
55+
set -euo pipefail
56+
printf '%s\n' "$*" >> "$PACK_COMMAND_LOG"
57+
exec "$NATIVE_PACKAGE_MANAGER" "$@"
58+
SH
59+
chmod +x "$wrapper_directory/$PACKAGE_MANAGER"
60+
echo "$wrapper_directory" >> "$GITHUB_PATH"
61+
4262
- name: Arrange - Prepare files and hooks that must not run
4363
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
4464
env:
@@ -55,6 +75,7 @@ jobs:
5575
manifest.scripts = Object.fromEntries(['preinstall', 'install', 'postinstall', 'preversion', 'version', 'postversion', 'prepare', 'prepack', 'postpack'].map(name => [name, 'exit 91']));
5676
manifest.devDependencies = { 'must-not-install': 'file:./missing' };
5777
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
78+
if (manifest.packageManager) fs.writeFileSync(path.join(directory, 'yarn.lock'), '# Conflicting lockfile: packageManager takes precedence.\n');
5879
await io.mkdirP(path.join(directory, 'dist'));
5980
fs.writeFileSync(path.join(directory, 'dist/prepared.txt'), 'prepared before packaging\n');
6081
@@ -108,6 +129,7 @@ jobs:
108129
const fs = require('node:fs');
109130
const path = require('node:path');
110131
const directory = process.env.WORKING_DIRECTORY;
132+
assert.match(fs.readFileSync(process.env.PACK_COMMAND_LOG, 'utf8'), /^pack /m);
111133
const { stdout: contents } = await exec.getExecOutput('tar', ['-xOf', process.env.TARBALL, 'package/dist/prepared.txt']);
112134
const { stdout: manifest } = await exec.getExecOutput('tar', ['-xOf', process.env.TARBALL, 'package/package.json']);
113135
assert.equal(contents, 'prepared before packaging\n');

‎.github/workflows/release.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ and the package manager before calling the [Package action](../../actions/packag
329329
Prepare the version, dependencies and build files first. The action uses the existing
330330
checkout and never installs dependencies, changes versions or runs lifecycle scripts.
331331
It can pack files already present in the job or restore an optional build artifact.
332+
333+
Package creation selects the native npm, pnpm or Yarn Classic command through
334+
`get-package-manager`, using `package.json.packageManager` before lockfiles.
335+
The command disables lifecycle scripts and writes to an isolated temporary
336+
folder, so packaging does not depend on the manager's console output format.
337+
332338
Include generated files normally produced by `prepare` or `prepack` in that build.
333339

334340
The shared release workflow requires absolute artifact paths and restores them with

‎actions/get-package-manager/README.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ Action to detect the package manager used. Supports Yarn, pnpm, and npm
6363

6464
## Outputs
6565

66-
| **Output** | **Description** |
67-
| --------------------------- | ----------------------------------------------------- |
68-
| **`package-manager`** | The package manager used. |
69-
| **`cache-dependency-path`** | The path to the dependency file for cache management. |
70-
| **`install-command`** | The command to install dependencies. |
71-
| **`run-script-command`** | The command to run a script in the package.json file. |
66+
| **Output** | **Description** |
67+
| --------------------------- | --------------------------------------------------------------------------------------------------- |
68+
| **`package-manager`** | The package manager used. |
69+
| **`cache-dependency-path`** | The path to the dependency file for cache management. |
70+
| **`install-command`** | The command to install dependencies. |
71+
| **`run-script-command`** | The command to run a script in the package.json file. |
72+
| **`pack-command`** | Bash command to pack prepared files without lifecycle scripts using npm, pnpm or Yarn Classic. |
73+
| | Set PACK_DESTINATION to an existing absolute output directory before executing the command. |
74+
| | Configure the project's package manager first; this command does not install dependencies or build. |
7275

7376
<!-- outputs:end -->
7477

‎actions/get-package-manager/action.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ outputs:
2626
run-script-command:
2727
description: "The command to run a script in the package.json file."
2828
value: ${{ steps.get-package-manager.outputs.run-script-command }}
29+
pack-command:
30+
description: |
31+
Bash command to pack prepared files without lifecycle scripts using npm, pnpm or Yarn Classic.
32+
Set PACK_DESTINATION to an existing absolute output directory before executing the command.
33+
Configure the project's package manager first; this command does not install dependencies or build.
34+
value: ${{ steps.get-package-manager.outputs.pack-command }}
2935

3036
runs:
3137
using: "composite"
@@ -149,16 +155,19 @@ runs:
149155
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/yarn.lock` : '',
150156
installCommand: 'yarn install --frozen-lockfile',
151157
runScriptCommand: 'yarn',
158+
packCommand: 'yarn pack --ignore-scripts --filename "${PACK_DESTINATION:?PACK_DESTINATION is required}/package.tgz"',
152159
},
153160
pnpm: {
154161
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/pnpm-lock.yaml` : '',
155162
installCommand: 'pnpm install --frozen-lockfile',
156163
runScriptCommand: 'pnpm',
164+
packCommand: 'pnpm pack --config.ignore-scripts=true --pack-destination "${PACK_DESTINATION:?PACK_DESTINATION is required}"',
157165
},
158166
npm: {
159167
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/package-lock.json` : '',
160168
installCommand: 'npm ci',
161169
runScriptCommand: 'npm run',
170+
packCommand: 'npm pack --ignore-scripts --pack-destination "${PACK_DESTINATION:?PACK_DESTINATION is required}"',
162171
},
163172
};
164173
@@ -173,4 +182,5 @@ runs:
173182
core.setOutput('cache-dependency-path', managerConfig.cacheDependencyPath);
174183
core.setOutput('install-command', managerConfig.installCommand);
175184
core.setOutput('run-script-command', managerConfig.runScriptCommand);
185+
core.setOutput('pack-command', managerConfig.packCommand);
176186
# jscpd:ignore-end

‎actions/package/create/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
Create and upload a package tarball from prepared files, optionally restored from a build artifact.
2929
Check out the project, configure Node.js and its package manager, and prepare dependencies, versions and build output before calling this action.
30+
Uses the project's npm, pnpm or Yarn Classic configuration to select its native pack command.
3031
Packaging never installs dependencies, changes versions or runs lifecycle scripts.
3132

3233
<!-- overview:end -->

‎actions/package/create/action.yml‎

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: "Package"
22
description: |
33
Create and upload a package tarball from prepared files, optionally restored from a build artifact.
44
Check out the project, configure Node.js and its package manager, and prepare dependencies, versions and build output before calling this action.
5+
Uses the project's npm, pnpm or Yarn Classic configuration to select its native pack command.
56
Packaging never installs dependencies, changes versions or runs lifecycle scripts.
67
author: hoverkraft
78
branding:
@@ -77,12 +78,18 @@ runs:
7778
artifact-ids: ${{ inputs.build-artifact-id }}
7879
path: ${{ inputs.build-artifact-path }}
7980

81+
- id: get-package-manager
82+
uses: $/actions/get-package-manager
83+
with:
84+
working-directory: ${{ steps.working-directory.outputs.absolute-path }}
85+
8086
- id: package
8187
name: Create package tarball for verification
8288
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
8389
env:
8490
WORKING_DIRECTORY: ${{ steps.working-directory.outputs.absolute-path }}
8591
PACKAGE_DIRECTORY: ${{ inputs.package-directory }}
92+
PACK_COMMAND: ${{ steps.get-package-manager.outputs.pack-command }}
8693
ARTIFACT_NAME_INPUT: ${{ inputs.artifact-name }}
8794
GITHUB_RUN_ID: ${{ github.run_id }}
8895
GITHUB_RUN_ATTEMPT: ${{ github.run_attempt }}
@@ -108,52 +115,34 @@ runs:
108115
return;
109116
}
110117
111-
const packCommand = 'npm';
112-
const packArgs = ['pack', '--json', '--pack-destination', runnerTemp, '--ignore-scripts'];
113-
const npmPackResult = await exec.getExecOutput(packCommand, packArgs, {
114-
cwd: packageDirectory,
115-
ignoreReturnCode: true,
116-
env: { ...process.env, npm_config_ignore_scripts: 'true' },
117-
});
118-
119-
core.debug(`${packCommand} pack stdout: ${npmPackResult.stdout}`);
120-
core.debug(`${packCommand} pack stderr: ${npmPackResult.stderr}`);
121-
122-
if (npmPackResult.exitCode !== 0) {
123-
core.setFailed(`${packCommand} pack failed with exit code ${npmPackResult.exitCode}: ${npmPackResult.stderr}`);
124-
return;
125-
}
126-
127-
let packageInfo;
128-
try {
129-
packageInfo = JSON.parse(npmPackResult.stdout);
130-
} catch (error) {
131-
core.setFailed(`Failed to parse ${packCommand} pack output: ${error.message}`);
132-
return;
133-
}
134-
135-
core.debug(`packageInfo: ${JSON.stringify(packageInfo)}`);
136-
137-
const tarballName = packageInfo?.[0]?.filename;
138-
if (!tarballName) {
139-
core.setFailed('No tarball filename returned by npm pack');
140-
return;
141-
}
142-
143-
const tarballPath = path.resolve(runnerTemp, tarballName);
144-
if (!fs.existsSync(tarballPath)) {
145-
core.setFailed(`Tarball was not created at expected path: ${tarballPath}`);
118+
const packCommand = process.env.PACK_COMMAND?.trim();
119+
if (!packCommand) {
120+
core.setFailed('Pack command is empty');
146121
return;
147122
}
148123
124+
const packDirectory = fs.mkdtempSync(path.join(runnerTemp, 'package-'));
149125
const artifactNameInput = (process.env.ARTIFACT_NAME_INPUT || '').trim();
150126
const artifactName = artifactNameInput || `package-tarball-${process.env.GITHUB_RUN_ID}-${process.env.GITHUB_RUN_ATTEMPT}-${randomUUID()}`;
151-
const tarballExtension = path.extname(tarballPath) || '.tgz';
152127
const sanitizedArtifactName = artifactName.replace(/[^a-zA-Z0-9._-]+/g, '-');
153-
const uniqueTarballPath = path.join(runnerTemp, `${sanitizedArtifactName}${tarballExtension}`);
128+
const uniqueTarballPath = path.join(runnerTemp, `${sanitizedArtifactName}.tgz`);
154129
155-
if (path.resolve(tarballPath) !== path.resolve(uniqueTarballPath)) {
156-
await io.mv(tarballPath, uniqueTarballPath);
130+
try {
131+
await exec.exec('bash', ['-e', '-o', 'pipefail', '-c', packCommand], {
132+
cwd: packageDirectory,
133+
env: { ...process.env, PACK_DESTINATION: packDirectory },
134+
});
135+
136+
const tarballs = await (await glob.create(`${packDirectory}/*.tgz`, {
137+
followSymbolicLinks: false,
138+
matchDirectories: false,
139+
})).glob();
140+
if (tarballs.length !== 1) {
141+
throw new Error(`Expected one package tarball, found ${tarballs.length}.`);
142+
}
143+
await io.mv(tarballs[0], uniqueTarballPath);
144+
} finally {
145+
await io.rmRF(packDirectory);
157146
}
158147
159148
core.info(`Generated package tarball: ${uniqueTarballPath}`);

‎docs/adr/0001-release-orchestration.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,13 @@ the alternatives and observations below preserve their history, not new validati
269269
check out the intended source and configure tooling before packaging; pnpm
270270
workspace linking belongs to the release workflow. Artifact restoration remains
271271
optional, so local build output is preserved and can be packed in the same job.
272+
- **2026-11-04, native packaging commands:** `get-package-manager` provides a
273+
`pack-command` output for npm, pnpm and Yarn Classic, with lifecycle scripts
274+
disabled. The Bash command receives its output directory through the quoted
275+
`PACK_DESTINATION` environment variable. `package/create` runs that command
276+
through GitHub Script's `exec` helper, discovers one tarball in an isolated
277+
temporary folder and cleans the folder on success or failure. This replaces
278+
npm-only packing and manager-specific console-output parsing without adding
279+
package-manager selection inputs or a shared JavaScript module. Modern Yarn
280+
packing is outside this script-free contract because its pack hooks cannot be
281+
disabled through the Classic command's flags.

0 commit comments

Comments
 (0)