TypeScript read file

60 TypeScript code examples are found related to "read file".
Example 1
Source File: FilesystemHelper.ts    From vscode-alxmldocumentation with MIT License 8 votes vote down vote up
/**
     * Read file content.
     * @param filePath Path to file
     * @returns File content.
     */
    public static ReadFile(filePath: string): string {
        if (!this.FileExists(filePath)) {
            return '';
        }
        return fs.readFileSync(filePath, 'utf8');
    }
Example 2
Source File: GenericUtils.ts    From bluebubbles-server with Apache License 2.0 8 votes vote down vote up
readFile = async (file: Blob): Promise<string> => {
    return await new Promise((resolve, reject) => {
        const reader = new FileReader();

        reader.onabort = () => {
            reject(new Error('File reading was aborted'));
        };
        reader.onerror = () => {
            reject(new Error(`File reading failed: ${reader?.error?.message}`));
        };

        reader.onload = () => {
            resolve(reader.result as string);
        };

        reader.readAsText(file);
    });
}
Example 3
Source File: utils.ts    From kliveide with MIT License 8 votes vote down vote up
/**
 * Reads the text of the specified file
 * @param filename File name
 * @param Handles UTF-8 with and without BOM header
 */
 export function readTextFile(filename: string): string {
  const sourceText = fs.readFileSync(filename, "utf8");
  if (sourceText.length < 4) {
    return sourceText;
  }
  return sourceText.charCodeAt(0) >= 0xbf00 ? sourceText.substr(1) : sourceText;
}
Example 4
Source File: util.ts    From openapi-cop with MIT License 8 votes vote down vote up
// tslint:disable-next-line:no-any
export function readJsonOrYamlSync(filePath: string): any {
  switch (path.extname(filePath)) {
    case '.json':
      return JSON.parse(fs.readFileSync(filePath, 'utf8'));
    case '.yaml':
    case '.yml':
      return yaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
    case '.':
      throw new Error('Will not convert a file that has no extension.');
    default:
      throw new Error('Wrong file extension.');
  }
}
Example 5
Source File: json-signer.ts    From pix-qrcode-utils with MIT License 8 votes vote down vote up
static async readPFX( filePath: string, passphrase: string ) {
    const result = new Promise<Key>( (resolve, reject) => {
      readPkcs12(
        readFileSync( filePath ),
        { p12Password: passphrase },
        (err,pfx) => {
          if ( err ) {
            reject( err );
          }

          let k: Key = asKey( pfx.key )

          resolve( k );
        }
      );
    });

    return result;
  }
