Skip to content

Commit

Permalink
feat(cloud-cli): move cloud commands to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-pichon committed Apr 23, 2024
1 parent 35e8fcc commit eaed54e
Show file tree
Hide file tree
Showing 39 changed files with 5,479 additions and 4,021 deletions.
4 changes: 4 additions & 0 deletions packages/cli/cloud/.eslintignore
@@ -0,0 +1,4 @@
node_modules/
.eslintrc.js
dist/
bin/
4 changes: 4 additions & 0 deletions packages/cli/cloud/.eslintrc.js
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['custom/back/typescript'],
};
22 changes: 22 additions & 0 deletions packages/cli/cloud/LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2015-present Strapi Solutions SAS

Portions of the Strapi software are licensed as follows:

* All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".

* All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.

MIT Expat License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/cli/cloud/README.md
@@ -0,0 +1,3 @@
# Cloud CLI

This package includes the `cloud` CLI to manage Strapi projects on the cloud.
7 changes: 7 additions & 0 deletions packages/cli/cloud/bin/index.js
@@ -0,0 +1,7 @@
#!/usr/bin/env node

'use strict';

const { runStrapiCloudCommand } = require('../dist/bin');

runStrapiCloudCommand(process.argv);
77 changes: 77 additions & 0 deletions packages/cli/cloud/package.json
@@ -0,0 +1,77 @@
{
"name": "@strapi/cloud-cli",
"version": "4.23.0",
"description": "Commands to interact with the Strapi Cloud",
"keywords": [
"strapi",
"cloud",
"cli"
],
"homepage": "https://strapi.io",
"bugs": {
"url": "https://github.com/strapi/strapi/issues"
},
"repository": {
"type": "git",
"url": "git://github.com/strapi/strapi.git"
},
"license": "SEE LICENSE IN LICENSE",
"author": {
"name": "Strapi Solutions SAS",
"email": "hi@strapi.io",
"url": "https://strapi.io"
},
"maintainers": [
{
"name": "Strapi Solutions SAS",
"email": "hi@strapi.io",
"url": "https://strapi.io"
}
],
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"bin": "./bin/index.js",
"files": [
"./dist",
"./bin"
],
"scripts": {
"build": "pack-up build",
"clean": "run -T rimraf ./dist",
"lint": "run -T eslint .",
"watch": "pack-up watch"
},
"dependencies": {
"@strapi/utils": "4.23.0",
"axios": "1.6.0",
"chalk": "4.1.2",
"cli-progress": "3.12.0",
"commander": "8.3.0",
"eventsource": "2.0.2",
"inquirer": "8.2.5",
"jsonwebtoken": "9.0.0",
"jwks-rsa": "3.1.0",
"lodash": "4.17.21",
"minimatch": "9.0.3",
"open": "8.4.0",
"ora": "5.4.1",
"pkg-up": "3.1.0",
"tar": "6.1.13",
"xdg-app-paths": "8.3.0",
"yup": "0.32.9"
},
"devDependencies": {
"@strapi/pack-up": "4.23.0",
"@types/cli-progress": "3.11.5",
"@types/eventsource": "1.1.15",
"@types/lodash": "^4.14.191",
"eslint-config-custom": "4.23.0",
"tsconfig": "4.23.0"
},
"engines": {
"node": ">=18.0.0 <=20.x.x",
"npm": ">=6.0.0"
}
}
14 changes: 14 additions & 0 deletions packages/cli/cloud/packup.config.ts
@@ -0,0 +1,14 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from '@strapi/pack-up';

export default defineConfig({
bundles: [
{
source: './src/index.ts',
import: './dist/index.mjs',
require: './dist/index.js',
},
],
dist: './dist',
runtime: 'node',
});
54 changes: 54 additions & 0 deletions packages/cli/cloud/src/bin.ts
@@ -0,0 +1,54 @@
import { Command } from 'commander';
import { createLogger } from './utils/logger';
import { CLIContext } from './types';
import { cli } from './index';

