Skip to content

Commit

Permalink
feat: add command login
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Sep 1, 2019
1 parent b4b6449 commit 2304388
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 56 deletions.
36 changes: 36 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { isProjectFileExists, saveProjectConfigToDisk } from "../lib/utils";
import {
askIfOverrideProjectFile,
askForProjectDetails,
askForFeatures
} from "../lib/prompt";
import chalk from "chalk";

module.exports = async function() {
if (isProjectFileExists()) {
const shouldOverrideConfigFile = await askIfOverrideProjectFile();
if (shouldOverrideConfigFile.override === false) {
process.exit(0);
}
}

const project = await askForProjectDetails();
const { features } = await askForFeatures();
const featuresConfiguration: any = {};

for await (let feature of features) {
console.log(`Configuring ${chalk.green(feature)}:`);
try {
const featureImplementation = require(`./lib/features/${feature}/index`);
const config = await featureImplementation();
featuresConfiguration[feature] = config;
} catch (error) {
console.error(error.toString());
}
}

saveProjectConfigToDisk({
project,
...featuresConfiguration
});
};
27 changes: 27 additions & 0 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { az, saveProjectConfigToDisk } from "../lib/utils";
import { chooseSubscription } from "../lib/prompt";

module.exports = async function() {
// console.log(chalk.green(`Fetching subscriptions...`));

// @todo save these subscriptions globally.
let subscriptions: string = await az(
`login --query '[].{name:name, state:state, id:id}'`,
`Loading your subscriptions...`
);

if (subscriptions.length) {
const subscriptionsList = JSON.parse(subscriptions) as AzureSubscription[];
let selectedSubscription = (await chooseSubscription(subscriptionsList))
.subscription as string;
const { id, name } = subscriptionsList.find(
(sub: AzureSubscription) => sub.name === selectedSubscription
) as AzureSubscription;
saveProjectConfigToDisk({
subscription: {
id,
name
}
});
}
};
13 changes: 13 additions & 0 deletions src/features/hosting/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { az } from "../../lib/utils";

export async function push() {
await az(
`storage blob service-properties update --account-name <storage-account-name> --static-website --404-document <error-document-name> --index-document <index-document-name>`
);
await az(
`storage blob upload-batch -s <source-path> -d \$web --account-name <storage-account-name>`
);
await az(
`storage account show -n <storage-account-name> -g <resource-group-name> --query "primaryEndpoints.web"`
);
}
23 changes: 23 additions & 0 deletions src/features/hosting/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { QuestionCollection } from "inquirer";
import { createDirectoryIfNotExists } from "../../lib/utils";
import inquirer = require("inquirer");

// Note: use commonJs exports
module.exports = async function(): Promise<inquirer.Answers> {
const questions: QuestionCollection = [
{
type: "input",
name: "folder",
message: "Enter public folder (will be created if not present):",
default: "public",
validate: function(value: string) {
if (value && value.length) {
return createDirectoryIfNotExists(value);
} else {
return "Please enter a public folder.";
}
}
}
];
return inquirer.prompt(questions);
};
33 changes: 33 additions & 0 deletions src/features/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { QuestionCollection } from "inquirer";
import inquirer = require("inquirer");

// Note: use commonJs exports
module.exports = async function(): Promise<inquirer.Answers> {
const questions: QuestionCollection = [
{
type: "input",
name: "name",
message: "Enter your storage account name:",
validate: function(value: string) {
if (value.length) {
return true;
} else {
return "Please enter a valid name.";
}
}
},
{
type: "input",
name: "sas",
message: "Enter your storage SAS token:",
validate: function(value: string) {
if (value.length) {
return true;
} else {
return "Please enter a valid SAS token.";
}
}
}
];
return inquirer.prompt(questions);
};
40 changes: 19 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@
import chalk from "chalk";
import clear from "clear";
import figlet from "figlet";
import { saveProjectConfigToDisk, isProjectFileExists } from "./lib/files";
import {
askForProjectDetails,
askIfOverrideProjectFile,
askForFeatures
} from "./lib/prompt";
import { Answers } from "inquirer";

const files = require("./lib/files");
import program from "commander";

clear();
console.log(
chalk.red(figlet.textSync("NITRO", { horizontalLayout: "fitted" }))
chalk.red(
figlet.textSync(" NITRO", {
font: "ANSI Shadow",
horizontalLayout: "full"
})
)
);

(async () => {
if (isProjectFileExists()) {
const shouldOverrideConfigFile = await askIfOverrideProjectFile();
if (shouldOverrideConfigFile.override === false) {
process.exit(0);
}
program
.name("nitro")
.usage("<command>")
.version(require("../package.json").version)
.option("--init", "initialise a new workspace")
.option("--login", "connect to your Azure")
.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}

const project = await askForProjectDetails();
const { features } = await askForFeatures();
const commandName = program.args[0];

saveProjectConfigToDisk({
project,
features
});
(await require(`./commands/${commandName}`))();
})();
1 change: 0 additions & 1 deletion src/lib/config.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/lib/files.ts

This file was deleted.

65 changes: 54 additions & 11 deletions src/lib/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
import inquirer, { QuestionCollection, Answers } from "inquirer";
import { getCurrentDirectoryBase } from "./files";
import PromptUI from "inquirer/lib/ui/prompt";
import inquirer, { Answers, QuestionCollection } from "inquirer";
import { getCurrentDirectoryBase } from "./utils";

export function chooseSubscription(subscriptionsList: any[]): Promise<Answers> {
const questions: QuestionCollection = [
{
type: "list",
name: "subscription",
message: "Choose your subscription:",
choices: subscriptionsList.map((sub: AzureSubscription) => {
return {
name: `${sub.name}`,
disabled: sub.state !== "Enabled"
};
}),
validate: function(value: string) {
if (value.length) {
return true;
} else {
return "Please enter a name for the project.";
}
}
}
];
return inquirer.prompt(questions);
}

export function askForFeatures(): Promise<Answers> {
const questions: QuestionCollection = [
{
type: "checkbox",
name: "features",
message: "Choose the features you want to enable",
choices: [{
name: "hosting",
checked: true
}],
choices: [
{
name: "storage",
checked: true,
required: true
},
{
name: "hosting"
},
{
name: "functions (coming soon)",
disabled: true
},
{
name: "database (coming soon)",
disabled: true
},
{
name: "cdn (coming soon)",
disabled: true
},
{
name: "auth (coming soon)",
disabled: true
}
],
validate: function(value: string) {
if (value.length) {
return true;
Expand All @@ -25,14 +70,12 @@ export function askForFeatures(): Promise<Answers> {
}

export function askForProjectDetails(): Promise<Answers> {
const argv = require("minimist")(process.argv.slice(2));

const questions: QuestionCollection = [
{
type: "input",
name: "name",
message: "Enter a name for the project:",
default: argv._[0] || getCurrentDirectoryBase(),
default: getCurrentDirectoryBase(),
validate: function(value: string) {
if (value.length) {
return true;
Expand All @@ -50,7 +93,7 @@ export function askIfOverrideProjectFile(): Promise<Answers> {
{
type: "confirm",
name: "override",
message: "nitro.json found. Do you want to override it?",
message: "Configuration file found. Do you want to override it?",
default: false
}
];
Expand Down
Loading

0 comments on commit 2304388

Please sign in to comment.