-
Notifications
You must be signed in to change notification settings - Fork 15
STCLI-285 Decouple from yarn classic #402
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
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3e5edb1
decouple from yarn classic
JohnC-80 e8f9581
Merge branch 'main' into STCLI-285
JohnC-80 a8094af
sonardad cleanup
JohnC-80 c74bcde
Merge branch 'STCLI-285' of https://github.com/folio-org/stripes-cli …
JohnC-80 ae9c6e3
throw errors
JohnC-80 a6928ef
actually throw error for the command check also
JohnC-80 d1d7fbc
add conditional to prevent unexpected logger errors in global-directo…
JohnC-80 1f1288a
Merge branch 'main' into STCLI-285
JohnC-80 656c4da
conform package-manager.js in error scenarios, add tests
JohnC-80 1237f78
conform for sonar-dad
JohnC-80 adfcd03
try the coverage command
JohnC-80 2859ba7
point coverage upload at the location
JohnC-80 eceef2a
clean up package-manager detection, tests
JohnC-80 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| const childProcess = require('node:child_process'); | ||
| const path = require('node:path'); | ||
| const fs = require('node:fs'); | ||
| const logger = require('./cli/logger')('package-manager'); | ||
|
|
||
| function hasCommand(cmd) { | ||
| try { | ||
| const v = childProcess.execSync(`${cmd} --version`, { encoding: 'utf8' }).trim(); | ||
| // simple check that command exists and returns something | ||
| return !!v; | ||
| } catch (e) { | ||
| logger.log(`Unable to determine ${cmd} version.`, e?.message ? e.message : e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function detect(projectDir) { | ||
| // Env override | ||
| const env = process.env.STRIPES_PKG_MANAGER; | ||
| if (env) return env; | ||
|
|
||
| // Prefer pnpm if a lockfile exists nearby | ||
| try { | ||
| const pnpmLock = path.join(projectDir || process.cwd(), 'pnpm-lock.yaml'); | ||
| if (fs.existsSync(pnpmLock) && hasCommand('pnpm')) return 'pnpm'; | ||
| } catch (e) { | ||
| throw new Error('There was an error in stripes-cli initialization: Unable to detect package manager.', { cause: e }); | ||
| } | ||
|
|
||
| if (hasCommand('pnpm')) return 'pnpm'; | ||
| if (hasCommand('yarn')) return 'yarn'; | ||
| if (hasCommand('npm')) return 'npm'; | ||
|
zburke marked this conversation as resolved.
Outdated
|
||
|
|
||
| // default to npm if nothing else | ||
| return 'npm'; | ||
| } | ||
|
|
||
| function execCmd(cmd, args, options) { | ||
| return new Promise((resolve, reject) => { | ||
| const full = `${cmd} ${args.join(' ')}`.trim(); | ||
| const p = childProcess.exec(full, options, (error) => { | ||
| if (error) return reject(error); | ||
| resolve({ isInstalled: true, appDir: options?.cwd }); | ||
| return undefined; | ||
| }); | ||
| if (p?.stdout) p.stdout.on('data', d => console.log(` ${d.toString().trim()}`)); | ||
| if (p?.stderr) p.stderr.on('data', d => console.error(` ${d.toString().trim()}`)); | ||
| }); | ||
| } | ||
|
|
||
| function install(projectDir) { | ||
| const pm = detect(projectDir); | ||
| console.log(`Using package manager: ${pm}`); | ||
|
zburke marked this conversation as resolved.
Outdated
|
||
| if (pm === 'pnpm') return execCmd('pnpm', ['install'], { cwd: projectDir }); | ||
| if (pm === 'yarn') return execCmd('yarn', [], { cwd: projectDir }); | ||
| return execCmd('npm', ['install'], { cwd: projectDir }); | ||
| } | ||
|
|
||
| function add(projectDir, packageName, isDev) { | ||
| const pm = detect(projectDir); | ||
| console.log(`Using package manager: ${pm}`); | ||
|
zburke marked this conversation as resolved.
Outdated
|
||
| const pkgs = packageName.flat().join(' '); | ||
| if (pm === 'pnpm') { | ||
| const flag = isDev ? ['-D'] : []; | ||
| return execCmd('pnpm', ['add'].concat(flag).concat([pkgs]), { cwd: projectDir }); | ||
| } | ||
| if (pm === 'yarn') { | ||
| const flag = isDev ? '--dev' : ''; | ||
| return execCmd('yarn', ['add', flag, pkgs].filter(Boolean), { cwd: projectDir }); | ||
| } | ||
| const flag = isDev ? ['--save-dev'] : ['--save']; | ||
| return execCmd('npm', ['install'].concat(flag).concat([pkgs]), { cwd: projectDir }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| detect, | ||
| install, | ||
| add, | ||
| }; | ||
|
zburke marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| const expect = require('chai').expect; | ||
| const childProcess = require('node:child_process'); | ||
| const fs = require('node:fs'); | ||
| const { EventEmitter } = require('node:events'); | ||
|
|
||
| const packageManager = require('../../lib/package-manager'); | ||
|
|
||
| // Stubs childProcess.exec to simulate a successful or failed command, | ||
| // emitting stdout/stderr asynchronously so listeners attached by execCmd | ||
| // are in place before data arrives. | ||
| function stubExec(sandbox, { error, stdout, stderr } = {}) { | ||
| return sandbox.stub(childProcess, 'exec').callsFake((command, options, callback) => { | ||
| const proc = new EventEmitter(); | ||
| proc.stdout = new EventEmitter(); | ||
| proc.stderr = new EventEmitter(); | ||
| setImmediate(() => { | ||
| if (stdout) proc.stdout.emit('data', stdout); | ||
| if (stderr) proc.stderr.emit('data', stderr); | ||
| callback(error || null); | ||
| }); | ||
| return proc; | ||
| }); | ||
| } | ||
|
|
||
| describe('The package-manager service', function () { | ||
| let originalEnv; | ||
|
|
||
| beforeEach(function () { | ||
| originalEnv = process.env.STRIPES_PKG_MANAGER; | ||
| delete process.env.STRIPES_PKG_MANAGER; | ||
| this.sandbox.stub(console, 'log'); | ||
| this.sandbox.stub(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| if (originalEnv === undefined) { | ||
| delete process.env.STRIPES_PKG_MANAGER; | ||
| } else { | ||
| process.env.STRIPES_PKG_MANAGER = originalEnv; | ||
| } | ||
| }); | ||
|
|
||
| describe('detect', function () { | ||
| it('honors the STRIPES_PKG_MANAGER environment variable', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| this.sandbox.stub(fs, 'existsSync'); | ||
| this.sandbox.stub(childProcess, 'execSync'); | ||
|
|
||
| expect(packageManager.detect('/project')).to.equal('npm'); | ||
| expect(fs.existsSync).not.to.have.been.called; | ||
| expect(childProcess.execSync).not.to.have.been.called; | ||
| }); | ||
|
|
||
| it('prefers pnpm when a pnpm-lock.yaml is present and pnpm is available', function () { | ||
| this.sandbox.stub(fs, 'existsSync').returns(true); | ||
| this.sandbox.stub(childProcess, 'execSync').returns('8.0.0'); | ||
|
|
||
| expect(packageManager.detect('/project')).to.equal('pnpm'); | ||
| expect(childProcess.execSync).to.have.been.calledWithMatch('pnpm --version'); | ||
| }); | ||
|
|
||
| it('falls back to yarn when pnpm is not installed', function () { | ||
| this.sandbox.stub(fs, 'existsSync').returns(false); | ||
| this.sandbox.stub(childProcess, 'execSync').callsFake((cmd) => { | ||
| if (cmd.startsWith('pnpm')) throw new Error('command not found: pnpm'); | ||
| if (cmd.startsWith('yarn')) return '1.22.0'; | ||
| return ''; | ||
| }); | ||
|
|
||
| expect(packageManager.detect('/project')).to.equal('yarn'); | ||
| }); | ||
|
|
||
| it('falls back to npm when neither pnpm nor yarn is installed', function () { | ||
| this.sandbox.stub(fs, 'existsSync').returns(false); | ||
| this.sandbox.stub(childProcess, 'execSync').callsFake((cmd) => { | ||
| if (cmd.startsWith('npm')) return '8.0.0'; | ||
| throw new Error('command not found'); | ||
| }); | ||
|
|
||
| expect(packageManager.detect('/project')).to.equal('npm'); | ||
| }); | ||
|
|
||
| it('defaults to npm when no other package manager is detected', function () { | ||
| this.sandbox.stub(fs, 'existsSync').returns(false); | ||
| this.sandbox.stub(childProcess, 'execSync').returns(''); | ||
|
|
||
| expect(packageManager.detect('/project')).to.equal('npm'); | ||
| }); | ||
|
|
||
| it('wraps errors encountered while probing for a pnpm lockfile', function () { | ||
| this.sandbox.stub(fs, 'existsSync').throws(new Error('disk error')); | ||
|
|
||
| expect(() => packageManager.detect('/project')).to.throw('Unable to detect package manager'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('install', function () { | ||
| it('installs with pnpm when pnpm is detected', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'pnpm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.install('/project').then((result) => { | ||
| expect(exec).to.have.been.calledWithMatch('pnpm install', { cwd: '/project' }); | ||
| expect(result).to.eql({ isInstalled: true, appDir: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('installs with yarn when yarn is detected', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'yarn'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.install('/project').then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('yarn', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('installs with npm by default', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.install('/project').then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('npm install', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('logs stdout and stderr from the child process', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| stubExec(this.sandbox, { stdout: 'installing...', stderr: 'a warning' }); | ||
|
|
||
| return packageManager.install('/project').then(() => { | ||
| expect(console.log).to.have.been.calledWithMatch('installing...'); | ||
| expect(console.error).to.have.been.calledWithMatch('a warning'); | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects when the child process fails', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| const failure = new Error('command failed'); | ||
| stubExec(this.sandbox, { error: failure }); | ||
|
|
||
| return packageManager.install('/project').then( | ||
| () => { throw new Error('expected install to reject'); }, | ||
| (err) => { expect(err).to.equal(failure); } | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('add', function () { | ||
| it('adds a dependency with pnpm, using the -D flag for dev dependencies', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'pnpm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', ['lodash'], true).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('pnpm add -D lodash', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a dependency with yarn, using the --dev flag for dev dependencies', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'yarn'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', ['lodash'], true).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('yarn add --dev lodash', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a dependency with yarn, omitting the flag for non-dev dependencies', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'yarn'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', ['lodash'], false).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('yarn add lodash', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a dependency with npm, using --save-dev for dev dependencies', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', ['lodash'], true).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('npm install --save-dev lodash', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('adds a dependency with npm, using --save by default', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', ['lodash'], false).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('npm install --save lodash', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('flattens nested package name arrays', function () { | ||
| process.env.STRIPES_PKG_MANAGER = 'npm'; | ||
| const exec = stubExec(this.sandbox); | ||
|
|
||
| return packageManager.add('/project', [['lodash', 'chalk']], false).then(() => { | ||
| expect(exec).to.have.been.calledWithMatch('npm install --save lodash chalk', { cwd: '/project' }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.