Example 6
Source File: semantic-model-provider.ts    From ui5-language-assistant with Apache License 2.0 8 votes vote down vote up
export async function readTestLibraryFile(
  version: string,
  fileName: string
): Promise<FetchResponse> {
  try {
    // version might not actually be a TestModelVersion but we'll return ok === false in that case
    // since the file won't exist
    const inputFolder = getModelFolder(version as TestModelVersion);
    const filePath = resolve(inputFolder, fileName);
    const ok = existsSync(filePath);
    return {
      ok: ok,
      json: (): Promise<unknown> => readJson(filePath),
    };
  } catch (error) {
    return {
      ok: false,
      json: (): never => {
        throw error;
      },
    };
  }
}
Example 7
Source File: read-file-or-url.ts    From graphql-mesh with MIT License 8 votes vote down vote up
export async function readFileOrUrl<T>(filePathOrUrl: string, config?: ReadFileOrUrlOptions): Promise<T> {
  if (isUrl(filePathOrUrl)) {
    config?.logger?.debug(`Fetching ${filePathOrUrl} via HTTP`);
    return readUrl(filePathOrUrl, config);
  } else if (filePathOrUrl.startsWith('{') || filePathOrUrl.startsWith('[')) {
    return JSON.parse(filePathOrUrl);
  } else {
    config?.logger?.debug(`Reading ${filePathOrUrl} from the file system`);
    return readFile(filePathOrUrl, config);
  }
}
Example 8
Source File: read-file-or-url.ts    From graphql-mesh with MIT License 8 votes vote down vote up
export async function readFile<T>(fileExpression: string, config?: ReadFileOrUrlOptions): Promise<T> {
  const { allowUnknownExtensions, cwd, fallbackFormat, importFn = defaultImportFn } = config || {};
  const [filePath] = fileExpression.split('#');
  if (/js$/.test(filePath) || /ts$/.test(filePath)) {
    return loadFromModuleExportExpression<T>(fileExpression, {
      cwd,
      importFn,
      defaultExportName: 'default',
    });
  }
  const actualPath = pathModule.isAbsolute(filePath) ? filePath : pathModule.resolve(cwd || process.cwd(), filePath);
  const rawResult = await fs.promises.readFile(actualPath, 'utf-8');
  if (/json$/.test(actualPath)) {
    return JSON.parse(rawResult);
  }
  if (/yaml$/.test(actualPath) || /yml$/.test(actualPath)) {
    return loadYaml(actualPath, rawResult, config?.logger);
  } else if (fallbackFormat) {
    switch (fallbackFormat) {
      case 'json':
        return JSON.parse(rawResult);
      case 'yaml':
        return loadYaml(actualPath, rawResult, config?.logger);
      case 'ts':
      case 'js':
        return importFn(actualPath);
    }
  } else if (!allowUnknownExtensions) {
    throw new Error(
      `Failed to parse JSON/YAML. Ensure file '${filePath}' has ` +
        `the correct extension (i.e. '.json', '.yaml', or '.yml).`
    );
  }
  return rawResult as unknown as T;
}
Example 9
Source File: FileStorage.ts    From beacon-sdk with MIT License 8 votes vote down vote up
/* eslint-disable prefer-arrow/prefer-arrow-functions */

