Skip to content

Commit 6232003

Browse files
author
Dimitar Tachev
authored
Merge pull request #5211 from NativeScript/tachev/env-check-hook
feat: add env check hook in order to allow cloud builds without any native local setup
2 parents 41ae8be + 889e9e9 commit 6232003

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NativeScript CLI Changelog
77
### New
88

99
* [Implemented #5205](https://github.com/NativeScript/nativescript-cli/issues/5205): Support build hooks
10+
* [Implemented #5210](https://github.com/NativeScript/nativescript-cli/issues/5210): Support environment check hooks
1011

1112
### Fixed
1213

lib/services/platform-environment-requirements.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NATIVESCRIPT_CLOUD_EXTENSION_NAME, TrackActionNames } from "../constants";
2-
import { isInteractive } from "../common/helpers";
2+
import { isInteractive, hook } from "../common/helpers";
33
import { EOL } from "os";
44

55
export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequirements {
@@ -39,6 +39,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
3939
"deploy": "tns cloud deploy"
4040
};
4141

42+
@hook("checkEnvironment")
4243
public async checkEnvironmentRequirements(input: ICheckEnvironmentRequirementsInput): Promise<ICheckEnvironmentRequirementsOutput> {
4344
const { platform, projectDir, runtimeVersion } = input;
4445
const notConfiguredEnvOptions = input.notConfiguredEnvOptions || {};

test/services/platform-environment-requirements.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ function createTestInjector() {
1818
testInjector.register("analyticsService", {
1919
trackEventActionInGoogleAnalytics: () => ({})
2020
});
21-
testInjector.register("commandsService", {currentCommandData: {commandName: "test", commandArguments: [""]}});
21+
testInjector.register("commandsService", { currentCommandData: { commandName: "test", commandArguments: [""] } });
2222
testInjector.register("doctorService", {});
23+
testInjector.register("hooksService", stubs.HooksServiceStub);
2324
testInjector.register("errors", {
2425
fail: (err: any) => {
2526
throw new Error(err.formatStr || err.message || err);
@@ -47,21 +48,21 @@ describe("platformEnvironmentRequirements ", () => {
4748
describe("checkRequirements", () => {
4849
let testInjector: IInjector = null;
4950
let platformEnvironmentRequirements: IPlatformEnvironmentRequirements = null;
50-
let promptForChoiceData: {message: string, choices: string[]}[] = [];
51+
let promptForChoiceData: { message: string, choices: string[] }[] = [];
5152
let isExtensionInstallCalled = false;
5253

53-
function mockDoctorService(data: {canExecuteLocalBuild: boolean, mockSetupScript?: boolean}) {
54+
function mockDoctorService(data: { canExecuteLocalBuild: boolean, mockSetupScript?: boolean }) {
5455
const doctorService = testInjector.resolve("doctorService");
5556
doctorService.canExecuteLocalBuild = () => data.canExecuteLocalBuild;
5657
if (data.mockSetupScript) {
5758
doctorService.runSetupScript = () => Promise.resolve();
5859
}
5960
}
6061

61-
function mockPrompter(data: {firstCallOptionName: string, secondCallOptionName?: string}) {
62+
function mockPrompter(data: { firstCallOptionName: string, secondCallOptionName?: string }) {
6263
const prompter = testInjector.resolve("prompter");
6364
prompter.promptForChoice = (message: string, choices: string[]) => {
64-
promptForChoiceData.push({message: message, choices: choices});
65+
promptForChoiceData.push({ message: message, choices: choices });
6566

6667
if (promptForChoiceData.length === 1) {
6768
return Promise.resolve(data.firstCallOptionName);
@@ -73,7 +74,7 @@ describe("platformEnvironmentRequirements ", () => {
7374
};
7475
}
7576

76-
function mockNativeScriptCloudExtensionService(data: {isInstalled: boolean}) {
77+
function mockNativeScriptCloudExtensionService(data: { isInstalled: boolean }) {
7778
const nativeScriptCloudExtensionService = testInjector.resolve("nativeScriptCloudExtensionService");
7879
nativeScriptCloudExtensionService.isInstalled = () => data.isInstalled;
7980
nativeScriptCloudExtensionService.install = () => { isExtensionInstallCalled = true; };
@@ -124,7 +125,7 @@ describe("platformEnvironmentRequirements ", () => {
124125
mockPrompter({ firstCallOptionName: PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME });
125126
mockNativeScriptCloudExtensionService({ isInstalled: true });
126127

127-
await assert.isRejected(platformEnvironmentRequirements.checkEnvironmentRequirements({ platform, notConfiguredEnvOptions: { hideSyncToPreviewAppOption: true }}));
128+
await assert.isRejected(platformEnvironmentRequirements.checkEnvironmentRequirements({ platform, notConfiguredEnvOptions: { hideSyncToPreviewAppOption: true } }));
128129
assert.isTrue(promptForChoiceData.length === 1);
129130
assert.isTrue(isExtensionInstallCalled);
130131
assert.deepEqual("To continue, choose one of the following options: ", promptForChoiceData[0].message);
@@ -135,13 +136,13 @@ describe("platformEnvironmentRequirements ", () => {
135136
mockPrompter({ firstCallOptionName: PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME });
136137
mockNativeScriptCloudExtensionService({ isInstalled: true });
137138

138-
await assert.isRejected(platformEnvironmentRequirements.checkEnvironmentRequirements({ platform, notConfiguredEnvOptions: { hideCloudBuildOption: true }}));
139+
await assert.isRejected(platformEnvironmentRequirements.checkEnvironmentRequirements({ platform, notConfiguredEnvOptions: { hideCloudBuildOption: true } }));
139140
assert.isTrue(promptForChoiceData.length === 1);
140141
assert.isTrue(isExtensionInstallCalled);
141142
assert.deepEqual("To continue, choose one of the following options: ", promptForChoiceData[0].message);
142143
assert.deepEqual(['Sync to Playground', 'Configure for Local Builds', 'Skip Step and Configure Manually'], promptForChoiceData[0].choices);
143144
});
144-
it("should skip env check when NS_SKIP_ENV_CHECK environment variable is passed", async() => {
145+
it("should skip env check when NS_SKIP_ENV_CHECK environment variable is passed", async () => {
145146
(<any>process.env).NS_SKIP_ENV_CHECK = true;
146147

147148
const output = await platformEnvironmentRequirements.checkEnvironmentRequirements({ platform });
@@ -153,7 +154,7 @@ describe("platformEnvironmentRequirements ", () => {
153154

154155
describe("when local setup option is selected", () => {
155156
beforeEach(() => {
156-
mockPrompter( {firstCallOptionName: PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME});
157+
mockPrompter({ firstCallOptionName: PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME });
157158
});
158159

159160
it("should return true when env is configured after executing setup script", async () => {
@@ -169,7 +170,7 @@ describe("platformEnvironmentRequirements ", () => {
169170

170171
describe("and env is not configured after executing setup script", () => {
171172
it("should setup manually when cloud extension is installed", async () => {
172-
mockDoctorService( { canExecuteLocalBuild: false, mockSetupScript: true });
173+
mockDoctorService({ canExecuteLocalBuild: false, mockSetupScript: true });
173174
mockPrompter({ firstCallOptionName: PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME, secondCallOptionName: PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME });
174175
mockNativeScriptCloudExtensionService({ isInstalled: true });
175176

0 commit comments

Comments
 (0)