Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports.readConfigFile = function (fileName) {
if (!fs.existsSync(fileName)) {
throw new ConfigurationError(`Could not find config file ${fileName}`);
}
try {
let fileContent = fs.readFileSync(fileName);
return yaml.load(fileContent); // valid JSON or YAML format
} catch (err) {
if (err instanceof yaml.YAMLException) {
logger.error(`Bad config file format: ${err}`);
}
throw err;
}
};
// for valid YAML that is not valid as a document
function InvalidDocumentError(message) {
this.name = 'InvalidDocumentError';
this.message = message || 'Invalid document';
this.stack = (new Error()).stack;
}
InvalidDocumentError.prototype = Object.create(Error.prototype);
InvalidDocumentError.prototype.constructor = InvalidDocumentError;
exports.stringifyDocument = stringifyDocument;
exports.parseDocument = parseDocument;
exports.InvalidDocumentError = InvalidDocumentError;
// Re-exports
exports.YAMLException = jsyaml.YAMLException;
// write key is optional, but must contain a char value if present
if ('write' in val) {
var writeStr = String(val.write);
if (writeStr.length === 1) {
symbol = writeStr;
} else {
throw new TMSpecError('Write requires a string of length 1');
}
}
return makeInstruction(symbol, move, state);
}
exports.TMSpecError = TMSpecError;
exports.parseSpec = parseSpec;
// re-exports
exports.YAMLException = jsyaml.YAMLException;
if (fs.existsSync(await getConfigPath(sg))) {
console.log('.pr-train.yml already exists');
process.exit(1);
}
const root = path.dirname(require.main.filename);
const cfgTpl = fs.readFileSync(`${root}/cfg_template.yml`);
fs.writeFileSync(await getConfigPath(sg), cfgTpl);
console.log(`Created a ".pr-train.yml" file. Please make sure it's gitignored.`);
process.exit(0);
}
let ymlConfig;
try {
ymlConfig = await loadConfig(sg);
} catch (e) {
if (e instanceof yaml.YAMLException) {
console.log('There seems to be an error in `.pr-train.yml`.');
console.log(e.message);
process.exit(1);
}
console.log('`.pr-train.yml` file not found. Please run `git pr-train --init` to create one.'.red);
process.exit(1);
}
const { current: currentBranch, all: allBranches } = await sg.branchLocal();
const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig);
if (!trainCfg) {
console.log(`Current branch ${currentBranch} is not a train branch.`);
process.exit(1);
}
const sortedTrainBranches = getBranchesInCurrentTrain(trainCfg);
const combinedTrainBranch = getCombinedBranch(trainCfg);
const ext = path.extname(filePath);
const content = fs.readFileSync(filePath, 'utf8');
if (ext === '.json') {
return JSON.parse(content);
} else if (ext === '.yml' || ext === '.yaml') {
return yaml.load(content);
} else {
try {
return JSON.parse(content);
} catch (e1) {
if (e1 instanceof SyntaxError) {
try {
return yaml.load(content);
} catch (e2) {
if (e2 instanceof yaml.YAMLException) {
throw new SyntaxError(
`File '${filePath}' is not valid JSON or YAML`,
);
}
throw e2;
}
}
throw e1;
}
}
}
constructor(filePath: string, preamble?: any[]) {
let dialogueDoc;
try {
dialogueDoc = jsYaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
if (!dialogueDoc) {
throw new DialogueInvalidError(`Not a valid yaml: ${filePath}`);
}
} catch (e) {
if (e instanceof jsYaml.YAMLException) {
throw new DialogueInvalidError(`File is not valid YAML: ${e.message}`);
} else {
throw new DialogueInvalidError(e.message);
}
}
this.title = dialogueDoc.Title ?
dialogueDoc.Title :
path.basename(filePath, path.extname(filePath));
if (!dialogueDoc.Dialogue) {
throw new DialogueInvalidError('No dialogue found');
}
if (!(dialogueDoc.Dialogue instanceof Array)) {
throw new DialogueInvalidError(
`Dialogue lines must start with dashes: ${dialogueDoc.Dialogue}`);
}
const configs = program.args.map((path) => {
try {
const config = yaml.safeLoad(fs.readFileSync(path, 'utf-8'));
term.log(`Loaded config from '${path}'`);
return config;
} catch (err) {
if (err instanceof yaml.YAMLException) {
term.err(`Invalid YAML file '${path}'`);
} else {
term.err(`Cannot access '${path}'`);
}
return {};
}
}).filter((x) => !!x).reverse();
function tryYAMLParse(contents, defaultValue) {
try {
return yaml.safeLoad(contents) || defaultValue;
} catch (err) {
if (err instanceof yaml.YAMLException) {
return defaultValue;
}
throw err;
}
}