Skip to content

Commit d255b97

Browse files
committed
feat: deprecate the legacy workflow and recommend the new one
1 parent 59a7310 commit d255b97

15 files changed

+239
-28
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ $injector.require("hmrStatusService", "./services/hmr-status-service");
190190
$injector.require("pacoteService", "./services/pacote-service");
191191
$injector.require("qrCodeTerminalService", "./services/qr-code-terminal-service");
192192
$injector.require("testInitializationService", "./services/test-initialization-service");
193+
$injector.require("workflowService", "./services/workflow-service");
193194

194195
$injector.require("networkConnectivityValidator", "./helpers/network-connectivity-validator");
195196
$injector.requirePublic("cleanupService", "./services/cleanup-service");

lib/commands/preview.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ export class PreviewCommand implements ICommand {
1414
private $options: IOptions,
1515
private $previewAppLogProvider: IPreviewAppLogProvider,
1616
private $previewQrCodeService: IPreviewQrCodeService,
17+
protected $workflowService: IWorkflowService,
1718
$cleanupService: ICleanupService) {
18-
this.$analyticsService.setShouldDispose(false);
19-
$cleanupService.setShouldDispose(false);
20-
}
19+
this.$analyticsService.setShouldDispose(false);
20+
$cleanupService.setShouldDispose(false);
21+
}
2122

