Skip to content

Commit adb9dcb

Browse files
authored
Merge pull request #20 from nitric-dev/develop
Release latest CLI version
2 parents 4e0fb4a + 7cf3c02 commit adb9dcb

File tree

7 files changed

+78
-23
lines changed

7 files changed

+78
-23
lines changed

packages/base/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/package-lock.json
77
/tmp
88
node_modules
9+
oclif.manifest.json

packages/base/oclif.manifest.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/base/src/commands/make/function.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { Command, flags } from '@oclif/command';
22
import { wrapTaskForListr } from '@nitric/cli-common';
33
import { MakeFunctionTask } from '../../tasks/make';
4+
import { getAvailableTemplates } from '../../utils';
45
import Listr from 'listr';
6+
import inquirer from 'inquirer';
7+
8+
interface MakeFunctionArgs {
9+
template: string;
10+
name: string;
11+
}
512

613
export default class Function extends Command {
714
static description = 'Builds a nitric function';
@@ -23,22 +30,40 @@ export default class Function extends Command {
2330
static args = [
2431
{
2532
name: 'template',
26-
required: true,
27-
description: 'template name to generate function from',
33+
required: false,
34+
description: 'Function template',
35+
choices: getAvailableTemplates(),
2836
},
2937
{
3038
name: 'name',
31-
required: true,
32-
description: 'the name of the new function to create',
39+
required: false,
40+
description: 'Function name',
3341
},
3442
];
3543

3644
async run(): Promise<void> {
3745
const { args, flags } = this.parse(Function);
38-
const { template, name } = args;
39-
const { directory = '.', file = './nitric.yaml' } = flags;
46+
// Prompt for any args that weren't provided in the command
47+
const prompts = Function.args
48+
.filter((arg) => !args[arg.name])
49+
.map((arg) => {
50+
const prompt = {
51+
name: arg.name,
52+
message: arg.description,
53+
type: 'string',
54+
};
55+
if (arg.choices) {
56+
prompt.type = 'list';
57+
prompt['choices'] = arg.choices;
58+
}
59+
return prompt;
60+
});
61+
const promptArgs = await inquirer.prompt(prompts);
62+
63+
const { template, name } = { ...args, ...promptArgs } as MakeFunctionArgs;
64+
const { directory = `./${name}`, file = './nitric.yaml' } = flags;
4065

41-
const functionDirectoryName = (name as string)
66+
const functionName = (name as string)
4267
.toLowerCase()
4368
.replace(/ /g, '-')
4469
.replace(/[^-a-z\d]/g, '');
@@ -48,7 +73,7 @@ export default class Function extends Command {
4873
new MakeFunctionTask({
4974
template,
5075
dir: directory,
51-
name: functionDirectoryName,
76+
name: functionName,
5277
file,
5378
}),
5479
),

packages/base/src/commands/make/project.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Command, flags } from '@oclif/command';
22
import { wrapTaskForListr } from '@nitric/cli-common';
33
import { MakeProject, MakeFunctionTask } from '../../tasks/make';
4+
import { getAvailableTemplates } from '../../utils';
45
import Listr, { ListrTask } from 'listr';
56
import inquirer from 'inquirer';
67

@@ -35,7 +36,7 @@ export default class Project extends Command {
3536
name: 'example',
3637
message: 'Include an example function?',
3738
type: 'list',
38-
choices: [{ name: 'nodejs12' }, { name: 'python37' }, { name: 'none' }],
39+
choices: getAvailableTemplates(),
3940
},
4041
]);
4142

packages/base/src/commands/run.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@ async function runContainers(stack: NitricStack, portStart: number, directory: s
7878
await Promise.all(
7979
Object.values(currentRunResults).map(async (container) => {
8080
// FIXME: only attempt to stop if currently running
81-
await container.stop();
82-
return container.remove();
81+
if (container && container.stop) {
82+
try {
83+
await container.stop();
84+
} catch (error) {
85+
// TODO: Possibly log error for the container
86+
} finally {
87+
await container.remove();
88+
}
89+
}
8390
}),
8491
);
8592
}
@@ -91,6 +98,17 @@ async function runContainers(stack: NitricStack, portStart: number, directory: s
9198
const rangeLength = images.length * 2;
9299
const portRange = getPort.makeRange(portStart, portStart + rangeLength);
93100

101+
// Sort the images by the names of their functions, to ensure a more predicatable order between reloads.
102+
images.sort(({ func: { name: nameA } }, { func: { name: nameB } }) => {
103+
if (nameA < nameB) {
104+
return -1;
105+
}
106+
if (nameA > nameB) {
107+
return 1;
108+
}
109+
return 0;
110+
});
111+
94112
const runTaskOptions = await Promise.all(
95113
images.map(async (image) => {
96114
return {
@@ -168,8 +186,15 @@ export default class Run extends Command {
168186
if (runResults) {
169187
await Promise.all(
170188
Object.values(runResults).map(async (container) => {
171-
await container.stop();
172-
return container.remove();
189+
if (container && container.stop) {
190+
try {
191+
await container.stop();
192+
} catch (error) {
193+
// console.log("there was an error stopping this container");
194+
} finally {
195+
await container.remove();
196+
}
197+
}
173198
}),
174199
);
175200
}

packages/base/src/tasks/make/function.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export class MakeFunctionTask extends Task<void> {
4040
const inPath = templateFunctionPath(this.template);
4141
//TODO: should probably do something to make sure the file exists
4242
// Make a copy of the function template, using the new name in the output directory
43-
const outPath = path.join(this.dir, this.functionName);
43+
const outPath = path.join(this.dir);
4444
if (fs.existsSync(outPath)) {
45-
throw new Error(`Function directory already exists: ${this.functionName}`);
45+
throw new Error(`Function directory already exists: ${this.dir}`);
4646
}
4747

4848
const outStream = tar.extract(outPath);
@@ -55,16 +55,11 @@ export class MakeFunctionTask extends Task<void> {
5555

5656
async do(): Promise<void> {
5757
this.update('Checking stack descriptor');
58-
const nitricFile = path.join(this.dir, this.file);
58+
const nitricFile = this.file;
5959
const stack = readNitricDescriptor(nitricFile);
6060

6161
const { functions = [] } = stack;
6262

63-
const funcDirName = this.functionName
64-
.toLowerCase()
65-
.replace(/ /g, '-')
66-
.replace(/[^-a-z\d]/g, '');
67-
6863
if (functions.find((func) => func.name === this.functionName) !== undefined) {
6964
throw new Error(`Function ${this.functionName} already defined in ${this.file}`);
7065
}
@@ -74,7 +69,7 @@ export class MakeFunctionTask extends Task<void> {
7469
await this.makeFunction();
7570
//then
7671

77-
const functionFolder = path.join(this.dir, funcDirName);
72+
const functionFolder = this.dir;
7873
const nitricFileDir = path.dirname(nitricFile);
7974

8075
this.update(`Updating ${this.file}`);

packages/base/src/utils/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export function isTemplateAvailable(templateName: string): boolean {
1010
return fs.existsSync(templateDirectory);
1111
}
1212

13+
export function getAvailableTemplates(): string[] {
14+
return fs
15+
.readdirSync(TEMPLATE_DIR, {
16+
withFileTypes: true,
17+
})
18+
.filter((dirent) => dirent.isDirectory())
19+
.map((dirent) => dirent.name);
20+
}
21+
1322
export function createNitricLogDir(): void {
1423
if (!fs.existsSync(LOG_DIR)) {
1524
fs.mkdirSync(LOG_DIR);

0 commit comments

Comments
 (0)