-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pos-cli modules overwrite (#664)
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |