Skip to content

Commit

Permalink
Extend typechecking script to work with different configs
Browse files Browse the repository at this point in the history
  • Loading branch information
sabio committed Oct 17, 2023
1 parent baff9e9 commit 22e7d43
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"scripts": {
"build": "npm run features && npm run ts-node -- ./scripts/build/build.ts",
"build:raw": "npm run build -- --no-uglify",
"code-linter": "eslint --cache --ext .js,.ts",
"features": "ts-node -- ./scripts/genFeatures.ts --files ./features.json -d",
"fix": "npm run lint -- --fix",
"fix:code": "npm run lint -- --fix --code",
Expand Down
54 changes: 32 additions & 22 deletions scripts/lint.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
import commandLineArgs from 'command-line-args';
import colors from 'chalk';
import { processCommands, FailedCommandData } from './utils/proc';
import { execSync } from 'child_process';

const options = commandLineArgs([
{ name: 'fix', type: Boolean },
{ name: 'typecheck', type: Boolean },
{ name: 'typecheck', type: String },
{ name: 'code', type: Boolean },
{ name: 'prettier', type: Boolean },
{ name: 'files', type: String },
]);

const commands = (files: string, fix: boolean) => ({
typecheck: 'tsc',
prettier: `prettier ${fix ? '--write --list-different' : '--check'} ${
files || '"./**/*.{ts,js,tsx}"'
}`,
code: `npm run code-linter -- ${fix ? '--fix' : ''} ${files || '.'}`,
});
const processCommand = (command: string) => {
console.log(colors.cyan(`\n👀 ${command}\n`));
execSync(command, { stdio: 'inherit' });
};

async function main() {
function main() {
const optionsCount = Object.keys(options).length;
const shouldRunAll =
(options.fix && optionsCount === 1) || optionsCount === 0;
const commandsMap = commands(options.files, options.fix);
const commandsToExecute = Object.entries(commandsMap)
.filter(([key, command]) => options[key] || shouldRunAll)
.map(([key, command]) => command);

await processCommands(commandsToExecute).catch(
({ exitCode }: FailedCommandData = {}) => {
if (exitCode) {
process.exit(exitCode);
}
},
);
const { typecheck, prettier, code, fix, files } = options;

if (typecheck !== undefined || shouldRunAll) {
const project = options.typecheck || 'tsconfig.json';
const tscCommand = `tsc --project ${project}`;
processCommand(tscCommand);
}

if (prettier || shouldRunAll) {
const prettierOptions = fix ? '--write --list-different' : '--check';
const pattern = files || '"./**/*.{ts,js,tsx}"';
const prettierCommand = `prettier ${prettierOptions} ${pattern}`;
processCommand(prettierCommand);
}

if (code || shouldRunAll) {
const eslintOptions = [
'--cache',
'--ext .js,.ts',
...(fix ? ['--fix'] : []),
].join(' ');
const pattern = files || '.';
const eslintCommand = `eslint ${eslintOptions} ${pattern}`;
processCommand(eslintCommand);
}

console.log(colors.greenBright('Success!\n'));
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/object/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ type CtxPath = {
* @type function(...?): ?
*/
export const ctxPath: CtxPath = curry2SwapArgs(getPath) as CtxPath;
export const len = ctxPath('length');
export const len = ctxPath('length') as <T>(
obj: T, // eslint-disable-line no-use-before-define
) => T extends ArrayLike<unknown> ? number : unknown;

/**
* Получаем из списка [1,2,3] объект {1: {2: 3}}
Expand Down

0 comments on commit 22e7d43

Please sign in to comment.