Skip to content

Commit 8f9283c

Browse files
committed
Use npm to fetch tns-core-modules package.
1 parent 5895849 commit 8f9283c

10 files changed

+51
-9
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ $injector.require("androidProjectService", "./services/android-project-service")
1111
$injector.require("iOSProjectService", "./services/ios-project-service");
1212

1313
$injector.require("projectTemplatesService", "./services/project-templates-service");
14+
$injector.require("tnsModulesService", "./services/tns-modules-service");
1415

1516
$injector.require("platformsData", "./platforms-data");
1617
$injector.require("platformService", "./services/platform-service");

lib/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export var APP_RESOURCES_FOLDER_NAME = "App_Resources";
55
export var PROJECT_FRAMEWORK_FOLDER_NAME = "framework";
66
export var NATIVESCRIPT_KEY_NAME = "nativescript";
77
export var NODE_MODULES_FOLDER_NAME = "node_modules";
8+
export var TNS_CORE_MODULES_NAME = "tns-core-modules";
89
export var PACKAGE_JSON_FILE_NAME = "package.json";
910
export var NODE_MODULE_CACHE_PATH_KEY_NAME = "node-modules-cache-path";
1011
export var DEFAULT_APP_IDENTIFIER_PREFIX = "org.nativescript";

lib/declarations.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface INodePackageManager {
66
cache(packageName: string, version: string, cache?: any): IFuture<IDependencyData>;
77
cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void>;
88
view(packageName: string, propertyName: string): IFuture<any>;
9+
executeNpmCommand(npmCommandName: string, currentWorkingDirectory: string): IFuture<any>;
910
}
1011

