nx/scripts/documentation/nx-dev-docs-latest-sync.ts
Benjamin Cabanes 848436bca5
docs(nxdev): master docs is latest (#6975)
* docs(nxdev): master docs is latest

* docs(nxdev): update latest documentation

* docs(nxdev): removing NX ROOT
2021-09-13 13:55:18 +00:00

77 lines
1.9 KiB
TypeScript

import * as chalk from 'chalk';
import * as yargs from 'yargs';
import { watch } from 'chokidar';
import * as shell from 'shelljs';
import { BehaviorSubject } from 'rxjs';
import { debounceTime, filter, switchMapTo, tap } from 'rxjs/operators';
/**
* Available colours
*/
const { bgGreen, white } = chalk;
const argv = yargs
.command(
'Usage: $0',
'Sync the public latest folder with the /docs folder one time'
)
.example(
'$0 --watch',
'Sync the public latest folder with the /docs folder whenever changes are done'
)
.option('watch', {
alias: 'w',
demandOption: false,
type: 'boolean',
description: 'Enable the watch mode',
}).argv;
function sync(): void {
return shell.exec(
'rsync -avrR --delete ./docs/./ ./nx-dev/nx-dev/public/documentation/latest',
{ sync: true }
);
}
function main(isWatched: boolean) {
if (isWatched) {
const isReady$ = new BehaviorSubject(false);
const syncR$ = new BehaviorSubject(null);
/**
* If we do not debounce, the sync will happen for every file detect by the watcher
*/
isReady$
.pipe(
filter((isReady) => isReady),
tap(() =>
console.log(
bgGreen(
white(
' => DOCS SYNC ENABLED & READY: You can modify `/docs`, changes will be synced '
)
)
)
),
switchMapTo(syncR$),
debounceTime(1000)
)
.subscribe(() => sync());
return watch('./docs', {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
awaitWriteFinish: true,
})
.on('ready', () => isReady$.next(true))
.on('add', (path) => syncR$.next(path))
.on('addDir', (path) => syncR$.next(path))
.on('change', (path) => syncR$.next(path))
.on('unlink', (path) => syncR$.next(path));
}
return sync();
}
main(argv.watch as boolean);