Skip to content

Commit c212b57

Browse files
committed
feat: select or create new resource group
THIS COMMIT INTRODUCES A LOT OF REFACTORING
1 parent b942c9b commit c212b57

File tree

12 files changed

+252
-76
lines changed

12 files changed

+252
-76
lines changed

src/commands/init.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isProjectFileExists, saveProjectConfigToDisk } from "../lib/utils";
1+
import { isProjectFileExists, saveWorkspace, Config } from "../lib/utils";
22
import {
33
askIfOverrideProjectFile,
44
askForProjectDetails,
@@ -15,21 +15,27 @@ module.exports = async function() {
1515
}
1616

1717
const project = await askForProjectDetails();
18+
19+
20+
await (require(`./login`)());
21+
await (require(`./resource-group-selection`)());
22+
1823
const { features } = await askForFeatures();
1924
const featuresConfiguration: any = {};
2025

2126
for await (let feature of features) {
2227
console.log(`Configuring ${chalk.green(feature)}:`);
2328
try {
24-
const featureImplementation = require(`./lib/features/${feature}/index`);
29+
const featureImplementation = require(`../features/${feature}/index`);
2530
const config = await featureImplementation();
2631
featuresConfiguration[feature] = config;
32+
Config.get(feature, config);
2733
} catch (error) {
2834
console.error(error.toString());
2935
}
3036
}
3137

32-
saveProjectConfigToDisk({
38+
saveWorkspace({
3339
project,
3440
...featuresConfiguration
3541
});

src/commands/login.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { az, saveProjectConfigToDisk } from "../lib/utils";
1+
import { az, saveWorkspace, Config } from "../lib/utils";
22
import { chooseSubscription } from "../lib/prompt";
33

44
module.exports = async function() {
5-
// console.log(chalk.green(`Fetching subscriptions...`));
6-
7-
// @todo save these subscriptions globally.
8-
let subscriptions: string = await az(
5+
let subscriptionsList = await az<AzureSubscription[]>(
96
`login --query '[].{name:name, state:state, id:id}'`,
107
`Loading your subscriptions...`
118
);
129

13-
if (subscriptions.length) {
14-
const subscriptionsList = JSON.parse(subscriptions) as AzureSubscription[];
15-
let selectedSubscription = (await chooseSubscription(subscriptionsList))
10+
Config.set("subscriptions", subscriptionsList);
11+
12+
if (subscriptionsList.length) {
13+
let selectedSubscriptionId = (await chooseSubscription(subscriptionsList))
1614
.subscription as string;
1715
const { id, name } = subscriptionsList.find(
18-
(sub: AzureSubscription) => sub.name === selectedSubscription
16+
(subscription: AzureSubscription) =>
17+
subscription.id === selectedSubscriptionId
1918
) as AzureSubscription;
20-
saveProjectConfigToDisk({
19+
20+
Config.set("subscription", { id, name });
21+
22+
saveWorkspace({
2123
subscription: {
2224
id,
2325
name

src/commands/push.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { push } from "../features/hosting/command";
2+
import { Config } from "../lib/utils";
3+
4+
module.exports = async function() {
5+
const subscription = Config.get("subscription") as AzureSubscription;
6+
const storage = Config.get("storage") as AzureStorage;
7+
8+
await push({
9+
subscriptionId: subscription.id,
10+
storageAccountName: storage.name
11+
});
12+
};

src/commands/resource-group-create.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { askForResourceGroupDetails } from "../lib/prompt";
2+
import { az, saveWorkspace } from "../lib/utils";
3+
4+
module.exports = async function() {
5+
let regionsList = await az<AzureRegion[]>(
6+
`account list-locations --query '[].{name:name, id:id, displayName:displayName}'`,
7+
`Loading your regions (this may take few minutes)...`
8+
);
9+
10+
const { resource, region } = await askForResourceGroupDetails(regionsList);
11+
12+
let resourceGroup = await az<AzureResourceGroup>(
13+
`group create -l ${region} -n ${resource} --tag cli=nitro --query '[].{name:name, id:id, location:location}'`,
14+
`Creating your resource group...`
15+
);
16+
17+
console.log("asdasdsdasdasd");
18+
19+
saveWorkspace({
20+
resourceGroup
21+
});
22+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { chooseResourceGroup } from "../lib/prompt";
2+
import { az, Config, saveWorkspace } from "../lib/utils";
3+
4+
module.exports = async function() {
5+
let resourceGroupsList = await az<AzureResourceGroup[]>(
6+
`group list --query '[].{name:name, id:id, location:location}'`,
7+
`Loading your resource groups...`
8+
);
9+
10+
if (resourceGroupsList.length) {
11+
let selectedResourceId = (await chooseResourceGroup(resourceGroupsList)).resourceGroup as string;
12+
13+
if (selectedResourceId === "") {
14+
// create a new resource group
15+
return (await require(`./resource-group-create`))();
16+
} else {
17+
const { id, name, location } = resourceGroupsList.find(
18+
(resourceGroup: AzureResourceGroup) => resourceGroup.id === selectedResourceId
19+
) as AzureResourceGroup;
20+
21+
saveWorkspace({
22+
resourceGroup: {
23+
id,
24+
location,
25+
name
26+
}
27+
});
28+
}
29+
}
30+
};

src/features/hosting/command.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { az } from "../../lib/utils";
22

3-
export async function push() {
3+
export async function push({ subscriptionId, storageAccountName }: { subscriptionId: string, storageAccountName: string }) {
44
await az(
5-
`storage blob service-properties update --account-name <storage-account-name> --static-website --404-document <error-document-name> --index-document <index-document-name>`
6-
);
7-
await az(
8-
`storage blob upload-batch -s <source-path> -d \$web --account-name <storage-account-name>`
9-
);
10-
await az(
11-
`storage account show -n <storage-account-name> -g <resource-group-name> --query "primaryEndpoints.web"`
5+
`storage blob service-properties update --account-name ${storageAccountName} --static-website --404-document 404.html --index-document index.html`
126
);
7+
// await az(
8+
// `storage blob upload-batch -s <source-path> -d \$web --account-name <storage-account-name>`
9+
// );
10+
// await az(
11+
// `storage account show -n <storage-account-name> -g <resource-group-name> --query "primaryEndpoints.web"`
12+
// );
1313
}

src/features/hosting/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = async function(): Promise<inquirer.Answers> {
1212
default: "public",
1313
validate: function(value: string) {
1414
if (value && value.length) {
15+
// TODO: copy template files if new created folder
1516
return createDirectoryIfNotExists(value);
1617
} else {
1718
return "Please enter a public folder.";

src/features/storage/commands.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { az } from "../../lib/utils";
2+
3+
export async function create({ name }: AzureStorage) {
4+
await az(
5+
`storage account create -n ${name} -g MyResourceGroup -l westus --sku Standard_LRS`
6+
);
7+
}

src/features/storage/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ module.exports = async function(): Promise<inquirer.Answers> {
1515
return "Please enter a valid name.";
1616
}
1717
}
18-
},
19-
{
20-
type: "input",
21-
name: "sas",
22-
message: "Enter your storage SAS token:",
23-
validate: function(value: string) {
24-
if (value.length) {
25-
return true;
26-
} else {
27-
return "Please enter a valid SAS token.";
28-
}
29-
}
3018
}
3119
];
3220
return inquirer.prompt(questions);

src/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env node
22

3+
process.env.DEBUG = "*";
4+
35
import chalk from "chalk";
46
import clear from "clear";
57
import figlet from "figlet";
@@ -16,25 +18,31 @@ console.log(
1618
);
1719

1820
(async () => {
21+
const runCommand = async (commandName: string) => {
22+
try {
23+
return (await require(`./commands/${commandName}`))();
24+
} catch (error) {
25+
console.error(chalk.red(`Command "${commandName}" not supported yet.`));
26+
console.error(chalk.red(error));
27+
program.outputHelp();
28+
}
29+
};
1930
program
2031
.name("nitro")
2132
.usage("<command>")
2233
.version(require("../package.json").version)
23-
.option("--init", "initialise a new workspace")
24-
.option("--login", "connect to your Azure")
25-
.option("--push", "deploy the app to Azure")
34+
.option("login, --login", "connect to your Azure")
35+
.option("init, --init", "initialise a new workspace")
36+
.option("push, --push", "deploy the app to Azure")
2637
.parse(process.argv);
2738

28-
if (!process.argv.slice(2).length) {
29-
program.outputHelp();
30-
}
39+
// use process.argv not program.argv
40+
const commandName = process.argv[2];
3141

32-
const commandName = program.args[0];
33-
34-
try {
35-
(await require(`./commands/${commandName}`))();
36-
} catch (error) {
37-
console.error(chalk.red(`Command ${commandName} not supported.`));
42+
if (!process.argv.slice(2).length || !commandName) {
3843
program.outputHelp();
44+
process.exit(0);
3945
}
46+
47+
runCommand(commandName.replace("--", ""));
4048
})();

0 commit comments

Comments
 (0)