export function readLocalFile(): Promise<JsonObject> {
  return new Promise((resolve: (_: JsonObject) => void, reject: (error: unknown) => void): void => {
    readFile(file, { encoding: 'utf8' }, (fileReadError: unknown, fileContent: string) => {
      if (fileReadError) {
        reject(fileReadError)
      }
      try {
        const json: JsonObject = JSON.parse(fileContent)
        resolve(json)
      } catch (jsonParseError) {
        reject(jsonParseError)
      }
    })
  })
}
Example 10
Source File: mock-utils.ts    From airnode with MIT License 8 votes vote down vote up
mockReadFileSync = (filePathSubstr: string, mockValue: string) => {
  return jest.spyOn(fs, 'readFileSync').mockImplementation((...args) => {
    const path = args[0].toString();
    if (path.includes(filePathSubstr)) {
      return mockValue;
    }
    return originalFs(...args);
  });
}
Example 11
Source File: mock-utils.ts    From airnode with MIT License 8 votes vote down vote up
mockReadFileSync = (filePathSubstr: string, mockValue: string) => {
  return jest.spyOn(fs, 'readFileSync').mockImplementation((...args) => {
    const path = args[0].toString();
    if (path.includes(filePathSubstr)) {
      return mockValue;
    }
    return originalFs(...args);
  });
}
Example 12
Source File: upload-hobo-data.ts    From aqualink-app with MIT License 8 votes vote down vote up
readCoordsFile = (rootPath: string, siteIds: number[]) => {
  // Read coords file
  const coordsFilePath = path.join(rootPath, COLONY_COORDS_FILE);
  const coordsHeaders = ['site', 'colony', 'lat', 'long'];
  const castFunction = castCsvValues(['site', 'colony'], ['lat', 'long'], []);
  return parseCSV<Coords>(coordsFilePath, coordsHeaders, castFunction).filter(
    (record) => {
      return siteIds.includes(record.site);
    },
  );
}
Example 13
Source File: events.ts    From eventcatalog with MIT License 8 votes vote down vote up
readMarkdownFile = (pathToFile: string) => {
  const file = fs.readFileSync(pathToFile, {
    encoding: 'utf-8',
  });
  return {
    parsed: matter(file),
    raw: file,
  };
}
Example 14
Source File: services.ts    From eventcatalog with MIT License 8 votes vote down vote up
readMarkdownFile = (pathToFile: string) => {
  const file = fs.readFileSync(pathToFile, {
    encoding: 'utf-8',
  });
  return {
    parsed: matter(file),
    raw: file,
  };
}
Example 15
Source File: file.ts    From omegga with ISC License 8 votes vote down vote up
// read cached json
export function readCachedJSON(file: string, expire = 5000) {
  const now = Date.now();
  if (cachedTimes[file] && cachedTimes[file] + expire > now)
    return cachedJSON[file];

  // update the cache with data
  return updateJSONCache(file);
}
Example 16
Source File: file.ts    From omegga with ISC License 8 votes vote down vote up
export function readWatchedJSON(file: string) {
  // if the file is already being watched, return the watched json
  if (watchers[file]) return cachedJSON[file];

  // check if the file exists
  if (!fs.existsSync(file)) return undefined;

  // create a watcher (no persistence means the process dies even if there's still a watcher)
  const watcher = chokidar.watch(file, { persistent: false });
  watchers[file] = watcher;

  const read = _.debounce(() => updateJSONCache(file), 500);

  // add listeners to the watcher
  watcher
    .on('add', () => read()) // on add, update the cache
    .on('change', () => read()) // on change, update the cache
    .on('unlink', () => {
      // on unlink (delete), destroy value in cache
      cachedJSON[file] = undefined;
      cachedTimes[file] = Date.now();
    });

  return updateJSONCache(file);
}
Example 17
Source File: fs-utils.ts    From utopia with MIT License 8 votes vote down vote up
export async function readFileAsUTF8(
  path: string,
): Promise<{ content: string; unsavedContent: string | null }> {
  const { content, unsavedContent } = await getFile(path)
  return {
    content: decoder.decode(content),
    unsavedContent: unsavedContent == null ? null : decoder.decode(unsavedContent),
  }
}
Example 18
Source File: validator.ts    From baleen3 with Apache License 2.0 8 votes vote down vote up
readUploadedFileAsText = async (
  inputFile: File
): Promise<string | ArrayBuffer | null> => {
  const temporaryFileReader = new FileReader()

  return new Promise((resolve, reject) => {
    temporaryFileReader.onerror = (): void => {
      temporaryFileReader.abort()
      reject(new DOMException('Unable to read file.'))
    }

    temporaryFileReader.onload = (): void => {
      resolve(temporaryFileReader.result)
    }
    temporaryFileReader.readAsText(inputFile)
  })
}
Example 19
Source File: utils.ts    From nova-plugin with MIT License 8 votes vote down vote up
/**
 * Reads content of given file. If `size` if given, reads up to `size` bytes
 */
export async function readFile(filePath: string, size?: number): Promise<ArrayBuffer> {
    if (isURL(filePath)) {
        const headers: HeadersInit = {};
        if (size) {
            headers.Range = `bytes=0-${size}`;
        }
        const req = await fetch(filePath, { headers });
        return await req.arrayBuffer();
    }

    const file = nova.fs.open(filePath, 'rb');
    const data = (size ? file.read(size) : file.read()) as ArrayBuffer;
    file.close();
    return data;
}
Example 20
Source File: file-utils.ts    From eta with MIT License 8 votes vote down vote up
/**
 * Reads a file synchronously
 */

function readFile(filePath: string): string {
  try {
    return readFileSync(filePath).toString().replace(_BOM, "") // TODO: is replacing BOM's necessary?
    ;
  } catch {
    throw EtaErr("Failed to read template at '" + filePath + "'");
  }
}
Example 21
Source File: file-utils.ts    From eta with MIT License 8 votes vote down vote up
/**
 * Reads a file synchronously
 */

