Skip to content

Commit

Permalink
chore: add some default ts and prettier linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kingnebby committed Apr 5, 2019
1 parent 401b82f commit 9324323
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
extends: 'eslint:recommended'
parser: "@typescript-eslint/parser"
extends:
- "plugin:@typescript-eslint/recommended"
- prettier
- "prettier/@typescript-eslint"
env:
es6: true
node: true
parserOptions:
ecmaVersion: 9
plugins:
- "@typescript-eslint"
- prettier
rules:
prettier/prettier: error
Expand Down
79 changes: 79 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Validates open source licenses for your project.",
"main": "built/index.js",
"scripts": {
"eslint": "eslint index.js src/** --fix",
"eslint": "eslint src/**",
"release": "standard-version",
"test": "nyc mocha",
"report-coverage": "nyc report --reporter=text-lcov | coveralls"
Expand Down Expand Up @@ -68,10 +68,13 @@
"@types/mocha": "^5.2.6",
"@types/node": "^11.13.0",
"@types/treeify": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^1.6.0",
"@typescript-eslint/parser": "^1.6.0",
"ansi-escapes": "^3.1.0",
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"eslint": "^5.13.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"husky": "^1.3.1",
"mocha": "^6.0.2",
Expand All @@ -85,4 +88,4 @@
"ts-node": "^8.0.3",
"typescript": "^3.4.1"
}
}
}
49 changes: 28 additions & 21 deletions src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const defaultLicenseInitOpts = {
production: true
}

export async function getAndValidateConfig (configPath) {
export async function getAndValidateConfig(configPath) {
const configExists = await fs.pathExists(configPath)
if (configExists) {
return loadConfig(configPath)
Expand All @@ -32,9 +32,9 @@ export async function getAndValidateConfig (configPath) {

// Simply loads the config file

export async function loadConfig (configPath) {
export async function loadConfig(configPath) {
let fileContents = await fs.readFile(configPath)
const config = safeLoad( fileContents.toString(), {
const config = safeLoad(fileContents.toString(), {
filename: configPath
})
if (!config) {
Expand All @@ -52,13 +52,13 @@ export async function loadConfig (configPath) {
}

// Writes the config
export async function writeConfig (configPath, configObject) {
export async function writeConfig(configPath, configObject) {
let updatedConfig = safeDump(configObject)
await fs.writeFile(configPath, updatedConfig)
}

// Builds the dependency tree of node modules.
export async function getDepTree () {
export async function getDepTree() {
// TODO: Prefer a programmatic way to do this, but performance matters.
let result = ""
// https://nodejs.org/api/child_process.html#child_process_maxbuffer_and_unicode
Expand All @@ -77,12 +77,12 @@ export async function getDepTree () {

// Runs license-checker to just the list of licenses in the format
// that license-checker handles so we can safely call other functions like `asSummary`
export async function getDependencies (opts = {}) {
export async function getDependencies(opts = {}) {
return await init({ ...defaultLicenseInitOpts, ...opts })
}

// Updates existing licenses based on user input and existing dependencies
export async function getUserLicenseInput (existingLicenses) {
export async function getUserLicenseInput(existingLicenses) {
let { licenses: licenseMap } = await generateLicensesMap()
const approvedLicenses = [...existingLicenses]
for (let licenseName in licenseMap) {
Expand All @@ -107,7 +107,7 @@ export async function getUserLicenseInput (existingLicenses) {
return approvedLicenses
}

export async function getUserModulesInput (existingLicenses, existingModules) {
export async function getUserModulesInput(existingLicenses, existingModules) {
let dependencies = await getDependencies({
summary: true
})
Expand Down Expand Up @@ -155,7 +155,11 @@ export async function getUserModulesInput (existingLicenses, existingModules) {
return approvedModules
}

export async function getUnallowedDependencies (existingLicenses, existingModules, dependencies) {
export async function getUnallowedDependencies(
existingLicenses,
existingModules,
dependencies
) {
let unallowedDependencyMap = {}

for (let dependencyName in dependencies) {
Expand All @@ -172,7 +176,7 @@ export async function getUnallowedDependencies (existingLicenses, existingModule
}

// Shows all the licenses in use for each module.
export async function summary (filePath) {
export async function summary(filePath) {
const currentConfig = await getAndValidateConfig(filePath)
let {
licenses: licenseMap,
Expand All @@ -193,7 +197,7 @@ export async function summary (filePath) {
return summary
}

export function prettySummary (summary) {
export function prettySummary(summary) {
let approvedTree = _.isEmpty(summary.approved)
? " None\n"
: treeify.asTree(summary.approved, true, true)
Expand Down Expand Up @@ -231,7 +235,7 @@ export function prettySummary (summary) {
* }
* }
*/
export async function generateLicensesMap () {
export async function generateLicensesMap() {
const opts = {
start: "./",
production: true,
Expand Down Expand Up @@ -262,18 +266,18 @@ export async function generateLicensesMap () {
}

// If it is not a string you have to specifically allow the module.
export function canBeProcessed (licenseEntry) {
export function canBeProcessed(licenseEntry) {
return typeof licenseEntry === "string"
}

// Main method that initiates the checking process
export async function getInvalidModuleDependencyTree (config) {
export async function getInvalidModuleDependencyTree(config) {
const licenses = await getDependencies()
const invalidLicensedModules = getInvalidModules(licenses, config)
if (invalidLicensedModules === undefined) {
return
}
const packageDepTree = await getDepTree() as any
const packageDepTree = (await getDepTree()) as any
return pruneTreeByLicenses(
packageDepTree.name,
packageDepTree,
Expand All @@ -282,7 +286,7 @@ export async function getInvalidModuleDependencyTree (config) {
}

// Compares a modules map with configured valid licenses.
export function getInvalidModules (moduleList, config) {
export function getInvalidModules(moduleList, config) {
const invalidModules = {}
for (let moduleName in moduleList) {
let currentModule = moduleList[moduleName]
Expand All @@ -302,18 +306,21 @@ export function getInvalidModules (moduleList, config) {
return invalidModules
}

export function isLicenseValidByConfig (configLicenses, license) {
export function isLicenseValidByConfig(configLicenses, license) {
return configLicenses.includes(license)
}

export function isModuleValidByConfig (configModules, moduleName) {
export function isModuleValidByConfig(configModules, moduleName) {
return configModules.includes(moduleName)
}

interface prunedNode { licenses: any; version: string }
interface prunedNode {
licenses: any
version: string
}
// Prune out all the 'valid' licensed modules so that the result is
// the tree of modules whose sub-dep licenses are invalid.
export function pruneTreeByLicenses (name, node, invalidLicensedModules) {
export function pruneTreeByLicenses(name, node, invalidLicensedModules) {
let prunedNode = {} as prunedNode

let prunedDeps = {} as prunedNode
Expand Down Expand Up @@ -345,4 +352,4 @@ export function pruneTreeByLicenses (name, node, invalidLicensedModules) {
}

return prunedNode
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ program.action(async args => {
process.exit(1)
}

const depTree = await getInvalidModuleDependencyTree(parsedConfig) as any
const depTree = (await getInvalidModuleDependencyTree(parsedConfig)) as any

if (!_.isEmpty(depTree)) {
let summaryMap = await summary(fileName)
Expand Down

0 comments on commit 9324323

Please sign in to comment.