Skip to content

Commit

Permalink
Generate lock file from pos-modules.json
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrze committed Oct 19, 2023
1 parent b24d1d9 commit caf3b5d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
31 changes: 20 additions & 11 deletions bin/pos-cli-modules-setup.js → bin/pos-cli-modules-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
};

Expand All @@ -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`);
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions bin/pos-cli-modules-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion bin/pos-cli-modules.js
Original file line number Diff line number Diff line change
@@ -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] <name>', 'pull a module for the instance')
.command('remove [environment] <name>', '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 <name>', '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]}`);
}
1 change: 0 additions & 1 deletion bin/pos-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/dependencies.js
Original file line number Diff line number Diff line change
@@ -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]));
Expand All @@ -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]];
Expand Down

0 comments on commit caf3b5d

Please sign in to comment.