-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnpm-support.ts
350 lines (309 loc) · 15.1 KB
/
npm-support.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import yok = require('../lib/common/yok');
import stubs = require('./stubs');
import ErrorsLib = require("../lib/common/errors");
import NpmLib = require("../lib/node-package-manager");
import PackageManagerLib = require("../lib/package-manager");
import YarnLib = require("../lib/yarn-package-manager");
import FsLib = require("../lib/common/file-system");
import OptionsLib = require("../lib/options");
import StaticConfigLib = require("../lib/config");
import HostInfoLib = require("../lib/common/host-info");
import PlatformsDataLib = require("../lib/platforms-data");
import PlatformServiceLib = require('../lib/services/platform-service');
import ProjectDataLib = require("../lib/project-data");
import ProjectHelperLib = require("../lib/common/project-helper");
import ProjectDataServiceLib = require("../lib/services/project-data-service");
import CommandsServiceLib = require("../lib/common/services/commands-service");
import NodeModulesLib = require("../lib/tools/node-modules/node-modules-builder");
import PluginsServiceLib = require("../lib/services/plugins-service");
import ChildProcessLib = require("../lib/common/child-process");
import ProjectFilesManagerLib = require("../lib/common/services/project-files-manager");
import { PreparePlatformNativeService } from "../lib/services/prepare-platform-native-service";
import { PreparePlatformJSService } from "../lib/services/prepare-platform-js-service";
import { LocalToDevicePathDataFactory } from "../lib/common/mobile/local-to-device-path-data-factory";
import { MobileHelper } from "../lib/common/mobile/mobile-helper";
import { ProjectFilesProvider } from "../lib/providers/project-files-provider";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import { XmlValidator } from "../lib/xml-validator";
import ProjectChangesLib = require("../lib/services/project-changes-service");
import { Messages } from "../lib/common/messages/messages";
import { NodeModulesDependenciesBuilder } from "../lib/tools/node-modules/node-modules-dependencies-builder";
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
import path = require("path");
import temp = require("temp");
temp.track();
const assert = require("chai").assert;
const nodeModulesFolderName = "node_modules";
const packageJsonName = "package.json";
function createTestInjector(): IInjector {
const testInjector = new yok.Yok();
testInjector.register("fs", FsLib.FileSystem);
testInjector.register("adb", {});
testInjector.register("options", OptionsLib.Options);
testInjector.register("errors", ErrorsLib.Errors);
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
testInjector.register("hostInfo", HostInfoLib.HostInfo);
testInjector.register("platformsData", PlatformsDataLib.PlatformsData);
testInjector.register("platformService", PlatformServiceLib.PlatformService);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("packageInstallationManager", {
install: () => Promise.resolve()
});
testInjector.register("prompter", {});
testInjector.register("sysInfo", {});
testInjector.register("androidProjectService", {});
testInjector.register("iOSProjectService", {});
testInjector.register("devicesService", {});
testInjector.register("resources", {});
testInjector.register("projectData", ProjectDataLib.ProjectData);
testInjector.register("projectHelper", ProjectHelperLib.ProjectHelper);
testInjector.register("projectDataService", ProjectDataServiceLib.ProjectDataService);
testInjector.register("commandsService", CommandsServiceLib.CommandsService);
testInjector.register("hooksService", stubs.HooksServiceStub);
testInjector.register("nodeModulesBuilder", NodeModulesLib.NodeModulesBuilder);
testInjector.register("pluginsService", PluginsServiceLib.PluginsService);
testInjector.register("userSettingsService", {
getSettingValue: async (settingName: string): Promise<void> => undefined
});
testInjector.register("npm", NpmLib.NodePackageManager);
testInjector.register("packageManager", PackageManagerLib.PackageManager);
testInjector.register("yarn", YarnLib.YarnPackageManager);
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
testInjector.register("preparePlatformNativeService", PreparePlatformNativeService);
testInjector.register("preparePlatformJSService", PreparePlatformJSService);
testInjector.register("pluginVariablesService", {});
testInjector.register("localToDevicePathDataFactory", LocalToDevicePathDataFactory);
testInjector.register("mobileHelper", MobileHelper);
testInjector.register("projectFilesProvider", ProjectFilesProvider);
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("xmlValidator", XmlValidator);
testInjector.register("config", StaticConfigLib.Configuration);
testInjector.register("projectChangesService", ProjectChangesLib.ProjectChangesService);
testInjector.register("analyticsService", {
track: async (): Promise<any> => undefined
});
testInjector.register("messages", Messages);
testInjector.register("nodeModulesDependenciesBuilder", NodeModulesDependenciesBuilder);
testInjector.register("settingsService", SettingsService);
testInjector.register("devicePathProvider", {});
testInjector.register("terminalSpinnerService", {
createSpinner: (msg: string) => ({
start: (): void => undefined,
stop: (): void => undefined,
message: (): void => undefined
})
});
testInjector.register("httpClient", {});
testInjector.register("androidResourcesMigrationService", stubs.AndroidResourcesMigrationServiceStub);
testInjector.register("filesHashService", {
getChanges: () => Promise.resolve({}),
generateHashes: () => Promise.resolve()
});
testInjector.register("pacoteService", {
extractPackage: async (packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> => undefined
});
testInjector.register("usbLiveSyncService", () => ({}));
testInjector.register("doctorService", {
checkForDeprecatedShortImportsInAppDir: (projectDir: string): void => undefined
});
return testInjector;
}
function createProject(testInjector: IInjector, dependencies?: any): string {
const tempFolder = temp.mkdirSync("npmSupportTests");
const options = testInjector.resolve("options");
options.path = tempFolder;
dependencies = dependencies || {
"lodash": "3.9.3"
};
const packageJsonData: any = {
"name": "testModuleName",
"version": "0.1.0",
"nativescript": {
"id": "org.nativescript.Test",
"tns-android": {
"version": "1.0.0"
}
},
"description": "dummy",
"license": "MIT",
"readme": "dummy",
"repository": "dummy"
};
packageJsonData["dependencies"] = dependencies;
packageJsonData["devDependencies"] = {};
testInjector.resolve("fs").writeJson(path.join(tempFolder, "package.json"), packageJsonData);
return tempFolder;
}
async function setupProject(dependencies?: any): Promise<any> {
const testInjector = createTestInjector();
const projectFolder = createProject(testInjector, dependencies);
const fs = testInjector.resolve("fs");
// Creates app folder
const appFolderPath = path.join(projectFolder, "app");
fs.createDirectory(appFolderPath);
const appResourcesFolderPath = path.join(appFolderPath, "App_Resources");
fs.createDirectory(appResourcesFolderPath);
fs.createDirectory(path.join(appResourcesFolderPath, "Android"));
fs.createDirectory(path.join(appResourcesFolderPath, "Android", "mockdir"));
fs.createDirectory(path.join(appFolderPath, "tns_modules"));
// Creates platforms/android folder
const androidFolderPath = path.join(projectFolder, "platforms", "android");
fs.ensureDirectoryExists(androidFolderPath);
// Mock platform data
const appDestinationFolderPath = path.join(androidFolderPath, "assets");
const platformsData = testInjector.resolve("platformsData");
platformsData.getPlatformData = (platform: string) => {
return {
appDestinationDirectoryPath: appDestinationFolderPath,
appResourcesDestinationDirectoryPath: path.join(appDestinationFolderPath, "app", "App_Resources"),
frameworkPackageName: "tns-android",
normalizedPlatformName: "Android",
projectRoot: projectFolder,
configurationFileName: "AndroidManifest.xml",
platformProjectService: {
prepareProject: (): any => null,
prepareAppResources: (): any => null,
beforePrepareAllPlugins: () => Promise.resolve(),
handleNativeDependenciesChange: () => Promise.resolve(),
getAppResourcesDestinationDirectoryPath: () => path.join(androidFolderPath, "src", "main", "res"),
processConfigurationFilesFromAppResources: () => Promise.resolve(),
ensureConfigurationFileInAppResources: (): any => null,
interpolateConfigurationFile: (): void => undefined,
isPlatformPrepared: (projectRoot: string) => false,
validatePlugins: (projectData: IProjectData) => Promise.resolve(),
checkForChanges: () => { /* */ },
cleanProject: () => Promise.resolve()
}
};
};
return {
testInjector: testInjector,
projectFolder: projectFolder,
appDestinationFolderPath: appDestinationFolderPath,
};
}
async function addDependencies(testInjector: IInjector, projectFolder: string, dependencies: any, devDependencies?: any): Promise<void> {
const fs = testInjector.resolve("fs");
const packageJsonPath = path.join(projectFolder, "package.json");
const packageJsonData = fs.readJson(packageJsonPath);
const currentDependencies = packageJsonData.dependencies;
_.extend(currentDependencies, dependencies);
if (devDependencies) {
const currentDevDependencies = packageJsonData.devDependencies;
_.extend(currentDevDependencies, devDependencies);
}
fs.writeJson(packageJsonPath, packageJsonData);
}
async function preparePlatform(testInjector: IInjector): Promise<void> {
const platformService: IPlatformService = testInjector.resolve("platformService");
const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();
const options: IOptions = testInjector.resolve("options");
await platformService.preparePlatform({
platform: "android",
appFilesUpdaterOptions: { bundle: !!options.bundle, release: options.release, useHotModuleReload: false },
platformTemplate: "",
projectData,
config: options,
env: {}
});
}
describe("Npm support tests", () => {
let testInjector: IInjector, projectFolder: string, appDestinationFolderPath: string;
beforeEach(async () => {
const projectSetup = await setupProject();
testInjector = projectSetup.testInjector;
projectFolder = projectSetup.projectFolder;
appDestinationFolderPath = projectSetup.appDestinationFolderPath;
});
it("Ensures that the installed dependencies are prepared correctly", async () => {
const fs: IFileSystem = testInjector.resolve("fs");
// Setup
await addDependencies(testInjector, projectFolder, { "bplist": "0.0.4" });
// Act
await preparePlatform(testInjector);
// Assert
const tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
const results = fs.enumerateFilesInDirectorySync(tnsModulesFolderPath, (file, stat) => {
return true;
}, { enumerateDirectories: true });
assert.isTrue(results.filter((val) => _.endsWith(val, "lodash")).length === 1);
assert.isTrue(results.filter((val) => _.endsWith(val, path.join(tnsModulesFolderPath, "bplist"))).length === 1);
assert.isTrue(results.filter((val) => _.endsWith(val, "bplist-creator")).length === 1);
assert.isTrue(results.filter((val) => _.endsWith(val, "bplist-parser")).length === 1);
});
it("Ensures that scoped dependencies are prepared correctly", async () => {
// Setup
const fs = testInjector.resolve("fs");
const scopedName = "@reactivex/rxjs";
const dependencies: any = {};
dependencies[scopedName] = "0.0.0-prealpha.3";
// Do not pass dependencies object as the sinopia cannot work with scoped dependencies. Instead move them manually.
await addDependencies(testInjector, projectFolder, dependencies);
// Act
await preparePlatform(testInjector);
// Assert
const tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
const scopedDependencyPath = path.join(tnsModulesFolderPath, "@reactivex", "rxjs");
assert.isTrue(fs.exists(scopedDependencyPath));
});
it("Ensures that scoped dependencies are prepared correctly when are not in root level", async () => {
// Setup
const customPluginName = "plugin-with-scoped-dependency";
const customPluginDirectory = temp.mkdirSync("custom-plugin-directory");
const fs: IFileSystem = testInjector.resolve("fs");
await fs.unzip(path.join("resources", "test", `${customPluginName}.zip`), customPluginDirectory);
await addDependencies(testInjector, projectFolder, { "plugin-with-scoped-dependency": `file:${path.join(customPluginDirectory, customPluginName)}` });
// Act
await preparePlatform(testInjector);
// Assert
const tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
const results = fs.enumerateFilesInDirectorySync(tnsModulesFolderPath, (file, stat) => {
return true;
}, { enumerateDirectories: true });
const filteredResults = results.filter((val) => {
return _.endsWith(val, path.join("@scoped-plugin", "inner-plugin"));
});
assert.isTrue(filteredResults.length === 1);
});
});
describe("Flatten npm modules tests", () => {
it("Doesn't handle the dependencies of devDependencies", async () => {
const projectSetup = await setupProject({});
const testInjector = projectSetup.testInjector;
const projectFolder = projectSetup.projectFolder;
const appDestinationFolderPath = projectSetup.appDestinationFolderPath;
const devDependencies = {
"gulp": "3.9.0",
"gulp-jscs": "1.6.0",
"gulp-jshint": "1.11.0"
};
await addDependencies(testInjector, projectFolder, {}, devDependencies);
await preparePlatform(testInjector);
// Assert
const fs = testInjector.resolve("fs");
const tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
const gulpFolderPath = path.join(tnsModulesFolderPath, "gulp");
assert.isFalse(fs.exists(gulpFolderPath));
const gulpJscsFolderPath = path.join(tnsModulesFolderPath, "gulp-jscs");
assert.isFalse(fs.exists(gulpJscsFolderPath));
const gulpJshint = path.join(tnsModulesFolderPath, "gulp-jshint");
assert.isFalse(fs.exists(gulpJshint));
// Get all gulp dependencies
const gulpJsonContent = fs.readJson(path.join(projectFolder, nodeModulesFolderName, "gulp", packageJsonName));
_.each(_.keys(gulpJsonContent.dependencies), dependency => {
assert.isFalse(fs.exists(path.join(tnsModulesFolderPath, dependency)));
});
// Get all gulp-jscs dependencies
const gulpJscsJsonContent = fs.readJson(path.join(projectFolder, nodeModulesFolderName, "gulp-jscs", packageJsonName));
_.each(_.keys(gulpJscsJsonContent.dependencies), dependency => {
assert.isFalse(fs.exists(path.join(tnsModulesFolderPath, dependency)));
});
// Get all gulp-jshint dependencies
const gulpJshintJsonContent = fs.readJson(path.join(projectFolder, nodeModulesFolderName, "gulp-jshint", packageJsonName));
_.each(_.keys(gulpJshintJsonContent.dependencies), dependency => {
assert.isFalse(fs.exists(path.join(tnsModulesFolderPath, dependency)));
});
});
});