quip-cli
Version:
A Command Line Interface for the Quip Live Apps platform
113 lines (112 loc) • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const mkdirp_1 = tslib_1.__importDefault(require("mkdirp"));
const path_1 = tslib_1.__importDefault(require("path"));
const manifest_1 = require("../lib/manifest");
const util_1 = require("../lib/util");
class Migration extends command_1.Command {
getMigrationName_(name) {
const desc = name
? `_${name.replace(/[^\w\d_-]/g, "_").toLowerCase()}`
: "_01";
const migrationDate = new Date()
.toISOString()
.substring(0, 10)
.replace(/-/g, "");
return `${migrationDate}${desc}`;
}
incrementMigrationName_(name) {
const match = name.match(/^(.+)_(\d+)$/);
if (match) {
const number = parseInt(match[2]);
const numString = String(number + 1).padStart(2, "0");
return `${match[1]}_${numString}`;
}
return `${name}_01`;
}
async run() {
const { args, flags } = this.parse(Migration);
const dryRun = flags["dry-run"];
let manifestPath = flags.manifest;
if (flags.manifest && !(await util_1.pathExists(flags.manifest))) {
throw new Error(`Provided manifest ${flags.manifest} does not exist.`);
}
if (!manifestPath) {
manifestPath = await manifest_1.findManifest(process.cwd());
if (!manifestPath) {
throw new Error(`Could not find a manifest.json file.`);
}
}
const ops = [];
const migrationsFolder = path_1.default.join(process.cwd(), flags.folder);
if (dryRun) {
if (!(await util_1.pathExists(migrationsFolder))) {
this.log(`Would create: ${path_1.default.relative(path_1.default.dirname(manifestPath), migrationsFolder)}`);
}
}
else {
ops.push(() => mkdirp_1.default(migrationsFolder));
}
const extension = ".js";
let name = this.getMigrationName_(args.name);
let migrationPath = path_1.default.join(migrationsFolder, name + extension);
while (await util_1.pathExists(migrationPath)) {
name = this.incrementMigrationName_(name);
migrationPath = path_1.default.join(migrationsFolder, name + extension);
}
if (dryRun) {
this.log(`Would create: ${path_1.default.relative(path_1.default.dirname(manifestPath), migrationPath)}`);
}
else {
ops.push(() => util_1.copy(path_1.default.join(__dirname, "../../templates/migration.example.js"), migrationPath));
}
const manifest = await manifest_1.getManifest(manifestPath);
const migrations = manifest.migrations || [];
migrations.push({
version_number: flags.version || manifest.version_number,
js_file: path_1.default.relative(path_1.default.dirname(manifestPath), migrationPath),
});
if (dryRun) {
this.log("Would add migration:");
this.log(JSON.stringify(migrations[migrations.length - 1], null, 4));
}
else {
ops.push(() => manifest_1.writeManifest(manifestPath, { migrations }));
}
for (const op of ops) {
await op();
}
}
}
exports.default = Migration;
Migration.description = "Creates a new migration";
Migration.flags = {
help: command_1.flags.help({ char: "h" }),
folder: command_1.flags.string({
char: "f",
description: "The folder where your migrations are stored",
default: "migrations",
}),
version: command_1.flags.integer({
char: "v",
description: "The version to generate this migration for. By default, it will use the current version_number in the manifest",
}),
["dry-run"]: command_1.flags.boolean({
char: "d",
description: "Print what this would do, but don't create any files.",
}),
manifest: command_1.flags.string({
char: "m",
description: "A manifest.json file to add the migration to. By default, we'll use the first one we find.",
hidden: true,
}),
};
Migration.args = [
{
name: "name",
optional: true,
description: "A short description to generate the filename with",
},
];