nativescript
Version:
Command-line interface for building NativeScript projects
117 lines • 5.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildController = void 0;
const constants = require("../constants");
const constants_1 = require("../common/constants");
const events_1 = require("events");
const helpers_1 = require("../common/helpers");
const yok_1 = require("../common/yok");
const perf_hooks_1 = require("perf_hooks");
class BuildController extends events_1.EventEmitter {
constructor($analyticsService, $buildArtifactsService, $buildInfoFileService, $fs, $logger, $injector, $mobileHelper, $projectDataService, $projectChangesService, $prepareController) {
super();
this.$analyticsService = $analyticsService;
this.$buildArtifactsService = $buildArtifactsService;
this.$buildInfoFileService = $buildInfoFileService;
this.$fs = $fs;
this.$logger = $logger;
this.$injector = $injector;
this.$mobileHelper = $mobileHelper;
this.$projectDataService = $projectDataService;
this.$projectChangesService = $projectChangesService;
this.$prepareController = $prepareController;
}
get $platformsDataService() {
return this.$injector.resolve("platformsDataService");
}
async prepareAndBuild(buildData) {
await this.$prepareController.prepare(buildData);
const result = await this.build(buildData);
return result;
}
async build(buildData) {
this.$logger.info("Building project...");
const startTime = perf_hooks_1.performance.now();
const platform = buildData.platform.toLowerCase();
const projectData = this.$projectDataService.getProjectData(buildData.projectDir);
const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
const action = "Build" /* constants.TrackActionNames.Build */;
const isForDevice = this.$mobileHelper.isAndroidPlatform(platform)
? null
: buildData && buildData.buildForDevice;
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action,
isForDevice,
platform,
projectDir: projectData.projectDir,
additionalData: `${buildData.release ? constants_1.Configurations.Release : constants_1.Configurations.Debug}_${buildData.clean
? "Clean" /* constants.BuildStates.Clean */
: "Incremental" /* constants.BuildStates.Incremental */}`,
});
if (buildData.clean) {
await platformData.platformProjectService.cleanProject(platformData.projectRoot);
}
const handler = (data) => {
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
this.$logger.info(data.data.toString(), {
[constants.LoggerConfigData.skipNewLine]: true,
});
};
await (0, helpers_1.attachAwaitDetach)(constants.BUILD_OUTPUT_EVENT_NAME, platformData.platformProjectService, handler, platformData.platformProjectService.buildProject(platformData.projectRoot, projectData, buildData));
const buildInfoFileDir = platformData.getBuildOutputPath(buildData);
this.$buildInfoFileService.saveLocalBuildInfo(platformData, buildInfoFileDir);
const endTime = perf_hooks_1.performance.now();
const buildTime = (endTime - startTime) / 1000;
this.$logger.info("Project successfully built.");
this.$logger.info(`Build time: ${buildTime.toFixed(3)} s.`);
const result = await this.$buildArtifactsService.getLatestAppPackagePath(platformData, buildData);
if (buildData.copyTo) {
this.$buildArtifactsService.copyLatestAppPackage(buildData.copyTo, platformData, buildData);
}
else {
this.$logger.info(`The build result is located at: ${result}`);
}
return result;
}
async buildIfNeeded(buildData) {
let result = null;
const shouldBuildPlatform = await this.shouldBuild(buildData);
if (shouldBuildPlatform) {
result = await this.build(buildData);
}
return result;
}
async shouldBuild(buildData) {
const projectData = this.$projectDataService.getProjectData(buildData.projectDir);
const platformData = this.$platformsDataService.getPlatformData(buildData.platform, projectData);
const outputPath = buildData.outputPath || platformData.getBuildOutputPath(buildData);
const changesInfo = this.$projectChangesService.currentChanges ||
(await this.$projectChangesService.checkForChanges(platformData, projectData, buildData));
if (changesInfo.changesRequireBuild) {
return true;
}
if (!this.$fs.exists(outputPath)) {
return true;
}
const validBuildOutputData = platformData.getValidBuildOutputData(buildData);
const packages = this.$buildArtifactsService.getAllAppPackages(outputPath, validBuildOutputData);
if (packages.length === 0) {
return true;
}
const prepareInfo = this.$projectChangesService.getPrepareInfo(platformData);
const buildInfo = this.$buildInfoFileService.getLocalBuildInfo(platformData, buildData);
if (!prepareInfo || !buildInfo) {
return true;
}
if (buildData.clean) {
return true;
}
if (prepareInfo.time === buildInfo.prepareTime) {
return false;
}
return prepareInfo.changesRequireBuildTime !== buildInfo.prepareTime;
}
}
exports.BuildController = BuildController;
yok_1.injector.register("buildController", BuildController);
//# sourceMappingURL=build-controller.js.map