-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathutils.js
149 lines (129 loc) · 4.2 KB
/
utils.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import fs from 'fs';
import path from 'path';
import {execSync} from 'child_process';
import editer from 'editer';
import _ from 'lodash';
import {mkdirsSync, outputFileSync} from 'fs-extra';
import yaml from 'js-yaml';
import {logger} from './logger';
function editFile(type, pathToFile, string, options) {
let fileContent = fs.readFileSync(pathToFile, {encoding: 'utf-8'});
let updatedContent;
if (type === 'insert') {
updatedContent = editer.insert(string, fileContent, options);
} else if (type === 'remove') {
updatedContent = editer.remove(string, fileContent, options);
}
fs.writeFileSync(pathToFile, updatedContent);
}
/**
* Writes a string on the file at the given path, at a location specified by
* options. Uses 'editer' module under the hood the find the exact location of
* the insertion.
*
* @param pathToFile {String} - the path to the file. Can be either absolute or
* relative.
*/
export function insertToFile(pathToFile, string, options) {
editFile('insert', pathToFile, string, options);
}
export function removeFromFile(pathToFile, string, options) {
editFile('remove', pathToFile, string, options);
}
/**
* Creates a directory and displays a message in the console
*
* @param path {String} - the path on which the directory is to be created
*/
export function createDir(path) {
mkdirsSync(path);
let displayPath = path.replace(/^\.\//, '')
.replace(/$/, '/');
logger.create(displayPath);
}
/**
* Reads the content of the template file and evaluates template variables
* in the template if necessary
*
* @param templatePath {String} - the path to the template file
* @param templateVariables {Object} - key value pairs of variables to be
* evaluated in the template
*/
export function getFileContent(templatePath, templateVariables) {
let templateContent = fs.readFileSync(templatePath);
if (templateVariables) {
return _.template(templateContent)(templateVariables);
} else {
return templateContent;
}
}
/**
* Creates a file at a given path using the template and template variables
* provided. Logs a message on the console.
* If the parent directory does not exist, recursively create parent directories
* by using `fs-extra` module.
*
* @param templatePath {String} - the path to the template file
* @param targetPath {String} - the path on which the file is to be generated
* @param templateVariables {Object} - key value pairs of variables to be
* evaluated in the template
*/
export function createFile(templatePath, targetPath, templateVariables) {
let fileContent = getFileContent(templatePath, templateVariables);
outputFileSync(targetPath, fileContent);
let displayPath = targetPath.replace(/^\.\//, '');
logger.create(displayPath);
}
/**
* Executes a command. Logs a message in the console.
*
* @param cmd {String} - the command to execute
* @param options {Object} - options to be provided to child_process.execSync
*/
export function executeCommand(cmd, options) {
logger.run(cmd);
execSync(cmd, options);
}
/**
* Checks if a file or directory exists at the given path
* @param path {String} - the path to the file or directory. Can be either
* absolute or relative.
* @return Boolean - true if the file or directory exists. Otherwise false.
*/
export function checkFileExists(path) {
try {
fs.lstatSync(path);
} catch (e) {
if (e.code === 'ENOENT') {
return false;
} else {
throw e;
}
}
return true;
}
export function runScriptFile(pathToScript, args = [], options = {}) {
let scriptName = path.basename(pathToScript).replace(/\..*$/, '');
logger.invoke(scriptName);
let commandPrefix = isWindows()?'': 'bash';
execSync(`${commandPrefix} ${pathToScript} ${args.join(' ')}`, options);
}
export function isWindows(){
return /^win/.test(process.platform);
}
export function getLineBreak() {
if (isWindows()) {
return '\r\n';
} else {
return '\n';
}
}
/**
* parseYamlFromFile parses YAML into object from a given file.
* @param path {String} - path to the YAML file
* @return {Object} - config object
*/
export function parseYamlFromFile(path) {
let content = fs.readFileSync(path, {encoding: 'utf-8'});
return yaml.safeLoad(content);
}