Skip to content

Allow require of nativescript-cli as library #2468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/nativescript-cli-lib-bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require("./bootstrap");

$injector.overrideAlreadyRequiredModule = true;

// Temporary!!! Should not require appbuilder's entry point of mobile-cli-lib,
// but once we separate logics in mobile-cli-lib, we should be able to require only specific bootstrap.
// Use this hack for now, as this will allow requiring {N} CLI as library directly and executing some device specific operations.
require("./common/appbuilder/proton-bootstrap");
3 changes: 3 additions & 0 deletions lib/nativescript-cli-lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./nativescript-cli-lib-bootstrap");

module.exports = $injector.publicApi;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"tns": "./bin/tns",
"nativescript": "./bin/tns"
},
"main": "./lib/nativescript-cli.js",
"main": "./lib/nativescript-cli-lib.js",
"scripts": {
"test": "node test-scripts/istanbul.js",
"postinstall": "node postinstall.js",
Expand Down
24 changes: 24 additions & 0 deletions test/nativescript-cli-lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { assert } from "chai";
import * as fs from "fs";
import * as path from "path";
import * as childProcess from "child_process";
const nodeArgs = require(path.join(__dirname, "..", "lib", "common", "scripts", "node-args")).getNodeArgs();

describe("nativescript-cli-lib", () => {
it("is main entry of the package", () => {
const packageJsonContent = fs.readFileSync(path.join(__dirname, "..", "package.json")).toString();
const jsonContent = JSON.parse(packageJsonContent);
const expectedEntryPoint = "./lib/nativescript-cli-lib.js";
assert.deepEqual(jsonContent.main, expectedEntryPoint);
});

it("resolves publicly available module - deviceEmitter, when it is required", () => {
const pathToEntryPoint = path.join(__dirname, "..", "lib", "nativescript-cli-lib.js").replace(/\\/g, "\\\\");
// HACK: If we try to require the entry point directly, the below code will fail as mocha requires all test files before starting the tests.
// When the files are required, $injector.register adds each dependency to $injector's cache.
// For example $injector.register("errors", Errors) will add the errors module with its resolver (Errors) to $injector's cache.
// Calling $injector.require("errors", <path to errors file>), that's executed in our bootstrap, will fail, as the module errors is already in the cache.
// In order to workaround this problem, start new process and assert there. This way all files will not be required in it and $injector.require(...) will work correctly.
childProcess.execSync(`"${process.execPath}" ${nodeArgs.join(" ")} -e "var assert = require('chai').assert; var result = require('${pathToEntryPoint}'); assert.ok(result.deviceEmitter);"`);
});
});