kmore-types
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [0.5.0](https://github.com/waitingsong/kmore-types/compare/v0.4.0...v0.5.0) (2019-08-14) | ||
### Features | ||
* add opts BuildSrcOpts['excludePathKeys'] for buildSource() ([4d03077](https://github.com/waitingsong/kmore-types/commit/4d03077)) | ||
## [0.4.0](https://github.com/waitingsong/kmore-types/compare/v0.3.0...v0.4.0) (2019-08-13) | ||
@@ -7,0 +14,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 0.4.0 | ||
* @version 0.5.0 | ||
* @author waiting | ||
@@ -28,3 +28,3 @@ * @license MIT | ||
const initOptions = Object.assign({}, initGenTbListFromTypeOpts, { exportVarPrefix: 'tbs', forceLoadTbListJs: false, forceLoadTbListJsPathReplaceRules: null, outputBanner: '/* eslint-disable */', outputFileNameSuffix: '__built-tables', refTablesPrefix: 'reftb_' }); | ||
const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5 }); | ||
const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5, excludePathKeys: ['node_modules'], maxScanLines: 128 }); | ||
const reservedTbListKeys = [ | ||
@@ -233,3 +233,3 @@ 'constructor', | ||
const opts = Object.assign({}, initBuildSrcOpts, options); | ||
const { path: basePath } = opts; | ||
const { path: basePath, excludePathKeys, maxScanLines } = opts; | ||
const maxDepth = 99; | ||
@@ -251,6 +251,9 @@ const concurrent = opts.concurrent && opts.concurrent > 0 | ||
}, rxjs.of(basePath), rxjs.from(basePath)); | ||
const path$ = dir$.pipe(operators.mergeMap(path => rxwalker.walk(path, { maxDepth }), concurrent), operators.filter(ev => ev.type === "file" /* file */ | ||
const path$ = dir$.pipe(operators.mergeMap(path => rxwalker.walk(path, { maxDepth }), concurrent), operators.filter((ev) => { | ||
const { path } = ev; | ||
return path ? !ifPathContainsKey(path, excludePathKeys) : false; | ||
}), operators.filter(ev => ev.type === "file" /* file */ | ||
&& ev.path.endsWith('.ts') | ||
&& !ev.path.endsWith('.d.ts')), operators.map(ev => ev.path), operators.mergeMap((path) => { | ||
const flag$ = ifFileContainsCallerFuncNames(matchFuncNameSet, path); | ||
const flag$ = ifFileContentContainsCallerFuncNames(matchFuncNameSet, maxScanLines, path); | ||
return flag$.pipe(operators.map((contains) => { | ||
@@ -262,11 +265,25 @@ return contains ? path : ''; | ||
} | ||
function ifFileContainsCallerFuncNames(matchFuncNameSet, path) { | ||
const file$ = rxjs.defer(() => sharedCore.readFileAsync(path)); | ||
const ret$ = file$.pipe(operators.map((buf) => { | ||
const ret = buf.length > 1024 ? buf.slice(0, 1024) : buf; | ||
return ret; | ||
}), operators.map((buf) => { | ||
const code = buf.toString(); | ||
return hasContainsCallerFuncNames(matchFuncNameSet, code); | ||
}), operators.catchError(() => rxjs.of(false))); | ||
function ifPathContainsKey(path, keys) { | ||
if (!path) { | ||
return false; | ||
} | ||
if (typeof keys === 'string' && keys) { | ||
return path.includes(keys); | ||
} | ||
else if (Array.isArray(keys)) { | ||
for (const key of keys) { | ||
if (key && path.includes(key)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
function ifFileContentContainsCallerFuncNames(matchFuncNameSet, maxLines, path) { | ||
const line$ = sharedCore.readFileLineRx(path); | ||
const scan$ = line$.pipe(operators.take(maxLines >= 0 ? maxLines : 128), operators.map((content) => { | ||
return hasContainsCallerFuncNames(matchFuncNameSet, content); | ||
}), operators.filter(exists => !!exists), operators.catchError(() => rxjs.of(false))); | ||
const notExists$ = rxjs.of(false); | ||
const ret$ = rxjs.concat(scan$, notExists$).pipe(operators.take(1)); | ||
return ret$; | ||
@@ -385,4 +402,5 @@ } | ||
function matchSourceFileWithFilePath(path) { | ||
const targetPath = sharedCore.pathResolve(path).replace(/\\/gu, '/'); | ||
const program = ts.createProgram([targetPath], { | ||
const srcPath = sharedCore.pathResolve(path).replace(/\\/gu, '/'); | ||
const srcLower = srcPath.toLowerCase(); | ||
const program = ts.createProgram([srcPath], { | ||
noEmitOnError: true, | ||
@@ -403,4 +421,4 @@ noImplicitAny: true, | ||
// @ts-ignore | ||
const srcPath = sourceFile.path ? sourceFile.path : ''; | ||
if (srcPath.toLowerCase() === targetPath.toLowerCase()) { | ||
const srcFilePath = sourceFile.path ? sourceFile.path : ''; | ||
if (srcFilePath.toLowerCase() === srcLower) { | ||
ret.sourceFile = sourceFile; | ||
@@ -461,2 +479,6 @@ } | ||
/** | ||
* Generate tables .ts files, | ||
* no path value emitted if no file generated. | ||
*/ | ||
function buildSource(options) { | ||
@@ -664,3 +686,2 @@ const opts = Object.assign({}, initBuildSrcOpts, options); | ||
exports.hasContainsCallerFuncNames = hasContainsCallerFuncNames; | ||
exports.ifFileContainsCallerFuncNames = ifFileContainsCallerFuncNames; | ||
exports.initBuildSrcOpts = initBuildSrcOpts; | ||
@@ -667,0 +688,0 @@ exports.initGenTbListFromTypeOpts = initGenTbListFromTypeOpts; |
import { Observable } from 'rxjs'; | ||
import { TTableListModel, FilePath, BuildSrcOpts } from './model'; | ||
/** | ||
* Generate tables .ts files, | ||
* no path value emitted if no file generated. | ||
*/ | ||
export declare function buildSource(options: BuildSrcOpts): Observable<FilePath>; | ||
@@ -4,0 +8,0 @@ /** |
@@ -7,2 +7,6 @@ import { pathResolve, writeFileAsync } from '@waiting/shared-core'; | ||
import { pickInfoFromCallerTypeId, genCallerTypeMapFromNodeSet, matchSourceFileWithFilePath, walkNode, } from './ts-util'; | ||
/** | ||
* Generate tables .ts files, | ||
* no path value emitted if no file generated. | ||
*/ | ||
export function buildSource(options) { | ||
@@ -9,0 +13,0 @@ const opts = Object.assign({}, initBuildSrcOpts, options); |
@@ -6,3 +6,3 @@ export const globalCallerFuncNameSet = new Set(['genTbListFromType', 'kmore']); | ||
export const initOptions = Object.assign({}, initGenTbListFromTypeOpts, { exportVarPrefix: 'tbs', forceLoadTbListJs: false, forceLoadTbListJsPathReplaceRules: null, outputBanner: '/* eslint-disable */', outputFileNameSuffix: '__built-tables', refTablesPrefix: 'reftb_' }); | ||
export const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5 }); | ||
export const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5, excludePathKeys: ['node_modules'], maxScanLines: 128 }); | ||
export const reservedTbListKeys = [ | ||
@@ -9,0 +9,0 @@ 'constructor', |
@@ -25,2 +25,6 @@ import * as ts from 'typescript'; | ||
concurrent?: number; | ||
/** String key to skip build under path. Default: node_modules */ | ||
excludePathKeys?: string | string[]; | ||
/** Maxium file lines to match CallerFuncName (import), Default: 128 */ | ||
maxScanLines?: number; | ||
} | ||
@@ -27,0 +31,0 @@ export interface PathReWriteRule extends Array<RegExp | string> { |
@@ -88,4 +88,5 @@ // eslint-disable-next-line import/no-extraneous-dependencies | ||
export function matchSourceFileWithFilePath(path) { | ||
const targetPath = pathResolve(path).replace(/\\/gu, '/'); | ||
const program = ts.createProgram([targetPath], { | ||
const srcPath = pathResolve(path).replace(/\\/gu, '/'); | ||
const srcLower = srcPath.toLowerCase(); | ||
const program = ts.createProgram([srcPath], { | ||
noEmitOnError: true, | ||
@@ -106,4 +107,4 @@ noImplicitAny: true, | ||
// @ts-ignore | ||
const srcPath = sourceFile.path ? sourceFile.path : ''; | ||
if (srcPath.toLowerCase() === targetPath.toLowerCase()) { | ||
const srcFilePath = sourceFile.path ? sourceFile.path : ''; | ||
if (srcFilePath.toLowerCase() === srcLower) { | ||
ret.sourceFile = sourceFile; | ||
@@ -110,0 +111,0 @@ } |
@@ -25,4 +25,3 @@ import { Observable } from 'rxjs'; | ||
export declare function walkDirForCallerFuncTsFiles(options: BuildSrcOpts): Observable<FilePath>; | ||
export declare function ifFileContainsCallerFuncNames(matchFuncNameSet: CallerFuncNameSet, path: FilePath): Observable<boolean>; | ||
export declare function hasContainsCallerFuncNames(matchFuncNameSet: CallerFuncNameSet, content: string): boolean; | ||
export declare function parseCallerFuncNames(callerFuncNameSet: CallerFuncNameSet, names: CallerFuncName | CallerFuncName[]): CallerFuncNameSet; |
import * as sourceMapSupport from 'source-map-support'; | ||
import { walk } from 'rxwalker'; | ||
import { from as ofrom, defer, of, iif } from 'rxjs'; | ||
import { map, filter, mergeMap, catchError } from 'rxjs/operators'; | ||
import { readFileAsync } from '@waiting/shared-core'; | ||
import { from as ofrom, of, iif, concat } from 'rxjs'; | ||
import { map, filter, mergeMap, catchError, take } from 'rxjs/operators'; | ||
import { readFileLineRx } from '@waiting/shared-core'; | ||
import { defaultPropDescriptor, reservedTbListKeys, initBuildSrcOpts, globalCallerFuncNameSet, } from './config'; | ||
@@ -187,3 +187,3 @@ /** Allow empty Object */ | ||
const opts = Object.assign({}, initBuildSrcOpts, options); | ||
const { path: basePath } = opts; | ||
const { path: basePath, excludePathKeys, maxScanLines } = opts; | ||
const maxDepth = 99; | ||
@@ -205,6 +205,9 @@ const concurrent = opts.concurrent && opts.concurrent > 0 | ||
}, of(basePath), ofrom(basePath)); | ||
const path$ = dir$.pipe(mergeMap(path => walk(path, { maxDepth }), concurrent), filter(ev => ev.type === "file" /* file */ | ||
const path$ = dir$.pipe(mergeMap(path => walk(path, { maxDepth }), concurrent), filter((ev) => { | ||
const { path } = ev; | ||
return path ? !ifPathContainsKey(path, excludePathKeys) : false; | ||
}), filter(ev => ev.type === "file" /* file */ | ||
&& ev.path.endsWith('.ts') | ||
&& !ev.path.endsWith('.d.ts')), map(ev => ev.path), mergeMap((path) => { | ||
const flag$ = ifFileContainsCallerFuncNames(matchFuncNameSet, path); | ||
const flag$ = ifFileContentContainsCallerFuncNames(matchFuncNameSet, maxScanLines, path); | ||
return flag$.pipe(map((contains) => { | ||
@@ -216,11 +219,25 @@ return contains ? path : ''; | ||
} | ||
export function ifFileContainsCallerFuncNames(matchFuncNameSet, path) { | ||
const file$ = defer(() => readFileAsync(path)); | ||
const ret$ = file$.pipe(map((buf) => { | ||
const ret = buf.length > 1024 ? buf.slice(0, 1024) : buf; | ||
return ret; | ||
}), map((buf) => { | ||
const code = buf.toString(); | ||
return hasContainsCallerFuncNames(matchFuncNameSet, code); | ||
}), catchError(() => of(false))); | ||
function ifPathContainsKey(path, keys) { | ||
if (!path) { | ||
return false; | ||
} | ||
if (typeof keys === 'string' && keys) { | ||
return path.includes(keys); | ||
} | ||
else if (Array.isArray(keys)) { | ||
for (const key of keys) { | ||
if (key && path.includes(key)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
function ifFileContentContainsCallerFuncNames(matchFuncNameSet, maxLines, path) { | ||
const line$ = readFileLineRx(path); | ||
const scan$ = line$.pipe(take(maxLines >= 0 ? maxLines : 128), map((content) => { | ||
return hasContainsCallerFuncNames(matchFuncNameSet, content); | ||
}), filter(exists => !!exists), catchError(() => of(false))); | ||
const notExists$ = of(false); | ||
const ret$ = concat(scan$, notExists$).pipe(take(1)); | ||
return ret$; | ||
@@ -227,0 +244,0 @@ } |
{ | ||
"name": "kmore-types", | ||
"author": "waiting", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Retrieve types info from ts file", | ||
@@ -65,3 +65,3 @@ "keywords": [ | ||
"@types/yargs": "^13.0.0", | ||
"@waiting/eslint-config": "^1.10.0", | ||
"@waiting/eslint-config": "^2.1.0", | ||
"coveralls": "^3.0.5", | ||
@@ -68,0 +68,0 @@ "cross-env": "^5.2.0", |
Sorry, the diff of this file is not supported yet
110288
1572