Skip to content

Commit

Permalink
feat: API revision
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Removed update method
  • Loading branch information
roman.vasilev committed Aug 24, 2018
1 parent ac90db9 commit 68044bb
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 207 deletions.
38 changes: 0 additions & 38 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
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': {
Expand All @@ -49,18 +17,12 @@ module.exports = {
'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 }],
Expand Down
39 changes: 16 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"test": "npm run eslint && npm run tscheck && npm run test:r",
"test:r": "npm run mocha -- src/*.spec.ts",
"mocha": "node -r ts-node/register/transpile-only node_modules/mocha/bin/_mocha",
"mocha": "node -r ts-node/register/transpile-only node_modules/mocha/bin/_mocha --timeout 5000",
"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",
Expand All @@ -20,35 +20,29 @@
"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",
"prepublishOnly": "npm run build && sed -i -e 's/devDependencies/_devDependencies/g' package.json",
"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",
"@semantic-release/changelog": "^3.0.0",
"@semantic-release/git": "^7.0.2",
"@semantic-release/npm": "^5.0.2",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.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": "^5.4.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-typescript": "^0.12.0",
"eslint-plugin-unicorn": "^4.0.3",
"eslint-plugin-unicorn": "^6.0.0",
"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",
"semantic-release": "^15.9.9",
"ts-node": "^7.0.1",
"typescript": "^2.9.2",
"typescript-eslint-parser": "^16.0.1"
},
Expand All @@ -63,10 +57,9 @@
"keywords": [],
"release": {
"generateNotes": {
"preset": "eslint"
},
"analyzeCommits": {
"preset": "eslint"
"writerOpts": {
"__keep": "me"
}
},
"verifyConditions": [
"@semantic-release/changelog",
Expand All @@ -92,7 +85,7 @@
},
"config": {
"commitizen": {
"path": "./node_modules/cz-adapter-eslint"
"path": "cz-conventional-changelog"
}
}
}
52 changes: 52 additions & 0 deletions src/create-program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';

type CreateProgramArgument = {
configFile: string;
compilerOptions?: ts.CompilerOptions;
projectDirectory?: string;
};

export function createProgram({ configFile, projectDirectory = path.dirname(configFile), compilerOptions = {} }: CreateProgramArgument): ts.Program {
const { config, error } = ts.readConfigFile(configFile, ts.sys.readFile);
if (error !== undefined) {
throw new Error(ts.formatDiagnostics([error], {
getCanonicalFileName: f => f,
getCurrentDirectory: process.cwd,
getNewLine: () => '\n',
}));
}
const parseConfigHost: ts.ParseConfigHost = {
fileExists: (path: string) => {
return fs.existsSync(path);
},
readDirectory: ts.sys.readDirectory,
readFile: (file) => {
return fs.readFileSync(file, 'utf8');
},
useCaseSensitiveFileNames: true,
};
config.compilerOptions = { ...(config.compilerOptions || {}), ...compilerOptions };
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, path.resolve(projectDirectory), {
noEmit: true,
sourceMap: false,
inlineSources: false,
inlineSourceMap: false,
});
if (parsed.errors !== undefined) {
// ignore warnings and 'TS18003: No inputs were found in config file ...'
const errors = parsed.errors.filter(d => d.category === ts.DiagnosticCategory.Error && d.code !== 18003);
if (errors.length !== 0) {
throw new Error(ts.formatDiagnostics(errors, {
getCanonicalFileName: f => f,
getCurrentDirectory: process.cwd,
getNewLine: () => '\n',
}));
}
}
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);

return program;
}
14 changes: 14 additions & 0 deletions src/get-source-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as ts from 'typescript';

export function getSourceFile(program: ts.Program, fileName: string, sourceText?: string): ts.SourceFile {
if (program !== undefined) {
const sourceFile = program.getSourceFile(fileName);
if (sourceFile) {
return sourceFile;
}
}
if (sourceText === undefined) {
throw new Error(`Invalid source file: ${fileName}`);
}
return ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.ES5, true);
}
40 changes: 27 additions & 13 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,43 @@ it('smoke', () => {
assert(lib);
});

it('create service no libs', () => {
const configFile = `${root}/test-project/tsconfig-nolibs.json`;
service = lib.createService({ configFile });
assert(service);
});

describe('tsconfig-files', () => {

before(() => {
const configFile = `${root}/test-project/tsconfig-files.json`;
service = lib.createService({ configFile });
});

it('smoke create', () => {
assert(service);
});

it('get source file which are not in files', () => {
const testFile = `${root}/test-project/file.spec.ts`;
const sourceFile = service.getSourceFile(testFile);
assert(sourceFile);
});
});

describe('create service', () => {

before(() => {
const configFile = `${root}/test-project/tsconfig.json`;
service = lib.createService({ configFile });
});

it('smoke create', () => {
assert(service);
});

it('errors', () => {
const testFile = `${root}/test-project/errors.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -30,7 +56,6 @@ describe('create service', () => {

it('number', () => {
const testFile = `${root}/test-project/number.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -39,7 +64,6 @@ describe('create service', () => {

it('built in', () => {
const testFile = `${root}/test-project/builtin.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -48,7 +72,6 @@ describe('create service', () => {

it('types', () => {
const testFile = `${root}/test-project/types.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -57,7 +80,6 @@ describe('create service', () => {

it('decorator', () => {
const testFile = `${root}/test-project/decorator.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -66,7 +88,6 @@ describe('create service', () => {

it('global types', () => {
const testFile = `${root}/test-project/global-types.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
Expand All @@ -75,17 +96,10 @@ describe('create service', () => {

it('date', () => {
const testFile = `${root}/test-project/date.ts`;
service.update({ fileName: testFile });
const sourceFile = service.getProgram().getSourceFile(testFile);
assert(sourceFile);
const diagnostics = service.getDiagnostics(testFile);
assert.equal(diagnostics.length, 0);
});

});

it('create service no libs', () => {
const configFile = `${root}/test-project/tsconfig-nolibs.json`;
service = lib.createService({ configFile });
assert(service);
});

0 comments on commit 68044bb

Please sign in to comment.