@sanity/migrate
Version:
Tooling for running data migrations on Sanity.io projects
547 lines (546 loc) • 22.1 kB
JavaScript
function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _async_to_generator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _instanceof(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
function _ts_generator(thisArg, body) {
var f, y, t, _ = {
label: 0,
sent: function() {
if (t[0] & 1) throw t[1];
return t[1];
},
trys: [],
ops: []
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
return d(g, "next", {
value: verb(0)
}), d(g, "throw", {
value: verb(1)
}), d(g, "return", {
value: verb(2)
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
value: function() {
return this;
}
}), g;
function verb(n) {
return function(v) {
return step([
n,
v
]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while(g && (g = 0, op[0] && (_ = 0)), _)try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [
op[0] & 2,
t.value
];
switch(op[0]){
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return {
value: op[1],
done: false
};
case 5:
_.label++;
y = op[1];
op = [
0
];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2]) _.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [
6,
e
];
y = 0;
} finally{
f = t = 0;
}
if (op[0] & 5) throw op[1];
return {
value: op[0] ? op[1] : void 0,
done: true
};
}
}
import { access, mkdir, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { Args } from '@oclif/core';
import { SanityCommand } from '@sanity/cli-core';
import { chalk, confirm, input, select } from '@sanity/cli-core/ux';
import { deburr } from 'lodash-es';
import { getMigrationRootDirectory } from '../../actions/migration/getMigrationRootDirectory.js';
import { minimalAdvanced, minimalSimple, renameField, renameType, stringToPTE } from '../../actions/migration/templates/index.js';
import { MIGRATIONS_DIRECTORY } from '../../utils/migration/constants.js';
var TEMPLATES = [
{
name: 'Minimalistic migration to get you started',
template: minimalSimple
},
{
name: 'Rename an object type',
template: renameType
},
{
name: 'Rename a field',
template: renameField
},
{
name: 'Convert string field to Portable Text',
template: stringToPTE
},
{
name: 'Advanced template using async iterators providing more fine grained control',
template: minimalAdvanced
}
];
export var CreateMigrationCommand = /*#__PURE__*/ function(SanityCommand) {
"use strict";
_inherits(CreateMigrationCommand, SanityCommand);
function CreateMigrationCommand() {
_class_call_check(this, CreateMigrationCommand);
return _call_super(this, CreateMigrationCommand, arguments);
}
_create_class(CreateMigrationCommand, [
{
key: "run",
value: function run() {
return _async_to_generator(function() {
var args, workDir, title, types, template, renderedTemplate, sluggedName, destDir, definitionFile, dirCreated;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
this.parse(CreateMigrationCommand)
];
case 1:
args = _state.sent().args;
return [
4,
getMigrationRootDirectory(this.output)
];
case 2:
workDir = _state.sent();
return [
4,
this.promptForTitle(args.title)
];
case 3:
title = _state.sent();
return [
4,
this.promptForDocumentTypes()
];
case 4:
types = _state.sent();
return [
4,
this.promptForTemplate()
];
case 5:
template = _state.sent().template;
renderedTemplate = (template || minimalSimple)({
documentTypes: types.split(',').map(function(t) {
return t.trim();
}).filter(Boolean),
migrationName: title
});
sluggedName = deburr(title.toLowerCase()).replaceAll(/\s+/g, '-').replaceAll(/[^a-z0-9-]/g, '');
destDir = path.join(workDir, MIGRATIONS_DIRECTORY, sluggedName);
definitionFile = path.join(destDir, 'index.ts');
return [
4,
this.createMigrationFile(destDir, definitionFile, renderedTemplate)
];
case 6:
dirCreated = _state.sent();
if (dirCreated) {
this.log();
this.log("".concat(chalk.green('✓'), " Migration created!"));
this.log();
this.log('Next steps:');
this.log("Open ".concat(chalk.bold(definitionFile), " in your code editor and write the code for your migration."));
this.log("Dry run the migration with:\n`".concat(chalk.bold("sanity migration run ".concat(sluggedName, " --project=<projectId> --dataset <dataset> ")), "`"));
this.log("Run the migration against a dataset with:\n `".concat(chalk.bold("sanity migration run ".concat(sluggedName, " --project=<projectId> --dataset <dataset> --no-dry-run")), "`"));
this.log();
this.log("\uD83D\uDC49 Learn more about schema and content migrations at ".concat(chalk.bold('https://www.sanity.io/docs/schema-and-content-migrations')));
}
return [
2
];
}
});
}).call(this);
}
},
{
key: "createMigrationFile",
value: function createMigrationFile(destDir, definitionFile, renderedTemplate) {
return _async_to_generator(function() {
var dirExists, shouldOverwrite, error, message, error1, message1;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
access(destDir).then(function() {
return true;
}).catch(function() {
return false;
})
];
case 1:
dirExists = _state.sent();
if (!dirExists) return [
3,
3
];
return [
4,
this.promptForOverwrite(destDir)
];
case 2:
shouldOverwrite = _state.sent();
if (!shouldOverwrite) return [
2,
false
];
_state.label = 3;
case 3:
_state.trys.push([
3,
5,
,
6
]);
return [
4,
mkdir(destDir, {
recursive: true
})
];
case 4:
_state.sent();
return [
3,
6
];
case 5:
error = _state.sent();
message = _instanceof(error, Error) ? error.message : String(error);
this.error("Failed to create migration directory: ".concat(message), {
exit: 1
});
return [
3,
6
];
case 6:
_state.trys.push([
6,
8,
,
9
]);
return [
4,
writeFile(definitionFile, renderedTemplate)
];
case 7:
_state.sent();
return [
3,
9
];
case 8:
error1 = _state.sent();
message1 = _instanceof(error1, Error) ? error1.message : String(error1);
this.error("Failed to create migration file: ".concat(message1), {
exit: 1
});
return [
3,
9
];
case 9:
return [
2,
true
];
}
});
}).call(this);
}
},
{
key: "promptForDocumentTypes",
value: function promptForDocumentTypes() {
return _async_to_generator(function() {
return _ts_generator(this, function(_state) {
return [
2,
input({
message: 'Type of documents to migrate. You can add multiple types separated by comma (optional)'
})
];
});
})();
}
},
{
key: "promptForOverwrite",
value: function promptForOverwrite(destDir) {
return _async_to_generator(function() {
return _ts_generator(this, function(_state) {
return [
2,
confirm({
default: false,
message: "Migration directory ".concat(chalk.cyan(destDir), " already exists. Overwrite?")
})
];
});
})();
}
},
{
key: "promptForTemplate",
value: function promptForTemplate() {
return _async_to_generator(function() {
var templatesByName, templateName;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
templatesByName = Object.fromEntries(TEMPLATES.map(function(t) {
return [
t.name,
t
];
}));
return [
4,
select({
choices: TEMPLATES.map(function(definedTemplate) {
return {
name: definedTemplate.name,
value: definedTemplate.name
};
}),
message: 'Select a template'
})
];
case 1:
templateName = _state.sent();
return [
2,
templatesByName[templateName]
];
}
});
})();
}
},
{
key: "promptForTitle",
value: function promptForTitle(providedTitle) {
return _async_to_generator(function() {
return _ts_generator(this, function(_state) {
if (providedTitle === null || providedTitle === void 0 ? void 0 : providedTitle.trim()) {
return [
2,
providedTitle
];
}
return [
2,
input({
message: 'Title of migration (e.g. "Rename field from location to address")',
validate: function(value) {
if (!value.trim()) {
return 'Title cannot be empty';
}
return true;
}
})
];
});
})();
}
}
]);
return CreateMigrationCommand;
}(SanityCommand);
_define_property(CreateMigrationCommand, "args", {
title: Args.string({
description: 'Title of migration',
required: false
})
});
_define_property(CreateMigrationCommand, "description", 'Create a new migration within your project');
_define_property(CreateMigrationCommand, "examples", [
{
command: '<%= config.bin %> <%= command.id %>',
description: 'Create a new migration, prompting for title and options'
},
{
command: '<%= config.bin %> <%= command.id %> "Rename field from location to address"',
description: 'Create a new migration with the provided title, prompting for options'
}
]);