2223
public async execute(): Promise<void> {
24+
await this.$workflowService.handleLegacyWorkflow(this.$projectData.projectDir, this.$options);
2325
this.$previewAppLogProvider.on(DEVICE_LOG_EVENT_NAME, (deviceId: string, message: string) => {
2426
this.$logger.info(message);
2527
});

lib/commands/test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ abstract class TestCommandBase {
1111
protected abstract $platformEnvironmentRequirements: IPlatformEnvironmentRequirements;
1212
protected abstract $errors: IErrors;
1313
protected abstract $cleanupService: ICleanupService;
14+
protected abstract $workflowService: IWorkflowService;
1415

1516
async execute(args: string[]): Promise<void> {
17+
await this.$workflowService.handleLegacyWorkflow(this.$projectData.projectDir, this.$options);
1618
await this.$testExecutionService.startKarmaServer(this.platform, this.$projectData, this.projectFilesConfig);
1719
}
1820

@@ -54,7 +56,8 @@ class TestAndroidCommand extends TestCommandBase implements ICommand {
5456
protected $options: IOptions,
5557
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
5658
protected $errors: IErrors,
57-
protected $cleanupService: ICleanupService) {
59+
protected $cleanupService: ICleanupService,
60+
protected $workflowService: IWorkflowService) {
5861
super();
5962
}
6063

@@ -69,7 +72,8 @@ class TestIosCommand extends TestCommandBase implements ICommand {
6972
protected $options: IOptions,
7073
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
7174
protected $errors: IErrors,
72-
protected $cleanupService: ICleanupService) {
75+
protected $cleanupService: ICleanupService,
76+
protected $workflowService: IWorkflowService) {
7377
super();
7478
}
7579

lib/commands/update.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export class UpdateCommand extends ValidatePlatformCommandBase implements IComma
1212
private $pluginsService: IPluginsService,
1313
private $projectDataService: IProjectDataService,
1414
private $fs: IFileSystem,
15-
private $logger: ILogger) {
15+
private $logger: ILogger,
16+
private $workflowService: IWorkflowService) {
1617
super($options, $platformsData, $platformService, $projectData);
1718
this.$projectData.initializeProjectData();
1819
}
@@ -28,6 +29,12 @@ export class UpdateCommand extends ValidatePlatformCommandBase implements IComma
2829
static readonly backupFailMessage: string = "Could not backup project folders!";
2930

3031
public async execute(args: string[]): Promise<void> {
32+
if (this.$options.workflow) {
33+
const forceWebpackWorkflow = true;
34+
await this.$workflowService.handleLegacyWorkflow(this.$projectData.projectDir, this.$options, forceWebpackWorkflow);
35+
return;
36+
}
37+
3138
const tmpDir = path.join(this.$projectData.projectDir, UpdateCommand.tempFolder);
3239

3340
try {

lib/declarations.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
571571
performance: Object;
572572
setupOptions(projectData: IProjectData): void;
573573
cleanupLogFile: string;
574+
workflow: boolean;
574575
}
575576

576577
interface IEnvOptions {
@@ -940,6 +941,12 @@ interface IBundleValidatorHelper {
940941
* @return {void}
941942
*/
942943
validate(minSupportedVersion?: string): void;
944+
945+
/**
946+
* Returns the installed bundler version.
947+
* @return {string}
948+
*/
949+
getBundlerDependencyVersion(bundlerName?: string): string;
943950
}
944951

945952

lib/definitions/platform.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ interface IBuildPlatformAction {
1414
buildPlatform(platform: string, buildConfig: IBuildConfig, projectData: IProjectData): Promise<string>;
1515
}
1616

17+
interface IWorkflowService {
18+
handleLegacyWorkflow(projectDir: string, settings: IWebpackWorkflowSettings, force?: boolean): Promise<void>;
19+
}
20+
21+
interface IWebpackWorkflowSettings {
22+
bundle?: boolean | string;
23+
useHotModuleReload?: boolean;
24+
release?: boolean;
25+
}
26+
1727
interface IPlatformService extends IBuildPlatformAction, NodeJS.EventEmitter {
1828
cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, config: IPlatformOptions, framework?: string): Promise<void>;
1929

lib/definitions/project.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ interface IProjectDataService {
142142
*/
143143
setNSValue(projectDir: string, key: string, value: any): void;
144144

145+
/**
146+
* Sets a value in the `useLegacyWorkflow` key in a project's nsconfig.json.
147+
* @param {string} projectDir The project directory - the place where the root package.json is located.
148+
* @param {any} value Value of the key to be set to `useLegacyWorkflow` key in project's nsconfig.json.
149+
* @returns {void}
150+
*/
151+
setUseLegacyWorkflow(projectDir: string, value: any): void;
152+
145153
/**
146154
* Removes a property from `nativescript` key in project's package.json.
147155
* @param {string} projectDir The project directory - the place where the root package.json is located.
@@ -584,7 +592,7 @@ interface IIOSExtensionsService {
584592
removeExtensions(options: IRemoveExtensionsOptions): void;
585593
}
586594

587-
interface IAddExtensionsFromPathOptions{
595+
interface IAddExtensionsFromPathOptions {
588596
extensionsFolderPath: string;
589597
projectData: IProjectData;
590598
platformData: IPlatformData;

lib/helpers/bundle-validator-helper.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ export class BundleValidatorHelper extends VersionValidatorHelper implements IBu
1616

1717
public validate(minSupportedVersion?: string): void {
1818
if (this.$options.bundle) {
19-
const bundlePluginName = this.bundlersMap[this.$options.bundle];
20-
const bundlerVersionInDependencies = this.$projectData.dependencies && this.$projectData.dependencies[bundlePluginName];
21-
const bundlerVersionInDevDependencies = this.$projectData.devDependencies && this.$projectData.devDependencies[bundlePluginName];
22-
if (!bundlePluginName || (!bundlerVersionInDependencies && !bundlerVersionInDevDependencies)) {
19+
const currentVersion = this.getBundlerDependencyVersion();
20+
if (!currentVersion) {
2321
this.$errors.failWithoutHelp(BundleValidatorMessages.MissingBundlePlugin);
2422
}
2523

26-
const currentVersion = bundlerVersionInDependencies || bundlerVersionInDevDependencies;
2724
const shouldThrowError = minSupportedVersion && this.isValidVersion(currentVersion) && this.isVersionLowerThan(currentVersion, minSupportedVersion);
2825
if (shouldThrowError) {
29-
this.$errors.failWithoutHelp(util.format(BundleValidatorMessages.NotSupportedVersion, minSupportedVersion));
26+
this.$errors.failWithoutHelp(util.format(BundleValidatorMessages.NotSupportedVersion, minSupportedVersion));
3027
}
3128
}
3229
}
30+
31+
public getBundlerDependencyVersion(bundlerName?: string): string {
32+
let dependencyVersion = null;
33+
const bundlePluginName = bundlerName || this.bundlersMap[this.$options.bundle];
34+
const bundlerVersionInDependencies = this.$projectData.dependencies && this.$projectData.dependencies[bundlePluginName];
35+
const bundlerVersionInDevDependencies = this.$projectData.devDependencies && this.$projectData.devDependencies[bundlePluginName];
36+
dependencyVersion = bundlerVersionInDependencies || bundlerVersionInDevDependencies;
37+
38+
return dependencyVersion;
39+
40+
}
3341
}
3442

3543
$injector.register("bundleValidatorHelper", BundleValidatorHelper);

lib/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class Options {
136136
file: { type: OptionType.String, hasSensitiveValue: true },
137137
force: { type: OptionType.Boolean, alias: "f", hasSensitiveValue: false },
138138
// remove legacy
139+
workflow: { type: OptionType.Boolean, hasSensitiveValue: false },
139140
companion: { type: OptionType.Boolean, hasSensitiveValue: false },
140141
emulator: { type: OptionType.Boolean, hasSensitiveValue: false },
141142
sdk: { type: OptionType.String, hasSensitiveValue: false },

lib/project-data.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,4 @@ export class ProjectData implements IProjectData {
263263
this.$logger.warnWithLabel("IProjectData.projectId is deprecated. Please use IProjectData.projectIdentifiers[platform].");
264264
}
265265
}
266-
$injector.register("projectData", ProjectData);
266+
$injector.register("projectData", ProjectData, true);

lib/services/livesync/playground/preview-app-livesync-service.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export class PreviewAppLiveSyncService extends EventEmitter implements IPreviewA
2222
private $previewAppFilesService: IPreviewAppFilesService,
2323
private $previewAppPluginsService: IPreviewAppPluginsService,
2424
private $previewDevicesService: IPreviewDevicesService,
25-
private $hmrStatusService: IHmrStatusService) {
26-
super();
27-
}
25+
private $hmrStatusService: IHmrStatusService,
26+
protected $workflowService: IWorkflowService) {
27+
super();
28+
}
2829

2930
@performanceLog()
3031
public async initialize(data: IPreviewAppLiveSyncData): Promise<void> {
3132
await this.$previewSdkService.initialize(data.projectDir, async (device: Device) => {
33+
await this.$workflowService.handleLegacyWorkflow(data.projectDir, data);
3234
try {
3335
if (!device) {
3436
this.$errors.failWithoutHelp("Sending initial preview files without a specified device is not supported.");
@@ -177,7 +179,7 @@ export class PreviewAppLiveSyncService extends EventEmitter implements IPreviewA
177179
if (status === HmrConstants.HMR_ERROR_STATUS) {
178180
const originalUseHotModuleReload = data.useHotModuleReload;
179181
data.useHotModuleReload = false;
180-
await this.syncFilesForPlatformSafe(data, { filesToSync: platformHmrData.fallbackFiles }, platform, previewDevice.id );
182+
await this.syncFilesForPlatformSafe(data, { filesToSync: platformHmrData.fallbackFiles }, platform, previewDevice.id);
181183
data.useHotModuleReload = originalUseHotModuleReload;
182184
}
183185
}));

lib/services/platform-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class PlatformService extends EventEmitter implements IPlatformService {
3636
private $terminalSpinnerService: ITerminalSpinnerService,
3737
private $pacoteService: IPacoteService,
3838
private $usbLiveSyncService: any,
39-
public $hooksService: IHooksService
39+
public $hooksService: IHooksService,
40+
public $workflowService: IWorkflowService
4041
) {
4142
super();
4243
}
@@ -221,6 +222,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
221222

222223
@performanceLog()
223224
public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
225+
await this.$workflowService.handleLegacyWorkflow(platformInfo.projectData.projectDir, platformInfo.appFilesUpdaterOptions);
224226
const changesInfo = await this.getChangesInfo(platformInfo);
225227
const shouldPrepare = await this.shouldPrepare({ platformInfo, changesInfo });
226228

lib/services/project-data-service.ts

+63-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as path from "path";
2+
import * as constants from "../constants";
23
import { ProjectData } from "../project-data";
4+
import { parseJson } from "../common/helpers";
35
import { exported } from "../common/decorators";
46
import {
57
NATIVESCRIPT_PROPS_INTERNAL_DELIMITER,
@@ -17,14 +19,21 @@ interface IProjectFileData {
1719
}
1820

1921
export class ProjectDataService implements IProjectDataService {
22+
private defaultProjectDir = "";
2023
private static DEPENDENCIES_KEY_NAME = "dependencies";
24+
private projectDataCache: IDictionary<IProjectData> = {};
2125

2226
constructor(private $fs: IFileSystem,
2327
private $staticConfig: IStaticConfig,
2428
private $logger: ILogger,
2529
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
2630
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
27-
private $injector: IInjector) {
31+
private $injector: IInjector,
32+
$projectData: IProjectData) {
33+
// add the ProjectData of the default projectDir to the projectData cache
34+
$projectData.initializeProjectData();
35+
this.defaultProjectDir = $projectData.projectDir;
36+
this.projectDataCache[this.defaultProjectDir] = $projectData;
2837
}
2938

3039
public getNSValue(projectDir: string, propertyName: string): any {
@@ -49,16 +58,19 @@ export class ProjectDataService implements IProjectDataService {
4958
// TODO: Remove $projectData and replace it with $projectDataService.getProjectData
5059
@exported("projectDataService")
5160
public getProjectData(projectDir: string): IProjectData {
52-
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
53-
projectDataInstance.initializeProjectData(projectDir);
54-
return projectDataInstance;
61+
projectDir = projectDir || this.defaultProjectDir;
62+
this.projectDataCache[projectDir] = this.projectDataCache[projectDir] || this.$injector.resolve<IProjectData>(ProjectData);
63+
this.projectDataCache[projectDir].initializeProjectData(projectDir);
64+
65+
return this.projectDataCache[projectDir];
5566
}
5667

5768
@exported("projectDataService")
5869
public getProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): IProjectData {
59-
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
60-
projectDataInstance.initializeProjectDataFromContent(packageJsonContent, nsconfigContent, projectDir);
61-
return projectDataInstance;
70+
projectDir = projectDir || this.defaultProjectDir;
71+
this.projectDataCache[projectDir] = this.projectDataCache[projectDir] || this.$injector.resolve<IProjectData>(ProjectData);
72+
this.projectDataCache[projectDir].initializeProjectDataFromContent(packageJsonContent, nsconfigContent, projectDir);
73+
return this.projectDataCache[projectDir];
6274
}
6375

6476
@exported("projectDataService")
@@ -124,6 +136,14 @@ export class ProjectDataService implements IProjectDataService {
124136
};
125137
}
126138

139+
public setUseLegacyWorkflow(projectDir: string, value: any): void {
140+
// TODO: use trace
141+
this.$logger.info(`useLegacyWorkflow will be set to ${value}`);
142+
this.updateNsConfigValue(projectDir, { useLegacyWorkflow: value });
143+
this.refreshProjectData(projectDir);
144+
this.$logger.info(`useLegacyWorkflow was set to ${value}`);
145+
}
146+
127147
public getAppExecutableFiles(projectDir: string): string[] {
128148
const projectData = this.getProjectData(projectDir);
129149

@@ -157,6 +177,34 @@ export class ProjectDataService implements IProjectDataService {
157177
return files;
158178
}
159179

180+
private refreshProjectData(projectDir: string) {
181+
if (this.projectDataCache[projectDir]) {
182+
this.projectDataCache[projectDir].initializeProjectData(projectDir);
183+
}
184+
}
185+
186+
private updateNsConfigValue(projectDir: string, updateObject: INsConfig): void {
187+
const nsConfigPath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
188+
const currentNsConfig = this.getNsConfig(nsConfigPath);
189+
const newNsConfig = Object.assign(currentNsConfig, updateObject);
190+
191+
this.$fs.writeJson(nsConfigPath, newNsConfig);
192+
}
193+
194+
private getNsConfig(nsConfigPath: string): INsConfig {
195+
let result = this.getNsConfigDefaultObject();
196+
if (this.$fs.exists(nsConfigPath)) {
197+
const nsConfigContent = this.$fs.readText(nsConfigPath);
198+
try {
199+
result = <INsConfig>parseJson(nsConfigContent);
200+
} catch (e) {
201+
// default
202+
}
203+
}
204+
205+
return result;
206+
}
207+
160208
private getImageDefinitions(): IImageDefinitionsStructure {
161209
const pathToImageDefinitions = path.join(__dirname, "..", "..", CLI_RESOURCES_DIR_NAME, AssetConstants.assets, AssetConstants.imageDefinitionsFileName);
162210
const imageDefinitions = this.$fs.readJson(pathToImageDefinitions);
@@ -308,10 +356,16 @@ export class ProjectDataService implements IProjectDataService {
308356
};
309357
}
310358

359+
private getNsConfigDefaultObject(data?: Object): INsConfig {
360+
const config: INsConfig = { useLegacyWorkflow: false };
361+
Object.assign(config, data);
362+
363+
return config;
364+
}
365+
311366
@exported("projectDataService")
312367
public getNsConfigDefaultContent(data?: Object): string {
313-
const config: INsConfig = {};
314-
Object.assign(config, data);
368+
const config = this.getNsConfigDefaultObject(data);
315369

316370
return JSON.stringify(config);
317371
}

0 commit comments

Comments
 (0)