Skip to content

Commit

Permalink
add pos-cli modules overwrite (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slashek authored Dec 19, 2024
1 parent 76e11c9 commit 31c597d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## unreleased

* Feature: `pos-cli modules overwrites` utility commands
* Bug: Updated `unzipper` to newest version to fix occassional issue with pos-cli modules download

## 5.2.0
Expand Down
45 changes: 45 additions & 0 deletions bin/pos-cli-modules-overwrites-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const { program } = require('commander');
const logger = require('../lib/logger');
const { execSync } = require('child_process');
const overwrites = require('../lib/overwrites');

function getGitChangesAsJSON(overwrites = []) {
try {
// Run git status in porcelain mode
const output = execSync('git status --porcelain', { encoding: 'utf8' });

const filesStatus = output
.split('\n')
.reduce((acc, line) => {
const status = line.slice(0, 2).trim(); // Status code (e.g., M, D, A)
const file = line.slice(3).trim(); // File path
if (overwrites.includes(file)) {
acc.push({ status, file });
}
return acc;
}, []);

// Organize changes by type
const jsonOutput = {
modified: filesStatus.filter(item => item.status === 'M').map(item => item.file),
removed: filesStatus.filter(item => item.status === 'D').map(item => item.file),
added: filesStatus.filter(item => item.status === 'A').map(item => item.file),
untracked: filesStatus.filter(item => item.status === '??').map(item => item.file)
};

return jsonOutput;
} catch (error) {
logger.Error(`Error running git status: ${error}`);
}
}

program
.name('pos-cli modules overwrites diff')
.action(() => {
const gitChanges = getGitChangesAsJSON(overwrites.list());
logger.Info(gitChanges);
});

program.parse(process.argv);
13 changes: 13 additions & 0 deletions bin/pos-cli-modules-overwrites-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

const { program } = require('commander');
const logger = require('../lib/logger');
const overwrites = require('../lib/overwrites');

program
.name('pos-cli modules overwrites list')
.action(() => {
logger.Info(overwrites.list());
});

program.parse(process.argv);
9 changes: 9 additions & 0 deletions bin/pos-cli-modules-overwrites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

const { program } = require('commander');

program
.name('pos-cli modules overwrites')
.command('diff', 'list all overwrites that have to be updated based on git status')
.command('list', 'list all overwrites')
.parse(process.argv);
1 change: 1 addition & 0 deletions bin/pos-cli-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ program
.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')
.command('overwrites [command]', 'helps with managing module overwrites')
.parse(process.argv);
38 changes: 38 additions & 0 deletions lib/overwrites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const path = require('path');
const overwritesPath = path.join(process.cwd(), 'app/modules');
const prefixToRemove = path.join(process.cwd(), 'app');
const logger = require('../lib/logger');

function getOverwrites(dir, baseDir = dir) {
const files = [];
const items = fs.readdirSync(dir, { withFileTypes: true });

items.forEach(item => {
const fullPath = path.join(dir, item.name);
if (item.isDirectory()) {
files.push(...getOverwrites(fullPath, baseDir));
} else {
// Create the relative path based on baseDir and remove the 'app/' prefix
const relativePath = path.relative(baseDir, fullPath).replace(/\\/g, '/');
files.push(relativePath);
}
});

return files;
}


const Overwrites = {
list: (moduleName) => {
let moduleOverwritesPath = overwritesPath;
if(moduleName) {
logger.Info(moduleName)
moduleOverwritesPath = path.join(overwritesPath, moduleName);
}

return getOverwrites(moduleOverwritesPath, prefixToRemove);
}
}

module.exports = Overwrites;

0 comments on commit 31c597d

Please sign in to comment.