This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathfile-utility.js
122 lines (100 loc) · 3.11 KB
/
file-utility.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
/*
After specifying the output folder for the Typescript compiler it was noted that the compiler does not copy
the accompany html & css files into the output folder(s). This utility program creates a folder structure similar to
the one Typescript would have created and copies the html and css files into the relevant folder.
*/
const fs = require('fs');
const chokidar = require('chokidar');
const watcher = chokidar.watch('src/app');
/**
* Gets paths of files in a directory
*
* @param directory path to the current directory
* @returns {Array} list of paths to files in the directory
*/
function getFilePaths(directory) {
const filePaths = [];
fs.readdirSync(directory)
.forEach((fileInDirectory) => {
const extension = fileInDirectory.split('.').pop();
if (extension === 'html' || extension === 'css') {
const pathToFile = directory + fileInDirectory;
filePaths.push(pathToFile);
}});
return filePaths;
}
/**
* Gets paths of child directories
* @param rootDir path to directory
* @returns {string[]} paths of child directories
*/
function getDirPaths(rootDir) {
return fs.readdirSync(rootDir)
.filter((childDir) => {
const path = rootDir + childDir;
const isDir = fs.lstatSync(path).isDirectory();
if (isDir) return path;
})
}
let paths = [];
/**
* Gets paths to all hmtl & css files in the root directory and child directories recursively.
*
* @param rootDir path to the root directory.
*/
function getPaths(rootDir) {
const files = getFilePaths(rootDir);
const dirs = getDirPaths(rootDir);
const hasFiles = files.length > 1;
const hasDirs = dirs.length > 1;
if (hasFiles) {
paths = paths.concat(files);
}
if (hasDirs) {
dirs.forEach((directory) => {
const newPath = rootDir + directory + '/';
getPaths(newPath)
})
}
}
/*
Typescript creates a similar folder structure as this function. The reason why we have to manually create these folders
is that this script is run before typescript has compiled. We need these folders to copy html and css files into.
*/
function createDirs() {
const src = 'src/app/';
const compilerOutput = 'src/compiler-output';
getPaths(src);
if (!fs.existsSync(compilerOutput)){
fs.mkdirSync(compilerOutput);
}
paths.forEach((path) => {
const dest = path.replace('src/app/', 'src/compiler-output/app/');
let dir = dest.split('/');
dir.pop();
dir = dir.join('/');
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
})
}
/*
The challenge with specifying the output folder for the typescript compiler is that Typescript does not output
the companion html and css files into these folders as well. So we have to copy these files into the directories
Typescript outputs to.
*/
function copyFiles() {
paths.forEach((path) => {
const dest = path.replace('src/app/', 'src/compiler-output/app/');
fs.copyFileSync(path, dest);
})
}
createDirs();
copyFiles();
// Watches for changes and copies the css & html files
function watch() {
watcher
.on('add', () => copyFiles())
.on('change', () => copyFiles());
}
watch();