Skip to content

Commit

Permalink
feat: check for renamed yarn.lock during install (#648)
Browse files Browse the repository at this point in the history
* feat: check for renamed yarn.lock during install

* chore: rename to oclif.lock

* chore: code review
  • Loading branch information
mdonnalley authored Sep 1, 2023
1 parent a7123ff commit 9830b0a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
42 changes: 37 additions & 5 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const initPJSON: Interfaces.PJSON.User = {
dependencies: {},
}

async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.promises.access(filePath)
return true
} catch {
return false
}
}

export default class Plugins {
verbose = false;

Expand Down Expand Up @@ -119,20 +128,43 @@ export default class Plugins {
}
}

// if yarn.lock exists, fetch locked dependencies
/**
* If a yarn.lock or oclif.lock exists at the root, refresh dependencies by
* rerunning yarn. If options.prod is true, only install production dependencies.
*
* As of v9 npm will always ignore the yarn.lock during `npm pack`]
* (see https://github.com/npm/cli/issues/6738). To get around this plugins can
* rename yarn.lock to oclif.lock before running `npm pack`.
*
* We still check for the existence of yarn.lock since it could be included if a plugin was
* packed using yarn or v8 of npm. Plugins installed directly from a git url will also
* have a yarn.lock.
*
* @param root string
* @param options {prod?: boolean}
* @returns Promise<void>
*/
async refresh(
root: string,
{prod = true}: { prod?: boolean } = {},
): Promise<void> {
if (fs.existsSync(path.join(root, 'yarn.lock'))) {
this.debug(`yarn.lock exists at ${root}. Installing prod dependencies`)
// use yarn.lock to fetch dependencies
const doRefresh = async () => {
await this.yarn.exec(prod ? ['--prod'] : [], {
cwd: root,
verbose: this.verbose,
})
}

if (await fileExists(path.join(root, 'yarn.lock'))) {
this.debug(`yarn.lock exists at ${root}. Installing prod dependencies`)
await doRefresh()
} else if (await fileExists(path.join(root, 'oclif.lock'))) {
this.debug(`oclif.lock exists at ${root}. Installing prod dependencies`)
await fs.promises.rename(path.join(root, 'oclif.lock'), path.join(root, 'yarn.lock'))
await doRefresh()
await fs.promises.unlink(path.join(root, 'yarn.lock'))
} else {
this.debug(`no yarn.lock exists at ${root}. Skipping dependency refresh`)
this.debug(`no yarn.lock or oclif.lock exists at ${root}. Skipping dependency refresh`)
}
}

Expand Down
39 changes: 2 additions & 37 deletions src/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Interfaces, ux} from '@oclif/core'
import {fork, spawn} from 'child_process'
import {fork} from 'child_process'
import NpmRunPath from 'npm-run-path'
import * as path from 'path'

Expand Down Expand Up @@ -42,37 +42,6 @@ export default class Yarn {
})
}

spawn(executable: string, args: string[] = [], options: any = {}): Promise<void> {
return new Promise((resolve, reject) => {
const spawned = spawn(executable, args, {...options, shell: true})
spawned.stderr.setEncoding('utf8')
spawned.stderr.on('data', (d: any) => {
debug('spawned yarn stderr:', d)
process.stderr.write(d)
})
spawned.stdout.setEncoding('utf8')
spawned.stdout.on('data', (d: any) => {
debug('spawned yarn stdout:', d)
if (options.verbose) process.stdout.write(d)
else ux.action.status = d.replace(/\n$/, '').split('\n').pop()
})

spawned.on('error', reject)
spawned.on('exit', (code: number) => {
if (code === 0) {
resolve()
} else {
reject(new Error(`${executable} ${args.join(' ')} exited with code ${code}`))
}
})

// Fix windows bug with node-gyp hanging for input forever
// if (this.config.windows) {
// forked.stdin.write('\n')
// }
})
}

// eslint-disable-next-line default-param-last
async exec(args: string[] = [], opts: {cwd: string; verbose: boolean}): Promise<void> {
const cwd = opts.cwd
Expand Down Expand Up @@ -111,11 +80,7 @@ export default class Yarn {

debug(`${cwd}: ${this.bin} ${args.join(' ')}`)
try {
// TODO: always use spawn instead of fork once this has been thoroughly tested
this.config.scopedEnvVarTrue('PLUGINS_INSTALL_USE_SPAWN') ?
await this.spawn(this.bin, args, options) :
await this.fork(this.bin, args, options)

await this.fork(this.bin, args, options)
debug('yarn done')
} catch (error: any) {
debug('yarn error', error)
Expand Down

0 comments on commit 9830b0a

Please sign in to comment.