Skip to content

Commit 47f8438

Browse files
committed
feat: Git-style diff viewer in CLI for single files (#27)
1 parent c0445de commit 47f8438

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ yarn cs diff
182182
npm run cs diff
183183
```
184184

185+
##### Argument: `<single>`
186+
187+
Add a single config name as the argument of the `diff` command to see the difference of that single file in a git-style diff viewer.
188+
189+
_Example:_
190+
191+
```bash
192+
# using yarn
193+
yarn cs diff user-role.public
194+
195+
# using npm
196+
npm run cs diff user-role.public
197+
```
198+
185199
## 🖥️ Admin panel (GUI)
186200
This plugin ships with a React app which can be accessed from the settings page in Strapi admin panel. On this page you can pretty much do the same as you can from the CLI. You can import, export and see the difference between the config as found in the sync directory, and the config as found in the database.
187201

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"chalk": "^4.1.2",
2828
"cli-table": "^0.3.6",
2929
"commander": "^8.3.0",
30+
"git-diff": "^2.0.6",
3031
"immutable": "^3.8.2",
3132
"inquirer": "^8.2.0",
3233
"react-diff-viewer": "^3.1.1",

server/cli.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const chalk = require('chalk');
77
const inquirer = require('inquirer');
88
const { isEmpty } = require('lodash');
99
const strapi = require('@strapi/strapi'); // eslint-disable-line
10+
const gitDiff = require('git-diff');
1011

1112
const warnings = require('./warnings');
1213
const packageJSON = require('../package.json');
@@ -214,9 +215,9 @@ program
214215
program
215216
.command('diff')
216217
.alias('d')
217-
// .option('-t, --type <type>', 'The type of config') // TODO: partial diff
218218
.description('The config diff')
219-
.action(async ({ type }) => {
219+
.action(async (options, { args }) => {
220+
const single = args[0];
220221
const app = await strapi().load();
221222
const diff = await app.plugin('config-sync').service('main').getFormattedDiff();
222223

@@ -226,6 +227,24 @@ program
226227
process.exit(0);
227228
}
228229

230+
// Single config diff.
231+
if (single) {
232+
// No changes.
233+
if (!diff.fileConfig[single] && !diff.databaseConfig[single]) {
234+
console.log(`${chalk.cyan.bold('[notice]')} No differences between DB and sync directory for ${single}.`);
235+
process.exit(0);
236+
}
237+
238+
// Git diff.
239+
console.log(gitDiff(
240+
JSON.stringify(diff.fileConfig[single], null, 2),
241+
JSON.stringify(diff.databaseConfig[single], null, 2),
242+
{ color: true },
243+
));
244+
245+
process.exit(0);
246+
}
247+
229248
// Init table.
230249
const table = initTable();
231250

yarn.lock

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ capture-exit@^2.0.0:
16691669
dependencies:
16701670
rsvp "^4.8.4"
16711671

1672-
chalk@^2.0.0:
1672+
chalk@^2.0.0, chalk@^2.3.2:
16731673
version "2.4.2"
16741674
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
16751675
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
@@ -2156,6 +2156,11 @@ diff-sequences@^26.6.2:
21562156
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1"
21572157
integrity sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q==
21582158

2159+
diff@^3.5.0:
2160+
version "3.5.0"
2161+
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
2162+
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==
2163+
21592164
diff@^4.0.1:
21602165
version "4.0.2"
21612166
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
@@ -2944,14 +2949,25 @@ get-value@^2.0.3, get-value@^2.0.6:
29442949
resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28"
29452950
integrity sha1-3BXKHGcjh8p2vTesCjlbogQqLCg=
29462951

2952+
git-diff@^2.0.6:
2953+
version "2.0.6"
2954+
resolved "https://registry.yarnpkg.com/git-diff/-/git-diff-2.0.6.tgz#4a8ece670d64d1f9f4e68191ad8b1013900f6c1e"
2955+
integrity sha512-/Iu4prUrydE3Pb3lCBMbcSNIf81tgGt0W1ZwknnyF62t3tHmtiJTRj0f+1ZIhp3+Rh0ktz1pJVoa7ZXUCskivA==
2956+
dependencies:
2957+
chalk "^2.3.2"
2958+
diff "^3.5.0"
2959+
loglevel "^1.6.1"
2960+
shelljs "^0.8.1"
2961+
shelljs.exec "^1.1.7"
2962+
29472963
glob-parent@^5.1.2:
29482964
version "5.1.2"
29492965
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
29502966
integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
29512967
dependencies:
29522968
is-glob "^4.0.1"
29532969

2954-
glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
2970+
glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
29552971
version "7.2.0"
29562972
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.0.tgz#d15535af7732e02e948f4c41628bd910293f6023"
29572973
integrity sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==
@@ -4203,6 +4219,11 @@ log-symbols@^4.1.0:
42034219
chalk "^4.1.0"
42044220
is-unicode-supported "^0.1.0"
42054221

4222+
loglevel@^1.6.1:
4223+
version "1.8.0"
4224+
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.8.0.tgz#e7ec73a57e1e7b419cb6c6ac06bf050b67356114"
4225+
integrity sha512-G6A/nJLRgWOuuwdNuA6koovfEV1YpqqAG4pRUlFaz3jj2QNZ8M4vBqnVA+HBTmU/AMNUtlOsMmSpF6NyOjztbA==
4226+
42064227
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
42074228
version "1.4.0"
42084229
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
@@ -5201,6 +5222,13 @@ readable-stream@^3.4.0, readable-stream@^3.6.0:
52015222
string_decoder "^1.1.1"
52025223
util-deprecate "^1.0.1"
52035224

5225+
rechoir@^0.6.2:
5226+
version "0.6.2"
5227+
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
5228+
integrity sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=
5229+
dependencies:
5230+
resolve "^1.1.6"
5231+
52045232
redux-immutable@^4.0.0:
52055233
version "4.0.0"
52065234
resolved "https://registry.yarnpkg.com/redux-immutable/-/redux-immutable-4.0.0.tgz#3a1a32df66366462b63691f0e1dc35e472bbc9f3"
@@ -5306,7 +5334,7 @@ resolve-url@^0.2.1:
53065334
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
53075335
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=
53085336

5309-
resolve@^1.10.0, resolve@^1.12.0, resolve@^1.18.1, resolve@^1.20.0, resolve@^1.22.0:
5337+
resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.18.1, resolve@^1.20.0, resolve@^1.22.0:
53105338
version "1.22.0"
53115339
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198"
53125340
integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw==
@@ -5515,6 +5543,20 @@ shebang-regex@^3.0.0:
55155543
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
55165544
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
55175545

5546+
shelljs.exec@^1.1.7:
5547+
version "1.1.8"
5548+
resolved "https://registry.yarnpkg.com/shelljs.exec/-/shelljs.exec-1.1.8.tgz#6f3c8dd017cb96d2dea82e712b758eab4fc2f68c"
5549+
integrity sha512-vFILCw+lzUtiwBAHV8/Ex8JsFjelFMdhONIsgKNLgTzeRckp2AOYRQtHJE/9LhNvdMmE27AGtzWx0+DHpwIwSw==
5550+
5551+
shelljs@^0.8.1:
5552+
version "0.8.5"
5553+
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.5.tgz#de055408d8361bed66c669d2f000538ced8ee20c"
5554+
integrity sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==
5555+
dependencies:
5556+
glob "^7.0.0"
5557+
interpret "^1.0.0"
5558+
rechoir "^0.6.2"
5559+
55185560
shellwords@^0.1.1:
55195561
version "0.1.1"
55205562
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"

0 commit comments

Comments
 (0)