source: frontend/node_modules/tsconfig-paths/src/filesystem.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[9af201e]1import * as fs from "fs";
2
3/**
4 * Typing for the fields of package.json we care about
5 */
6export interface PackageJson {
7 [key: string]: string;
8}
9
10/**
11 * A function that json from a file
12 */
13export interface ReadJsonSync {
14 // tslint:disable-next-line:no-any
15 (packageJsonPath: string): any | undefined;
16}
17
18export interface FileExistsSync {
19 (name: string): boolean;
20}
21
22export interface FileExistsAsync {
23 (path: string, callback: (err?: Error, exists?: boolean) => void): void;
24}
25
26export interface ReadJsonAsyncCallback {
27 // tslint:disable-next-line:no-any
28 (err?: Error, content?: any): void;
29}
30
31export interface ReadJsonAsync {
32 (path: string, callback: ReadJsonAsyncCallback): void;
33}
34
35export function fileExistsSync(path: string): boolean {
36 try {
37 const stats = fs.statSync(path);
38 return stats.isFile();
39 } catch (err) {
40 // If error, assume file did not exist
41 return false;
42 }
43}
44
45/**
46 * Reads package.json from disk
47 * @param file Path to package.json
48 */
49// tslint:disable-next-line:no-any
50export function readJsonFromDiskSync(packageJsonPath: string): any | undefined {
51 if (!fs.existsSync(packageJsonPath)) {
52 return undefined;
53 }
54 return require(packageJsonPath);
55}
56
57export function readJsonFromDiskAsync(
58 path: string,
59 // tslint:disable-next-line:no-any
60 callback: (err?: Error, content?: any) => void
61): void {
62 fs.readFile(path, "utf8", (err, result) => {
63 // If error, assume file did not exist
64 if (err || !result) {
65 return callback();
66 }
67 const json = JSON.parse(result);
68 return callback(undefined, json);
69 });
70}
71
72export function fileExistsAsync(
73 path2: string,
74 callback2: (err?: Error, exists?: boolean) => void
75): void {
76 fs.stat(path2, (err: Error, stats: fs.Stats) => {
77 if (err) {
78 // If error assume file does not exist
79 return callback2(undefined, false);
80 }
81 callback2(undefined, stats ? stats.isFile() : false);
82 });
83}
84
85export function removeExtension(path: string): string {
86 return path.substring(0, path.lastIndexOf(".")) || path;
87}
Note: See TracBrowser for help on using the repository browser.