Skip to content

Commit

Permalink
feat: re-enable preview command (#5676)
Browse files Browse the repository at this point in the history
* feat: enable preview command

* feat: allow disabling install & pass down args + error handling

* chore: use @latest

* chore(release): 8.3.0-beta.0
  • Loading branch information
rigor789 committed Jul 22, 2022
1 parent e9d9d5a commit 98124e7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 21 deletions.
85 changes: 79 additions & 6 deletions lib/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,90 @@
import { ICommandParameter, ICommand } from "../common/definitions/commands";
import { IErrors } from "../common/declarations";
import { IChildProcess, IErrors } from "../common/declarations";
import { injector } from "../common/yok";
import { IOptions, IPackageManager } from "../declarations";
import { IProjectData } from "../definitions/project";
import path = require("path");
import { resolvePackagePath } from "@rigor789/resolve-package-path";
import { PackageManagers } from "../constants";

const PREVIEW_CLI_PACKAGE = "@nativescript/preview-cli";

export class PreviewCommand implements ICommand {
allowedParameters: ICommandParameter[] = [];

constructor(private $errors: IErrors) {}
constructor(
private $logger: ILogger,
private $errors: IErrors,
private $projectData: IProjectData,
private $packageManager: IPackageManager,
private $childProcess: IChildProcess,
private $options: IOptions
) {}

private getPreviewCLIPath(): string {
return resolvePackagePath(PREVIEW_CLI_PACKAGE, {
paths: [this.$projectData.projectDir],
});
}

async execute(args: string[]): Promise<void> {
this.$errors.fail(
`The Preview service has been disabled until further notice.\n\n` +
`Configure local builds and use "ns run ${args.join(" ")}" instead.`
);
if (!this.$options.disableNpmInstall) {
// ensure latest is installed
await this.$packageManager.install(
`${PREVIEW_CLI_PACKAGE}@latest`,
this.$projectData.projectDir,
{
"save-dev": true,
"save-exact": true,
} as any
);
}

const previewCLIPath = this.getPreviewCLIPath();

if (!previewCLIPath) {
const packageManagerName = await this.$packageManager.getPackageManagerName();
let installCommand = "";

switch (packageManagerName) {
case PackageManagers.npm:
installCommand = "npm install --save-dev @nativescript/preview-cli";
break;
case PackageManagers.yarn:
installCommand = "yarn add -D @nativescript/preview-cli";
break;
case PackageManagers.pnpm:
installCommand = "pnpm install --save-dev @nativescript/preview-cli";
break;
}
this.$logger.info(
[
`Uhh ohh, no Preview CLI found.`,
"",
`This should not happen under regular circumstances, but seems like it did somehow... :(`,
`Good news though, you can install the Preview CLI by running`,
"",
" " + installCommand.green,
"",
"Once installed, run this command again and everything should work!",
"If it still fails, you can invoke the preview-cli directly as a last resort with",
"",
" ./node_modules/.bin/preview-cli".cyan,
"",
"And if you are still having issues, try again - or reach out on Discord/open an issue on GitHub.",
].join("\n")
);

this.$errors.fail("Running preview failed.");
}

const previewCLIBinPath = path.resolve(previewCLIPath, "./dist/index.js");

const commandIndex = process.argv.indexOf("preview");
const commandArgs = process.argv.slice(commandIndex + 1);
this.$childProcess.spawn(previewCLIBinPath, commandArgs, {
stdio: "inherit",
});
}

async canExecute(args: string[]): Promise<boolean> {
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nativescript",
"preferGlobal": true,
"version": "8.2.3",
"version": "8.3.0-beta.0",
"author": "NativeScript <support@nativescript.org>",
"description": "Command-line interface for building NativeScript projects",
"bin": {
Expand Down Expand Up @@ -106,7 +106,7 @@
"shelljs": "0.8.4",
"simple-git": "^2.20.1",
"simple-plist": "1.1.0",
"source-map": "0.7.3",
"source-map": "0.7.4",
"stringify-package": "1.0.1",
"tabtab": "https://github.com/Icenium/node-tabtab/tarball/master",
"tar": "6.0.2",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6594,10 +6594,10 @@
"resolved" "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz"
"version" "0.4.0"

"source-map@*", "source-map@0.7.3":
"integrity" "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ=="
"resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz"
"version" "0.7.3"
"source-map@*", "source-map@0.7.4":
"integrity" "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA=="
"resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz"
"version" "0.7.4"

"source-map@^0.5.6":
"integrity" "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w="
Expand Down

0 comments on commit 98124e7

Please sign in to comment.