How to use the yaml.default.parseDocument function in yaml

To help you get started, we’ve selected a few yaml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github back4app / antframework / packages / ant / lib / config / Config.js View on Github external
constructor(params) {
    assert(params, 'Parameter "params" can not be undefined');
    assert(typeof params === 'string' || typeof params === 'object', 'Configuration \
file "params" must be a string (configuration file path) or object');
    this._jsonHandlers = Config.DefaultJSONHandlers;
    if (typeof params === 'string') {
      this._path = params;
      if (fs.existsSync(this._path)) {
        const configFileContent = fs.readFileSync(this._path, 'utf-8');
        // If file is empty, does not try to parse it.
        if (configFileContent) {
          try {
            this._config = yaml.parseDocument(configFileContent);
            const configJson = this._config.toJSON();
            if(typeof configJson === 'string') {
              throw new AntError(`The configuration "${configJson}" is invalid`);
            }
            return;
          } catch (e) {
            throw new AntError(
              `Could not load config ${this._path}`,
              e
            );
          }
        }
        this._config = new yaml.Document();
        this._config.contents = new Map();
        fs.writeFileSync(this._path, this._config.toString());
        logger.log(`Configuration file successfully written at: ${this._path}`);