Skip to content

Commit

Permalink
feat: checking the uncommitted git diff only (webdoky#166)
Browse files Browse the repository at this point in the history
* feat: checking the uncommitted git diff only

* fix: check-diff also takes untracked files into account
  • Loading branch information
AdriandeCita authored Jan 11, 2022
1 parent 97857ae commit 9cd81a0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"chalk": "~4.1.2",
"glob": "~7.1.7",
"html-to-text": "~8.0.0",
"markdown-it": "~12.2.0"
"markdown-it": "~12.2.0",
"yargs": "^17.3.1"
},
"engines": {
"node": ">= 14"
Expand All @@ -15,6 +16,7 @@
"repository": "https://github.com/webdoky/content",
"scripts": {
"check": "node --experimental-specifier-resolution=node scripts/check.js",
"check-diff": "node --experimental-specifier-resolution=node scripts/check.js --changedOnly",
"check:debug": "DEBUG=true yarn run check",
"postinstall": "docker build . -t lt-custom",
"rebuild": "docker build . --no-cache -t lt-custom",
Expand Down
34 changes: 30 additions & 4 deletions scripts/check.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import checkFile from './utils/check-file';
import { executeWithResult } from './utils/execute';
import {
startLanguageTool,
stopLanguageTool,
waitForLanguageTool,
} from './utils/language-tool';
import checkAll from './check-all';

const CLI_ARGUMENT = 2;

const targetFile = process.argv[CLI_ARGUMENT];
const { argv } = yargs(hideBin(process.argv));
const LIST_GIT_UPDATES = 'git ls-files -m -o --exclude-standard';

async function check() {
let result = true;
let targetFiles;
if (argv.changedOnly) {
const gitUpdates = await executeWithResult(LIST_GIT_UPDATES);
const { stdout } = gitUpdates;

targetFiles = stdout
.split('\n')
.filter((filePath) => filePath.endsWith('.md'));
} else {
targetFiles = argv._.length;
}
try {
await startLanguageTool();
await waitForLanguageTool();
result = await (targetFile ? checkFile(targetFile) : checkAll());
if (targetFiles.length > 0) {
const tasks = targetFiles.map(async (file) => {
const intermediateResult = await checkFile(file);
if (result) {
// false result remains false regardless of any intermediate results
result = intermediateResult;
}
});

await Promise.all(tasks);
} else {
result = await checkAll();
}
} finally {
await stopLanguageTool();
}
Expand Down
12 changes: 12 additions & 0 deletions scripts/utils/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ export default async function execute(command) {
throw new Error('Failure');
}
}

export async function executeWithResult(command) {
return new Promise((resolve, reject) => {
childProcess.exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,11 @@ yargs-parser@^20.2.2:
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==

yargs-parser@^21.0.0:
version "21.0.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.0.tgz#a485d3966be4317426dd56bdb6a30131b281dc55"
integrity sha512-z9kApYUOCwoeZ78rfRYYWdiU/iNL6mwwYlkkZfJoyMR1xps+NEBX5X7XmRpxkZHhXJ6+Ey00IwKxBBSW9FIjyA==

yargs@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.2.0.tgz#1c82bf0f6b6a66eafce7ef30e376f49a12477f66"
Expand All @@ -4076,3 +4081,16 @@ yargs@^16.2.0:
string-width "^4.2.0"
y18n "^5.0.5"
yargs-parser "^20.2.2"

yargs@^17.3.1:
version "17.3.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.3.1.tgz#da56b28f32e2fd45aefb402ed9c26f42be4c07b9"
integrity sha512-WUANQeVgjLbNsEmGk20f+nlHgOqzRFpiGWVaBrYGYIGANIIu3lWjoyi0fNlFmJkvfhCZ6BXINe7/W2O2bV4iaA==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.3"
y18n "^5.0.5"
yargs-parser "^21.0.0"

0 comments on commit 9cd81a0

Please sign in to comment.