Skip to content

Commit

Permalink
feat(ts-morph): add ORM version to cache invalidation logic
Browse files Browse the repository at this point in the history
Now we also use the ORM version when computing the cache invalidation hash,
so when we upgrade the ORM, cache will be invalidated automatically.
  • Loading branch information
B4nan committed Aug 11, 2020
1 parent 9be725f commit f28119a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
15 changes: 2 additions & 13 deletions packages/cli/src/CLIHelper.ts
Expand Up @@ -44,7 +44,7 @@ export class CLIHelper {
// noinspection HtmlDeprecatedTag
return yargs
.scriptName('mikro-orm')
.version(CLIHelper.getORMVersion())
.version(Utils.getORMVersion())
.usage('Usage: $0 <command> [options]')
.example('$0 schema:update --run', 'Runs schema synchronization')
.alias('v', 'version')
Expand All @@ -66,17 +66,6 @@ export class CLIHelper {
.strict();
}

static getORMVersion(): string {
/* istanbul ignore next */
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('../package.json').version;
} catch {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('./package.json').version;
}
}

static getNodeVersion(): string {
return process.versions.node;
}
Expand Down Expand Up @@ -105,7 +94,7 @@ export class CLIHelper {

static async dumpDependencies() {
CLIHelper.dump(' - dependencies:');
CLIHelper.dump(` - mikro-orm ${c.green(CLIHelper.getORMVersion())}`);
CLIHelper.dump(` - mikro-orm ${c.green(Utils.getORMVersion())}`);
CLIHelper.dump(` - node ${c.green(CLIHelper.getNodeVersion())}`);

if (await pathExists(process.cwd() + '/package.json')) {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/cache/FileCacheAdapter.ts
Expand Up @@ -6,6 +6,9 @@ import { Utils } from '../utils';

export class FileCacheAdapter implements CacheAdapter {

// eslint-disable-next-line @typescript-eslint/no-var-requires
private readonly VERSION = Utils.getORMVersion();

constructor(private readonly options: { cacheDir: string },
private readonly baseDir: string,
private readonly pretty = false) { }
Expand Down Expand Up @@ -40,7 +43,7 @@ export class FileCacheAdapter implements CacheAdapter {
]);

const opts = this.pretty ? { spaces: 2 } : {};
await writeJSON(path!, { data, origin, hash }, opts);
await writeJSON(path!, { data, origin, hash, version: this.VERSION }, opts);
}

/**
Expand All @@ -66,7 +69,7 @@ export class FileCacheAdapter implements CacheAdapter {

const contents = await readFile(origin);

return Utils.hash(contents.toString());
return Utils.hash(contents.toString() + this.VERSION);
}

}
13 changes: 13 additions & 0 deletions packages/core/src/utils/Utils.ts
Expand Up @@ -618,4 +618,17 @@ export class Utils {
return (createRequire || createRequireFromPath)(resolve(from))(id);
}

static getORMVersion(): string {
/* istanbul ignore next */
try {
// this works with ts-node during development (where we have `src` folder)
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('../../package.json').version;
} catch {
// this works with node in production build (where we do not have the `src` folder)
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('../package.json').version;
}
}

}

0 comments on commit f28119a

Please sign in to comment.