forked from OfficeDev/office-js-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
290 lines (254 loc) · 7.61 KB
/
Copy pathhelpers.ts
File metadata and controls
290 lines (254 loc) · 7.61 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as chalk from 'chalk';
import * as jsyaml from 'js-yaml';
import { console } from './status';
import * as rimraf from 'rimraf';
export const officeHostsToAppNames = {
'ACCESS': 'Access',
'EXCEL': 'Excel',
'ONENOTE': 'OneNote',
'OUTLOOK': 'Outlook',
'POWERPOINT': 'PowerPoint',
'PROJECT': 'Project',
'WORD': 'Word'
};
export interface SnippetFileInput {
file_name: string;
relativePath: string;
fullPath: string;
host: string;
group: string;
isPublic: boolean;
}
export interface SnippetProcessedData {
id: string;
name: string;
fileName: string;
relativePath: string;
fullPath: string;
description: string;
host: string;
rawUrl: string;
group: string;
order: number;
api_set: {
[index: string]: number
};
isPublic: boolean;
endpoints?: string[];
}
/**
* Creates a chalk based section with the specific color.
* @param title Title of the banner.
* @param message Message of the banner.
* @param chalkFunction Chalk color function.
*/
export const banner = (title: string, message: string = null, chalkFn: chalk.ChalkChain = null) => {
if (!chalkFn) {
chalkFn = chalk.bold;
}
const dashes = Array(Math.max(title.length + 1, 30)).join('-');
console.log(chalkFn(`\n\n${dashes}`));
console.log(chalkFn(`${title}`));
if (message) {
console.log(chalkFn(dashes));
console.log(message);
}
console.log(chalkFn(`${dashes}\n`));
};
export function getPrintableDetails(item: any, indent: number) {
const details = jsyaml.safeDump(item, {
indent: 4,
lineWidth: -1,
skipInvalid: true
});
return details.split('\n').map(line => new Array(indent).join(' ') + line).join('\n');
}
export const getDestinationBranch = (sourceBranch: 'master' | 'prod' | any): 'deploy-beta' | 'deploy-prod' | null => {
if (sourceBranch === 'master') {
return 'deploy-beta';
}
else if (sourceBranch === 'prod') {
return 'deploy-prod';
}
else {
return null;
}
};
/**
* Creates a folder.
* @param dir An absolute path to the directory.
*/
export const mkDir = (dir: string) =>
new Promise<string>((resolve, reject) => {
const location = path.resolve(dir);
fs.mkdir(location, (err) => {
if (err) {
return reject(err);
}
return resolve(location);
});
});
/**
* Deletes a folder.
* @param dir An absolute path to the directory.
*/
export const rmRf = (dir: string) =>
new Promise<string>((resolve, reject) => {
const location = path.resolve(dir);
rimraf(location, (err) => {
if (err) {
return reject(err);
}
return resolve(location);
});
});
/**
* Load all the files and folders in a given directory.
* @param dir An absolute path to the directory.
*/
export const readDir = (dir: string) =>
new Promise<string[]>((resolve, reject) => {
fs.readdir(dir, (err, files) => {
if (err) {
return reject(err);
}
return resolve(files);
});
});
/**
* Write to file
* @param filename
* @param contents
*/
export const writeFile = (filename: string, contents: string) =>
new Promise((resolve, reject) => {
fs.writeFile(filename, contents, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
/**
* Check whether the given path is a file or a directory.
* @param path An absolute path to the directory.
*/
export const isDir = (path: string) =>
new Promise<boolean>((resolve, reject) => {
fs.stat(path, (err, file) => {
if (err) {
return reject(err);
}
return resolve(file.isDirectory());
});
});
/**
* Load the contents of the YAML file.
* @param path Absolute to the yaml file.
*/
export const loadFileContents = (path: string) =>
new Promise<string>(async (resolve, reject) => {
let pathIsDirectory = await isDir(path);
if (pathIsDirectory) {
return reject(new Error(`Cannot open a directory @ ${chalk.bold.red(path)}`));
}
else {
fs.readFile(path, 'UTF8', (err, contents) => {
try {
if (err) {
return reject(err);
}
return resolve(contents);
}
catch (err) {
reject(err);
}
});
}
});
/**
* Check the file path against validations and return a 'File' object.
* @param fullPath An absolute path to the file.
* @param root An absolute path to the root directory.
*/
export function getFileMetadata(fullPath: string, root: string): SnippetFileInput {
/* Determine the platform as windows uses '\' where as linux uses '/' */
const delimiter = os.platform() === 'win32' ? '\\' : '/';
/* Get the relative path to the file from the root directory '/' */
const relativePath = path.relative(root, fullPath);
/* Extract the required properties */
let [file_name, group, host, ...additional] = relativePath.split(delimiter).reverse();
/* Additional must be null or empty */
if (additional && additional.length > 0) {
throw new Error(`Invalid folder structure at ${chalk.bold.red(relativePath)}.File ${chalk.bold.yellow(name)} was located too deep.`);
}
if (host == null) {
host = group;
}
host = host.toLowerCase();
return {
relativePath: relativePath,
fullPath,
isPublic: !(/[\\/]private-samples$/.test(root)),
host,
group,
file_name
};
}
/**
* Recurrsively crawl through a folder and return all the files in it.
* @param root An absolute path to the directory.
*/
export function getFiles(root: string): SnippetFileInput[] {
let files: SnippetFileInput[] = [];
syncRecurseThroughDirectory(root);
return files;
// Helper
function syncRecurseThroughDirectory(dir: string) {
fs.readdirSync(dir)
.forEach(file => {
const fullPath = path.join(dir, file);
const withoutExt = file.replace('.yaml', '');
/* Check for file/folder naming guidelines */
if (!followsNamingGuidelines(withoutExt)) {
throw new Error(`Invalid name at ${chalk.bold.red(fullPath)}. Name must only contain lowercase letters, numbers, and hyphens.`);
}
if (fs.statSync(fullPath).isDirectory()) {
syncRecurseThroughDirectory(fullPath);
} else {
files.push(getFileMetadata(fullPath, root));
}
});
}
}
/**
Naming guidelines: only allow lowercase letters, numbers, and hyphens
OK:
sample
sample-with-hyphen
sample-es5
BAD:
sample with space
Any-uppercase
anyWhere
or_underscores
or.dots
$likethistoo
*/
export function followsNamingGuidelines(name: string) {
return /^[a-z0-9\-]+$/.test(name);
}
/** Determines if a name is really just a 25-character machine-generated ID */
export function isCUID(id: string) {
if (id.trim().length === 25 && id.indexOf('_') === -1) {
// not likely to be a real id, with a name of that precise length and all as one word.
return true;
}
return false;
}
export function pause(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}