function readFile(filePath: string): string {
  try {
    return readFileSync(filePath).toString().replace(_BOM, '') // TODO: is replacing BOM's necessary?
  } catch {
    throw EtaErr("Failed to read template at '" + filePath + "'")
  }
}
Example 22
Source File: headers.ts    From bee-js with BSD 3-Clause "New" or "Revised" License 8 votes vote down vote up
export function readFileHeaders(headers: Headers): FileHeaders {
  const name = readContentDispositionFilename(headers.get('content-disposition'))
  const tagUid = readTagUid(headers.get('swarm-tag-uid'))
  const contentType = headers.get('content-type') || undefined

  return {
    name,
    tagUid,
    contentType,
  }
}
Example 23
Source File: file.ts    From fakesharper with MIT License 8 votes vote down vote up
/**
 * Synchronously reads the entire contents of a file.
 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
 * URL support is _experimental_.
 * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
 */
export function readFileSync(path: string): string {
	let data: string = fs.readFileSync(path).toString();

	return removeBOM(data);
}
Example 24
Source File: core-util.ts    From attranslate with MIT License 8 votes vote down vote up
export async function readTFileCore(
  fileFormat: TFileType,
  args: ReadTFileArgs
): Promise<TSet> {
  const module = await instantiateTFileFormat(fileFormat);
  const rawTSet = await module.readTFile(args);

  const tSet: TSet = new Map();
  const keyRegExp = new RegExp(args.keySearch, "g");
  rawTSet.forEach((value, key) => {
    const replacedKey = key.replace(keyRegExp, args.keyReplace);
    tSet.set(replacedKey, value);
  });
  return tSet;
}
Example 25
Source File: file.ts    From form-schematic with MIT License 8 votes vote down vote up
readIntoSourceFile = (host: Tree, modulePath: string) => {
	const text = host.read(modulePath);
	if (text === null) {
		throw new SchematicsException(`File ${modulePath} does not exist.`);
	}

	return ts.createSourceFile(
		modulePath,
		text.toString('utf-8'),
		ts.ScriptTarget.Latest,
		true
	);
}
Example 26
Source File: leanpkg.ts    From vscode-lean4 with Apache License 2.0 8 votes vote down vote up
// Return file contents with whitespace normalized.
    private async readWhitespaceNormalized(fileUri: Uri) : Promise<string> {
        try{
            const contents = (await workspace.fs.readFile(fileUri)).toString();
            // ignore whitespace changes by normalizing whitespace.
            const re = /[ \t\r\n]+/g
            const result = contents.replace(re, ' ');
            return result.trim();
        }
        catch(ex) {
            // In case there is an error in the read
            return '';
        }
    }
Example 27
Source File: projectInfo.ts    From vscode-lean4 with Apache License 2.0 8 votes vote down vote up
async function readLeanVersionFile(packageFileUri : Uri) : Promise<string> {
    const url = new URL(packageFileUri.toString());
    const tomlFileName = 'leanpkg.toml';
    if (packageFileUri.scheme !== 'file'){
        return '';
    }

    if (packageFileUri.path.endsWith(tomlFileName))
    {
        const data = (await fs.promises.readFile(url, {encoding: 'utf-8'})).trim();
        if (data) {
            const match = /lean_version\s*=\s*"([^"]*)"/.exec(data);
            if (match) return match[1].trim();
        }

    } else {
        // must be a lean-toolchain file, these are much simpler they only contain a version.
        return (await fs.promises.readFile(url, {encoding: 'utf-8'})).trim();

    }
    return '';

}
Example 28
Source File: file-handler.ts    From karma-test-explorer with MIT License 8 votes vote down vote up
public readFileSync(filePath: string, encoding?: BufferEncoding): string | undefined {
    this.logger.debug(() => `Reading file synchronously: ${filePath}`);
    let fileContents: string | undefined;

    try {
      fileContents = readFileSync(filePath, encoding ?? this.fileEncoding);
    } catch (error) {
      this.logger.error(() => `Failed reading file ${filePath}: ${error}`);
    }
    return fileContents;
  }
