nativescript
Version:
Command-line interface for building NativeScript projects
92 lines • 5.35 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectFilesManager = void 0;
const minimatch_1 = require("minimatch");
const path = require("path");
const util = require("util");
const _ = require("lodash");
const yok_1 = require("../yok");
class ProjectFilesManager {
constructor($fs, $localToDevicePathDataFactory, $logger, $projectFilesProvider) {
this.$fs = $fs;
this.$localToDevicePathDataFactory = $localToDevicePathDataFactory;
this.$logger = $logger;
this.$projectFilesProvider = $projectFilesProvider;
}
getProjectFiles(projectFilesPath, excludedProjectDirsAndFiles, filter, opts) {
const projectFiles = this.$fs.enumerateFilesInDirectorySync(projectFilesPath, (filePath, stat) => {
const isFileExcluded = this.isFileExcluded(path.relative(projectFilesPath, filePath), excludedProjectDirsAndFiles);
const isFileFiltered = filter ? filter(filePath, stat) : false;
return !isFileExcluded && !isFileFiltered;
}, opts);
this.$logger.trace("enumerateProjectFiles: %s", util.inspect(projectFiles));
return projectFiles;
}
isFileExcluded(filePath, excludedProjectDirsAndFiles) {
const isInExcludedList = !!_.find(excludedProjectDirsAndFiles, (pattern) => (0, minimatch_1.minimatch)(filePath, pattern, { nocase: true }));
return (isInExcludedList || this.$projectFilesProvider.isFileExcluded(filePath));
}
async createLocalToDevicePaths(deviceAppData, projectFilesPath, files, excludedProjectDirsAndFiles, projectFilesConfig) {
const deviceProjectRootPath = await deviceAppData.getDeviceProjectRootPath();
files =
files ||
this.getProjectFiles(projectFilesPath, excludedProjectDirsAndFiles, null, { enumerateDirectories: true });
const localToDevicePaths = Promise.all(files
.map((projectFile) => this.$projectFilesProvider.getProjectFileInfo(projectFile, deviceAppData.platform, projectFilesConfig))
.filter((projectFileInfo) => projectFileInfo.shouldIncludeFile)
.map(async (projectFileInfo) => this.$localToDevicePathDataFactory.create(projectFileInfo.filePath, projectFilesPath, projectFileInfo.onDeviceFileName, deviceProjectRootPath)));
return localToDevicePaths;
}
processPlatformSpecificFiles(directoryPath, platform, projectFilesConfig, excludedDirs) {
const contents = this.$fs.readDirectory(directoryPath);
const files = [];
_.each(contents, (fileName) => {
const filePath = path.join(directoryPath, fileName);
const fsStat = this.$fs.getFsStats(filePath);
if (fsStat.isDirectory() && !_.includes(excludedDirs, fileName)) {
this.processPlatformSpecificFilesCore(platform, this.$fs.enumerateFilesInDirectorySync(filePath), projectFilesConfig);
}
else if (fsStat.isFile()) {
files.push(filePath);
}
});
this.processPlatformSpecificFilesCore(platform, files, projectFilesConfig);
}
processPlatformSpecificFilesCore(platform, files, projectFilesConfig) {
// Renames the files that have `platform` as substring and removes the files from other platform
_.each(files, (filePath) => {
const projectFileInfo = this.$projectFilesProvider.getProjectFileInfo(filePath, platform, projectFilesConfig);
if (!projectFileInfo.shouldIncludeFile) {
this.$fs.deleteFile(filePath);
}
else if (projectFileInfo.onDeviceFileName) {
const onDeviceFilePath = path.join(path.dirname(filePath), projectFileInfo.onDeviceFileName);
// Fix .js.map entries
const extension = path.extname(projectFileInfo.onDeviceFileName);
if (onDeviceFilePath !== filePath) {
if (extension === ".js" || extension === ".map") {
const oldName = extension === ".map"
? this.getFileName(filePath, extension)
: path.basename(filePath);
const newName = extension === ".map"
? this.getFileName(projectFileInfo.onDeviceFileName, extension)
: path.basename(projectFileInfo.onDeviceFileName);
let fileContent = this.$fs.readText(filePath);
fileContent = fileContent.replace(new RegExp(oldName, "g"), newName);
this.$fs.writeFile(filePath, fileContent);
}
// Rename the file
// this.$fs.rename is not called as it is error prone on some systems with slower hard drives and rigorous antivirus software
this.$fs.writeFile(onDeviceFilePath, this.$fs.readText(filePath));
this.$fs.deleteFile(filePath);
}
}
});
}
getFileName(filePath, extension) {
return path.basename(filePath.replace(extension === ".map" ? ".js.map" : ".js", ""));
}
}
exports.ProjectFilesManager = ProjectFilesManager;
yok_1.injector.register("projectFilesManager", ProjectFilesManager);
//# sourceMappingURL=project-files-manager.js.map
;