try handling as a Scratch 1 file after scratch-parser validation fails

This commit is contained in:
Michael "Z" Goddard 2018-12-20 13:38:07 -05:00
parent a92f33960f
commit 5593b28984
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -290,26 +290,33 @@ class VirtualMachine extends EventEmitter {
}
const validationPromise = new Promise((resolve, reject) => {
try {
const sb1 = new SB1File(input);
const json = sb1.json;
json.projectVersion = 2;
return resolve([json, sb1.zip]);
} catch (e) {
if (e instanceof ValidationError) {
// The input does not validate as a Scratch 1 file.
} else {
throw e;
}
}
// The second argument of false below indicates to the validator that the
// input should be parsed/validated as an entire project (and not a single sprite)
validate(input, false, (error, res) => {
if (error) return reject(error);
resolve(res);
});
});
})
.catch(error => {
try {
const sb1 = new SB1File(input);
const json = sb1.json;
json.projectVersion = 2;
return [json, sb1.zip];
} catch (sb1Error) {
if (sb1Error instanceof ValidationError) {
// The input does not validate as a Scratch 1 file.
} else {
// The project appears to be a Scratch 1 file but it
// could not be successfully translated into a Scratch 2
// project.
throw sb1Error;
}
}
// Through original error since the input does not appear to be
// an SB1File.
throw error;
});
return validationPromise
.then(validatedInput => this.deserializeProject(validatedInput[0], validatedInput[1]))