-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-insert.js
More file actions
71 lines (64 loc) · 2.31 KB
/
batch-insert.js
File metadata and controls
71 lines (64 loc) · 2.31 KB
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
/**
* @file
* The functions for mass-creating components from a text file
*
*/
var readline = require('readline');
var fs = require('fs');
var path = require('path');
var rqt = require('./rqt');
/**
* Creates a set of ReactJS skeleton components based on the user's specifications
* in a text file containing paths to multiple components
* @param {String} version - Should the template be ES5 or ES6
* @param {String} defaultType - Component type to use when none is specified
* @param {String} txtFile - Path to the text file being read
* @param {callback} callback - Callback function after EACH component write is executed
*/
function createComponentsFromFile(version, defaultType, txtFile, callback) {
// Store a partial path specified by the user in the file (if specified)
var partialPath = '';
// Read file line by line
var reader = readline.createInterface({
input: fs.createReadStream(txtFile)
});
reader.on('line', function (line) {
// If the line specifies a file
if (line.indexOf('.js') > -1 || line.indexOf('.ts') > -1) {
// Split the file from the component type
var splitLine = line.split(',');
var filePath = splitLine[0].trim();
// If the filePath should include the most recent partial path
if (filePath.indexOf('./') < 0) {
filePath = path.normalize(path.join(partialPath, filePath));
}
/*
If the filePath doesn't use the most recent parital path, assume the
user is done with the current parital path and reset it
*/
else {
partialPath = '';
}
var componentType = defaultType;
// If component type was specified and valid, don't use the default
if (splitLine.length > 1 && _validComponentType(splitLine[1].trim())) {
componentType = splitLine[1].trim().toLowerCase();
}
// Do the actual component write now that we have the information we need
rqt.createComponent(version, componentType, filePath, callback);
}
// Else line is part of a filepath which should be joined with upcoming lines
else if (line !== '\n') {
partialPath = line;
}
});
}
/**
* Checks if the component type entered by the user is valid
* @param {String} type - The component type
* @returns {Boolean} Whether or not the component type is valid
*/
function _validComponentType(type) {
return type === 'p' || type === 'c';
}
module.exports = createComponentsFromFile;