-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathfsAngular.ts
More file actions
91 lines (87 loc) · 2.82 KB
/
fsAngular.ts
File metadata and controls
91 lines (87 loc) · 2.82 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
import {watch} from 'chokidar';
import {copy, remove} from 'fs-extra';
import {join} from 'path';
import {Observable} from 'rxjs';
import {debounceTime, filter, tap} from 'rxjs/operators';
import {restartStaticServer, startScullyWatchMode} from '../watchMode';
import {baseFilter} from './cli-options';
import {scullyConfig} from './config';
import {createFolderFor} from './createFolderFor';
import {green, log, logWarn} from './log';
import {createOptimisticUniqueName} from 'typescript';
import {existFolder} from './fsFolder';
export async function checkChangeAngular(
folder = join(scullyConfig.homeFolder, scullyConfig.distFolder) ||
join(scullyConfig.homeFolder, './dist/browser'),
reset = true,
// tslint:disable-next-line:no-shadowed-variable
watch = false
) {
reWatch(folder, reset, watch);
}
// tslint:disable-next-line:no-shadowed-variable
function reWatch(folder, reset = true, watch = false) {
const filename = join(folder);
// watching 1 folder level above.
watchFolder(join(folder, '../'))
.pipe(
// TODO test on mac, figure out what's coming in.
// only act upon changes of the actual folder I'm interested in.
filter(r => r.fileName.includes(filename)),
filter(r => !r.fileName.includes('scully-routes.json')),
/** give the CLI some time to finnish */
debounceTime(1000),
// tap(console.log),
tap(() => moveDistAngular(folder, scullyConfig.outDir, {reset}, watch))
// take(2)
)
.subscribe({
complete: async () => {
// await moveDistAngular(folder, scullyConfig.outDir);
},
});
}
function watchFolder(folder): Observable<{eventType: string; fileName: string}> {
return new Observable(obs => {
let watcher;
try {
watcher = watch(folder).on('all', (eventType, fileName) => {
obs.next({eventType, fileName});
});
} catch (e) {
obs.error(e);
}
return () => watcher.close();
});
}
// tslint:disable-next-line:no-shadowed-variable
export async function moveDistAngular(src, dest, {reset = true, removeStaticDist = false}, watch = false) {
try {
// delete files
if (removeStaticDist) {
logWarn(`Cleaned up ${dest} folder.`);
await remove(dest);
}
// make sure the static folder exists
createFolderFor(dest);
await copy(src, dest);
const source404 = join(src, 'index.html');
const page404 = join(dest, '404.html');
if (!existFolder(page404)) {
/** only add a 404 if there isn't one */
await copy(source404, page404);
}
log(`${green(` ☺ `)} new Angular build imported`);
if (reset) {
restartStaticServer();
} else if (watch) {
startScullyWatchMode(baseFilter);
}
} catch (e) {
/**
* Ignore errors.
* Those are probably cause by too fast iterations
* TODO: make sure this is solid
*/
}
}