Alex Rickabaugh d442b6855f feat(service-worker): introduce the @angular/service-worker package (#19274)
This service worker is a conceptual derivative of the existing @angular/service-worker maintained at github.com/angular/mobile-toolkit, but has been rewritten to support use across a much wider variety of applications.

Entrypoints include:

@angular/service-worker: a library for use within Angular client apps to communicate with the service worker.
@angular/service-worker/gen: a library for generating ngsw.json files from glob-based SW config files.
@angular/service-worker/ngsw-worker.js: the bundled service worker script itself.
@angular/service-worker/ngsw-cli.js: a CLI tool for generating ngsw.json files from glob-based SW config files.
2017-09-28 16:18:12 -07:00

42 lines
1.4 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Filesystem} from '@angular/service-worker/config';
const fs = require('fs');
const path = require('path');
export class NodeFilesystem implements Filesystem {
constructor(private base: string) {}
async list(_path: string): Promise<string[]> {
const dir = this.canonical(_path);
const entries = fs.readdirSync(dir).map(
(entry: string) => ({entry, stats: fs.statSync(path.join(dir, entry))}));
const files = entries.filter((entry: any) => !entry.stats.isDirectory())
.map((entry: any) => path.join(_path, entry.entry));
return entries.filter((entry: any) => entry.stats.isDirectory())
.map((entry: any) => path.join(_path, entry.entry))
.reduce(
async(list: string[], subdir: string) => (await list).concat(await this.list(subdir)),
Promise.resolve(files));
}
async read(_path: string): Promise<string> {
const file = this.canonical(_path);
return fs.readFileSync(file).toString();
}
async write(_path: string, contents: string): Promise<void> {
const file = this.canonical(_path);
fs.writeFileSync(file, contents);
}
private canonical(_path: string): string { return path.join(this.base, _path); }
}