Example 29
Source File: file-handler.ts    From karma-test-explorer with MIT License 8 votes vote down vote up
public async readFile(filePath: string, encoding?: BufferEncoding): Promise<string> {
    this.logger.debug(() => `Reading file async: ${filePath}`);
    const deferredFileContents = new DeferredPromise<string>();

    readFile(filePath, encoding ?? this.fileEncoding, (error, data) => {
      if (error) {
        this.logger.error(() => `Failed reading file ${filePath}: ${error}`);
        deferredFileContents.reject(new Error(`Failed to read file '${filePath}': ${error}`));
      } else {
        this.logger.trace(() => `Done reading file ${filePath}: ${error}`);
        deferredFileContents.fulfill(data?.toString());
      }
    });
    return deferredFileContents.promise();
  }
Example 30
Source File: utils.ts    From vscode-rss with MIT License 8 votes vote down vote up
export function readFile(path: string) {
    return new Promise<string>((resolve, reject) => {
        fs.readFile(path, (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data.toString('utf-8'));
            }
        });
    });
}
Example 31
Source File: commonUtils.ts    From mandarinets with MIT License 8 votes vote down vote up
public static readFile(filePath: string, decoderType?: string): any {
        if(decoderType == (null || undefined)) decoderType = "utf-8";

        let decoder;
        try {
            decoder = new TextDecoder(decoderType);
        } catch(error) {
            decoder = new TextDecoder();
        }

        const data = Leaf.readFileSync(filePath);
        return decoder.decode(data);
    }
