Skip to content
Permalink
Browse files

fix(cli): load ts-node if possible (#248)

  • Loading branch information
goldcaddy77 committed Nov 17, 2019
1 parent 45eacec commit bc6957093c6015c95147a892f76eb9c1a16d7a98
Showing with 21 additions and 11 deletions.
  1. +11 −3 bin/warthog
  2. +10 −8 src/core/BaseService.ts
@@ -7,13 +7,21 @@ var devMode = require('fs').existsSync(`${__dirname}/../src/cli`);
var wantsCompiled = process.argv.indexOf('--compiled-build') >= 0;

if (wantsCompiled || !devMode) {
// TODO: had to load up ts-node so that we can use the warthog CLI in TypeScript projects
// NOTE: commented the line below because in production, ts-node is not installed
// require('ts-node').register({ project: path.join(process.cwd(), 'tsconfig.json') });
// Load ts-node if it's available
try {
require('ts-node').register({ project: path.join(process.cwd(), 'tsconfig.json') });
} catch (e) {}

// this runs from the compiled javascript source
require(`${__dirname}/../dist/cli/cli`).run(process.argv);
} else {
try {
require.resolve('ts-node');
} catch (e) {
console.error('Error: ts-node is required.');
process.exit(1);
}

// this runs from the typescript source (for dev only)
// hook into ts-node so we can run typescript on the fly
require('ts-node').register({ project: path.join(process.cwd(), 'tsconfig.json') });
@@ -13,8 +13,7 @@ export class BaseService<E extends BaseModel> {
columnMap: StringMap;
klass: string;

// TODO: need to figure out why we couldn't type this as Repository<E>
constructor(protected entityClass: any, protected repository: Repository<any>) {
constructor(protected entityClass: any, protected repository: Repository<E>) {
if (!entityClass) {
throw new Error('BaseService requires an entity Class');
}
@@ -42,7 +41,7 @@ export class BaseService<E extends BaseModel> {

async find<W extends WhereInput>(
where?: any,
orderBy?: any, // Fix this
orderBy?: string,
limit?: number,
offset?: number,
fields?: string[]
@@ -138,7 +137,8 @@ export class BaseService<E extends BaseModel> {
}

// TODO: remove any when this is fixed: https://github.com/Microsoft/TypeScript/issues/21592
return this.repository.save(obj, { reload: true });
// TODO: Fix `any`
return this.repository.save(obj as any, { reload: true });
}

async createMany(data: DeepPartial<E>[], userId: string): Promise<E[]> {
@@ -160,7 +160,8 @@ export class BaseService<E extends BaseModel> {
}

// TODO: remove any when this is fixed: https://github.com/Microsoft/TypeScript/issues/21592
return this.repository.save(results, { reload: true });
// TODO: Fix `any`
return this.repository.save(results as any, { reload: true });
}

// TODO: There must be a more succinct way to:
@@ -183,8 +184,8 @@ export class BaseService<E extends BaseModel> {
throw new ArgumentValidationError(errors);
}

// TODO: remove `any` - getting issue here
const result = await this.repository.save(merged);
// TODO: Fix `any`
const result = await this.repository.save(merged as any);
return this.repository.findOneOrFail({ where: { id: result.id } });
}

@@ -198,7 +199,8 @@ export class BaseService<E extends BaseModel> {
const idData = ({ id: found.id } as any) as DeepPartial<E>;
const merged = this.repository.merge(new this.entityClass(), data as any, idData);

await this.repository.save(merged);
// TODO: Fix `any`
await this.repository.save(merged as any);

return { id: found.id };
}

0 comments on commit bc69570

Please sign in to comment.
You can’t perform that action at this time.