Skip to content

Commit

Permalink
fix: adapt package extractor with yarn 3
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyi.levi committed Sep 2, 2022
1 parent 1a0b093 commit 99bd9ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 12 additions & 4 deletions tools/cli/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ limitations under the License.
*/

import { mkdir, rename, rm, writeFile } from 'fs/promises'
import { basename, resolve } from 'path'
import { basename, resolve, join } from 'path'

import { Option } from 'clipanion'

import { allPackageNames, getPackage, PackageName, packagePath } from '../utils'
import { allPackageNames, getPackage, PackageName, packagePath, writeFileAsync } from '../utils'

import { Command } from './command'

Expand Down Expand Up @@ -47,7 +47,7 @@ export class ExtractCommand extends Command {
await mkdir(tmpPath, { recursive: true })

// 2. run `yarn pack` and then unpack, to clean up unnecessary files
await this.execAsync([`yarn`, 'pack', '-f', tmpPath + `/${pkg.dirname}.tgz`], { cwd: packagePath(this.package) })
await this.execAsync([`yarn`, 'pack', '--out', tmpPath + `/${pkg.dirname}.tgz`], { cwd: packagePath(this.package) })
await this.execAsync(['tar', '-xvzf', tmpPath + `/${pkg.dirname}.tgz`], { cwd: tmpPath })

// 3. Move the package to the output directory
Expand Down Expand Up @@ -89,7 +89,15 @@ export class ExtractCommand extends Command {
)

// 5. install dependencies
await this.execAsync('yarn --prod', { cwd: extractPath })
// make `yarn install` available in non-workspace package
await writeFileAsync(join(extractPath, 'yarn.lock'), '')
await writeFileAsync(join(extractPath, '.yarnrc.yml'), `enableGlobalCache: true`)
await this.execAsync('yarn', {
cwd: extractPath,
env: {
NODE_ENV: 'production',
},
})

// 6. Compress the package
await this.execAsync(['tar', '-czf', basename(extractPath) + '.tgz', '-C', extractPath, '.'], { cwd: outputPath })
Expand Down
8 changes: 4 additions & 4 deletions tools/utils/subprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { Logger } from './log'
const children = new Set<ChildProcess>()

export function spawn(tag: string, cmd: string | string[], options: SpawnOptions = {}) {
const isYarnSpawn = typeof cmd === 'string' ? cmd.startsWith('yarn') : cmd[0] === 'yarn'
cmd = typeof cmd === 'string' ? cmd.split(' ') : cmd
const isYarnSpawn = cmd[0] === 'yarn'

const spawnOptions: SpawnOptions = {
stdio: isYarnSpawn ? ['inherit', 'inherit', 'inherit'] : ['inherit', 'pipe', 'pipe'],
Expand All @@ -33,9 +34,8 @@ export function spawn(tag: string, cmd: string | string[], options: SpawnOptions
}

const logger = new Logger(isYarnSpawn ? '' : tag)
!isYarnSpawn && logger.info(cmd)
const childProcess =
typeof cmd === 'string' ? RawSpawn(cmd, spawnOptions) : RawSpawn(cmd[0], cmd.slice(1), spawnOptions)
!isYarnSpawn && logger.info(cmd.join(' '))
const childProcess = RawSpawn(cmd[0], cmd.slice(1), spawnOptions)
children.add(childProcess)

const drain = (_code: number | null, signal: any) => {
Expand Down

0 comments on commit 99bd9ab

Please sign in to comment.