Example 32
Source File: io.ts    From DefinitelyTyped-tools with MIT License 8 votes vote down vote up
/** If a file doesn't exist, warn and tell the step it should have been generated by. */
export async function readFileAndWarn(generatedBy: string, filePath: string): Promise<object> {
  try {
    return await readJson(filePath, isObject);
  } catch (e) {
    console.error(`Run ${generatedBy} first!`);
    throw e;
  }
}
Example 33
Source File: read-properties-file.ts    From relate with GNU General Public License v3.0 8 votes vote down vote up
export async function readPropertiesFile(path: string): Promise<PropertyEntries> {
    const conf = await readFile(path, 'utf8');
    const lines = map(split(conf, NEW_LINE), trim);

    return map(lines, (line): [string, string] => {
        const [key, ...rest] = split(line, PROPERTIES_SEPARATOR);

        return [key, join(rest, PROPERTIES_SEPARATOR)];
    });
}
Example 34
Source File: fs-utils.ts    From open-design-sdk with Apache License 2.0 8 votes vote down vote up
export async function readJsonFile(
  filename: string,
  options: {
    cancelToken?: CancelToken | null
  } = {}
): Promise<unknown> {
  const readFile = promisify(fs.readFile)
  const json = await readFile(filename, 'utf8')
  options.cancelToken?.throwIfCancelled()

  return JSON.parse(json)
}
Example 35
Source File: fs-utils.ts    From open-design-sdk with Apache License 2.0 8 votes vote down vote up
export async function readFileBlob(
  filename: string,
  options: {
    cancelToken?: CancelToken | null
  } = {}
): Promise<Buffer> {
  const readFile = promisify(fs.readFile)
  const result = await readFile(filename)
  options.cancelToken?.throwIfCancelled()

  return result
}
Example 36
Source File: auth-utils.ts    From plasmic with MIT License 8 votes vote down vote up
export function readAuth(authFile: string) {
  if (!existsBuffered(authFile)) {
    throw new HandledError(`No Plasmic auth file found at ${authFile}`);
  }
  try {
    const parsed = JSON.parse(readFileText(authFile)) as AuthConfig;
    // Strip trailing slashes.
    return {
      ...parsed,
      host: parsed.host.replace(/\/+$/, ""),
    };
  } catch (e) {
    logger.error(
      `Error encountered reading plasmic credentials at ${authFile}: ${e}`
    );
    throw e;
  }
}
Example 37
Source File: config-utils.ts    From plasmic with MIT License 8 votes vote down vote up
export function readConfig(
  configFile: string,
  autoFillDefaults: boolean
): PlasmicConfig {
  if (!existsBuffered(configFile)) {
    const err = new HandledError(
      `No Plasmic config file found at ${configFile}`
    );
    throw err;
  }
  try {
    const result = JSON.parse(readFileText(configFile!)) as PlasmicConfig;
    return autoFillDefaults ? fillDefaults(result) : result;
  } catch (e) {
    logger.error(
      `Error encountered reading ${CONFIG_FILE_NAME} at ${configFile}: ${e}`
    );
    throw e;
  }
}
Example 38
Source File: file-utils.ts    From plasmic with MIT License 8 votes vote down vote up
export function readFileText(path: string): string {
  if (buffering) {
    const action = buffer.get(path);
    if (action) {
      switch (action.type) {
        case "create":
          return ensureString(action.content);
        case "rename":
          return readFileText(action.newPath);
        case "delete":
          throw new HandledError("File does not exists");
      }
    }
  }

  // eslint-disable-next-line no-restricted-properties
  return fs.readFileSync(path, "utf8");
}
Example 39
Source File: get-context.ts    From plasmic with MIT License 8 votes vote down vote up
export function readLock(lockFile: string): PlasmicLock {
  if (!existsBuffered(lockFile)) {
    return createPlasmicLock();
  }
  try {
    const result = JSON.parse(readFileText(lockFile!)) as PlasmicLock;
    return {
      ...result,
    };
  } catch (e) {
    logger.error(
      `Error encountered reading ${LOCK_FILE_NAME} at ${lockFile}: ${e}`
    );
    throw e;
  }
}
Example 40
Source File: files.ts    From slice-machine with Apache License 2.0 8 votes vote down vote up
function readEntity<T>(
  pathToFile: string,
  validate: (payload: unknown) => Error | T
): Error | T {
  const entity = safeReadJson(pathToFile);
  if (entity) {
    return validate(entity);
  }
  return new Error(`Could not parse file "${path.basename(pathToFile)}"`);
}
Example 41
Source File: files.ts    From slice-machine with Apache License 2.0 8 votes vote down vote up
function safeReadEntity<T>(
  pathToFile: string,
  validate: (payload: unknown) => null | T
): null | T {
  try {
    const result = readEntity(pathToFile, validate);
    if (result instanceof Error) return null;
    return result;
  } catch (e) {
    return null;
  }
}
Example 42
Source File: files.ts    From slice-machine with Apache License 2.0 8 votes vote down vote up
function readFirstOf<
  V,
  O extends Record<string, unknown> = Record<string, never>
