-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·62 lines (55 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const chalk = require('chalk');
const log = console.log;
// Define some helper functions
// Check that the file exists, if it does exist then the
// programme may proceed, else kill it and alert the user
// via a prompt in the terminal.
fileExists = (file) => {
try {
return fs.statSync(file).isFile();
} catch (err) {
console.log(chalk.red('❗ That ain\'t no file ❗'))
return false;
}
}
getFilePath = () => {
const getPath = [
{
name: 'file',
type: 'input',
message: 'Gimme the path to your file:',
validate: (value) => {
if (value.length) {
return true;
} else {
return 'Hmm.. that doesn\'t seem right, try again'
}
}
}
];
return inquirer.prompt(getPath);
}
init = async () => {
try {
const file = await getFilePath();
if (fileExists(file.file)) {
const fileArray = file.file.split('.');
log(chalk.green('Let\'s do this!'));
log(chalk(`We're going to convert ${chalk.underline.bold(file.file)} and create a new file called ${chalk.underline.bold(fileArray[0] + '-stringified.' + fileArray[1])}`));
const contents = await fs.readFileSync(file.file, 'utf8');
const string = await JSON.stringify(contents);
fs.writeFile(fileArray[0] + '-stringified.' + fileArray[1], string, (err) => {
if (err) log(chalk.red('Something wen\'t wrong!'));
log(chalk.green(`Successfully created ${chalk.underline.bold(fileArray[0] + '-stringified.' + fileArray[1])}`));
});
}
} catch (err) {
console.log(chalk.red('Something wen\'t wrong'));
}
}
// Let's do this
init();