From caf3b5d078bbb704566e797a4f7c57314d0cc0bd Mon Sep 17 00:00:00 2001 From: piotrze Date: Thu, 19 Oct 2023 16:43:18 +0200 Subject: [PATCH] Generate lock file from pos-modules.json --- ...es-setup.js => pos-cli-modules-install.js} | 31 ++++++++++++------- bin/pos-cli-modules-version.js | 4 +-- bin/pos-cli-modules.js | 9 +++++- bin/pos-cli.js | 1 - lib/modules.js | 2 +- lib/modules/dependencies.js | 2 ++ 6 files changed, 33 insertions(+), 16 deletions(-) rename bin/{pos-cli-modules-setup.js => pos-cli-modules-install.js} (55%) diff --git a/bin/pos-cli-modules-setup.js b/bin/pos-cli-modules-install.js similarity index 55% rename from bin/pos-cli-modules-setup.js rename to bin/pos-cli-modules-install.js index 5757f806..e93dc8c6 100755 --- a/bin/pos-cli-modules-setup.js +++ b/bin/pos-cli-modules-install.js @@ -12,9 +12,11 @@ const spinner = ora({ text: 'Setup', stream: process.stdout, spinner: 'bouncingB const files = require('../lib/files'); const { resolveDependencies } = require('../lib/modules/dependencies') +const posModulesFilePath = 'app/pos-modules.json'; +const posModulesLockFilePath = 'app/pos-modules.lock.json'; + const readLocalModules = () => { - const modulesConfigFileName = `pos-modules.json`; - const config = files.readJSON(modulesConfigFileName, { throwDoesNotExistError: true }); + const config = files.readJSON(posModulesFilePath, { throwDoesNotExistError: true }); return config['modules']; }; @@ -32,16 +34,23 @@ program progress.__moduleInfo = spinner.start('Loading module version info'); const localModules = readLocalModules(); - logger.Info('Resolving module dependencies', { hideTimestamp: true }) - lock['modules'] = await resolveDependencies(localModules, (list) => gateway.getModuleVersions(list)); - - if (errors.length && false) { - errors.map(e => logger.Warn(e, { hideTimestamp: true })); - logger.Error('Some errors occured during module setup'); + if(!localModules) { + progress.__moduleInfo.stop(); } else { - fs.writeFileSync(path.join(process.cwd(), 'pos-modules.lock.json'), JSON.stringify(lock, null, 2)); - logger.Info('Modules lock file created'); - progress.__moduleInfo.succeed(`Modules installed`); + logger.Info('Resolving module dependencies', { hideTimestamp: true }) + lock['modules'] = await resolveDependencies(localModules, (list) => gateway.getModuleVersions(list)); + + if (errors.length) { + errors.map(e => logger.Warn(e, { hideTimestamp: true })); + logger.Error('Some errors occured during module setup'); + } else { + fs.writeFileSync( + path.join(process.cwd(), posModulesLockFilePath), + JSON.stringify(lock, null, 2) + ); + logger.Info('Modules lock file created'); + progress.__moduleInfo.succeed(`Modules installed`); + } } }); diff --git a/bin/pos-cli-modules-version.js b/bin/pos-cli-modules-version.js index 4a8361c8..698c5986 100644 --- a/bin/pos-cli-modules-version.js +++ b/bin/pos-cli-modules-version.js @@ -27,7 +27,7 @@ const storeNewVersion = (config, version) => { files.writeJSON(configPath, config); }; -const validateVersions = (config, version) => { +const validateVersions = (config, version, moduleName) => { // Validate versions. if (!semver.valid(config.version)) { report('[ERR] The current version is not valid'); @@ -48,7 +48,7 @@ function crateNewVersion(version, options) { const moduleName = config['machine_name']; if (options.package) version = readVersionFromPackage(options); - if (!validateVersions(config, version)) return; + if (!validateVersions(config, version, moduleName)) return; storeNewVersion(config, version); } diff --git a/bin/pos-cli-modules.js b/bin/pos-cli-modules.js index c6e3a88b..b46bde79 100755 --- a/bin/pos-cli-modules.js +++ b/bin/pos-cli-modules.js @@ -1,14 +1,21 @@ #!/usr/bin/env node const program = require('commander'); +const logger = require('../lib/logger'); program .name('pos-cli modules') .command('list [environment]', 'list installed modules on the instance') .command('pull [environment] ', 'pull a module for the instance') .command('remove [environment] ', 'remove module from the instance (removes configuration and data)') - .command('setup', 'initialize module lock file for the instance') + .command('install', 'adds module to pos-modules file and resolve dependencies') .command('init ', 'initialize a module with the starter structure') .command('version [version] --package', 'create a new version of the module') .command('push', 'publish module version') .parse(process.argv); + +const commandList = Object.keys(program._execs); +if (!commandList.includes(program.args[0])) { + program.outputHelp(); + logger.Error(`unknown command: ${program.args[0]}`); +} diff --git a/bin/pos-cli.js b/bin/pos-cli.js index 797d51e1..f51160c0 100755 --- a/bin/pos-cli.js +++ b/bin/pos-cli.js @@ -34,7 +34,6 @@ program .parse(process.argv); const commandList = Object.keys(program._execs); - if (!commandList.includes(program.args[0])) { program.outputHelp(); logger.Error(`unknown command: ${program.args[0]}`); diff --git a/lib/modules.js b/lib/modules.js index 61832bcc..9b6fd9df 100644 --- a/lib/modules.js +++ b/lib/modules.js @@ -19,7 +19,7 @@ const moduleConfig = () => { const createArchive = async (moduleName) => { return new Promise(async (resolve, reject) => { const releaseArchive = prepareArchive(archivePath, resolve, true); - releaseArchive.glob('{private,public}/**', {}, { prefix: moduleName }); + releaseArchive.glob(['{private,public}/**', 'template-values.json'], {}, { prefix: moduleName }); await releaseArchive.finalize(); }); }; diff --git a/lib/modules/dependencies.js b/lib/modules/dependencies.js index a2a41cf7..7fb38d86 100644 --- a/lib/modules/dependencies.js +++ b/lib/modules/dependencies.js @@ -1,6 +1,7 @@ const flatten = require('lodash.flatten'); const uniq = require('lodash.uniq'); const semver = require('semver'); +const logger = require('../logger'); const resolveBestVersion = async (dependencyList, getVersions) => { const dependenciesNames = uniq(dependencyList.map(dep => Object.keys(dep)[0])); @@ -25,6 +26,7 @@ const resolveDependencies = async (modules, getVersions) => { const deps = Object.assign({}, modules); const modulesNames = Object.keys(modules); const modulesVersions = await getVersions(modulesNames); + logger.Debug(`modulesVersions: ${JSON.stringify(modulesVersions)}`); const dependenciesList = flatten( modulesVersions.map(module => { const version = module.versions[modules[module.module]];