Skip to content

Commit

Permalink
fixup yargs, implement require
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke committed Feb 8, 2025
1 parent f9e2cac commit acfbde7
Show file tree
Hide file tree
Showing 23 changed files with 189 additions and 32 deletions.
3 changes: 3 additions & 0 deletions lib/cli/stripes-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import path from 'path';
import resolvePkg from 'resolve-pkg';
import resolveFrom from 'resolve-from';
import importLazy from 'import-lazy';
import { createRequire } from 'module';

import getLogger from './logger.js';
import getStripesWebpackConfig from '../test/webpack-config.js';

const logger = getLogger();
const require = createRequire(import.meta.url);

// Wraps stripes-core modules and API:
// Includes centralized logic to locate stripes-core for build and runtime
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { bigtest, create, perms } from './app/index.js';

export default {
command: 'app <command>',
describe: 'Commands to create and manage stripes UI apps',
builder: yargs => yargs.commandDir('app'),
builder: yargs => yargs.command([bigtest, create, perms]),
handler: () => {},
};
3 changes: 3 additions & 0 deletions lib/commands/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as bigtest } from './bigtest.js';
export { default as create } from './create.js';
export { default as perms } from './perms.js';
31 changes: 31 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import alias from './alias.js';
import app from './app.js';
import build from './build.js';
import inventory from './inventory.js';
import mod from './mod.js';
import okapi from './okapi.js';
import perm from './perm.js';
import platform from './platform.js';
import serve from './serve.js';
import status from './status.js';
import test from './test.js';
import translate from './translate.js';
import transpile from './transpile.js';
import workspace from './workspace.js';

export const commands = [
alias,
app,
build,
inventory,
mod,
okapi,
perm,
platform,
serve,
status,
test,
translate,
transpile,
workspace,
];
32 changes: 31 additions & 1 deletion lib/commands/mod.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import {
add,
descriptor,
disable,
discover,
enable,
filter,
install,
list,
perms,
pull,
remove,
update,
view,
} from './mod/index.js';

export default {
command: 'mod <command>',
describe: 'Commands to manage UI module descriptors',
builder: yargs => yargs.commandDir('mod'),
builder: yargs => yargs.command([
add,
descriptor,
disable,
discover,
enable,
filter,
install,
list,
perms,
pull,
remove,
update,
view,
]),
handler: () => {},
};
14 changes: 14 additions & 0 deletions lib/commands/mod/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export { default as add } from './add.js';
export { default as descriptor } from './descriptor.js';
export { default as disable } from './disable.js';
export { default as discover } from './discover.js';
export { default as enable } from './enable.js';
export { default as filter } from './filter.js';
export { default as install } from './install.js';
export { default as list } from './list.js';
export { default as perms } from './perms.js';
export { default as pull } from './pull.js';
export { default as remove } from './remove.js';
export { default as update } from './update.js';
export { default as view } from './view.js';

22 changes: 21 additions & 1 deletion lib/commands/okapi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import {
cookies,
login,
logout,
pathDelete,
pathGet,
pathPost,
pathPut,
token,
} from './okapi/index.js';

export default {
command: 'okapi <command>',
describe: 'Okapi commands (login and logout)',
builder: yargs => yargs.commandDir('okapi'),
builder: yargs => yargs.command([
cookies,
login,
logout,
pathDelete,
pathGet,
pathPost,
pathPut,
token,
]),
handler: () => {},
};
8 changes: 8 additions & 0 deletions lib/commands/okapi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { default as cookies } from './cookies.js';
export { default as login } from './login.js';
export { default as logout } from './logout.js';
export { default as pathDelete } from './pathDelete.js';
export { default as pathGet } from './pathGet.js';
export { default as pathPost } from './pathPost.js';
export { default as pathPut } from './pathPut.js';
export { default as token } from './token.js';
16 changes: 15 additions & 1 deletion lib/commands/perm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import {
assign,
create,
filter,
list,
unassign,
} from './perm/index.js';

