Skip to content

Commit

Permalink
chore: use shared configs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Sep 27, 2023
1 parent 0814fd8 commit 81c0235
Show file tree
Hide file tree
Showing 17 changed files with 1,737 additions and 130 deletions.
5 changes: 0 additions & 5 deletions .commitlintrc

This file was deleted.

3 changes: 3 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
15 changes: 5 additions & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"parser": "@typescript-eslint/parser",
"extends": ["plugin:@typescript-eslint/recommended", "prettier"],
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": ["error", {"argsIgnorePattern": "^_"}],
"@typescript-eslint/explicit-function-return-type": [
"error",
{"allowExpressions": true}
]
}
"extends": [
"@rambler-tech/eslint-config",
"@rambler-tech/eslint-config/ts",
"prettier"
]
}
13 changes: 1 addition & 12 deletions .licenserc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
{
"summary": true,
"deny": [
"MPL.+2",
"EPL.+2",
"IPL.+1",
"GPL.+2",
"GPL.+3",
"AGPL.+3",
"LGPL.+2.1",
"UNKNOWN",
"CUSTOM"
]
"extends": "@rambler-tech/licenselint-config"
}
9 changes: 0 additions & 9 deletions .lintstagedrc

This file was deleted.

4 changes: 4 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.{js,ts}": ["prettier --write", "eslint --fix"],
"*.json": ["prettier --write"]
}
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@rambler-tech/prettier-config"
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"devDependencies": {
"@commitlint/cli": "^17.4.1",
"@commitlint/config-conventional": "^17.4.0",
"@rambler-tech/eslint-config": "^0.0.2",
"@rambler-tech/licenselint-config": "^0.0.2",
"@rambler-tech/prettier-config": "^0.0.2",
"@rambler-tech/ts-config": "^0.0.2",
"@types/debug": "^4.1.7",
"@types/jest": "^29.2.5",
"@types/license-checker": "^25.0.3",
Expand Down
4 changes: 4 additions & 0 deletions src/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const results = [

test('format success results', () => {
const output = format(results, {})

expect(output).toContain(`[email protected] ${chalk.dim('MIT')}`)
expect(output).toContain(`[email protected] ${chalk.dim('ISC')}`)
expect(output).toContain(`[email protected] ${chalk.dim('MIT')}`)
Expand All @@ -33,6 +34,7 @@ test('format error results', () => {
}
]
const output = format(resultsWithError, {})

expect(output).toContain(`[email protected] ${chalk.dim('MIT')}`)
expect(output).toContain(`[email protected] ${chalk.dim('ISC')}`)
expect(output).toContain(`1 error`)
Expand All @@ -41,6 +43,7 @@ test('format error results', () => {

test('format summary success results', () => {
const output = format(results, {summary: true})

expect(output).toContain(`MIT ${chalk.dim('2')}`)
expect(output).toContain(`ISC ${chalk.dim('1')}`)
})
Expand All @@ -55,6 +58,7 @@ test('format summary error results', () => {
}
]
const output = format(resultsWithError, {summary: true})

expect(output).toContain(`MIT ${chalk.dim('1')}`)
expect(output).toContain(`ISC ${chalk.dim('1')}`)
expect(output).toContain(`1 error`)
Expand Down
6 changes: 2 additions & 4 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const formatSummaryLines = (
summaryLines[line.license].push(line)
})

const output = Object.values(summaryLines)
return Object.values(summaryLines)
.sort((a, b) => b.length - a.length)
.map((line) =>
[
Expand All @@ -54,8 +54,6 @@ const formatSummaryLines = (
].join(' ')
)
.join('\n')

return output
}

export const format = (results: LicenseResult[], options: Options): string => {
Expand Down Expand Up @@ -90,8 +88,8 @@ export const format = (results: LicenseResult[], options: Options): string => {
})

const formatLines = options.summary ? formatSummaryLines : formatFullLines

let output = '\n'

output += formatLines(successLines, maxNameWidth, maxLicenseWidth)
output += '\n'

Expand Down
30 changes: 20 additions & 10 deletions src/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,56 @@ import {lint} from './lint'

const entry = process.cwd()

const APACHE = 'Apache-2.0'
const BSD = 'BSD-2-Clause'
const MIT = 'MIT'
const ISC = 'ISC'
const CC0 = 'CC0-1.0'

test('lint licenses', async () => {
const options = {
production: true
}
const results = await lint(entry, options)
const failed = results.filter((result) => result.error)

expect(failed).toEqual([])
})

test('lint licenses with deny list', async () => {
const options = {
production: true,
deny: ['Apache-2.0', 'BSD-2-Clause', 'CC0-1.0']
deny: [APACHE, BSD, CC0]
}
const results = await lint(entry, options)
const errors = results.filter((result) => result.error)
expect(errors.map((result) => result.licenses)).toContain('Apache-2.0')
expect(errors.map((result) => result.licenses)).toContain('BSD-2-Clause')
expect(errors.map((result) => result.licenses)).toContain('CC0-1.0')

expect(errors.map((result) => result.licenses)).toContain(APACHE)
expect(errors.map((result) => result.licenses)).toContain(BSD)
expect(errors.map((result) => result.licenses)).toContain(CC0)
expect(errors.map((result) => result.licenses)).not.toContain(
'(MIT OR CC0-1.0)'
`(${MIT} OR ${CC0})`
)
})

test('lint licenses with allow list', async () => {
const options = {
production: true,
allow: ['MIT', 'ISC']
allow: [MIT, ISC]
}
const results = await lint(entry, options)
const errors = results.filter((result) => result.error)
expect(errors.map((result) => result.licenses)).not.toContain('MIT')
expect(errors.map((result) => result.licenses)).not.toContain('ISC')

expect(errors.map((result) => result.licenses)).not.toContain(MIT)
expect(errors.map((result) => result.licenses)).not.toContain(ISC)
})

test('lint licenses with exclude list', async () => {
const options = {
production: true,
exclude: ['Apache-2.0']
exclude: [APACHE]
}
const results = await lint(entry, options)
expect(results.map((result) => result.licenses)).not.toContain('Apache-2.0')

expect(results.map((result) => result.licenses)).not.toContain(APACHE)
})
101 changes: 47 additions & 54 deletions src/lint.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,63 @@
import {promisify} from 'util'
import licenseChecker, {InitOpts, ModuleInfo} from 'license-checker'
import {Options} from './options'

const checkLicense = promisify(licenseChecker.init)

const matchLicense = (license: string) => (reference: string) =>
// eslint-disable-next-line security/detect-non-literal-regexp
license.match(new RegExp(reference, 'i'))

export interface LicenseResult extends Omit<ModuleInfo, 'licenses'> {
name: string
licenses?: string
error?: string
}

const matchLicense = (license: string) => (reference: string) =>
license.match(new RegExp(reference, 'i'))

export const lint = (
export const lint = async (
entry: string,
options: Options
): Promise<LicenseResult[]> =>
new Promise<LicenseResult[]>((resolve, reject) => {
const {
production,
development,
deny = [],
allow = [],
exclude = []
} = options
): Promise<LicenseResult[]> => {
const {production, development, deny = [], allow = [], exclude = []} = options

const checkerOptions: InitOpts = {
start: entry,
production,
development,
// NOTE fix wrong `exclude` type
exclude: exclude.toString() as unknown as string[]
}
const checkerOptions: InitOpts = {
start: entry,
production,
development,
// NOTE fix wrong `exclude` type
exclude: exclude.toString() as unknown as string[]
}

licenseChecker.init(checkerOptions, (error: Error, modulesInfo) => {
if (error) {
reject(error)
return
}
const modulesInfo = await checkLicense(checkerOptions)

return Object.entries(modulesInfo)
.map<LicenseResult>(([name, {licenses, ...info}]) => ({
name,
...info,
licenses: Array.isArray(licenses)
? `(${licenses.join(' AND ')})`
: licenses
}))
.map((result) => {
if (allow.length > 0) {
const isNotAllow = !allow.some(matchLicense(result.licenses ?? ''))

const results = Object.entries(modulesInfo)
.map<LicenseResult>(([name, {licenses, ...info}]) => ({
name,
...info,
licenses: Array.isArray(licenses)
? `(${licenses.join(' AND ')})`
: licenses
}))
.map((result) => {
if (allow.length > 0) {
const isNotAllow = !allow.some(matchLicense(result.licenses ?? ''))
if (isNotAllow) {
result.error = `${result.licenses} is not allowed by \`allow\` list`
}
} else if (deny.length > 0) {
const multipleLicenses = result.licenses?.split(' OR ')
const isDeny = Array.isArray(multipleLicenses)
? multipleLicenses.every((license) =>
deny.some(matchLicense(license ?? ''))
)
: deny.some(matchLicense(result.licenses ?? ''))
if (isDeny) {
result.error = `${result.licenses} is denied by \`deny\` list`
}
}
return result
})
if (isNotAllow) {
result.error = `${result.licenses} is not allowed by \`allow\` list`
}
} else if (deny.length > 0) {
const multipleLicenses = result.licenses?.split(' OR ')
const isDeny = Array.isArray(multipleLicenses)
? multipleLicenses.every((license) =>
deny.some(matchLicense(license ?? ''))
)
: deny.some(matchLicense(result.licenses ?? ''))

resolve(results)
if (isDeny) {
result.error = `${result.licenses} is denied by \`deny\` list`
}
}

return result
})
})
}
7 changes: 7 additions & 0 deletions src/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import {loadOptions, defaultOptions} from './options'

test('load default options', async () => {
const options = await loadOptions()

expect(options).toEqual({
...defaultOptions,
summary: true,
production: true,
deny: [
'MPL.+1.1',
'MPL.+2',
'EPL.+1.0',
'EPL.+2',
'IPL.+1',
'GPL.+2',
'GPL.+3',
'AGPL.+3',
'LGPL.+2.1',
'CDDL.+1.0',
'UNKNOWN',
'CUSTOM'
]
Expand All @@ -21,11 +26,13 @@ test('load default options', async () => {

test('load custom options', async () => {
const options = await loadOptions('./test/options.json')

expect(options).toEqual({...defaultOptions, development: true})
})

test('load extended options', async () => {
const options = await loadOptions('./test/options-extended.json')

expect(options).toEqual({
...defaultOptions,
development: true,
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const loadOptions = async (

if (extendsFileName) {
const extendsFileDir = path.dirname(optionsFilePath)

extendsOptions = await loadOptions(extendsFileName, extendsFileDir)
}

Expand Down
14 changes: 2 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
{
"extends": "@rambler-tech/ts-config/base",
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "src",
"outDir": "dist",
"allowJs": true,
"strict": true,
"sourceMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noEmitOnError": true,
"lib": ["dom", "esnext"]
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
Expand Down
Loading

0 comments on commit 81c0235

Please sign in to comment.