Skip to content

Commit ec9f70a

Browse files
committed
fix broken tests
1 parent 77f86a3 commit ec9f70a

File tree

2 files changed

+51
-56
lines changed

2 files changed

+51
-56
lines changed

lib/tools/node-modules/node-modules-builder.ts

+31-40
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
156156
}
157157

158158
public getProductionDependencies(projectPath: string) {
159-
var deps: any = [];
160-
var seen: any = {};
159+
let deps: any = [];
160+
let seen: any = {};
161161

162-
var pJson = path.join(projectPath, "package.json");
163-
var nodeModulesDir = path.join(projectPath, "node_modules");
162+
let pJson = path.join(projectPath, "package.json");
163+
let nodeModulesDir = path.join(projectPath, "node_modules");
164164

165-
var content = require(pJson);
165+
let content = require(pJson);
166166

167167
Object.keys(content.dependencies).forEach((key) => {
168-
var depth = 0;
169-
var directory = path.join(nodeModulesDir, key);
168+
let depth = 0;
169+
let directory = path.join(nodeModulesDir, key);
170170

171171
// find and traverse child with name `key`, parent's directory -> dep.directory
172172
traverseChild(key, directory, depth);
@@ -176,66 +176,59 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
176176

177177
function traverseChild(name: string, currentModulePath: string, depth: number) {
178178
// check if key appears in a scoped module dependency
179-
var isScoped = name.indexOf('@') === 0;
179+
let isScoped = name.indexOf('@') === 0;
180180

181181
if (!isScoped) {
182182
// Check if child has been extracted in the parent's node modules, AND THEN in `node_modules`
183183
// Slower, but prevents copying wrong versions if multiple of the same module are installed
184184
// Will also prevent copying project's devDependency's version if current module depends on another version
185-
var modulePath = path.join(currentModulePath, "node_modules", name); // /node_modules/parent/node_modules/<package>
186-
var exists = ensureModuleExists(modulePath);
185+
let modulePath = path.join(currentModulePath, "node_modules", name); // /node_modules/parent/node_modules/<package>
186+
let exists = ensureModuleExists(modulePath);
187187

188188
if (exists) {
189-
var dep = addDependency(deps, name, modulePath, depth + 1);
190-
189+
let dep = addDependency(deps, name, modulePath, depth + 1);
191190
traverseModule(modulePath, depth + 1, dep);
192191
} else {
193192
modulePath = path.join(nodeModulesDir, name); // /node_modules/<package>
194193
exists = ensureModuleExists(modulePath);
195194

196-
if(!exists) {
195+
if (!exists) {
197196
return;
198197
}
199198

200-
var dep = addDependency(deps, name, modulePath, 0);
201-
199+
let dep = addDependency(deps, name, modulePath, 0);
202200
traverseModule(modulePath, 0, dep);
203201
}
202+
} else { // module is scoped
203+
let scopeSeparatorIndex = name.indexOf('/');
204+
let scope = name.substring(0, scopeSeparatorIndex);
205+
let moduleName = name.substring(scopeSeparatorIndex + 1, name.length);
206+
let scopedModulePath = path.join(nodeModulesDir, scope, moduleName);
204207

205-
}
206-
// module is scoped
207-
else {
208-
var scopeSeparatorIndex = name.indexOf('/');
209-
var scope = name.substring(0, scopeSeparatorIndex);
210-
var moduleName = name.substring(scopeSeparatorIndex + 1, name.length);
211-
var scopedModulePath = path.join(nodeModulesDir, scope, moduleName);
212-
213-
var exists = ensureModuleExists(scopedModulePath);
208+
let exists = ensureModuleExists(scopedModulePath);
214209

215210
if (exists) {
216-
var dep = addDependency(deps, name, scopedModulePath, 0);
211+
let dep = addDependency(deps, name, scopedModulePath, 0);
217212
traverseModule(scopedModulePath, depth, dep);
218-
}
219-
else {
213+
} else {
220214
scopedModulePath = path.join(currentModulePath, "node_modules", scope, moduleName);
221-
222215
exists = ensureModuleExists(scopedModulePath);
223216

224217
if (!exists) {
225218
return;
226219
}
227220

228-
var dep = addDependency(deps, name, scopedModulePath, depth + 1);
221+
let dep = addDependency(deps, name, scopedModulePath, depth + 1);
229222
traverseModule(scopedModulePath, depth + 1, dep);
230223
}
231224
}
232225

233226
function traverseModule(modulePath: string, depth: number, currentDependency: any) {
234-
var packageJsonPath = path.join(modulePath, 'package.json');
235-
var packageJsonExists = fs.lstatSync(packageJsonPath).isFile();
227+
let packageJsonPath = path.join(modulePath, 'package.json');
228+
let packageJsonExists = fs.lstatSync(packageJsonPath).isFile();
236229

237230
if (packageJsonExists) {
238-
var packageJsonContents = require(packageJsonPath);
231+
let packageJsonContents = require(packageJsonPath);
239232

240233
if (!!packageJsonContents.nativescript) {
241234
// add `nativescript` property, necessary for resolving plugins
@@ -244,27 +237,25 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
244237

245238
if (packageJsonContents.dependencies) {
246239
Object.keys(packageJsonContents.dependencies).forEach((key) => {
247-
248240
traverseChild(key, modulePath, depth);
249241
});
250242
}
251243
}
252244
}
253245

254246
function addDependency(deps: any[], name: string, directory: string, depth: number) {
255-
var dep: any = {};
247+
let dep: any = {};
256248
dep.name = name;
257249
dep.directory = directory;
258250
dep.depth = depth;
259-
260251
deps.push(dep);
261252

262253
return dep;
263254
}
264255

265256
function ensureModuleExists(modulePath: string): boolean {
266257
try {
267-
var exists = fs.lstatSync(modulePath);
258+
let exists = fs.lstatSync(modulePath);
268259
return exists.isDirectory();
269260
} catch (e) {
270261
return false;
@@ -273,10 +264,10 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
273264
}
274265

275266
function filterUniqueDirectories(dependencies: any) {
276-
var unique: any = [];
277-
var distinct: any = [];
278-
for (var i in dependencies) {
279-
var dep = dependencies[i];
267+
let unique: any = [];
268+
let distinct: any = [];
269+
for (let i in dependencies) {
270+
let dep = dependencies[i];
280271
if (distinct.indexOf(dep.directory) > -1) {
281272
continue;
282273
}

test/npm-support.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ describe("Npm support tests", () => {
191191
appDestinationFolderPath = projectSetup.appDestinationFolderPath;
192192
});
193193
it("Ensures that the installed dependencies are prepared correctly", () => {
194+
let fs: IFileSystem = testInjector.resolve("fs");
194195
// Setup
195196
addDependencies(testInjector, projectFolder, { "bplist": "0.0.4" }).wait();
196197

@@ -199,16 +200,15 @@ describe("Npm support tests", () => {
199200

200201
// Assert
201202
let tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
202-
let lodashFolderPath = path.join(tnsModulesFolderPath, "lodash");
203-
let bplistFolderPath = path.join(tnsModulesFolderPath, "bplist");
204-
let bplistCreatorFolderPath = path.join(tnsModulesFolderPath, "bplist-creator");
205-
let bplistParserFolderPath = path.join(tnsModulesFolderPath, "bplist-parser");
206203

207-
let fs = testInjector.resolve("fs");
208-
assert.isTrue(fs.exists(lodashFolderPath).wait());
209-
assert.isTrue(fs.exists(bplistFolderPath).wait());
210-
assert.isTrue(fs.exists(bplistCreatorFolderPath).wait());
211-
assert.isTrue(fs.exists(bplistParserFolderPath).wait());
204+
let results = fs.enumerateFilesInDirectorySync(tnsModulesFolderPath, (file, stat) => {
205+
return true;
206+
}, { enumerateDirectories: true });
207+
208+
assert.isTrue(results.filter((val) => _.endsWith(val, "lodash")).length === 1);
209+
assert.isTrue(results.filter((val) => _.endsWith(val, path.join(tnsModulesFolderPath, "bplist"))).length === 1);
210+
assert.isTrue(results.filter((val) => _.endsWith(val, "bplist-creator")).length === 1);
211+
assert.isTrue(results.filter((val) => _.endsWith(val, "bplist-parser")).length === 1);
212212
});
213213
it("Ensures that scoped dependencies are prepared correctly", () => {
214214
// Setup
@@ -219,11 +219,8 @@ describe("Npm support tests", () => {
219219
let dependencies: any = {};
220220
dependencies[scopedName] = "0.0.0-prealpha.3";
221221
// Do not pass dependencies object as the sinopia cannot work with scoped dependencies. Instead move them manually.
222-
addDependencies(testInjector, projectFolder, {}).wait();
223-
//create module dir, and add a package.json
224-
shelljs.mkdir("-p", scopedModule);
225-
fs.writeFile(scopedPackageJson, JSON.stringify({ name: scopedName, version: "1.0.0" })).wait();
226-
222+
addDependencies(testInjector, projectFolder, dependencies).wait();
223+
227224
// Act
228225
preparePlatform(testInjector).wait();
229226

@@ -249,9 +246,16 @@ describe("Npm support tests", () => {
249246

250247
// Assert
251248
let tnsModulesFolderPath = path.join(appDestinationFolderPath, "app", "tns_modules");
249+
250+
let results = fs.enumerateFilesInDirectorySync(tnsModulesFolderPath, (file, stat) => {
251+
return true;
252+
}, { enumerateDirectories: true });
252253

253-
let scopedDependencyPath = path.join(tnsModulesFolderPath, "@scoped-plugin", "inner-plugin");
254-
assert.isTrue(fs.exists(scopedDependencyPath).wait());
254+
let filteredResults = results.filter((val) => {
255+
return _.endsWith(val, path.join("@scoped-plugin", "inner-plugin"));
256+
});
257+
258+
assert.isTrue(filteredResults.length === 1);
255259
});
256260

257261
it("Ensures that tns_modules absent when bundling", () => {

0 commit comments

Comments
 (0)