export default {
command: 'perm <command>',
describe: 'Commands to manage UI module permissions',
builder: yargs => yargs.commandDir('perm'),
builder: yargs => yargs.command([
assign,
create,
filter,
list,
unassign,
]),
handler: () => {},
};
5 changes: 5 additions & 0 deletions lib/commands/perm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as assign } from './assign.js';
export { default as create } from './create.js';
export { default as filter } from './filter.js';
export { default as list } from './list.js';
export { default as unassign } from './unassign.js';
14 changes: 13 additions & 1 deletion lib/commands/platform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import {
backend,
clean,
install,
pull,
} from './platform/index.js';

export default {
command: 'platform <command>',
describe: 'Commands to manage stripes UI platforms',
builder: yargs => yargs.commandDir('platform'),
builder: yargs => yargs.command([
backend,
clean,
install,
pull,
]),
handler: () => {},
};
2 changes: 1 addition & 1 deletion lib/commands/platform/backend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contextMiddleware } from '../../cli/context-middleware.js';
import { stripesConfigMiddleware } from '../../cli/stripes-config-middleware.js';
import Okapi from '../../okapi';
import Okapi from '../../okapi/index.js';
import OkapiError from '../../okapi/okapi-error.js';
import ModuleService from '../../okapi/module-service.js';
import DescriptorService from '../../okapi/descriptor-service.js';
Expand Down
4 changes: 4 additions & 0 deletions lib/commands/platform/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as backend } from './backend.js';
export { default as clean } from './clean.js';
export { default as install } from './install.js';
export { default as pull } from './pull.js';
8 changes: 6 additions & 2 deletions lib/commands/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
karma
} from './test/index.js';

import { contextMiddleware } from '../cli/context-middleware.js';
import { stripesConfigMiddleware } from '../cli/stripes-config-middleware.js';
import { serverOptions, okapiOptions, stripesConfigFile, stripesConfigOptions } from './common-options.js';
import cliConfig from '../cli/config.js';

Check warning on line 8 in lib/commands/test.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'cliConfig' is defined but never used. Allowed unused vars must match /React/u

Check warning on line 8 in lib/commands/test.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'cliConfig' is defined but never used. Allowed unused vars must match /React/u
import karma from './test/karma.js';

function testCommand(argv) {
// Maintain backwards compatibility with original commands
Expand All @@ -20,7 +23,8 @@ export default {
contextMiddleware(),
stripesConfigMiddleware(true)
])
.commandDir('test', cliConfig.commandDirOptions)
//@@

Check failure on line 26 in lib/commands/test.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Expected exception block, space or tab after '//' in comment

Check failure on line 26 in lib/commands/test.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Expected exception block, space or tab after '//' in comment
.command([karma])
.positional('configFile', stripesConfigFile.configFile)
.options(Object.assign({}, serverOptions, okapiOptions, stripesConfigOptions))
.example('$0 test karma', 'Run Karma tests for the current app module');
Expand Down
1 change: 1 addition & 0 deletions lib/commands/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as karma } from './karma.js';
5 changes: 4 additions & 1 deletion lib/commands/translate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { compile, pcheck, stats } from './translate/index.js';

export default {
command: 'translate <command>',
describe: 'Commands to manage translations in UI platforms',
builder: yargs => yargs.commandDir('translate'),
// @@builder: yargs => yargs.commandDir('translate'),
builder: yargs => yargs.command([compile, pcheck, stats]),
handler: () => {},
};
3 changes: 3 additions & 0 deletions lib/commands/translate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as compile } from './compile.js';
export { default as pcheck } from './pcheck.js';
export { default as stats } from './stats.js';
2 changes: 1 addition & 1 deletion lib/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import kebabCase from 'just-kebab-case';
import semver from 'semver';
import pascalCase from 'just-pascal-case';
import simpleGit from 'simple-git';
import { templates } from './environment/inventory.js';

import { templates } from './environment/inventory.js';

const pkgPath = path.join(import.meta.dirname, '..', 'package.json');
const { version: currentCLIVersion } = fs.readJsonSync(pkgPath, { throws: false });
Expand Down
6 changes: 3 additions & 3 deletions lib/environment/development.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import kopy from 'kopy';
import path from 'path';
import fs from 'fs';
import fs from 'fs-extra';
import simpleGit from 'simple-git';
import semver from 'semver';

