@angular/cdk
Version:
Angular Material Component Development Kit
83 lines • 3.17 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevkitFileSystem = void 0;
const core_1 = require("@angular-devkit/core");
const file_system_1 = require("../update-tool/file-system");
const path = require("path");
/**
* File system that leverages the virtual tree from the CLI devkit. This file
* system is commonly used by `ng update` migrations that run as part of the
* Angular CLI.
*/
class DevkitFileSystem extends file_system_1.FileSystem {
constructor(_tree) {
super();
this._tree = _tree;
this._updateRecorderCache = new Map();
}
resolve(...segments) {
// Note: We use `posix.resolve` as the devkit paths are using posix separators.
return (0, core_1.normalize)(path.posix.resolve('/', ...segments.map(core_1.normalize)));
}
edit(filePath) {
if (this._updateRecorderCache.has(filePath)) {
return this._updateRecorderCache.get(filePath);
}
const recorder = this._tree.beginUpdate(filePath);
this._updateRecorderCache.set(filePath, recorder);
return recorder;
}
commitEdits() {
this._updateRecorderCache.forEach(r => this._tree.commitUpdate(r));
this._updateRecorderCache.clear();
}
fileExists(filePath) {
return this._tree.exists(filePath);
}
directoryExists(dirPath) {
// The devkit tree does not expose an API for checking whether a given
// directory exists. It throws a specific error though if a directory
// is being read as a file. We use that to check if a directory exists.
try {
this._tree.get(dirPath);
}
catch (e) {
// Note: We do not use an `instanceof` check here. It could happen that
// the devkit version used by the CLI is different than the one we end up
// loading. This can happen depending on how Yarn/NPM hoists the NPM
// packages / whether there are multiple versions installed. Typescript
// throws a compilation error if the type isn't specified and we can't
// check the type, so we have to cast the error output to any.
if (e.constructor.name === 'PathIsDirectoryException') {
return true;
}
}
return false;
}
overwrite(filePath, content) {
this._tree.overwrite(filePath, content);
}
create(filePath, content) {
this._tree.create(filePath, content);
}
delete(filePath) {
this._tree.delete(filePath);
}
read(filePath) {
const buffer = this._tree.read(filePath);
return buffer !== null ? buffer.toString() : null;
}
readDirectory(dirPath) {
const { subdirs: directories, subfiles: files } = this._tree.getDir(dirPath);
return { directories, files };
}
}
exports.DevkitFileSystem = DevkitFileSystem;
//# sourceMappingURL=devkit-file-system.js.map
;