-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data.ts
224 lines (187 loc) · 6.82 KB
/
project-data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { Yok } from "../lib/common/yok";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import { assert } from "chai";
import * as stubs from "./stubs";
import * as path from "path";
import { IProjectData } from "../lib/definitions/project";
import { IInjector } from "../lib/common/definitions/yok";
import { IStringDictionary, IProjectHelper } from "../lib/common/declarations";
import { ProjectConfigService } from "../lib/services/project-config-service";
import { ProjectData } from "../lib/project-data";
describe("projectData", () => {
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("projectHelper", {
projectDir: null,
sanitizeName: (name: string) => name,
});
testInjector.register("fs", {
exists: () => true,
readJson: (): any => null,
readText: (): any => null,
});
testInjector.register("staticConfig", {
CLIENT_NAME_KEY_IN_PROJECT_FILE: "nativescript",
PROJECT_FILE_NAME: "package.json",
});
testInjector.register("errors", stubs.ErrorsStub);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("options", {});
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("androidResourcesMigrationService", {
hasMigrated: () => true,
});
testInjector.register("projectData", ProjectData);
testInjector.register("projectConfigService", ProjectConfigService);
return testInjector;
};
const projectDir = "projectDir";
const prepareTest = (opts?: {
packageJsonData?: {
dependencies?: IStringDictionary;
devDependencies: IStringDictionary;
};
configData?: {
shared?: boolean;
webpackConfigPath?: string;
projectName?: string;
};
}): IProjectData => {
const testInjector = createTestInjector();
const fs = testInjector.resolve("fs");
testInjector.register(
"projectConfigService",
stubs.ProjectConfigServiceStub.initWithConfig(opts?.configData)
);
fs.exists = (filePath: string) => {
return filePath && path.basename(filePath) === "package.json";
};
fs.readText = (filePath: string) => {
if (path.basename(filePath) === "package.json") {
return JSON.stringify({
dependencies:
opts && opts.packageJsonData && opts.packageJsonData.dependencies,
devDependencies:
opts &&
opts.packageJsonData &&
opts.packageJsonData.devDependencies,
});
}
return null;
};
const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = projectDir;
const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();
return projectData;
};
describe("projectType", () => {
const assertProjectType = (
dependencies: any,
devDependencies: any,
expectedProjecType: string
) => {
const projectData = prepareTest({
packageJsonData: {
dependencies,
devDependencies,
},
});
assert.deepStrictEqual(projectData.projectType, expectedProjecType);
};
it("detects project as Angular when @angular/core exists as dependency", () => {
assertProjectType({ "@angular/core": "*" }, null, "Angular");
});
it("detects project as Angular when nativescript-angular exists as dependency", () => {
assertProjectType({ "nativescript-angular": "*" }, null, "Angular");
});
it("detects project as Vue.js when nativescript-vue exists as dependency", () => {
assertProjectType({ "nativescript-vue": "*" }, null, "Vue.js");
});
it("detects project as Vue.js when nativescript-vue exists as dependency and typescript is devDependency", () => {
assertProjectType(
{ "nativescript-vue": "*" },
{ typescript: "*" },
"Vue.js"
);
});
it("detects project as React when react-nativescript exists as dependency and typescript is devDependency", () => {
assertProjectType({ "react-nativescript": "*" }, null, "React");
});
it("detects project as Svelte when svelte-native exists as dependency and typescript is devDependency", () => {
assertProjectType({ "svelte-native": "*" }, null, "Svelte");
});
it("detects project as TypeScript when nativescript-dev-typescript exists as dependency", () => {
assertProjectType(
null,
{ "nativescript-dev-typescript": "*" },
"Pure TypeScript"
);
});
it("detects project as TypeScript when typescript exists as dependency", () => {
assertProjectType(null, { typescript: "*" }, "Pure TypeScript");
});
it("detects project as JavaScript when no other project type is detected", () => {
assertProjectType(null, null, "Pure JavaScript");
});
});
describe("isShared", () => {
it("is false when nsconfig.json does not exist", () => {
const projectData = prepareTest();
assert.isFalse(projectData.isShared);
});
it("is false when nsconfig.json exists, but shared value is not populated", () => {
const projectData = prepareTest({ configData: { shared: undefined } });
assert.isFalse(projectData.isShared);
});
it("is false when nsconfig.json exists and shared value is false", () => {
const projectData = prepareTest({ configData: { shared: false } });
assert.isFalse(projectData.isShared);
});
it("is true when nsconfig.json exists and shared value is true", () => {
const projectData = prepareTest({ configData: { shared: true } });
assert.isTrue(projectData.isShared);
});
});
describe("projectName", () => {
it("has correct name when no value is set in nativescript.conf", () => {
const projectData = prepareTest();
assert.isString("projectDir", projectData.projectName);
});
it("has correct name when a project name is set in nativescript.conf", () => {
const projectData = prepareTest({
configData: { projectName: "specifiedProjectName" },
});
assert.isString("specifiedProjectName", projectData.projectName);
});
});
describe("webpackConfigPath", () => {
it("default path to webpack.config.js is set when nsconfig.json does not set value", () => {
const projectData = prepareTest();
assert.equal(
projectData.webpackConfigPath,
path.join(projectDir, "webpack.config.js")
);
});
it("returns correct path when full path is set in nsconfig.json", () => {
const pathToConfig = path.resolve(
path.join("/testDir", "innerDir", "mywebpack.config.js")
);
const projectData = prepareTest({
configData: { webpackConfigPath: pathToConfig },
});
assert.equal(projectData.webpackConfigPath, pathToConfig);
});
it("returns correct path when relative path is set in nsconfig.json", () => {
const pathToConfig = path.resolve(
path.join("projectDir", "innerDir", "mywebpack.config.js")
);
const projectData = prepareTest({
configData: {
webpackConfigPath: path.join("./innerDir", "mywebpack.config.js"),
},
});
assert.equal(projectData.webpackConfigPath, pathToConfig);
});
});
});