| [9af201e] | 1 | import * as fs from "fs";
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * Typing for the fields of package.json we care about
|
|---|
| 5 | */
|
|---|
| 6 | export interface PackageJson {
|
|---|
| 7 | [key: string]: string;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * A function that json from a file
|
|---|
| 12 | */
|
|---|
| 13 | export interface ReadJsonSync {
|
|---|
| 14 | // tslint:disable-next-line:no-any
|
|---|
| 15 | (packageJsonPath: string): any | undefined;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | export interface FileExistsSync {
|
|---|
| 19 | (name: string): boolean;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | export interface FileExistsAsync {
|
|---|
| 23 | (path: string, callback: (err?: Error, exists?: boolean) => void): void;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | export interface ReadJsonAsyncCallback {
|
|---|
| 27 | // tslint:disable-next-line:no-any
|
|---|
| 28 | (err?: Error, content?: any): void;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | export interface ReadJsonAsync {
|
|---|
| 32 | (path: string, callback: ReadJsonAsyncCallback): void;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | export 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
|
|---|
| 50 | export function readJsonFromDiskSync(packageJsonPath: string): any | undefined {
|
|---|
| 51 | if (!fs.existsSync(packageJsonPath)) {
|
|---|
| 52 | return undefined;
|
|---|
| 53 | }
|
|---|
| 54 | return require(packageJsonPath);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | export 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 |
|
|---|
| 72 | export 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 |
|
|---|
| 85 | export function removeExtension(path: string): string {
|
|---|
| 86 | return path.substring(0, path.lastIndexOf(".")) || path;
|
|---|
| 87 | }
|
|---|