Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .github/workflows/ui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ jobs:
secrets: inherit
with:
jest-enabled: true
jest-test-command: yarn run test
jest-test-command: yarn run test:coverage
jest-coverage-report-dir: artifacts/coverage
sonar-sources: ./lib
compile-translations: false
generate-module-descriptor: false
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/global-dirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function isYarnVersion(version) {
logger.log('Yarn version', yarnVersion);
return semver.satisfies(yarnVersion, version);
} catch (err) {
logger.error('Unable to determine Yarn version.', err);
const logError = logger?.error ? logger.error : console.error;
logError('Unable to determine Yarn version.', err?.message ? err.message : err);
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/cli/stripes-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ module.exports = class StripesCore {
}

getCoreModulePath(moduleId) {
const coreModulePath = (this.corePath === this.coreAlias)
? path.join(this.corePath, moduleId)
: resolveFrom(this.corePath, `@folio/stripes-webpack/${moduleId}`);
const coreModulePath = (this.corePath.includes('node_modules'))
? resolveFrom(this.corePath, `@folio/stripes-webpack/${moduleId}`)
: path.join(this.corePath, moduleId);
return coreModulePath;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/app/bigtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const importLazy = require('import-lazy')(require);

const { contextMiddleware } = importLazy('../../cli/context-middleware');
const bigtest = importLazy('../../test/setup-bigtest');
const yarn = importLazy('../../yarn');
const packageManager = importLazy('../../package-manager');

function setupBigTestCommand(argv) {
const context = argv.context;
Expand All @@ -17,7 +17,7 @@ function setupBigTestCommand(argv) {
.then(() => {
if (argv.install) {
console.log('Installing dependencies...');
return yarn.add(context.cwd, bigtest.packages, true);
return packageManager.add(context.cwd, bigtest.packages, true);
} else {
return { isInstalled: false };
}
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const importLazy = require('import-lazy')(require);

const { contextMiddleware, applyContext } = importLazy('../../cli/context-middleware');
const createApp = importLazy('../../create-app');
const yarn = importLazy('../../yarn');
const packageManager = importLazy('../../package-manager');
const { promptMiddleware } = importLazy('../../cli/prompt-middleware');
const addModCommand = importLazy('../mod/add');
const enableModCommand = importLazy('../mod/enable');
Expand All @@ -27,7 +27,7 @@ function createAppCommand(argv) {
if (argv.install) {
console.log('Installing dependencies...');
const targetDir = isWorkspace ? context.cwd : path.resolve(context.cwd, appData.appDir);
return yarn.install(targetDir);
return packageManager.install(targetDir);
} else {
return { isInstalled: false, appDir: appData.appDir };
}
Expand Down
73 changes: 73 additions & 0 deletions lib/package-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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

const pnpmLock = path.join(projectDir || process.cwd(), 'pnpm-lock.yaml');
if (fs.existsSync(pnpmLock) && hasCommand('pnpm')) return 'pnpm';
if (hasCommand('yarn')) return 'yarn';

// 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);
logger.log(`Using package manager: ${pm}`);
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);
logger.log(`Using package manager: ${pm}`);
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,
};
69 changes: 0 additions & 69 deletions lib/yarn.js

This file was deleted.

14 changes: 7 additions & 7 deletions test/commands/app/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ const expect = require('chai').expect;
const path = require('path');

const context = require('../../../lib/cli/context');
const yarn = require('../../../lib/yarn');
const packageManager = require('../../../lib/package-manager');
const createApp = require('../../../lib/create-app');
const appCreateCommand = require('../../../lib/commands/app/create');
const addModCommand = require('../../../lib/commands/mod/add');
const enableModCommand = require('../../../lib/commands/mod/enable');
const assignPermissionCommand = require('../../../lib/commands/perm/assign');


const yarnStub = () => Promise.resolve({
const pmStub = () => Promise.resolve({
isInstalled: true,
appDir: 'ui-hello-world',
});
Expand All @@ -37,7 +37,7 @@ describe('The app create command', function () {
this.sut = appCreateCommand;
this.sandbox.stub(context, 'getContext').returns(this.argv.context);
this.sandbox.stub(createApp, 'createApp').callsFake(createAppStub);
this.sandbox.stub(yarn, 'install').callsFake(yarnStub);
this.sandbox.stub(packageManager, 'install').callsFake(pmStub);
this.sandbox.stub(addModCommand, 'handler').callsFake(addModStub);
this.sandbox.stub(enableModCommand, 'handler').callsFake(() => Promise.resolve());
this.sandbox.stub(assignPermissionCommand, 'handler').callsFake(() => Promise.resolve());
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('The app create command', function () {
this.argv.install = true;
this.sut.handler(this.argv)
.then(() => {
expect(yarn.install).to.have.been.calledWith(path.resolve(this.argv.context.cwd, 'ui-hello-world'));
expect(packageManager.install).to.have.been.calledWith(path.resolve(this.argv.context.cwd, 'ui-hello-world'));
expect(console.log).to.have.been.calledWithMatch('then "stripes serve" to run your new app');
done();
});
Expand All @@ -79,7 +79,7 @@ describe('The app create command', function () {
this.argv.context.isEmpty = false;
this.sut.handler(this.argv)
.then(() => {
expect(yarn.install).to.have.been.calledWith('/path/to/working/directory');
expect(packageManager.install).to.have.been.calledWith('/path/to/working/directory');
expect(console.log).to.have.been.calledWithMatch('then "stripes serve" to run your new app');
done();
});
Expand All @@ -89,7 +89,7 @@ describe('The app create command', function () {
this.argv.install = false;
this.sut.handler(this.argv)
.then(() => {
expect(yarn.install).not.to.have.been.called;
expect(packageManager.install).not.to.have.been.called;
expect(console.log).to.have.been.calledWithMatch('"cd ui-hello-world", "yarn install",');
done();
});
Expand All @@ -101,7 +101,7 @@ describe('The app create command', function () {
this.argv.context.isEmpty = false;
this.sut.handler(this.argv)
.then(() => {
expect(yarn.install).not.to.have.been.called;
expect(packageManager.install).not.to.have.been.called;
expect(console.log).to.have.been.calledWithMatch('"yarn install", "cd ui-hello-world",');
done();
});
Expand Down
Loading
Loading