>(filePaths: ReadonlyArray<{ path: string; options?: O } | string>) {
  return (
    converter: (value: string) => V
  ): ({ path: string; value: V } & O) | undefined => {
    return filePaths.reduce(
      (
        acc: ({ path: string; value: V } & O) | undefined,
        filePath: { path: string; options?: O } | string
      ) => {
        if (acc) return acc;
        else {
          const pathWithOpts =
            typeof filePath === "string" ? { path: filePath } : filePath;

          if (exists(pathWithOpts.path)) {
            const optsOrDefault = pathWithOpts.options || ({} as O);

            const test: { path: string; value: V } & O = {
              path: pathWithOpts.path,
              ...optsOrDefault,
              value: converter(readString(pathWithOpts.path)),
            };
            return test;
          } else return acc;
        }
      },
      undefined
    );
  };
}
Example 43
Source File: fileSystem.ts    From hermes-profile-transformer with MIT License 8 votes vote down vote up
readFileAsync = async (path: string): Promise<any> => {
  try {
    const readFileAsync = promisify(readFile);
    const fileString: string = (await readFileAsync(path, 'utf-8')) as string;
    if (fileString.length === 0) {
      throw new Error(`${path} is an empty file`);
    }
    const obj = JSON.parse(fileString);
    return obj;
  } catch (err) {
    throw err;
  }
}
Example 44
Source File: utils.ts    From drake with MIT License 8 votes vote down vote up
/** Read the entire contents of a file synchronously to a UTF-8 string. */
export function readFile(filename: string): string {
  try {
    const result = Deno.readTextFileSync(filename);
    debug(
      "readFile",
      `${filename}: ${result.length} characters read`,
    );
    return result;
  } catch (e) {
    abort(`readFile: ${filename}: ${e.message}`);
  }
}
Example 45
Source File: _util.ts    From Bundler with MIT License 8 votes vote down vote up
export async function readFile(path: string | URL) {
  try {
    return await Deno.readFile(path);
  } catch (error) {
    if (error instanceof Deno.errors.NotFound) {
      error.message += `: ${path}`;
    }
    throw error;
  }
}
Example 46
Source File: _util.ts    From Bundler with MIT License 8 votes vote down vote up
export async function readTextFile(path: string | URL) {
  try {
    return await Deno.readTextFile(path);
  } catch (error) {
    if (error instanceof Deno.errors.NotFound) {
      error.message += `: ${path}`;
    }
    throw error;
  }
}
Example 47
Source File: yc-config.ts    From serverless-plugin with MIT License 8 votes vote down vote up
readYcConfigFile = () => {
    let config;

    try {
        config = yaml.parse(fs.readFileSync(YC_CONFIG_PATH, 'utf8'));
    } catch (error) {
        throw new Error(`Failed to read config ${YC_CONFIG_PATH}: ${error}`);
    }

    const { current, profiles } = config;

    if (!current) {
        throw new Error(`Invalid config in ${YC_CONFIG_PATH}: no current profile selected`);
    }

    if (!profiles[current]) {
        throw new Error(`Invalid config in ${YC_CONFIG_PATH}: no profile named ${current} exists`);
    }

    return profiles[current];
}
Example 48
Source File: MediaController.tsx    From Demae with MIT License 7 votes vote down vote up
function readFileAsync(file: File) {
	return new Promise((resolve, reject) => {
		const reader = new FileReader();
		reader.onload = () => {
			resolve(reader.result as string);
		};
		reader.onerror = reject;
		reader.readAsDataURL(file);
	})
}
Example 49
Source File: ALAppJsonReader.ts    From vscode-alxmldocumentation with MIT License 7 votes vote down vote up
public static ReadManifest(filePath: string): any {
        if (!fs.existsSync(filePath)) {
            throw new Error(`App manifest (app.json) could not been found. Path: ${filePath}`);
        }

        let appJson: any = JSON.parse(fs.readFileSync(filePath, 'utf8'));
        return appJson;
    }
Example 50
Source File: index.ts    From bluebubbles-server with Apache License 2.0 7 votes vote down vote up
static readFileChunk(filePath: string, start: number, chunkSize = 1024): Uint8Array {
        // Get the file size
        const stats = fs.statSync(filePath);
        let fStart = start;

        // Make sure the start are not bigger than the size
        if (fStart > stats.size) fStart = stats.size;
        return Uint8Array.from(sync(filePath, fStart, chunkSize));
    }
Example 51
Source File: readRemoteFile.tsx    From react-papaparse with MIT License 7 votes vote down vote up
export default function ReadRemoteFile() {
  const { readRemoteFile } = usePapaParse();

  const handleReadRemoteFile = () => {
    readRemoteFile('https://react-papaparse.js.org/static/csv/normal.csv', {
      complete: (results) => {
        console.log('---------------------------');
        console.log('Results:', results);
        console.log('---------------------------');
      },
    });
  };

  return <button onClick={() => handleReadRemoteFile()}>readRemoteFile</button>;
}
Example 52
Source File: Keys.ts    From clarity with Apache License 2.0 7 votes vote down vote up
/**
   * Read the Base64 content of a file, get rid of PEM frames.
   *
   * @param path the path of file to read from
   */
  private static readBase64File(path: string): Uint8Array {
    const content = fs.readFileSync(path).toString();
    return Ed25519.readBase64WithPEM(content);
  }
