@fewer/cli
Version:
The CLI to scaffold and perform operations for Fewer.
60 lines • 2.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const command_1 = require("@oclif/command");
const commonFlags_1 = __importDefault(require("../../commonFlags"));
const getConfig_1 = __importDefault(require("../../getConfig"));
const utils_1 = require("../../utils");
function pad(num) {
return String(num).padStart(2, '0');
}
// Gets a UTC date formatted YYYYMMDDHHMMSS
function getMigrationVersion() {
const now = new Date();
const year = now.getUTCFullYear();
const month = pad(now.getUTCMonth() + 1);
const day = pad(now.getUTCDate());
const hours = pad(now.getUTCHours());
const minutes = pad(now.getUTCMinutes());
const seconds = pad(now.getUTCSeconds());
return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
class GenerateMigration extends command_1.Command {
async run() {
const { args } = this.parse(GenerateMigration);
const config = await getConfig_1.default();
if (!config.databases.length) {
this.warn('We did not find any configured databases in your Fewer configuration file.');
}
let [database] = config.databases;
if (config.databases.length > 1) {
database = await utils_1.prompt({
type: 'select',
message: 'Which database is this migration for?',
choices: config.databases,
});
}
const version = getMigrationVersion();
const migrationFileName = path_1.default.join(config.migrations, `${version}_${args.name}.${config.typescript ? 'ts' : 'js'}`);
const databaseImportPath = utils_1.resolve(migrationFileName, database);
;
utils_1.createFile('migration', migrationFileName, {
version,
database: databaseImportPath,
}, config.cjs);
}
}
GenerateMigration.description = 'Generates a migration that can be run.';
GenerateMigration.flags = Object.assign({}, commonFlags_1.default);
GenerateMigration.args = [
{
name: 'name',
description: 'The name of the migration that will be generated.',
required: true,
},
];
exports.default = GenerateMigration;
//# sourceMappingURL=migration.js.map