Skip to content

Commit

Permalink
Fix Lint task (#1873)
Browse files Browse the repository at this point in the history
* Lint exits 1 on lint errors

* fix lint

* Create quiet-pots-cheer.md

* Update package.json

* more concise resolve
  • Loading branch information
TheSonOfThomp authored Jul 27, 2023
1 parent cfba537 commit 496c312
Show file tree
Hide file tree
Showing 23 changed files with 66 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-pots-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lg-tools/lint': patch
---

Lint task now exits with code `1` if lint errors are found
5 changes: 3 additions & 2 deletions tools/build/src/tsdoc/test-package/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "test-package",
"private": "true"
}
"version": "0.0.0",
"private": true
}
1 change: 1 addition & 0 deletions tools/build/src/tsdoc/tsdoc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import child_process, { ChildProcess } from 'child_process';
import path from 'path';

import { parseTSDoc } from './tsdocParser';

const spawnSpy = jest.spyOn(child_process, 'spawn');
Expand Down
3 changes: 1 addition & 2 deletions tools/create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable no-console */
import { getLGConfig } from '@lg-tools/meta';
import chalk from 'chalk';
import fs from 'fs';
import { camelCase, kebabCase, startCase } from 'lodash';
import path from 'path';

import { getLGConfig } from '@lg-tools/meta';

