1+ import { detectPnpmMajor } from './pnpm-internals.ts' ;
12import { transforms , type TransformFn } from './tooling/transforms.ts' ;
23
4+ type YamlMap = {
5+ get ( key : string ) : unknown ;
6+ set ( key : string , value : unknown ) : void ;
7+ has ( key : string ) : boolean ;
8+ } ;
9+
10+ type YamlSeq = { items ?: Array < { value : string } | string > } ;
11+
12+ type YamlDoc = {
13+ get ( key : string ) : unknown ;
14+ set ( key : string , value : unknown ) : void ;
15+ has ( key : string ) : boolean ;
16+ delete ( key : string ) : boolean ;
17+ createNode ( value : unknown , options ?: { flow ?: boolean } ) : unknown ;
18+ } ;
19+
320/**
4- * Returns a TransformFn for `pnpm-workspace.yaml` that adds packages to `onlyBuiltDependencies`.
21+ * Returns a TransformFn for `pnpm-workspace.yaml` that adds packages to the
22+ * pnpm "allow builds" config.
23+ *
24+ * The helper detects the installed pnpm version (via `pnpm --version`) and:
25+ * - on pnpm `>= 11` writes to the unified `allowBuilds` map (`{ pkg: true }`),
26+ * migrating any legacy `onlyBuiltDependencies` list into the map;
27+ * - on pnpm `< 11` writes to the legacy `onlyBuiltDependencies` list.
528 *
6- * Use with `sv.file`:
729 * ```ts
830 * if (packageManager === 'pnpm') {
9- * sv.file(file.findUp('pnpm-workspace.yaml'), pnpm.onlyBuiltDependencies ('my-native-dep'));
31+ * sv.file(file.findUp('pnpm-workspace.yaml'), pnpm.allowBuilds ('my-native-dep'));
1032 * }
1133 * ```
1234 */
13- export function onlyBuiltDependencies ( ...packages : string [ ] ) : TransformFn {
35+ export function allowBuilds ( ...packages : string [ ] ) : TransformFn {
36+ const major = detectPnpmMajor ( ) ;
37+ if ( major !== undefined && major < 11 ) return writeLegacy ( packages ) ;
38+ return writeAllowBuilds ( packages ) ;
39+ }
40+
41+ function writeAllowBuilds ( packages : string [ ] ) : TransformFn {
1442 return transforms . yaml ( ( { data } ) => {
15- const existing = data . get ( 'onlyBuiltDependencies' ) as
16- | { items ?: Array < { value : string } | string > }
17- | undefined ;
43+ const doc = data as unknown as YamlDoc ;
44+
45+ const toMigrate : string [ ] = [ ] ;
46+ const legacy = doc . get ( 'onlyBuiltDependencies' ) as YamlSeq | undefined ;
47+ if ( legacy ?. items ) {
48+ for ( const item of legacy . items ) {
49+ toMigrate . push ( typeof item === 'object' ? item . value : item ) ;
50+ }
51+ }
52+
53+ let map = doc . get ( 'allowBuilds' ) as YamlMap | undefined ;
54+ if ( ! map || typeof map . set !== 'function' ) {
55+ map = doc . createNode ( { } , { flow : false } ) as YamlMap ;
56+ doc . set ( 'allowBuilds' , map ) ;
57+ }
58+
59+ for ( const pkg of [ ...toMigrate , ...packages ] ) {
60+ if ( ! map . has ( pkg ) ) map . set ( pkg , true ) ;
61+ }
62+
63+ if ( legacy ) doc . delete ( 'onlyBuiltDependencies' ) ;
64+ } ) ;
65+ }
66+
67+ function writeLegacy ( packages : string [ ] ) : TransformFn {
68+ return transforms . yaml ( ( { data } ) => {
69+ const existing = data . get ( 'onlyBuiltDependencies' ) as YamlSeq | undefined ;
1870 const items : Array < { value : string } | string > = existing ?. items ?? [ ] ;
1971 for ( const pkg of packages ) {
2072 if ( items . includes ( pkg ) ) continue ;
@@ -24,3 +76,10 @@ export function onlyBuiltDependencies(...packages: string[]): TransformFn {
2476 data . set ( 'onlyBuiltDependencies' , items ) ;
2577 } ) ;
2678}
79+
80+ /**
81+ * @deprecated Use {@link allowBuilds} instead.
82+ */
83+ export function onlyBuiltDependencies ( ...packages : string [ ] ) : TransformFn {
84+ return allowBuilds ( ...packages ) ;
85+ }
0 commit comments