Expand All @@ -11,8 +11,8 @@ import AliasService from '../platform/alias-service.js';

import logger from '../cli/logger.js';


const { version: currentCLIVersion } = fs.readJsonSync('../../package.json', { throws: false });
const pkgPath = path.join(import.meta.dirname, '..', '..', 'package.json');
const { version: currentCLIVersion } = fs.readJsonSync(pkgPath, { throws: false });

// Compare a list of module names against those known to be valid
function validateModules(theModules) {
Expand Down
3 changes: 3 additions & 0 deletions lib/platform/alias-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs';
import { createRequire } from 'module';

import PlatformStorage from './platform-storage.js';
import cliConfig from '../cli/config.js';
Expand All @@ -12,7 +13,9 @@ const cwd = path.resolve();
// Manages parsing and validation logic for aliases
export default class AliasService {
constructor(platformStorage) {
//@@ require

Check failure on line 16 in lib/platform/alias-service.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Expected exception block, space or tab after '//' in comment

Check failure on line 16 in lib/platform/alias-service.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Expected exception block, space or tab after '//' in comment
// Cheap way to mock require later on
const require = createRequire(import.meta.url);
this.require = require;
// Aliases stored with "alias add" command
this.storage = platformStorage || new PlatformStorage();
Expand Down
31 changes: 14 additions & 17 deletions lib/stripes-cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env node

import path from 'path';
import yargs from 'yargs/yargs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import isInstalledGlobally from 'is-installed-globally';
import updateNotifier from 'update-notifier';

import fs from 'fs-extra';

import AliasError from './platform/alias-error.js';
import cliConfig from './cli/config.js';
import getLogger from './cli/logger.js';
import { commands } from './commands/index.js';

const { yargsConfig, commandDirOptions } = cliConfig;

Check warning on line 16 in lib/stripes-cli.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'commandDirOptions' is assigned a value but never used. Allowed unused vars must match /React/u

Check warning on line 16 in lib/stripes-cli.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'commandDirOptions' is assigned a value but never used. Allowed unused vars must match /React/u

Expand All @@ -19,25 +22,19 @@ const logger = getLogger();
process.title = 'stripes-cli';
logger.log('stripes-cli', packageJson.version);

// update-notifier is pure ESM so needs to be loaded via dynamic import
// until we refactor from CJS to ESM. Until then, say hello to our old
// friend, the IIFE.
(async () => {
// Update notifier runs async in a child process
const updateNotifier = await import('update-notifier');
updateNotifier.default({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7,
}).notify({
isGlobal: isInstalledGlobally,
// TODO: Consider reverting to default message once update-notifier detects global yarn installs
message: 'Update available - Refer to README.md:\nhttps://github.com/folio-org/stripes-cli',
});
})();
// Update notifier runs async in a child process
updateNotifier({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7,
}).notify({
isGlobal: isInstalledGlobally,
// TODO: Consider reverting to default message once update-notifier detects global yarn installs
message: 'Update available - Refer to README.md:\nhttps://github.com/folio-org/stripes-cli',
});

try {
yargs(hideBin(process.argv))
.commandDir('./commands', commandDirOptions) // NOSONAR
.command(commands)
.config(yargsConfig)
.option('interactive', {
describe: 'Enable interactive input (use --no-interactive to disable)',
Expand Down
3 changes: 2 additions & 1 deletion lib/test/setup-bigtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import copy from 'kopy';
import path from 'path';
import fs from 'fs-extra';
import simpleGit from 'simple-git';
import { templates } from '../environment/inventory';

import { templates } from '../environment/inventory.js';

const tempDir = '_bigtest_temp';
const bigTestDir = 'test/bigtest';
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"chai": "^4.1.2",
"colors": "1.4.0",
"eslint": "^7.32.0",
"jscodeshift": "^17.1.2",
"sinon": "^15.0.4",
"sinon-chai": "^3.7.0"
}
Expand Down

0 comments on commit acfbde7

Please sign in to comment.