-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·52 lines (49 loc) · 1.37 KB
/
index.js
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
#!/usr/bin/env node
const fs = require('fs');
const inquirer = require('inquirer');
const chalk = require('chalk');
const QUESTION = [
{
name: 'num',
type: 'input',
message: 'Enter your homework number :',
validate: val => {
val = val.trim()
if (isNaN(val) || val === '') {
return `Homework number only includes ${chalk.red.underline('numbers')}`;
} else {
return true;
}
}
}
]
inquirer.prompt(QUESTION)
.then(obj => {
const input = obj['num'].trim();
const DIR = `./${input}/`
const HW_NAME = `${input}-homework.md`
const WS_NAME = `${input}-workshop.md`
const HW_CONTENT = `# ${input} Homework\n`
const WS_CONTENT = `# ${input} Workshop\n`
if (!fs.existsSync(DIR)) {
fs.mkdirSync(DIR)
// Create Homework file
fs.writeFile(HW_NAME, HW_CONTENT, err => {
if (err) throw err;
// Create Workshop fil
fs.writeFile(WS_NAME, WS_CONTENT, err => {
if (err) throw err;
// Move files into the directory
fs.rename(HW_NAME, DIR + HW_NAME, err => {
if (err) throw err;
})
fs.rename(WS_NAME, DIR + WS_NAME, err => {
if (err) throw err;
})
console.log(chalk.cyan('✔︎ Successfully created in ') + chalk.bgCyan.black(input) + chalk.cyan(' folder'));
});
});
} else {
console.log(chalk.yellow('» ') + chalk.bgYellow.black(input) + chalk.yellow(' folder already exists'));
}
});