import { CreatePackageOptions } from './create.types';
import {
component,
Expand Down
3 changes: 1 addition & 2 deletions tools/install/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-console */
import { getPackageManager } from '@lg-tools/meta';
import { spawn } from 'child_process';
import fetch from 'node-fetch';

import { getPackageManager } from '@lg-tools/meta';
export interface InstallCommandOptions {
ignoreWorkspaceRootCheck: boolean;
verbose: boolean;
Expand Down
4 changes: 3 additions & 1 deletion tools/link/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-console */
import { getLGConfig } from '@lg-tools/meta';
import chalk from 'chalk';
import { spawn } from 'child_process';
import fs from 'fs';
import { homedir } from 'os';
import path from 'path';
import { getLGConfig } from '@lg-tools/meta';

import { formatLog } from './utils';

Expand Down Expand Up @@ -39,6 +39,7 @@ export async function linkPackages(destination: string, opts: LinkOptions) {
const { scopes: availableScopes } = getLGConfig();

const linkPromises: Array<Promise<void>> = [];

for (const [scopeName, scopePath] of Object.entries(availableScopes)) {
if (!scopeFlag || scopeFlag.includes(scopeName)) {
linkPromises.push(
Expand All @@ -57,6 +58,7 @@ export async function linkPackages(destination: string, opts: LinkOptions) {

console.log(chalk.green('Finished linking packages.'));
}

async function linkPackagesForScope(
scopeName: string,
scopePath: string,
Expand Down
2 changes: 1 addition & 1 deletion tools/link/src/unlink.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */
import { getLGConfig } from '@lg-tools/meta';
import chalk from 'chalk';
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
import { getLGConfig } from '@lg-tools/meta';

import { formatLog } from './utils';

Expand Down
17 changes: 9 additions & 8 deletions tools/lint/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import chalk from 'chalk';
import { spawn } from 'child_process';
import path from 'path';

import { LintCommandOptions } from './lint.types';
import { LintFn } from './lint.types';

const rootDir = process.cwd();
const eslintConfigPath = path.resolve(__dirname, '../config/eslint.config.js');
export const esLintExtensions = ['js', 'ts', 'tsx'];

/** Spawns an eslint job */
export function eslint({
fix,
verbose,
}: Pick<LintCommandOptions, 'fix' | 'verbose'>) {
return new Promise<void>(resolve => {
export const eslint: LintFn = ({ fix, verbose }) => {
return new Promise<boolean>((resolve, reject) => {
console.log(chalk.blue('Running ESLint...'));
spawn(
'eslint',
Expand All @@ -28,6 +25,10 @@ export function eslint({
{
stdio: 'inherit',
},
).on('close', resolve);
)
.on('exit', code => {
resolve(!code);
})
.on('error', reject);
});
}
};
9 changes: 7 additions & 2 deletions tools/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { LintCommandOptions } from './lint.types';
import { npmPkgJsonLint } from './npmPkgJsonLint';
import { prettier } from './prettier';

const isTrue = (test: any) => !!test;

export const lint = (options: LintCommandOptions) => {
const { fix, prettierOnly, eslintOnly, pkgJsonOnly, verbose } = options;

Expand All @@ -22,8 +24,11 @@ export const lint = (options: LintCommandOptions) => {
}

Promise.all(linters)
.then(() => {
process.exit(0);
.then(results => {
if (results.every(isTrue)) {
process.exit(0);
}
process.exit(1);
})
.catch(() => {
process.exit(1);
Expand Down
4 changes: 4 additions & 0 deletions tools/lint/src/lint.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export interface LintCommandOptions {
pkgJsonOnly: boolean;
verbose: boolean;
}

export type LintFn = (
options: Pick<LintCommandOptions, 'fix' | 'verbose'>,
) => Promise<boolean>;
15 changes: 7 additions & 8 deletions tools/lint/src/npmPkgJsonLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk';
import { spawn } from 'child_process';
import path from 'path';

import { LintCommandOptions } from './lint.types';
import { LintFn } from './lint.types';
// import { NpmPackageJsonLint } from 'npm-package-json-lint';
const rootDir = process.cwd();
const npmPkgLintConfigPath = path.resolve(
Expand All @@ -12,20 +12,19 @@ const npmPkgLintConfigPath = path.resolve(
);

/** Spawns a npmPkgJsonLint job */
export function npmPkgJsonLint({
fix,
verbose,
}: Pick<LintCommandOptions, 'fix' | 'verbose'>) {
return new Promise<void>((resolve, reject) => {
export const npmPkgJsonLint: LintFn = ({ fix, verbose }) => {
return new Promise<boolean>((resolve, reject) => {
console.log(chalk.yellow('Running npmPkgJsonLint...'));

spawn('npmPkgJsonLint', [rootDir, '--configFile', npmPkgLintConfigPath], {
cwd: rootDir,
stdio: 'inherit',
})
.on('close', resolve)
.on('exit', code => {
resolve(!code);
})
.on('error', reject);

/* TODO: use the JS API */
});
}
};
17 changes: 9 additions & 8 deletions tools/lint/src/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'child_process';
import path from 'path';

import { esLintExtensions } from './eslint';
import { LintCommandOptions } from './lint.types';
import { LintFn } from './lint.types';

const rootDir = process.cwd();
const prettierConfigPath = path.resolve(
Expand All @@ -14,11 +14,8 @@ const prettierConfigPath = path.resolve(
const prettierExtensions = [...esLintExtensions, 'mjs', 'json', 'md', 'yml'];

/** Spawns a prettier job */
export function prettier({
fix,
verbose,
}: Pick<LintCommandOptions, 'fix' | 'verbose'>) {
return new Promise<void>(resolve => {
export const prettier: LintFn = ({ fix, verbose }) => {
return new Promise<boolean>((resolve, reject) => {
console.log(chalk.magenta('Running Prettier...'));
spawn(
'prettier',
Expand All @@ -31,6 +28,10 @@ export function prettier({
{
stdio: 'inherit',
},
).on('close', resolve);
)
.on('exit', code => {
resolve(!code);
})
.on('error', reject);
});
}
};
2 changes: 2 additions & 0 deletions tools/meta/src/getAllPackages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';

import { getLGConfig } from './getLGConfig';
import { getPackageName } from './getPackageName';

Expand All @@ -12,6 +13,7 @@ export const getAllPackages = () => {

for (let scopePath of Object.values(scopes)) {
const scopeDir = path.resolve(rootDir, scopePath);

if (fs.existsSync(scopeDir)) {
const pkgNames = fs.readdirSync(scopeDir);
const pkgPaths = pkgNames.map(name => path.resolve(scopeDir, name));
Expand Down
2 changes: 2 additions & 0 deletions tools/meta/src/getPackageName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-console */
import chalk from 'chalk';
import fs from 'fs';

import { getLGConfig } from './getLGConfig';

/**
Expand Down
2 changes: 1 addition & 1 deletion tools/meta/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { getAllPackageNames, getAllPackages } from './getAllPackages';
export { getLGConfig, type LGConfig } from './getLGConfig';
export { getPackageManager } from './getPackageManager';
export { getAllPackages, getAllPackageNames } from './getAllPackages';
export { getPackageName } from './getPackageName';
2 changes: 1 addition & 1 deletion tools/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env node
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import path from 'path';

export interface TestCommandOptions {
watch: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tools/update/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */
import { getPackageManager } from '@lg-tools/meta';
import chalk from 'chalk';
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
import { getPackageManager } from '@lg-tools/meta';

export interface UpdateCommandOptions {
/**
Expand Down
3 changes: 2 additions & 1 deletion tools/validate/src/builds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*/

/* eslint-disable no-console */
import { getAllPackages, getPackageName } from '@lg-tools/meta';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import vm from 'vm';

import { getAllPackages, getPackageName } from '@lg-tools/meta';
import { ValidateCommandOptions } from '../validate.types';

import { ModuleType } from './modules.types';

// A list of
Expand Down
2 changes: 1 addition & 1 deletion tools/validate/src/builds/modules.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const ModuleType = {
amd: 'amd',
steal: 'steal',
} as const;
export type ModuleType = (typeof ModuleType)[keyof typeof ModuleType];
export type ModuleType = typeof ModuleType[keyof typeof ModuleType];
1 change: 1 addition & 0 deletions tools/validate/src/dependencies/checkPackage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { getPackageName } from '@lg-tools/meta';
import depcheck from 'depcheck';

import { ValidateCommandOptions } from '../validate.types';

import { depcheckOptions, DependencyIssues } from './config';
Expand Down
1 change: 1 addition & 0 deletions tools/validate/src/dependencies/fixDependencyIssues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function fixDependencies(

// Remove all unused dependencies
const unused = [...unusedDependencies, ...unusedDevDependencies];

if (unused.length > 0) {
verbose && console.log('Removing unused dependencies...', unused);
spawnSync('npx', ['[email protected]', 'remove', ...unused], spawnContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { getAllPackageNames } from '@lg-tools/meta';
import { readFileSync } from 'fs';
import { defaults } from 'lodash';

import { getAllPackageNames } from '@lg-tools/meta';

interface DependenciesOptions {
dev?: boolean;
peer?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion tools/validate/src/dependencies/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { readFileSync, writeFileSync } from 'fs';
import { isEqual } from 'lodash';
import path from 'path';

import { getPackageLGDependencies } from './getPackageDependencies';
import { ignoreFilePatterns, ignoreMatches } from '../config';

import { getPackageLGDependencies } from './getPackageDependencies';

const rootDir = process.cwd();

/**
Expand Down

0 comments on commit 496c312

Please sign in to comment.