ts-generator
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -6,2 +6,4 @@ "use strict"; | ||
const deps_1 = require("../deps"); | ||
const parseConfigFile_1 = require("../parseConfigFile"); | ||
const loadPlugin_1 = require("../plugins/loadPlugin"); | ||
async function cli(configPathRel, customDeps) { | ||
@@ -11,4 +13,6 @@ const configPath = path_1.join(process.cwd(), configPathRel); | ||
const deps = Object.assign({}, deps_1.createDeps(), customDeps); | ||
await tsGen_1.tsGen(deps, { configPath, cwd }); | ||
const cfg = await parseConfigFile_1.parseConfigFile(deps, { configPath, cwd }); | ||
const plugins = cfg.plugins.map(pluginCfg => loadPlugin_1.loadPlugin(deps, { cwd: cfg.cwd, rawConfig: pluginCfg })); | ||
await tsGen_1.tsGen(cfg, plugins, deps); | ||
} | ||
exports.cli = cli; |
/// <reference types="node" /> | ||
import * as fs from "fs"; | ||
import * as prettier from "prettier"; | ||
import { sync as mkdirp } from "mkdirp"; | ||
import { TLogger } from "./logger"; | ||
@@ -10,3 +11,4 @@ export interface TDeps { | ||
logger: TLogger; | ||
mkdirp: typeof mkdirp; | ||
} | ||
export declare function createDeps(): TDeps; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const prettier = require("prettier"); | ||
const mkdirp_1 = require("mkdirp"); | ||
const logger_1 = require("./logger"); | ||
@@ -11,2 +12,3 @@ function createDeps() { | ||
prettier, | ||
mkdirp: mkdirp_1.sync, | ||
resolve: require.resolve.bind(require), | ||
@@ -13,0 +15,0 @@ logger: new logger_1.ConsoleLogger(false), |
import { TDeps } from "./deps"; | ||
import { Options as PrettierOptions } from "prettier"; | ||
import { TArgs } from "./tsGen"; | ||
export interface TPluginCfg { | ||
import { Dictionary } from "./stl"; | ||
export declare type TPluginCfg<T = Dictionary<any>> = { | ||
files: string; | ||
generator: string; | ||
[key: string]: any; | ||
} | ||
} & T; | ||
export interface TTsGenCfg { | ||
prettier: PrettierOptions; | ||
cwd: string; | ||
plugins: TPluginCfg[]; | ||
prettier?: PrettierOptions; | ||
} | ||
interface TArgs { | ||
cwd: string; | ||
configPath: string; | ||
} | ||
export declare function parseConfigFile({ fs, prettier, logger }: TDeps, { cwd, configPath }: TArgs): Promise<TTsGenCfg>; | ||
export {}; |
@@ -15,3 +15,4 @@ "use strict"; | ||
return { | ||
prettier: Object.assign({}, (prettierCfg || {}), { parser: "typescript" }), | ||
cwd, | ||
prettier: prettierCfg, | ||
plugins: pluginCfg, | ||
@@ -18,0 +19,0 @@ }; |
import { TDeps } from "../deps"; | ||
import { TContext } from "./types"; | ||
import { TPlugin } from "./types"; | ||
export declare function loadPlugin(deps: TDeps, ctx: TContext): TPlugin; | ||
import { TContext, TsGeneratorPlugin } from "./types"; | ||
export declare function loadPlugin(deps: TDeps, ctx: TContext): TsGeneratorPlugin; |
@@ -5,3 +5,3 @@ "use strict"; | ||
function loadPlugin(deps, ctx) { | ||
const pluginPath = resolvePlugin_1.resolvePlugin(deps, ctx.config.generator, ctx.cwd); | ||
const pluginPath = resolvePlugin_1.resolvePlugin(deps, ctx.rawConfig.generator, ctx.cwd); | ||
const PluginCtr = require(pluginPath).default; | ||
@@ -8,0 +8,0 @@ return new PluginCtr(ctx); |
import { TPluginCfg } from "../parseConfigFile"; | ||
export interface TContext { | ||
import { Dictionary } from "../stl"; | ||
export declare type TPluginState = "uninitialized" | "initialized"; | ||
export declare type TOutput = void | TFileDesc | TFileDesc[]; | ||
export declare abstract class TsGeneratorPlugin { | ||
readonly ctx: TContext; | ||
state: TPluginState; | ||
abstract readonly name: string; | ||
constructor(ctx: TContext); | ||
beforeRun(): TOutput | Promise<TOutput>; | ||
afterRun(): TOutput | Promise<TOutput>; | ||
abstract transformFile(file: TFileDesc): TOutput | Promise<TOutput>; | ||
} | ||
export interface TContext<T = Dictionary<any>> { | ||
cwd: string; | ||
config: TPluginCfg; | ||
rawConfig: TPluginCfg<T>; | ||
} | ||
@@ -10,8 +22,1 @@ export interface TFileDesc { | ||
} | ||
export interface TPluginConstructor { | ||
new (ctx: TContext): TPlugin; | ||
} | ||
export interface TPlugin { | ||
init: () => void | Promise<void>; | ||
transformFile: (file: TFileDesc) => TFileDesc | TFileDesc[] | Promise<TFileDesc | TFileDesc[]>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class TsGeneratorPlugin { | ||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.state = "uninitialized"; | ||
} | ||
beforeRun() { } | ||
afterRun() { } | ||
} | ||
exports.TsGeneratorPlugin = TsGeneratorPlugin; |
@@ -7,3 +7,4 @@ "use strict"; | ||
__export(require("./utils")); | ||
__export(require("./plugins/types")); | ||
var tsGen_1 = require("./tsGen"); | ||
exports.tsGen = tsGen_1.tsGen; |
@@ -0,6 +1,7 @@ | ||
import { Options as PrettierOptions } from "prettier"; | ||
import { TOutput, TsGeneratorPlugin } from "./plugins/types"; | ||
import { TTsGenCfg } from "./parseConfigFile"; | ||
import { TDeps } from "./deps"; | ||
export interface TArgs { | ||
cwd: string; | ||
configPath: string; | ||
} | ||
export declare function tsGen(deps: TDeps, args: TArgs): Promise<void>; | ||
import { Omit } from "./stl"; | ||
export declare function tsGen(cfg: Omit<TTsGenCfg, "plugins">, _plugins: TsGeneratorPlugin | TsGeneratorPlugin[], _deps?: TDeps): Promise<void>; | ||
export declare function processOutput({ fs, prettier, logger, mkdirp }: TDeps, prettierCfg: PrettierOptions | undefined, output: TOutput): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const glob = require("glob"); | ||
const parseConfigFile_1 = require("./parseConfigFile"); | ||
const loadPlugin_1 = require("./plugins/loadPlugin"); | ||
const deps_1 = require("./deps"); | ||
const util_1 = require("util"); | ||
async function tsGen(deps, args) { | ||
const { cwd } = args; | ||
const { fs, logger, prettier } = deps; | ||
const genConfig = await parseConfigFile_1.parseConfigFile(deps, args); | ||
for (const config of genConfig.plugins) { | ||
const ctx = { cwd, config }; | ||
const plugin = loadPlugin_1.loadPlugin(deps, ctx); | ||
await plugin.init(); | ||
const filePaths = glob.sync(config.files, { ignore: "node_modules/**", absolute: true, cwd }); | ||
const path_1 = require("path"); | ||
async function tsGen(cfg, _plugins, _deps) { | ||
const deps = _deps || deps_1.createDeps(); | ||
const plugins = util_1.isArray(_plugins) ? _plugins : [_plugins]; | ||
const { cwd, prettier } = cfg; | ||
const { fs, logger } = deps; | ||
for (const plugin of plugins) { | ||
logger.info("Running before run"); | ||
processOutput(deps, prettier, await plugin.beforeRun()); | ||
const filePaths = glob.sync(plugin.ctx.rawConfig.files, { ignore: "node_modules/**", absolute: true, cwd }); | ||
const fileDescs = filePaths.map(path => ({ | ||
@@ -21,9 +21,22 @@ path, | ||
for (const fd of fileDescs) { | ||
logger.info(`Processing ${fd.path} with ${config.generator} plugin`); | ||
const output = await plugin.transformFile(fd); | ||
const outputFds = util_1.isArray(output) ? output : [output]; | ||
outputFds.forEach(fd => fs.writeFileSync(fd.path, prettier.format(fd.contents, genConfig.prettier), "utf8")); | ||
logger.info(`Processing ${fd.path} with ${plugin.name} plugin`); | ||
processOutput(deps, prettier, await plugin.transformFile(fd)); | ||
} | ||
logger.info("Running after run"); | ||
processOutput(deps, prettier, await plugin.afterRun()); | ||
} | ||
} | ||
exports.tsGen = tsGen; | ||
function processOutput({ fs, prettier, logger, mkdirp }, prettierCfg, output) { | ||
if (!output) { | ||
return; | ||
} | ||
const outputFds = util_1.isArray(output) ? output : [output]; | ||
outputFds.forEach(fd => { | ||
// ensure directory first | ||
mkdirp(path_1.dirname(fd.path)); | ||
logger.info("Writing file: ", fd.path); | ||
fs.writeFileSync(fd.path, prettier.format(fd.contents, Object.assign({}, (prettierCfg || {}), { parser: "typescript" })), "utf8"); | ||
}); | ||
} | ||
exports.processOutput = processOutput; |
export declare function getFilenameWithoutAnyExtensions(filePath: string): string; | ||
export declare function getRelativeModulePath(cwd: string, to: string): string; | ||
/** | ||
* Both paths have to be file paths, not directories! | ||
* @param from | ||
* @param to | ||
*/ | ||
export declare function getRelativeModulePath(from: string, to: string): string; |
@@ -9,5 +9,14 @@ "use strict"; | ||
exports.getFilenameWithoutAnyExtensions = getFilenameWithoutAnyExtensions; | ||
function getRelativeModulePath(cwd, to) { | ||
return ("./" + path_1.relative(cwd, to)).replace(".ts", ""); // @note(kk): any better way to find it? | ||
/** | ||
* Both paths have to be file paths, not directories! | ||
* @param from | ||
* @param to | ||
*/ | ||
function getRelativeModulePath(from, to) { | ||
const fromDir = path_1.dirname(from); | ||
const relativePathWithExtension = "./" + path_1.relative(fromDir, to); | ||
const extension = path_1.extname(relativePathWithExtension); | ||
const relativePathWithoutExtension = relativePathWithExtension.slice(0, -extension.length); | ||
return relativePathWithoutExtension; | ||
} | ||
exports.getRelativeModulePath = getRelativeModulePath; |
@@ -11,5 +11,6 @@ { | ||
], | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"bin": "dist/cli/run.js", | ||
"main": "./dist/publicApi.js", | ||
"repository": "https://github.com/krzkaczor/ts-generator", | ||
"license": "MIT", | ||
@@ -27,7 +28,8 @@ "scripts": { | ||
"lint:fix": "yarn format:fix && yarn tslint:fix && yarn tsc", | ||
"test:mocha": "NODE_ENV=test mocha --require ts-node/register '**/*.spec.ts'", | ||
"test:mocha:coverage": "NODE_ENV=test nyc mocha --require ts-node/register --require source-map-support/register '**/*.spec.ts'", | ||
"test": "yarn lint && yarn test:mocha", | ||
"test:fix": "yarn lint:fix && yarn test:mocha", | ||
"test:coverage": "yarn lint && yarn test:mocha:coverage", | ||
"check-example": "yarn build && ./example/scripts/test.sh", | ||
"test:mocha": "NODE_ENV=test mocha --require ts-node/register --require ./test/setup-chai.ts '**/*.spec.ts'", | ||
"test:mocha:coverage": "NODE_ENV=test nyc mocha --require ts-node/register --require ./test/setup-chai.ts --require source-map-support/register '**/*.spec.ts'", | ||
"test": "yarn lint && yarn test:mocha && yarn check-example", | ||
"test:fix": "yarn lint:fix && yarn test:mocha && yarn check-example", | ||
"test:coverage": "yarn lint && yarn test:mocha:coverage && yarn check-example", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
@@ -38,6 +40,8 @@ }, | ||
"@types/glob": "^5.0.35", | ||
"@types/mkdirp": "^0.5.2", | ||
"@types/mocha": "^5.2.0", | ||
"@types/node": "^10.0.3", | ||
"@types/prettier": "^1.13.2", | ||
"@types/sinon": "^5.0.1", | ||
"@types/sinon": "^5.0.2", | ||
"@types/sinon-chai": "^3.2.0", | ||
"chai": "^4.1.2", | ||
@@ -48,2 +52,3 @@ "coveralls": "^3.0.2", | ||
"sinon": "^6.1.5", | ||
"sinon-chai": "^3.2.0", | ||
"snap-shot-it": "^6.0.1", | ||
@@ -58,2 +63,3 @@ "source-map-support": "^0.5.8", | ||
"glob": "^7.1.2", | ||
"mkdirp": "^0.5.1", | ||
"prettier": "^1.14.2" | ||
@@ -60,0 +66,0 @@ }, |
@@ -1,5 +0,7 @@ | ||
# ts-gen | ||
# ts-generator | ||
The missing piece for fully typesafe Typescript apps | ||
Generate Typescript typings for any kind of files (json, graphql queries, css modules). | ||
[![Coverage Status](https://coveralls.io/repos/github/krzkaczor/ts-gen/badge.svg?branch=master)](https://coveralls.io/github/krzkaczor/ts-gen?branch=master) | ||
@@ -10,5 +12,9 @@ | ||
- plugin based architecture allowing one tool to handle all different "untyped" data (css modules, graphql queries, | ||
ethereum smartcontracts, json files) — this babel for types | ||
ethereum smartcontracts, json files) — think babel for types | ||
- unified config for all plugins | ||
- automatically prettifies output to match code style of the project | ||
- watch mode | ||
## Example: | ||
- [generate types for simple JSON files](https://github.com/krzkaczor/ts-generator/blob/master/test/integration/ts-gen-plugins/dummy-json/index.ts) |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
14568
26
311
20
3
20
+ Addedmkdirp@^0.5.1
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)