-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
92 lines (75 loc) · 3.11 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as fs from 'fs';
import {task as addActionTask} from './src/tasks/add-action';
import {task as addReducerTask} from './src/tasks/add-reducer';
import {task as addSubReducerTask} from './src/tasks/add-sub-reducer';
import {task as addComponentTask} from './src/tasks/add-component';
import {task as addContainerTask} from './src/tasks/add-container';
import {task as addReduxTask} from './src/tasks/add-redux';
import {task as addApolloQueryTask} from './src/tasks/add-apollo-query';
require('colors');
import * as jsdiff from 'diff';
import {promptYesOrNo} from './src/utils/prompt-utils';
const tasks: TsToolboxTask[] = [addActionTask, addReducerTask, addSubReducerTask, addComponentTask, addContainerTask,
addReduxTask, addApolloQueryTask];
const command = process.argv[2];
const task = tasks.find(t => t.command === command);
if (!task) {
console.log(`No tasks for specified command '${command}' found.`);
console.log(`The following commands are available: `);
tasks.forEach(t => console.log(t.command));
process.exit();
}
const taskArgs = process.argv.slice(3);
// Validate task arguments
const numberOfRequiredArguments = task.argumentInfo.filter(i => i.required).length;
const numberOfArguments = task.argumentInfo.length;
if (numberOfRequiredArguments > taskArgs.length ||
numberOfArguments < taskArgs.length) {
console.log(`Wrong number of arguments.`);
console.log(`The command '${command}' expects the following arguments: `);
task.argumentInfo.forEach(i => console.log(`${i.description} [${i.required ? 'required' : 'optional'}]`));
process.exit();
}
let writeActions: { [filePath: string]: string } = {};
const readFile = (filePath: string) => {
const fileExists = fs.existsSync(filePath);
return fileExists ? fs.readFileSync(filePath, 'utf8') : '';
};
const writeFile = (filePath: string, content: string) => {
writeActions[filePath] = content;
};
task.execute(taskArgs, readFile, writeFile);
// TODO: Get user confirmation
Object.keys(writeActions).forEach(filePath => {
const oldCode = readFile(filePath);
const newCode = writeActions[filePath];
const diff = jsdiff.diffLines(oldCode, newCode);
console.log(`The following changes will be applied to ${filePath}:`);
diff.forEach(function (part) {
if (!part.added && !part.removed) {
return;
}
const outPutPrefix = part.added ? '+ ' : part.removed ? '- ' : '';
// green for additions, red for deletions
// grey for common parts
const color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
const outputText = outPutPrefix + part.value;
console.log(outputText[color]);
});
console.log()
});
promptYesOrNo('Do you want to apply these changes?', result => {
console.log(result);
if (!result) {
console.log('Change are not applied.');
return;
}
console.log('Applying changes..');
// Write files
Object.keys(writeActions).forEach(filePath => {
console.log(filePath);
fs.writeFileSync(filePath, writeActions[filePath], 'utf8');
console.log('OK ✔');
});
});