egg-ts-helper
Advanced tools
Comparing version 1.25.5 to 1.25.6
@@ -18,3 +18,6 @@ import { TsGenConfig, TsHelperConfig } from '..'; | ||
}; | ||
export declare function checkConfigReturnType(f: string): 2 | 1 | 3 | undefined; | ||
export declare function checkConfigReturnType(f: string): { | ||
type: number | undefined; | ||
usePowerPartial: boolean; | ||
}; | ||
//# sourceMappingURL=config.d.ts.map |
@@ -12,3 +12,3 @@ "use strict"; | ||
const EXPORT = 3; | ||
const cache = {}; | ||
const globalCache = {}; | ||
exports.defaultConfig = { | ||
@@ -22,2 +22,3 @@ // only need to parse config.default.ts or config.ts | ||
const dist = path_1.default.resolve(config.dtsDir, 'index.d.ts'); | ||
const cache = globalCache[baseConfig.id] = globalCache[baseConfig.id] || {}; | ||
if (!fileList.length) { | ||
@@ -33,3 +34,8 @@ return { dist }; | ||
if (!cache[abUrl] || config.file === abUrl) { | ||
const type = checkConfigReturnType(abUrl); | ||
const skipLibCheck = !!baseConfig.tsConfig.skipLibCheck; | ||
const { type, usePowerPartial } = checkConfigReturnType(abUrl); | ||
// skip when not usePowerPartial and skipLibCheck in ts file | ||
// because it maybe cause types error. | ||
if (path_1.default.extname(f) !== '.js' && !usePowerPartial && !skipLibCheck) | ||
return; | ||
const { moduleName: sModuleName } = utils.getModuleObjByPath(f); | ||
@@ -79,12 +85,35 @@ const moduleName = `Export${sModuleName}`; | ||
const result = utils.findExportNode(fs_1.default.readFileSync(f, 'utf-8')); | ||
const resp = { | ||
type: undefined, | ||
usePowerPartial: false, | ||
}; | ||
if (result.exportDefaultNode) { | ||
return typescript_1.default.isFunctionLike(result.exportDefaultNode) | ||
? EXPORT_DEFAULT_FUNCTION | ||
: EXPORT_DEFAULT; | ||
const exportDefaultNode = result.exportDefaultNode; | ||
if (typescript_1.default.isFunctionLike(exportDefaultNode)) { | ||
if ((typescript_1.default.isFunctionDeclaration(exportDefaultNode) || typescript_1.default.isArrowFunction(exportDefaultNode)) && exportDefaultNode.body) { | ||
exportDefaultNode.body.forEachChild(tNode => { | ||
if (!resp.usePowerPartial && typescript_1.default.isVariableStatement(tNode)) { | ||
// check wether use PowerPartial<EggAppInfo> | ||
resp.usePowerPartial = !!tNode.declarationList.declarations.find(decl => { | ||
let typeText = decl.type ? decl.type.getText() : undefined; | ||
if (decl.initializer && typescript_1.default.isAsExpression(decl.initializer) && decl.initializer.type) { | ||
typeText = decl.initializer.type.getText(); | ||
} | ||
return !!(typeText && typeText.includes('PowerPartial') && typeText.includes('EggAppConfig')); | ||
}); | ||
} | ||
}); | ||
} | ||
resp.type = EXPORT_DEFAULT_FUNCTION; | ||
} | ||
else { | ||
resp.type = EXPORT_DEFAULT; | ||
} | ||
} | ||
else if (result.exportNodeList.length) { | ||
return EXPORT; | ||
resp.type = EXPORT; | ||
} | ||
return resp; | ||
} | ||
exports.checkConfigReturnType = checkConfigReturnType; | ||
//# sourceMappingURL=config.js.map |
@@ -6,2 +6,3 @@ /// <reference types="node" /> | ||
import * as utils from './utils'; | ||
import { CompilerOptions } from 'typescript'; | ||
declare global { | ||
@@ -29,3 +30,6 @@ interface PlainObject<T = any> { | ||
export declare type WatchItem = WatchItem; | ||
export declare type TsHelperConfig = typeof defaultConfig; | ||
export declare type TsHelperConfig = typeof defaultConfig & { | ||
id: string; | ||
tsConfig: CompilerOptions; | ||
}; | ||
export declare type TsGenConfig = { | ||
@@ -32,0 +36,0 @@ name: string; |
@@ -229,6 +229,9 @@ "use strict"; | ||
(Array.isArray(configFile) ? configFile : [configFile]).forEach(f => { | ||
this.mergeConfig(config, utils.requireFile(utils.getAbsoluteUrlByCwd(f, config.cwd))); | ||
this.mergeConfig(config, utils.requireFile(path_1.default.resolve(config.cwd, f))); | ||
}); | ||
// merge local config and options to config | ||
this.mergeConfig(config, options); | ||
// create extra config | ||
config.id = `${Date.now()}-${Math.ceil(Math.random() * 1000000)}`; | ||
config.tsConfig = utils.loadTsConfig(path_1.default.resolve(config.cwd, './tsconfig.json')); | ||
} | ||
@@ -239,3 +242,3 @@ // configure | ||
if (options.cwd) { | ||
options.cwd = utils.getAbsoluteUrlByCwd(options.cwd, exports.defaultConfig.cwd); | ||
options.cwd = path_1.default.resolve(exports.defaultConfig.cwd, options.cwd); | ||
} | ||
@@ -247,3 +250,3 @@ // base config | ||
config.watchDirs = getDefaultWatchDirs(config); | ||
config.typings = utils.getAbsoluteUrlByCwd(config.typings, config.cwd); | ||
config.typings = path_1.default.resolve(config.cwd, config.typings); | ||
this.config = config; | ||
@@ -250,0 +253,0 @@ // load watcher config |
@@ -5,27 +5,7 @@ import ts from 'typescript'; | ||
}; | ||
export declare const TS_CONFIG: { | ||
compilerOptions: { | ||
target: string; | ||
module: string; | ||
strict: boolean; | ||
noImplicitAny: boolean; | ||
experimentalDecorators: boolean; | ||
emitDecoratorMetadata: boolean; | ||
allowSyntheticDefaultImports: boolean; | ||
charset: string; | ||
allowJs: boolean; | ||
pretty: boolean; | ||
lib: string[]; | ||
noEmitOnError: boolean; | ||
noUnusedLocals: boolean; | ||
noUnusedParameters: boolean; | ||
allowUnreachableCode: boolean; | ||
allowUnusedLabels: boolean; | ||
strictPropertyInitialization: boolean; | ||
noFallthroughCasesInSwitch: boolean; | ||
skipLibCheck: boolean; | ||
skipDefaultLibCheck: boolean; | ||
inlineSourceMap: boolean; | ||
}; | ||
}; | ||
export declare const TS_CONFIG: Partial<TsConfigJson>; | ||
export interface TsConfigJson { | ||
extends: string; | ||
compilerOptions: ts.CompilerOptions; | ||
} | ||
export interface GetEggInfoOpt { | ||
@@ -50,3 +30,2 @@ async?: boolean; | ||
export declare function getEggInfo<T extends 'async' | 'sync' = 'sync'>(cwd: string, option?: GetEggInfoOpt): T extends 'async' ? Promise<EggInfoResult> : EggInfoResult; | ||
export declare function getJson(jsonStr: string): any; | ||
export declare function convertString<T>(val: string | undefined, defaultVal: T): T; | ||
@@ -65,3 +44,2 @@ export declare function isIdentifierName(s: string): boolean; | ||
export declare function log(msg: string, prefix?: boolean): void; | ||
export declare function getAbsoluteUrlByCwd(p: string, cwd: string): string; | ||
export declare function getImportStr(from: string, to: string, moduleName?: string, importStar?: boolean): string; | ||
@@ -81,5 +59,8 @@ export declare function writeFileSync(fileUrl: any, content: any): void; | ||
export declare function extend<T = any>(obj: any, ...args: Array<Partial<T>>): T; | ||
export declare function parseJson(jsonStr: string): any; | ||
export declare function getPkgInfo(cwd: string): any; | ||
export declare function readJson(jsonUrl: string): any; | ||
export declare function formatProp(prop: string): string; | ||
export declare function camelProp(property: string, caseStyle: string | ((...args: any[]) => string)): string; | ||
export declare function loadTsConfig(tsconfigPath: string): ts.CompilerOptions; | ||
/** | ||
@@ -86,0 +67,0 @@ * ts ast utils |
@@ -17,4 +17,4 @@ "use strict"; | ||
compilerOptions: { | ||
target: 'es2017', | ||
module: 'commonjs', | ||
target: typescript_1.default.ScriptTarget.ES2017, | ||
module: typescript_1.default.ModuleKind.CommonJS, | ||
strict: true, | ||
@@ -74,3 +74,3 @@ noImplicitAny: false, | ||
reject(err); | ||
resolve(end(getJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8')))); | ||
resolve(end(parseJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8')))); | ||
}); | ||
@@ -83,3 +83,3 @@ }); | ||
child_process_1.execSync(cmd, opt); | ||
return end(getJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8'))); | ||
return end(parseJson(fs_1.default.readFileSync(config_1.eggInfoPath, 'utf-8'))); | ||
} | ||
@@ -92,16 +92,2 @@ catch (e) { | ||
exports.getEggInfo = getEggInfo; | ||
function getJson(jsonStr) { | ||
if (jsonStr) { | ||
try { | ||
return JSON.parse(jsonStr); | ||
} | ||
catch (e) { | ||
return {}; | ||
} | ||
} | ||
else { | ||
return {}; | ||
} | ||
} | ||
exports.getJson = getJson; | ||
// convert string to same type with default value | ||
@@ -216,6 +202,2 @@ function convertString(val, defaultVal) { | ||
exports.log = log; | ||
function getAbsoluteUrlByCwd(p, cwd) { | ||
return path_1.default.isAbsolute(p) ? p : path_1.default.resolve(cwd, p); | ||
} | ||
exports.getAbsoluteUrlByCwd = getAbsoluteUrlByCwd; | ||
// get import context | ||
@@ -329,15 +311,29 @@ function getImportStr(from, to, moduleName, importStar) { | ||
exports.extend = extend; | ||
// load package.json | ||
function getPkgInfo(cwd) { | ||
const pkgPath = path_1.default.resolve(cwd, './package.json'); | ||
if (!fs_1.default.existsSync(pkgPath)) | ||
return {}; | ||
try { | ||
return JSON.parse(fs_1.default.readFileSync(pkgPath, 'utf-8')); | ||
// parse json | ||
function parseJson(jsonStr) { | ||
if (jsonStr) { | ||
try { | ||
return JSON.parse(jsonStr); | ||
} | ||
catch (e) { | ||
return {}; | ||
} | ||
} | ||
catch (e) { | ||
else { | ||
return {}; | ||
} | ||
} | ||
exports.parseJson = parseJson; | ||
// load package.json | ||
function getPkgInfo(cwd) { | ||
return readJson(path_1.default.resolve(cwd, './package.json')); | ||
} | ||
exports.getPkgInfo = getPkgInfo; | ||
// read json file | ||
function readJson(jsonUrl) { | ||
if (!fs_1.default.existsSync(jsonUrl)) | ||
return {}; | ||
return parseJson(fs_1.default.readFileSync(jsonUrl, 'utf-8')); | ||
} | ||
exports.readJson = readJson; | ||
// format property | ||
@@ -372,2 +368,13 @@ function formatProp(prop) { | ||
exports.camelProp = camelProp; | ||
// load tsconfig.json | ||
function loadTsConfig(tsconfigPath) { | ||
tsconfigPath = path_1.default.extname(tsconfigPath) === '.json' ? tsconfigPath : `${tsconfigPath}.json`; | ||
const tsConfig = readJson(tsconfigPath); | ||
if (tsConfig.extends) { | ||
const extendTsConfig = loadTsConfig(path_1.default.resolve(path_1.default.dirname(tsconfigPath), tsConfig.extends)); | ||
return Object.assign({}, tsConfig.compilerOptions, extendTsConfig); | ||
} | ||
return tsConfig.compilerOptions || {}; | ||
} | ||
exports.loadTsConfig = loadTsConfig; | ||
/** | ||
@@ -374,0 +381,0 @@ * ts ast utils |
@@ -41,3 +41,3 @@ "use strict"; | ||
const baseDir = options.directory.replace(/\/|\\/, path_1.default.sep); | ||
this.dir = utils.getAbsoluteUrlByCwd(baseDir, this.config.cwd); | ||
this.dir = path_1.default.resolve(this.config.cwd, baseDir); | ||
this.dtsDir = path_1.default.resolve(this.config.typings, path_1.default.relative(this.config.cwd, this.dir)); | ||
@@ -103,3 +103,3 @@ // watch file change | ||
onChange(filePath) { | ||
filePath = utils.getAbsoluteUrlByCwd(filePath, this.dir); | ||
filePath = path_1.default.resolve(this.dir, filePath); | ||
debug('file changed %s %o', filePath, this.throttleStack); | ||
@@ -106,0 +106,0 @@ if (!this.throttleStack.includes(filePath)) { |
1.25.6 / 2019-08-21 | ||
================== | ||
**fixes** | ||
* [[`368e2a9`](http://github.com/whxaxes/egg-ts-helper/commit/368e2a99d303668ac540a56d47a489149d634a2c)] - fix: fix config types error (#51) (吖猩 <<whxaxes@gmail.com>>) | ||
* [[`a357f28`](http://github.com/whxaxes/egg-ts-helper/commit/a357f286d3c41c3d87211f8fb72897b5ee7330ec)] - fix: travis ci (wanghx <<whxaxes@gmail.com>>) | ||
1.25.5 / 2019-06-23 | ||
@@ -3,0 +10,0 @@ ================== |
{ | ||
"name": "egg-ts-helper", | ||
"version": "1.25.5", | ||
"version": "1.25.6", | ||
"description": "egg typescript helper", | ||
@@ -5,0 +5,0 @@ "bin": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
180790
2052