function buildStrapiCloudCommand(argv = process.argv, command = new Command()) {
const cloudCommands = {
loginCommand: cli.login.command,
logoutCommand: cli.logout.command,
deployCommand: cli.deployProject.command,
} as const;

// Initial program setup
command.storeOptionsAsProperties(false).allowUnknownOption(true);

// Help command
command.helpOption('-h, --help', 'Display help for command');
command.addHelpCommand('help [command]', 'Display help for command');

// Debug and silent options
command.option('-d, --debug', 'Output extra debugging');
command.option('-s, --silent', 'Output less information');

const cwd = process.cwd();

const hasDebug = argv.includes('--debug');
const hasSilent = argv.includes('--silent');

const logger = createLogger({ debug: hasDebug, silent: hasSilent, timestamp: false });

const ctx = {
cwd,
logger,
} satisfies CLIContext;

const keys = Object.keys(cloudCommands) as (keyof typeof cloudCommands)[];

// Load all commands
keys.forEach((name) => {
try {
// Add this command to the Commander command object
cloudCommands[name]({ command, argv, ctx });
} catch (e) {
console.error(`Failed to load command ${name}`, e);
}
});
}

function runStrapiCloudCommand(argv = process.argv, command = new Command()) {
buildStrapiCloudCommand(argv, command);
command.parse(argv);
}

export { buildStrapiCloudCommand, runStrapiCloudCommand };
File renamed without changes.
File renamed without changes.
@@ -1,10 +1,8 @@
import inquirer from 'inquirer';
import { AxiosError } from 'axios';
import { defaults } from 'lodash/fp';
import { cloudApiFactory, type ProjectInput } from '../services/cli-api';
import type { CLIContext } from '../../../types';
import { tokenServiceFactory } from '../utils/token';
import type { ProjectAnswers } from '../types';
import type { CLIContext, ProjectAnswers, ProjectInput } from '../types';
import { tokenServiceFactory, cloudApiFactory } from '../services';

