1- import { mkdtemp , readFile } from 'node:fs/promises'
2- import { tmpdir } from 'node:os'
31import path from 'node:path'
42import { dim } from 'ansis'
53import { createDebug } from 'obug'
6- import { exec } from 'tinyexec'
7- import { fsRemove } from '../../utils/fs.ts'
84import { importWithError , slash } from '../../utils/general.ts'
95import type { ResolvedConfig } from '../../config/index.ts'
106import type {
117 CheckPackageOptions ,
12- CheckResult ,
138 Problem ,
149 ProblemKind ,
1510} from '@arethetypeswrong/core'
11+ import type { Buffer } from 'node:buffer'
1612
1713const debug = createDebug ( 'tsdown:attw' )
1814const label = dim `[attw]`
@@ -96,7 +92,10 @@ const profiles: Record<Required<AttwOptions>['profile'], string[]> = {
9692 'esm-only' : [ 'node10' , 'node16-cjs' ] ,
9793}
9894
99- export async function attw ( options : ResolvedConfig ) : Promise < void > {
95+ export async function attw (
96+ options : ResolvedConfig ,
97+ tarball : Buffer < ArrayBuffer > ,
98+ ) : Promise < void > {
10099 if ( ! options . attw ) return
101100 if ( ! options . pkg ) {
102101 options . logger . warn ( 'attw is enabled but package.json is not found' )
@@ -121,34 +120,12 @@ export async function attw(options: ResolvedConfig): Promise<void> {
121120 const t = performance . now ( )
122121 debug ( 'Running attw check' )
123122
124- const tempDir = await mkdtemp ( path . join ( tmpdir ( ) , 'tsdown-attw-' ) )
125-
126123 const attwCore = await importWithError <
127124 typeof import ( '@arethetypeswrong/core' )
128125 > ( '@arethetypeswrong/core' , options . attw . resolvePaths )
129- let checkResult : CheckResult
130-
131- try {
132- const { stdout : tarballInfo } = await exec (
133- 'npm' ,
134- [ 'pack' , '--json' , '--ignore-scripts' , '--pack-destination' , tempDir ] ,
135- { nodeOptions : { cwd : options . cwd } } ,
136- )
137- const parsed = JSON . parse ( tarballInfo )
138- if ( ! Array . isArray ( parsed ) || ! parsed [ 0 ] ?. filename ) {
139- throw new Error ( 'Invalid npm pack output format' )
140- }
141- const tarballPath = path . join ( tempDir , parsed [ 0 ] . filename as string )
142- const tarball = await readFile ( tarballPath )
143126
144- const pkg = attwCore . createPackageFromTarballData ( tarball )
145- checkResult = await attwCore . checkPackage ( pkg , attwOptions )
146- } catch ( error ) {
147- options . logger . error ( 'ATTW check failed:' , error )
148- return
149- } finally {
150- await fsRemove ( tempDir )
151- }
127+ const pkg = attwCore . createPackageFromTarballData ( tarball )
128+ const checkResult = await attwCore . checkPackage ( pkg , attwOptions )
152129
153130 let errorMessage : string | undefined
154131 if ( checkResult . types ) {
0 commit comments