Skip to content

Commit

Permalink
feat(cli) : cli compatibility with operator (#1793)
Browse files Browse the repository at this point in the history
* feat(cli) : changes in package.json & readme

* feat(cli) : changes in deploy service

* feat(cli) : env command added

* fix(cli) : comments resolved

* feat(cli) : common package updated for CLI

* feat(cli) : comments added for spaship env
  • Loading branch information
soumyadip007 authored Jul 7, 2022
1 parent ff7eca8 commit b1d7716
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 69 deletions.
11 changes: 0 additions & 11 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.3.0](https://github.com/spaship/spaship/compare/v0.15.0...v1.3.0) (2022-06-02)


### Bug Fixes

* **deps:** update dependency next-global-css to v1.3.1 ([#1728](https://github.com/spaship/spaship/issues/1728)) ([1429a5d](https://github.com/spaship/spaship/commit/1429a5d44cf97fd8bb4f679d7d3246a5c5ab684e))





# [0.14.0](https://github.com/spaship/spaship/compare/v0.13.2...v0.14.0) (2021-02-11)

### Bug Fixes
Expand Down
84 changes: 73 additions & 11 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,112 @@ A command line interface for SPAship.
[![License](https://img.shields.io/npm/l/@spaship/cli.svg)](https://github.com/spaship/cli/blob/master/package.json)

<!-- toc -->
* [SPAship CLI](#spaship-cli)
* [Usage](#usage)
* [Commands](#commands)
* [spashiprc & SPAship environments](#spashiprc--spaship-environments)
* [Writing tests](#writing-tests)

- [SPAship CLI](#spaship-cli)
- [Usage](#usage)
- [Commands](#commands)
- [spashiprc & SPAship environments](#spashiprc--spaship-environments)
- [Writing tests](#writing-tests)
<!-- tocstop -->

# Usage

<!-- usage -->

```sh-session
$ npm install -g @spaship/cli
$ spaship COMMAND
running command...
$ spaship (-v|--version|version)
@spaship/cli/0.14.0 linux-x64 node-v14.18.3
@spaship/cli/0.13.2 linux-x64 node-v12.18.3
$ spaship --help [COMMAND]
USAGE
$ spaship COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->
* [`spaship help [COMMAND]`](#spaship-help-command)

- [`spaship deploy [ARCHIVE]`](#spaship-deploy-archive)
- [`spaship help [COMMAND]`](#spaship-help-command)
- [`spaship init`](#spaship-init)

## `spaship deploy [ARCHIVE]`

deploy to a SPAship host

```
USAGE
$ spaship deploy [ARCHIVE]
ARGUMENTS
ARCHIVE An archive (zip, tarball, or bzip2) file containing SPA static assets and a spaship.yaml file. You can omit
this if you specify the build artifact path as `buildDir` in the spaship.yaml file.
OPTIONS
-b, --builddir=builddir path of your SPAs artifact. Defaults to 'buildDir' if specified in the spaship.yaml.
-e, --env=env [default: default] either the name of a SPAship environment as defined in your .spashiprc.yml file,
or a URL to a SPAship environment
-p, --path=path a custom URL path for your app under the SPAship domain. Defaults to the 'path' in your spaship.yaml.
ex: /my/app
-r, --ref=ref [default: undefined] a version tag, commit hash, or branch to identify this release
--apikey=apikey a SPAship API key
DESCRIPTION
Send an archive containing a SPA to a SPAship host for deployment. Supports .tar.gz/.tgz, .zip, and .tar.bz2.
EXAMPLES
$ npm pack && spaship deploy your-app-1.0.0.tgz # deploying an archive created with npm pack
$ spaship deploy # deploying a buildDir directory
```

_See code: [src/commands/deploy.js](https://github.com/spaship/spaship/blob/v0.13.2/src/commands/deploy.js)_

## `spaship help [COMMAND]`

Display help for spaship.
display help for spaship

```
USAGE
$ spaship help [COMMAND]
ARGUMENTS
COMMAND Command to show help for.
COMMAND command to show help for
OPTIONS
-n, --nested-commands Include all nested commands in the output.
--all see all commands in CLI
```

_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v5.1.10/src/commands/help.ts)_
_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v3.2.0/src/commands/help.ts)_

## `spaship init`

Initialize a SPAship config file for your app.

```
USAGE
$ spaship init
OPTIONS
-n, --name=name (required) a human-friendly title for your app
-p, --path=path (required) the URL path for your app under the SPAship domain. ex: /my/app
-s, --[no-]single route all non-asset requests to index.html
--overwrite overwrite existing spaship.yaml
DESCRIPTION
Without arguments, init will ask you a few questions and generate a spaship.yaml config file. The answers can also be
passed in as CLI options.
```

_See code: [src/commands/init.js](https://github.com/spaship/spaship/blob/v0.13.2/src/commands/init.js)_

<!-- commandsstop -->

# spashiprc & SPAship environments
Expand Down
38 changes: 23 additions & 15 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@spaship/cli",
"description": "A command line interface for SPAship!",
"version": "1.3.0",
"version": "1.3.1",
"author": "[email protected]",
"bin": {
"spaship": "./bin/run"
Expand All @@ -15,38 +15,46 @@
"main": "src/index.js",
"repository": {
"type": "git",
"url": "git@github.com:spaship/spaship.git",
"url": "https://github.com/spaship/spaship.git",
"directory": "packages/cli"
},
"scripts": {
"postpack": "rm -f oclif.manifest.json",
"prepack": "oclif-dev manifest && oclif-dev readme",
"prepack": "oclif-dev manifest",
"test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
"preversion": "oclif-dev readme && git add README.md"
},
"dependencies": {
"@oclif/command": "^1.5.19",
"@oclif/command": "^1.8.16",
"@oclif/config": "^1.13.3",
"@oclif/plugin-help": "^5.0.0",
"@spaship/common": "^1.3.0",
"@oclif/plugin-help": "^3.3.1",
"@spaship/common": "^0.14.0",
"ajv": "^8.6.3",
"archiver": "^5.0.0",
"axios": "^0.23.0",
"chalk": "^4.1.1",
"cosmiconfig": "^7.0.0",
"form-data": "^4.0.0",
"inquirer": "^8.0.0",
"inquirer-loop": "^0.3.0",
"js-yaml": "^4.1.0",
"jwt-check-expiration": "^1.0.5",
"lodash": "^4.17.15",
"nconf": "^0.11.0",
"ora": "^6.0.0",
"ora": "^5.0.0",
"pretty-bytes": "^5.3.0",
"read-pkg-up": "^7.0.1"
"read-package-json": "^4.1.1",
"read-pkg-up": "^7.0.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@oclif/dev-cli": "1.26.10",
"@oclif/test": "2.1.0",
"chai": "4.3.6",
"@oclif/dev-cli": "1.26.0",
"@oclif/test": "1.2.8",
"chai": "4.3.4",
"eslint-config-oclif": "4.0.0",
"globby": "12.2.0",
"mocha": "9.2.2",
"nock": "13.2.2",
"globby": "^11.0.4",
"mocha": "9.1.2",
"nock": "13.1.3",
"nyc": "15.1.0",
"shortid": "2.2.16",
"temp-dir": "2.0.0"
Expand All @@ -70,4 +78,4 @@
"publishConfig": {
"access": "public"
}
}
}
19 changes: 11 additions & 8 deletions packages/cli/src/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class DeployCommand extends Command {
}

try {
host = new URL(host).origin;
host = new URL(host);
} catch (error) {
this.error(`The API url ${host} is invalid`);
}
Expand All @@ -99,15 +99,15 @@ class DeployCommand extends Command {
this.log("Creating a zip archive...");
try {
args.archive = await zipDirectory(buildDirPath, rawSpashipYml);
this.log(buildDirPath);
this.log(rawSpashipYml);
this.log("Done creating the archive...");
} catch (e) {
this.error(e);
}
} else {
// No buildDir is specified in the spaship.yaml
this.error(
"You should specify the build artifact path as `buildDir` in the spaship.yaml to run `spaship deploy` without the archive path."
);
this.error("You should provide the archive file name which contains the spaship.yaml file.");
}
}
const spinner = ora(`Start deploying SPA`);
Expand All @@ -123,10 +123,10 @@ class DeployCommand extends Command {
const data = new FormData();
data.append("name", name);
data.append("path", path);
data.append("ref", flags.ref);
data.append("ref", this.getRef(flags));
data.append("upload", fs.createReadStream(args.archive));

const response = await DeployService.upload(nodePath.join(host, apiPath), data, apikey, (progress) => {
const response = await DeployService.upload(host, data, apikey, (progress) => {
if (progress.percent < 1) {
const percent = Math.round(progress.percent * 100);
const takenTime = performance.now() - startTime;
Expand Down Expand Up @@ -158,6 +158,10 @@ class DeployCommand extends Command {
}
}
}

getRef(flags) {
return flags.ref;
}
}

DeployCommand.description = `deploy to a SPAship host
Expand Down Expand Up @@ -194,8 +198,7 @@ DeployCommand.flags = assign(
builddir: flags.string({
char: "b",
required: false,
description:
"path of your SPAs artifact. Defaults to 'buildDir' if specified in the spaship.yaml.",
description: "path of your SPAs artifact. Defaults to 'buildDir' if specified in the spaship.yaml.",
}),
},
commonFlags.apikey,
Expand Down
61 changes: 61 additions & 0 deletions packages/cli/src/commands/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { Command, flags } = require("@oclif/command");
const fs = require("fs");
const { loadRcFile } = require("../common/spashiprc-loader");
const { assign, get, isEmpty, omit } = require("lodash");
const { config } = require("../common/config/index");
const commonFlags = require("../common/flags");
const os = require("os");

class EnvCommand extends Command {
async run() {
let existingConfig;
const userHomeDir = os.homedir();
try {
await config.read(`${userHomeDir}/.spashiprc.yml`);
existingConfig = true;
} catch (e) {
existingConfig = false;
}
const { flags } = this.parse(EnvCommand);
const dataObj = {};
dataObj[flags.name] = { apikey: flags.apikey, url: flags.url };
const finalObj = { envs: dataObj };
try {
if (!existingConfig) {
await config.write(`${userHomeDir}/.spashiprc.yml`, JSON.parse(JSON.stringify(finalObj)));
} else {
let yamlData = await config.read(`${userHomeDir}/.spashiprc.yml`);
yamlData.envs = { ...yamlData.envs, ...dataObj };
await config.append(`${userHomeDir}/.spashiprc.yml`, JSON.parse(JSON.stringify(yamlData)));
}
this.log("Generated .spashiprc.yaml at " + userHomeDir);
} catch (error) {
this.log(error);
}
}
}

EnvCommand.flags = assign(
{
name: flags.string({
char: "n",
description: "name of the environment",
required: false, // not required for interactive mode
default: "undefined",
}),
},
{
url: flags.string({
char: "u",
description: "url of the environment",
required: false, // not required for interactive mode
default: "undefined",
}),
},
commonFlags.apikey
);

EnvCommand.description = `set env for .spashiprc.yml file (for setting environment & authentication).
`;

module.exports = EnvCommand;
Loading

0 comments on commit b1d7716

Please sign in to comment.