@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
89 lines • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateEditorProviders = migrateEditorProviders;
const ng_morph_1 = require("ng-morph");
const add_unique_import_1 = require("../../../utils/add-unique-import");
const colored_log_1 = require("../../../utils/colored-log");
const get_named_import_references_1 = require("../../../utils/get-named-import-references");
const import_manipulations_1 = require("../../../utils/import-manipulations");
const insert_todo_1 = require("../../../utils/insert-todo");
const EDITOR = '@taiga-ui/editor';
const DEFAULT_EXTENSIONS = 'TUI_EDITOR_DEFAULT_EXTENSIONS';
const CUSTOM_EXTENSIONS_MESSAGE = 'custom editor extensions detected; they were replaced by provideTuiEditor() and must be migrated manually.';
const PROVIDE_TUI_EDITOR_WITH_PLUGINS = `provideTuiEditor({
// You can disable these plugins
// if you don't need them
image: true,
iframe: true,
video: true,
source: true,
audio: true,
details: true,
detailsSummary: true,
detailsContent: true,
})`;
function migrateEditorProviders(_, options) {
if (!options['skip-logs']) {
(0, colored_log_1.infoLog)('Migrating TUI_EDITOR_EXTENSIONS provider to provideTuiEditor()...');
}
const migratedFiles = new Set();
for (const ref of (0, get_named_import_references_1.getNamedImportReferences)('TUI_EDITOR_EXTENSIONS', EDITOR)) {
if (ref.wasForgotten()) {
continue;
}
const property = ref.getParent();
const isProvideProperty = ng_morph_1.Node.isPropertyAssignment(property) && property.getName() === 'provide';
if (!isProvideProperty) {
continue;
}
const provider = property.getParent();
if (!ng_morph_1.Node.isObjectLiteralExpression(provider)) {
continue;
}
const hasExtraPlugins = provider.getProperty('useFactory') !== undefined;
const replacement = hasExtraPlugins
? PROVIDE_TUI_EDITOR_WITH_PLUGINS
: 'provideTuiEditor()';
provider.replaceWithText(hasCustomExtensions(provider)
? `// ${insert_todo_1.TODO_MARK} ${CUSTOM_EXTENSIONS_MESSAGE}\n${replacement}`
: replacement);
migratedFiles.add(ref.getSourceFile().getFilePath());
}
removeUnusedImport('TUI_EDITOR_EXTENSIONS', migratedFiles);
removeUnusedImport('TUI_EDITOR_DEFAULT_EXTENSIONS', migratedFiles);
for (const filePath of migratedFiles) {
(0, add_unique_import_1.addUniqueImport)(filePath, 'provideTuiEditor', EDITOR);
}
(0, ng_morph_1.saveActiveProject)();
}
function hasCustomExtensions(provider) {
var _a, _b, _c;
const useValue = provider.getProperty('useValue');
if (ng_morph_1.Node.isPropertyAssignment(useValue)) {
return ((_a = useValue.getInitializer()) === null || _a === void 0 ? void 0 : _a.getText()) !== DEFAULT_EXTENSIONS;
}
const useFactory = provider.getProperty('useFactory');
return ng_morph_1.Node.isPropertyAssignment(useFactory)
? !((_c = (_b = useFactory.getInitializer()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : '').includes(DEFAULT_EXTENSIONS)
: false;
}
function removeUnusedImport(name, files) {
const refs = (0, get_named_import_references_1.getNamedImportReferences)(name, EDITOR);
for (const ref of refs) {
if (ref.wasForgotten()) {
continue;
}
const specifier = ref.getParent();
const filePath = ref.getSourceFile().getFilePath();
if (!ng_morph_1.Node.isImportSpecifier(specifier) || !files.has(filePath)) {
continue;
}
const stillUsed = refs.some((other) => !other.wasForgotten() &&
other.getSourceFile().getFilePath() === filePath &&
!ng_morph_1.Node.isImportSpecifier(other.getParent()));
if (!stillUsed) {
(0, import_manipulations_1.removeImport)(specifier);
}
}
}
//# sourceMappingURL=migrate-editor-providers.js.map