-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
roman.vasilev
committed
Jun 30, 2018
1 parent
9c1f430
commit 7f31a56
Showing
11 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.json] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const tslintConfigRulesDirectory = [ | ||
'node_modules/tslint/lib/rules', | ||
// 'node_modules/tslint-clean-code/dist/src', | ||
// 'node_modules/tslint-microsoft-contrib', | ||
]; | ||
const tslintConfigRules = Object.assign({}, | ||
require('tslint/lib/configs/recommended').rules, | ||
// require('tslint-clean-code/recommended_ruleset').rules, | ||
// require('tslint-microsoft-contrib/recommended_ruleset').rules, | ||
{ | ||
'member-access': false, | ||
'ordered-imports': false, | ||
'quotemark': false, | ||
'no-var-keyword': false, | ||
'object-literal-sort-keys': false, | ||
'no-console': false, | ||
'arrow-parens': false, | ||
'max-line-length': false, | ||
'object-literal-key-quotes': false, | ||
'no-shadowed-variable': false, | ||
'only-arrow-functions': false, | ||
'no-var-requires': false, | ||
'semicolon': false, | ||
'interface-over-type-literal': false, | ||
'align': false, | ||
'trailing-comma': false, | ||
'typedef': false, | ||
'newline-before-return': false, | ||
'interface-name': false, | ||
} | ||
); | ||
|
||
module.exports = { | ||
'root': true, | ||
'env': { | ||
'es6': true, | ||
'node': true | ||
}, | ||
'extends': [ | ||
'eslint:recommended', | ||
'plugin:unicorn/recommended', | ||
], | ||
'parser': 'typescript-eslint-parser', | ||
'parserOptions': { | ||
'ecmaVersion': 2017, | ||
'sourceType': 'module' | ||
}, | ||
'plugins': [ | ||
'unicorn', | ||
'typescript', | ||
'import', | ||
'tslint', | ||
], | ||
'rules': { | ||
'no-undef': 0, | ||
'no-unused-vars': 0, | ||
'indent': 0, | ||
'unicorn/import-index': 0, | ||
'tslint/config': [1, { | ||
// tsconfigFile: 'tsconfig.json', | ||
rules: tslintConfigRules, | ||
rulesDirectory: tslintConfigRulesDirectory, | ||
}], | ||
'import/newline-after-import': 0, | ||
'import/no-duplicates': 1, | ||
'import/max-dependencies': [1, { 'max': 10 }], | ||
'quotes': [1, 'single', { 'allowTemplateLiterals': true }], | ||
'semi': [1, 'always'], | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/node_modules | ||
/.idea | ||
/.awcache | ||
/.vscode | ||
/.nyc_output | ||
*.log | ||
~* | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# .gitignore | ||
/node_modules | ||
/.idea | ||
/.awcache | ||
/.vscode | ||
/.nyc_output | ||
*.log | ||
~* | ||
# .npmignore | ||
/tsconfig.json | ||
/.eslintrc.js | ||
/.editorconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock = false | ||
# save-exact = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
typescript-service | ||
=== | ||
|
||
|
||
CHANGELOG | ||
--- | ||
See [CHANGELOG.md](CHANGELOG.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{ | ||
"name": "typescript-service", | ||
"version": "0.0.0-dev", | ||
"license": "MIT", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"typings": "src/index.ts", | ||
"scripts": { | ||
"t": "npm run mocha -- src/*.spec.ts", | ||
"test": "npm run eslint && npm run tscheck && npm run t", | ||
"test:r": "npm run mocha -- src/*.spec.ts", | ||
"mocha": "node -r ts-node/register/transpile-only node_modules/mocha/bin/_mocha", | ||
"test:w": "npm run mocha -- --watch-extensions ts --watch src/**/*.spec.ts", | ||
"test:d": "node --inspect-brk -r ts-node/register/transpile-only node_modules/mocha/bin/_mocha --no-timeouts src/**/*.spec.ts", | ||
"tscheck": "echo tscheck... && tsc --noEmit", | ||
"tscheck:w": "npm run tscheck -- --watch", | ||
"tsclint": "tsc --noEmit --pretty --strict --forceConsistentCasingInFileNames --noImplicitReturns --noImplicitThis --noUnusedLocals --noUnusedParameters", | ||
"tsclint:w": "npm run tsclint -- --watch", | ||
"eslint": "eslint src --ext ts", | ||
"eslint:fix": "eslint src --ext \"ts\" --fix", | ||
"eslint:w": "chokidar \"src/**/*.ts\" --initial -c \"npm run eslint\"", | ||
"lint:w": "run-p tsclint:w eslint:w", | ||
"semantic-release": "semantic-release", | ||
"prepublishOnly": "npm run build", | ||
"build": "tsc", | ||
"prebuild": "npm run clean", | ||
"clean": "rimraf dist", | ||
"commit": "git-cz" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@semantic-release/changelog": "^2.1.1", | ||
"@semantic-release/git": "^6.0.1", | ||
"@semantic-release/npm": "^3.3.4", | ||
"@types/mocha": "^5.2.3", | ||
"@types/node": "^10.5.1", | ||
"chokidar-cli": "^1.2.0", | ||
"conventional-changelog-eslint": "^3.0.0", | ||
"cz-adapter-eslint": "^0.1.2", | ||
"eslint": "^5.0.1", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-tslint": "^2.1.0", | ||
"eslint-plugin-typescript": "^0.12.0", | ||
"eslint-plugin-unicorn": "^4.0.3", | ||
"mocha": "^5.2.0", | ||
"npm-run-all": "^4.1.3", | ||
"rimraf": "^2.6.2", | ||
"semantic-release": "^15.6.0", | ||
"ts-node": "^7.0.0", | ||
"tslint": "^5.10.0", | ||
"tslint-clean-code": "^0.2.7", | ||
"tslint-microsoft-contrib": "^5.0.3", | ||
"typescript": "^2.9.2", | ||
"typescript-eslint-parser": "^16.0.1" | ||
}, | ||
"engines": { | ||
"node": ">=6", | ||
"npm": ">=3" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/unlight/typescript-service.git" | ||
}, | ||
"keywords": [], | ||
"release": { | ||
"generateNotes": { | ||
"preset": "eslint" | ||
}, | ||
"analyzeCommits": { | ||
"preset": "eslint" | ||
}, | ||
"verifyConditions": [ | ||
"@semantic-release/changelog", | ||
"@semantic-release/github", | ||
"@semantic-release/npm", | ||
"@semantic-release/git" | ||
], | ||
"prepare": [ | ||
"@semantic-release/changelog", | ||
"@semantic-release/npm", | ||
"@semantic-release/git" | ||
], | ||
"publish": [ | ||
"@semantic-release/npm", | ||
"@semantic-release/github" | ||
], | ||
"success": [ | ||
"@semantic-release/github" | ||
], | ||
"fail": [ | ||
"@semantic-release/github" | ||
] | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-adapter-eslint" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import * as ts from 'typescript'; | ||
import { readFileSync, existsSync } from 'fs'; | ||
import { normalizeCompilerOptions } from './utils'; | ||
|
||
type createServiceOptions = { | ||
configFile: string; | ||
compilerOptions?: ts.CompilerOptions; | ||
}; | ||
|
||
export function createService({ compilerOptions, configFile }: createServiceOptions) { | ||
|
||
const config = ts.readConfigFile(configFile, ts.sys.readFile); | ||
if (config.error) { | ||
throw new Error(ts.formatDiagnostics([config.error], { | ||
getCanonicalFileName: file => file, | ||
getCurrentDirectory: process.cwd, | ||
getNewLine: () => '\n', | ||
})); | ||
} | ||
|
||
const compilationSettings: ts.CompilerOptions = normalizeCompilerOptions({ | ||
...((config.config && config.config.compilerOptions) || {}), | ||
...(compilerOptions || {}), | ||
noEmit: true, | ||
sourceMap: false, | ||
inlineSources: false, | ||
inlineSourceMap: false, | ||
}); | ||
|
||
const files: ts.MapLike<{ version: number, snapshot: ts.IScriptSnapshot | undefined }> = {}; | ||
|
||
// Caches | ||
const fileExistsCache = Object.create(null); | ||
const readFileCache = Object.create(null); | ||
|
||
// Create the language service host to allow the LS to communicate with the host | ||
const servicesHost: ts.LanguageServiceHost = { | ||
getScriptFileNames: () => { | ||
return Object.keys(files); | ||
}, | ||
getScriptVersion: (fileName) => { | ||
return files[fileName] && String(files[fileName].version); | ||
}, | ||
getScriptSnapshot(this: typeof servicesHost, fileName: string) { | ||
let fileRef = files[fileName]; | ||
if (!fileRef) { | ||
files[fileName] = fileRef = { version: 0, snapshot: undefined }; | ||
if (fileName === 'lib.d.ts') { | ||
fileName = require.resolve('typescript/lib/lib.d.ts').replace(/\\/g, '/'); | ||
} | ||
} | ||
if (fileRef.snapshot === undefined) { | ||
const data = this.readFile && this.readFile(fileName); | ||
fileRef.snapshot = (data != null) ? ts.ScriptSnapshot.fromString(data) : undefined; | ||
} | ||
return fileRef.snapshot; | ||
}, | ||
getCurrentDirectory: ts.sys.getCurrentDirectory, | ||
getCompilationSettings: () => compilationSettings, | ||
getDefaultLibFileName: (options) => { | ||
return ts.getDefaultLibFileName(options); | ||
}, | ||
fileExists: (file) => { | ||
let result = fileExistsCache[file]; | ||
if (result === undefined) { | ||
fileExistsCache[file] = result = existsSync(file); | ||
} | ||
return result; | ||
}, | ||
readFile: (file) => { | ||
let result = readFileCache[file]; | ||
if (result === undefined) { | ||
readFileCache[file] = result = readFileSync(file, 'utf8'); | ||
} | ||
return result; | ||
}, | ||
getDirectories: (directory) => { | ||
if (existsSync(directory)) { | ||
return ts.sys.getDirectories(directory); | ||
} | ||
return []; | ||
} | ||
}; | ||
|
||
// Adding libs | ||
(compilationSettings.lib || []).forEach(lib => { | ||
const fileName = require.resolve(`typescript/lib/lib.${lib}.d.ts`).replace(/\\/g, '/'); | ||
files[fileName] = { version: 0, snapshot: servicesHost.getScriptSnapshot(fileName) }; | ||
}); | ||
|
||
// Create the language service files | ||
const service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); | ||
|
||
return { | ||
update({ fileName, fileContent }: { fileName: string, fileContent: string }) { | ||
fileName = fileName.replace(/\\/g, '/'); | ||
let fileRef = files[fileName]; | ||
if (!fileRef) { | ||
files[fileName] = fileRef = { version: 0, snapshot: undefined }; | ||
} | ||
fileRef.snapshot = ts.ScriptSnapshot.fromString(fileContent); | ||
fileRef.version++; | ||
|
||
fileExistsCache[fileName] = true; | ||
readFileCache[fileName] = fileContent; | ||
}, | ||
getDiagnostics(fileName: string) { | ||
const program = service.getProgram(); | ||
const sourceFile = program.getSourceFile(fileName); | ||
return [ | ||
...program.getSyntacticDiagnostics(sourceFile), | ||
...program.getSemanticDiagnostics(sourceFile), | ||
]; | ||
}, | ||
getProgram: () => service.getProgram(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export function normalizeCompilerOptions(options: ts.CompilerOptions = {}) { | ||
if (options.target) { | ||
options.target = toEnum(ts.ScriptTarget, options.target); | ||
} | ||
if (options.module) { | ||
options.module = toEnum(ts.ModuleKind, options.module); | ||
} | ||
if (options.moduleResolution) { | ||
if (String(options.moduleResolution).toLowerCase() === 'node') { | ||
options.moduleResolution = ts.ModuleResolutionKind.NodeJs; | ||
} | ||
options.moduleResolution = toEnum(ts.ModuleResolutionKind, options.moduleResolution); | ||
} | ||
return options; | ||
} | ||
|
||
export function toEnum(collection: any, value: any) { | ||
let result = value; | ||
const valueLower = String(value).toLowerCase(); | ||
const key = Object.keys(collection).find(value => String(value).toLowerCase() === valueLower); | ||
if (key !== undefined) { | ||
result = Number(collection[key]); | ||
if (Number.isNaN(result)) { | ||
result = value; | ||
} | ||
} | ||
return result; | ||
} |
Oops, something went wrong.