1112
interface INpmInstallationManager {
@@ -78,6 +79,7 @@ interface IOptions extends ICommonOptions {
7879
keyStoreAliasPassword: string;
7980
sdk: string;
8081
ignoreScripts: boolean;
82+
tnsModulesVersion: string;
8183
}
8284

8385
interface IProjectFilesManager {

lib/definitions/project.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
interface IProjectService {
23
createProject(projectName: string): IFuture<void>;
34
}

lib/node-package-manager.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import Future = require("fibers/future");
55
import npm = require("npm");
66

77
export class NodePackageManager implements INodePackageManager {
8-
constructor(private $logger: ILogger,
8+
constructor(private $childProcess: IChildProcess,
9+
private $logger: ILogger,
910
private $errors: IErrors,
1011
private $fs: IFileSystem,
1112
private $lockfile: ILockFile,
@@ -53,6 +54,10 @@ export class NodePackageManager implements INodePackageManager {
5354
public view(packageName: string, propertyName: string): IFuture<any> {
5455
return this.loadAndExecute("view", [[packageName, propertyName], [false]]);
5556
}
57+
58+
public executeNpmCommand(npmCommandName: string, currentWorkingDirectory: string): IFuture<any> {
59+
return this.$childProcess.exec(npmCommandName, { cwd: currentWorkingDirectory });
60+
}
5661

5762
private loadAndExecute(commandName: string, args: any[], opts?: { config?: any, subCommandName?: string }): IFuture<any> {
5863
return (() => {

lib/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export class Options extends commonOptionsLibPath.OptionsBase {
2727
keyStoreAlias: { type: OptionType.String },
2828
keyStoreAliasPassword: { type: OptionType.String },
2929
sdk: { type: OptionType.String },
30-
ignoreScripts: {type: OptionType.Boolean }
30+
ignoreScripts: {type: OptionType.Boolean },
31+
tnsModulesVersion: { type: OptionType.String }
3132
},
3233
path.join($hostInfo.isWindows ? process.env.LocalAppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
3334
$errors, $staticConfig);

lib/services/platform-service.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ export class PlatformService implements IPlatformService {
175175
platformData.platformProjectService.prepareProject().wait();
176176

177177
// Process node_modules folder
178-
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
179-
let tnsModulesDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, PlatformService.TNS_MODULES_FOLDER_NAME);
180-
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, this.$projectData.projectDir, platform, lastModifiedTime).wait();
178+
try {
179+
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
180+
let tnsModulesDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, PlatformService.TNS_MODULES_FOLDER_NAME);
181+
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, this.$projectData.projectDir, platform, lastModifiedTime).wait();
182+
} catch(error) {
183+
this.$logger.debug(error);
184+
this.$errors.fail(`Processing node_modules failed. Error:${error}`);
185+
shell.rm("-rf", appResourcesDirectoryPath);
186+
}
181187

182188
// Process platform specific files
183189
let directoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);

lib/services/project-service.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import util = require("util");
1010

1111
export class ProjectService implements IProjectService {
1212

13-
constructor(private $errors: IErrors,
13+
constructor(private $npm: INodePackageManager,
14+
private $errors: IErrors,
1415
private $fs: IFileSystem,
1516
private $logger: ILogger,
1617
private $projectDataService: IProjectDataService,
@@ -83,13 +84,12 @@ export class ProjectService implements IProjectService {
8384
throw err;
8485
}
8586

86-
8787
this.$logger.out("Project %s was successfully created", projectName);
8888

8989
}).future<void>()();
9090
}
9191

92-
private createProjectCore(projectDir: string, appSourcePath: string, projectId: string): IFuture<void> {
92+
private createProjectCore(projectDir: string, appSourcePath: string, projectId: string): IFuture<void> {
9393
return (() => {
9494
this.$fs.ensureDirectoryExists(projectDir).wait();
9595

@@ -101,6 +101,7 @@ export class ProjectService implements IProjectService {
101101
} else {
102102
shell.cp('-R', path.join(appSourcePath, "*"), appDestinationPath);
103103
}
104+
104105
this.createBasicProjectStructure(projectDir, projectId).wait();
105106
}).future<void>()();
106107
}
@@ -111,6 +112,14 @@ export class ProjectService implements IProjectService {
111112

112113
this.$projectDataService.initialize(projectDir);
113114
this.$projectDataService.setValue("id", projectId).wait();
115+
116+
let tnsModulesVersion = this.$options.tnsModulesVersion;
117+
let packageName = constants.TNS_CORE_MODULES_NAME;
118+
if (tnsModulesVersion) {
119+
packageName = `${packageName}@${tnsModulesVersion}`;
120+
}
121+
122+
this.$npm.executeNpmCommand(`npm install ${packageName} --save --save-exact`, projectDir).wait();
114123
}).future<void>()();
115124
}
116125

lib/tools/broccoli/node-modules-dest-copy.ts

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class DestCopy implements IBroccoliPlugin {
7575
if(isPlugin) {
7676
this.$pluginsService.prepare(dependency).wait();
7777
}
78+
79+
if (dependency.name === constants.TNS_CORE_MODULES_NAME) {
80+
let tnsCoreModulesResourcePath = path.join(this.outputRoot, constants.TNS_CORE_MODULES_NAME);
81+
shelljs.cp("-Rf", path.join(tnsCoreModulesResourcePath, "*"), this.outputRoot);
82+
this.$fs.deleteDirectory(tnsCoreModulesResourcePath).wait();
83+
}
7884
});
7985
}
8086

test/project-service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import yok = require('../lib/common/yok');
55
import stubs = require('./stubs');
6+
import * as constants from "./../lib/constants";
7+
import * as ChildProcessLib from "../lib/common/child-process";
68

79
import ProjectServiceLib = require("../lib/services/project-service");
810
import ProjectDataServiceLib = require("../lib/services/project-data-service");
@@ -69,11 +71,14 @@ class ProjectIntegrationTest {
6971
var appDirectoryPath = path.join(projectDir, "app");
7072
var platformsDirectoryPath = path.join(projectDir, "platforms");
7173
let tnsProjectFilePath = path.join(projectDir, "package.json");
74+
let tnsModulesPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME, constants.TNS_CORE_MODULES_NAME);
75+
7276
var options = this.testInjector.resolve("options");
7377

7478
assert.isTrue(fs.exists(appDirectoryPath).wait());
7579
assert.isTrue(fs.exists(platformsDirectoryPath).wait());
7680
assert.isTrue(fs.exists(tnsProjectFilePath).wait());
81+
assert.isTrue(fs.exists(tnsModulesPath).wait());
7782

7883
assert.isFalse(fs.isEmptyDir(appDirectoryPath).wait());
7984
assert.isTrue(fs.isEmptyDir(platformsDirectoryPath).wait());
@@ -82,6 +87,9 @@ class ProjectIntegrationTest {
8287
var expectedAppId = appId;
8388
assert.equal(actualAppId, expectedAppId);
8489

90+
let tnsCoreModulesRecord = fs.readJson(tnsProjectFilePath).wait()["dependencies"][constants.TNS_CORE_MODULES_NAME];
91+
assert.isTrue(tnsCoreModulesRecord !== null);
92+
8593
var actualFiles = fs.enumerateFilesInDirectorySync(options.copyFrom);
8694
var expectedFiles = fs.enumerateFilesInDirectorySync(appDirectoryPath);
8795

@@ -99,7 +107,7 @@ class ProjectIntegrationTest {
99107

100108
private createTestInjector(): void {
101109
this.testInjector = new yok.Yok();
102-
110+
this.testInjector.register("childProcess", ChildProcessLib.ChildProcess);
103111
this.testInjector.register("errors", stubs.ErrorsStub);
104112
this.testInjector.register('logger', stubs.LoggerStub);
105113
this.testInjector.register("projectService", ProjectServiceLib.ProjectService);
@@ -171,6 +179,8 @@ function createTestInjector() {
171179
testInjector.register("httpClient", HttpClientLib.HttpClient);
172180
testInjector.register("config", {});
173181
testInjector.register("lockfile", stubs.LockFile);
182+
183+
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
174184

175185
testInjector.register('projectData', ProjectDataLib.ProjectData);
176186
testInjector.register("options", optionsLib.Options);

0 commit comments

Comments
 (0)