Example 53
Source File: Keys.ts    From clarity with Apache License 2.0 7 votes vote down vote up
/**
   * Read the Base64 content of a file, get rid of PEM frames.
   *
   * @param path the path of file to read from
   */
  private static readBase64File(path: string): Uint8Array {
    const content = fs.readFileSync(path).toString();
    return Secp256K1.readBase64WithPEM(content);
  }
Example 54
Source File: solc_comparison.spec.ts    From solc-typed-ast with Apache License 2.0 7 votes vote down vote up
async function readAST(
    fileName: string,
    version: string,
    compilerKind: CompilerKind,
    source?: string
): Promise<SourceUnit[]> {
    const reader = new ASTReader();

    const result = await (source === undefined
        ? compileSol(fileName, version, undefined, undefined, undefined, compilerKind)
        : compileSourceString(
              fileName,
              source,
              version,
              undefined,
              undefined,
              undefined,
              compilerKind
          ));

    return reader.read(result.data);
}
Example 55
Source File: File.ts    From compound-protocol with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
export async function readFile<T>(world: World | null, file: string, def: T, fn: (data: string) => T): Promise<T> {
  if (world && world.fs) {
    let data = world.fs[file];
    return Promise.resolve(data ? fn(data) : def);
  } else {
    return new Promise((resolve, reject) => {
      fs.access(file, fs.constants.F_OK, (err) => {
        if (err) {
          resolve(def);
        } else {
          fs.readFile(file, 'utf8', (err, data) => {
            return err ? reject(err) : resolve(fn(data));
          });
        }
      });
    });
  }
}
Example 56
Source File: readFromFile.ts    From Nishan with MIT License 7 votes vote down vote up
/**
 * Reads and extracts data from a local file
 * @param file_path Path of the file to read from
 * @returns Extracted data from the read file
 */
export async function readFromFile (file_path: string) {
	const ext = path.extname(file_path);
	let data: INotionSyncFileShape = {} as any;
	if (ext === '.json') data = JSON.parse(await fs.promises.readFile(file_path, 'utf-8'));
	else if (ext === '.yaml' || ext === '.yml')
		data = load(await fs.promises.readFile(file_path, 'utf-8')) as INotionSyncFileShape;
	else throw new Error('Unsupported output file extension. Use either json or yaml file when specifying the filepath');

	return NotionSync.extractData(data);
}
Example 57
Source File: index.ts    From dbm with Apache License 2.0 7 votes vote down vote up
/**
 * Read a file
 *
 * @param file - File
 *
 * @returns File data observable
 */
export function read(file: string): Observable<string> {
  return defer(() => fs.readFile(file, "utf8"))
}
Example 58
Source File: file.ts    From a18n with MIT License 7 votes vote down vote up
readFile = (path: string) => {
  return readFileSync(path, {
    encoding: 'utf-8',
  })
}
Example 59
Source File: driveFileSystemProvider.ts    From google-drive-vscode with MIT License 7 votes vote down vote up
readFile(uri: Uri): Thenable<Uint8Array> {
        return new Promise((resolve, reject) => {
            const fileId = uri.fragment;
            this.model.retrieveFileContentStream(fileId)
                .then(contentStream => {


                    const byteArray: any[] = [];
                    contentStream.on('data', d => byteArray.push(d));
                    contentStream.on('end', () => {
                        const result = Buffer.concat(byteArray);
                        resolve(result);
                    });


                }).catch(err => reject(err));
        });
    }
Example 60
Source File: base-generator.ts    From jmix-frontend with Apache License 2.0 7 votes vote down vote up
/**
 * @deprecated
 * @param modelFilePath
 */
export function readProjectModel(modelFilePath?: string): ProjectModel {
  if (!modelFilePath || !fs.existsSync(modelFilePath)) {
    throw new Error('Specified model file does not exist ' + modelFilePath);
  }
  return JSON.parse(fs.readFileSync(modelFilePath, "utf8"));
}