-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnew.ts
54 lines (45 loc) · 1.4 KB
/
new.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Command } from '@oclif/command';
import { cli } from 'cli-ux';
import { writeFile, existsSync } from 'fs-extra';
import { join } from 'path';
import dedent from 'dedent';
import runCommand from '../../lib/runCommand';
import TerrainCLI from '../../TerrainCLI';
export default class TaskNew extends Command {
static description = 'Create a new task.';
static args = [{ name: 'task' }];
async run() {
const { args } = this.parse(TaskNew);
const execPath = 'tasks';
const newTaskPath = join(execPath, `${args.task}.ts`);
const command = async () => {
cli.action.start(`Creating task: ${args.task}`);
await writeFile(
newTaskPath,
dedent`
import { Env, task } from "@terra-money/terrain";
task(async (env:Env) => {
console.log(env);
console.log("Template")
});\n
`,
);
cli.action.stop();
};
// Error check to be performed upon each backtrack iteration.
const errorCheck = () => {
if (existsSync(newTaskPath)) {
TerrainCLI.error(
`A task with the name "${args.task}" already exists in the "tasks" directory. Try using another name for the task.`,
'Task Already Exists',
);
}
};
// Attempt to execute command while backtracking through file tree.
await runCommand(
execPath,
command,
errorCheck,
);
}
}