From 264c442442c63d2d2fd3fe38865e06e1e62dc727 Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Wed, 30 Aug 2017 23:36:13 -0300 Subject: [PATCH 1/3] initial try at detecting package.json settings --- eslint-server/src/server.ts | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/eslint-server/src/server.ts b/eslint-server/src/server.ts index 1161a6c..6dbe00a 100644 --- a/eslint-server/src/server.ts +++ b/eslint-server/src/server.ts @@ -19,6 +19,7 @@ import { import Uri from 'vscode-uri'; import path = require('path'); +import readFileSync = require('fs'); const deglob = require('deglob'); namespace Is { const toString = Object.prototype.toString; @@ -343,7 +344,35 @@ documents.onDidOpen((event) => { if (!supportedLanguages[event.document.languageId]) { return; } - const style = settings.standard.semistandard ? 'semistandard' : 'standard'; + const usePackageJSON = settings.standard.usePackageJSON; + let pkgStyle + let pkgOptions + if (usePackageJSON) { + const pkgPath = path.join(workspaceRoot, 'package.json'); + const pkgStr = readFileSync(pkgPath, 'utf8'); + const pkg = JSON.parse(pkgStr); + if (pkg && pkg.devDependencies && pkg.devDependencies.standard) { + pkgStyle = 'standard'; + } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { + pkgStyle = 'semistandard'; + } + + if (pkgStyle) { + pkgOptions = pkg[pkgStyle]; + } else { + connection.console.info('no standard in package.json'); + return; + } + } + + let style = settings.standard.semistandard ? 'semistandard' : 'standard'; + if (pkgStyle) { + style = pkgStyle; + } + if (pkgOptions) { + options = pkgOptions; + } + if (!document2Library[event.document.uri]) { let uri = Uri.parse(event.document.uri); let promise: Thenable @@ -1145,4 +1174,4 @@ connection.onExecuteCommand((params) => { connection.console.error(`Failed to apply command: ${params.command}`); }); }) -connection.listen(); \ No newline at end of file +connection.listen(); From b523f250cffc88eb09586a2f1cc419e91c7fe01a Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Fri, 1 Sep 2017 21:35:03 -0300 Subject: [PATCH 2/3] update formatting and package.json --- eslint-server/src/server.ts | 59 +++++++++++++++++++------------------ eslint/package.json | 5 ++++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/eslint-server/src/server.ts b/eslint-server/src/server.ts index 6dbe00a..f38e2a2 100644 --- a/eslint-server/src/server.ts +++ b/eslint-server/src/server.ts @@ -19,7 +19,7 @@ import { import Uri from 'vscode-uri'; import path = require('path'); -import readFileSync = require('fs'); +import fs = require('fs'); const deglob = require('deglob'); namespace Is { const toString = Object.prototype.toString; @@ -134,6 +134,7 @@ interface Settings { semistandard?: boolean; validate?: (string | ValidateItem)[]; workingDirectories?: (string | DirectoryItem)[]; + usePackageJson?: boolean; } [key: string]: any; } @@ -344,35 +345,35 @@ documents.onDidOpen((event) => { if (!supportedLanguages[event.document.languageId]) { return; } - const usePackageJSON = settings.standard.usePackageJSON; - let pkgStyle - let pkgOptions - if (usePackageJSON) { - const pkgPath = path.join(workspaceRoot, 'package.json'); - const pkgStr = readFileSync(pkgPath, 'utf8'); - const pkg = JSON.parse(pkgStr); - if (pkg && pkg.devDependencies && pkg.devDependencies.standard) { - pkgStyle = 'standard'; - } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { - pkgStyle = 'semistandard'; + const usePackageJson = settings.standard.usePackageJson; + let pkgStyle + let pkgOptions + if (usePackageJson) { + const pkgPath = path.join(workspaceRoot, 'package.json'); + const pkgStr = fs.readFileSync(pkgPath, 'utf8'); + const pkg = JSON.parse(pkgStr); + if (pkg && pkg.devDependencies && pkg.devDependencies.standard) { + pkgStyle = 'standard'; + } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { + pkgStyle = 'semistandard'; } - - if (pkgStyle) { - pkgOptions = pkg[pkgStyle]; - } else { - connection.console.info('no standard in package.json'); - return; - } - } - - let style = settings.standard.semistandard ? 'semistandard' : 'standard'; - if (pkgStyle) { - style = pkgStyle; - } - if (pkgOptions) { - options = pkgOptions; - } - + + if (pkgStyle) { + pkgOptions = pkg[pkgStyle]; + } else { + connection.console.info('no standard in package.json'); + return; + } + } + + let style = settings.standard.semistandard ? 'semistandard' : 'standard'; + if (pkgStyle) { + style = pkgStyle; + } + if (pkgOptions) { + options = pkgOptions; + } + if (!document2Library[event.document.uri]) { let uri = Uri.parse(event.document.uri); let promise: Thenable diff --git a/eslint/package.json b/eslint/package.json index 15d9aaa..4f951c6 100644 --- a/eslint/package.json +++ b/eslint/package.json @@ -131,6 +131,11 @@ "javascriptreact" ], "description": "An array of language ids which should be validated by JavaScript Standard Style" + }, + "standard.usePackageJson": { + "type": "boolean", + "default": false, + "description": "Activate only if present in package.json and use settings specified there." } } }, From d1d88d8ab4a3e987494edc8ee736b616893afd52 Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Thu, 7 Sep 2017 12:49:57 -0300 Subject: [PATCH 3/3] check for existance of package.json and read properties only if it exists --- eslint-server/src/server.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/eslint-server/src/server.ts b/eslint-server/src/server.ts index f38e2a2..1f8da89 100644 --- a/eslint-server/src/server.ts +++ b/eslint-server/src/server.ts @@ -350,20 +350,23 @@ documents.onDidOpen((event) => { let pkgOptions if (usePackageJson) { const pkgPath = path.join(workspaceRoot, 'package.json'); - const pkgStr = fs.readFileSync(pkgPath, 'utf8'); - const pkg = JSON.parse(pkgStr); - if (pkg && pkg.devDependencies && pkg.devDependencies.standard) { - pkgStyle = 'standard'; - } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { - pkgStyle = 'semistandard'; - } - - if (pkgStyle) { - pkgOptions = pkg[pkgStyle]; - } else { - connection.console.info('no standard in package.json'); - return; - } + const pkgExists = fs.existsSync(pkgPath); + if (pkgExists) { + const pkgStr = fs.readFileSync(pkgPath, 'utf8'); + const pkg = JSON.parse(pkgStr); + if (pkg && pkg.devDependencies && pkg.devDependencies.standard) { + pkgStyle = 'standard'; + } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { + pkgStyle = 'semistandard'; + } + + if (pkgStyle) { + pkgOptions = pkg[pkgStyle]; + } else { + connection.console.info('no standard in package.json'); + return; + } + } } let style = settings.standard.semistandard ? 'semistandard' : 'standard';