@melchyore/adonis-cache
Version:
Cache package for AdonisJS V5
149 lines (148 loc) • 5.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const CONFIG_PARTIALS_BASE = './config/partials';
const STORE_PROMPT_CHOICES = [
{
name: 'database',
message: 'Database',
hint: '(Use a database table as cache store)'
},
{
name: 'redis',
message: 'Redis',
hint: '(Use a Redis as cache store)'
},
{
name: 'memcached',
message: 'Memcached',
hint: '(Use a memcached server as cache store)'
},
{
name: 'in_memory',
message: 'InMemory',
hint: '(Use memory as cache store)'
},
{
name: 'dyanmodb',
message: 'DynamoDB',
hint: '(Use a DynamoDB table as cache store)'
},
{
name: 'file',
message: 'File',
hint: '(Use files as cache store)'
}
];
async function getStore(sink) {
return sink
.getPrompt()
.choice('Select which store you want to use for Cache (select using space)', STORE_PROMPT_CHOICES, {
validate(choice) {
return choice ? true : 'Select at least one store';
}
});
}
function getStub(...paths) {
return (0, path_1.join)(__dirname, 'templates', ...paths);
}
function makeConfig(projectRoot, app, sink, state) {
const store = state.store;
const configDirectory = app.directoriesMap.get('config') || 'config';
const configPath = (0, path_1.join)(configDirectory, 'cache.ts');
const template = new sink.files.MustacheFile(projectRoot, configPath, getStub('config/config.txt'));
template.overwrite = true;
const partial = getStub(CONFIG_PARTIALS_BASE, `${store.replace('_', '-')}-store.txt`);
template
.apply(state)
.partials({ [`${store}_store`]: partial })
.commit();
sink.logger.action('create').succeeded(configPath);
}
function makeCacheMigration(projectRoot, app, sink, state) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database';
const migrationPath = (0, path_1.join)(migrationsDirectory, `${Date.now()}_${state.cacheTableName}.ts`);
const template = new sink.files.MustacheFile(projectRoot, migrationPath, getStub('migration.txt'));
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`);
return;
}
template.apply(state).commit();
sink.logger.action('create').succeeded(migrationPath);
}
function makeContract(projectRoot, app, sink) {
const contractsDirectory = app.directoriesMap.get('contracts') || 'contracts';
const contractPath = (0, path_1.join)(contractsDirectory, 'cache.ts');
const template = new sink.files.MustacheFile(projectRoot, contractPath, getStub('contract/contract.txt'));
template.overwrite = true;
template.commit();
sink.logger.action('create').succeeded(contractPath);
}
async function getCacheTableName(sink) {
return sink.getPrompt().ask('Enter the cache table name', {
default: 'cache',
validate(value) {
return !!value.trim().length;
}
});
}
async function getDynamoDBCacheTableName(sink) {
return sink.getPrompt().ask('Enter the DynamoDB cache table name', {
default: 'Cache',
validate(value) {
return !!value.trim().length;
}
});
}
async function getCacheDisk(sink) {
return sink.getPrompt().ask('Enter the disk cache name', {
default: 'cache',
validate(value) {
return !!value.trim().length;
}
});
}
async function getMigrationConsent(sink, tableName) {
return sink
.getPrompt()
.confirm(`Create migration for the ${sink.logger.colors.underline(tableName)} table?`);
}
async function instructions(projectRoot, app, sink) {
const state = {
store: '',
cacheTableName: '',
dynamoDBCacheTableName: '',
disk: '',
stores: {
database: false,
redis: false,
in_memory: false,
memcached: false,
dynamodb: false,
file: false
}
};
state.store = await getStore(sink);
state.stores[state.store] = true;
const stores = {
async database() {
state.cacheTableName = await getCacheTableName(sink);
const cacheMigrationConsent = await getMigrationConsent(sink, state.cacheTableName);
if (cacheMigrationConsent) {
makeCacheMigration(projectRoot, app, sink, state);
}
},
async dynamodb() {
state.dynamoDBCacheTableName = await getDynamoDBCacheTableName(sink);
},
async file() {
state.disk = await getCacheDisk(sink);
}
};
if (Object.keys(stores).includes(state.store)) {
await stores[state.store]();
}
makeContract(projectRoot, app, sink);
makeConfig(projectRoot, app, sink, state);
}
exports.default = instructions;