Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for renamed yarn.lock during install #648

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 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 @@ -124,15 +133,24 @@ export default class Plugins {
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`)
const doRefresh = async () => {
// use yarn.lock to fetch dependencies
await this.yarn.exec(prod ? ['--prod'] : [], {
cwd: root,
verbose: this.verbose,
})
}

if (await fileExists(path.join(root, 'oclif.lock'))) {
this.debug(`oclif.lock exists at ${root}. Installing prod dependencies`)
iowillhoit marked this conversation as resolved.
Show resolved Hide resolved
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 if (await fileExists(path.join(root, 'yarn.lock'))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for yarn.lock first? Say npm@10 comes out and it allows you to package a yarn.lock. I could see a situation where someone has an old oclif.lock checked into their repo (even though we discourage that here) and the shipped yarn.lock would get overwritten with an old oclif.lock.

this.debug(`yarn.lock exists at ${root}. Installing prod dependencies`)
await doRefresh()
} 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