function handleError(ctx: CLIContext, error: Error) {
const tokenService = tokenServiceFactory(ctx);
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/cloud/src/create-project/index.ts
@@ -0,0 +1,9 @@
import action from './action';

export { action };

export default {
name: 'create-project',
description: 'Create a new project',
action,
};
Expand Up @@ -3,15 +3,12 @@ import path from 'path';
import { AxiosError } from 'axios';
import { apiConfig } from '../config/api';
import { compressFilesToTar } from '../utils/compress-files';
import { cloudApiFactory, type ProjectInfos } from '../services/cli-api';
import * as localSave from '../services/strapi-info-save';
import createProjectAction from '../create-project/action';
import { type CLIContext } from '../../../types';
import type { CLIContext, ProjectInfos } from '../types';
import { getTmpStoragePath } from '../config/local';
import { tokenServiceFactory } from '../utils/token';
import { cloudApiFactory, tokenServiceFactory, local } from '../services';
import { notificationServiceFactory } from '../utils/notification-service';
import { loadPkg } from '../../../utils/pkg';
import { LOCAL_SAVE_FILENAME } from '../services/strapi-info-save';
import { loadPkg } from '../utils/pkg';

const FILE_SIZE_LIMIT = 100 * 1024 * 1024; // 100MB

Expand Down Expand Up @@ -92,7 +89,7 @@ async function upload(ctx: CLIContext, project: ProjectInfos, token: string) {
if (error instanceof AxiosError && error.response?.data) {
if (error.response.status === 404) {
ctx.logger.error(
`The project does not exist. Remove the ${LOCAL_SAVE_FILENAME} file and try again.`
`The project does not exist. Remove the ${local.LOCAL_SAVE_FILENAME} file and try again.`
);
} else {
ctx.logger.error(error.response.data);
Expand All @@ -112,7 +109,7 @@ async function upload(ctx: CLIContext, project: ProjectInfos, token: string) {
}

async function getProject(ctx: CLIContext) {
const { project } = localSave.retrieve();
const { project } = local.retrieve();
if (!project) {
try {
return await createProjectAction(ctx)();
Expand Down
@@ -1,11 +1,11 @@
import { type StrapiCommand } from '../../../types';
import { runAction } from '../../../utils/helpers';
import { type StrapiCloudCommand } from '../types';
import { runAction } from '../utils/helpers';
import action from './action';

/**
* `$ deploy project to the cloud`
*/
const command: StrapiCommand = ({ command, ctx }) => {
const command: StrapiCloudCommand = ({ command, ctx }) => {
return command
.command('cloud:deploy')
.alias('deploy')
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/cloud/src/deploy-project/index.ts
@@ -0,0 +1,11 @@
import action from './action';
import command from './command';

export { action, command };

export default {
name: 'deploy-project',
description: 'Deploy a Strapi Cloud project',
action,
command,
};
15 changes: 15 additions & 0 deletions packages/cli/cloud/src/index.ts
@@ -0,0 +1,15 @@
import * as deployProject from './deploy-project';
import * as login from './login';
import * as logout from './logout';
import * as createProject from './create-project';

export const cli = {
deployProject,
login,
logout,
createProject,
};

export * as services from './services';

export * from './types';
@@ -1,9 +1,7 @@
import axios, { AxiosResponse, AxiosError } from 'axios';
import { tokenServiceFactory } from '../utils/token';
import { tokenServiceFactory, cloudApiFactory } from '../services';

import type { CloudCliConfig } from '../types';
import { cloudApiFactory } from '../services/cli-api';
import type { CLIContext } from '../../../types';
import type { CloudCliConfig, CLIContext } from '../types';

const openModule = import('open');
const cloudApiService = cloudApiFactory();
Expand Down
@@ -1,11 +1,11 @@
import type { StrapiCommand } from '../../../types';
import { runAction } from '../../../utils/helpers';
import type { StrapiCloudCommand } from '../types';
import { runAction } from '../utils/helpers';
import action from './action';

/**
* `$ cloud device flow login`
*/
const command: StrapiCommand = ({ command, ctx }) => {
const command: StrapiCloudCommand = ({ command, ctx }) => {
return command
.command('cloud:login')
.alias('login')
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/cloud/src/login/index.ts
@@ -0,0 +1,11 @@
import action from './action';
import command from './command';

export { action, command };

export default {
name: 'login',
description: 'Strapi Cloud Login',
action,
command,
};
@@ -1,5 +1,5 @@
import type { CLIContext } from '../../../types';
import { tokenServiceFactory } from '../utils/token';
import type { CLIContext } from '../types';
import { tokenServiceFactory } from '../services';

export default async (ctx: CLIContext) => {
const { logger } = ctx;
Expand Down
@@ -1,11 +1,11 @@
import type { StrapiCommand } from '../../../types';
import { runAction } from '../../../utils/helpers';
import type { StrapiCloudCommand } from '../types';
import { runAction } from '../utils/helpers';
import action from './action';

/**
* `$ cloud device flow logout`
*/
const command: StrapiCommand = ({ command, ctx }) => {
const command: StrapiCloudCommand = ({ command, ctx }) => {
return command
.command('cloud:logout')
.alias('logout')
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/cloud/src/logout/index.ts
@@ -0,0 +1,11 @@
import action from './action';
import command from './command';

export { action, command };

export default {
name: 'logout',
description: 'Strapi Cloud Logout',
action,
command,
};
File renamed without changes.
3 changes: 3 additions & 0 deletions packages/cli/cloud/src/services/index.ts
@@ -0,0 +1,3 @@
export { cloudApiFactory } from './cli-api';
export * as local from './strapi-info-save';
export { tokenServiceFactory } from './token';
Expand Up @@ -2,9 +2,8 @@ import jwksClient, { type JwksClient, type SigningKey } from 'jwks-rsa';
import type { JwtHeader, VerifyErrors } from 'jsonwebtoken';
import jwt from 'jsonwebtoken';
import { getLocalConfig, saveLocalConfig } from '../config/local';
import type { CloudCliConfig } from '../types';
import { cloudApiFactory } from '../services/cli-api';
import type { CLIContext } from '../../../types';
import type { CloudCliConfig, CLIContext } from '../types';
import { cloudApiFactory } from './cli-api';

const cloudApiService = cloudApiFactory();

Expand Down

0 comments on commit eaed54e

Please sign in to comment.