c119c44c13
When this is merged https://gitlab.com/ligolang/ligo-web-ide/ will be marked as deprecated. This MR does not hook up the webide build to the main CI. The CI integration will come in a subsequent MR for the sake of making review easier.
119 lines
2.7 KiB
JavaScript
119 lines
2.7 KiB
JavaScript
const createHash = require('crypto').createHash;
|
|
const glob = require('glob');
|
|
const join = require('path').join;
|
|
const fs = require('fs');
|
|
const YAML = require('yamljs');
|
|
|
|
function urlFriendlyHash(content) {
|
|
const hash = createHash('md5');
|
|
hash.update(content);
|
|
|
|
return hash
|
|
.digest('base64')
|
|
.replace(/\+/g, '-')
|
|
.replace(/\//g, '_')
|
|
.replace(/=/g, '');
|
|
}
|
|
|
|
function convertToJson(content, path) {
|
|
const METADATA_REGEX = /\(\*_\*([^]*?)\*_\*\)\s*/;
|
|
const match = content.match(METADATA_REGEX);
|
|
|
|
if (!match || !match[1]) {
|
|
throw new Error(`Unable to find compiler configuration in ${path}.`);
|
|
}
|
|
|
|
try {
|
|
const config = YAML.parse(match[1]);
|
|
config.editor = {
|
|
language: config.language,
|
|
code: content.replace(METADATA_REGEX, '')
|
|
};
|
|
delete config.language;
|
|
|
|
return config;
|
|
} catch (ex) {
|
|
throw new Error(`${path} doesn't contain valid metadata. ${ex}`);
|
|
}
|
|
}
|
|
|
|
function findFiles(pattern, dir) {
|
|
return new Promise((resolve, reject) => {
|
|
glob(pattern, { cwd: dir }, (error, files) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve(files);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function readFile(path) {
|
|
return new Promise((resolve, reject) => {
|
|
fs.readFile(path, 'utf8', (error, content) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve(content);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function writeFile(path, config) {
|
|
return new Promise((resolve, reject) => {
|
|
fs.writeFile(path, JSON.stringify(config), error => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
async function processExample(srcDir, file, destDir) {
|
|
const path = join(srcDir, file);
|
|
|
|
console.log(`Processing ${path}`);
|
|
|
|
const content = await readFile(path);
|
|
const config = convertToJson(content, path);
|
|
const id = urlFriendlyHash(file);
|
|
|
|
config.id = id;
|
|
|
|
await writeFile(join(destDir, id), config);
|
|
|
|
return { id: id, name: config.name };
|
|
}
|
|
|
|
function processExamples(srcDir, files, destDir) {
|
|
return Promise.all(files.map(file => processExample(srcDir, file, destDir)));
|
|
}
|
|
|
|
async function main() {
|
|
process.on('unhandledRejection', error => {
|
|
throw error;
|
|
});
|
|
|
|
const EXAMPLES_DEST_DIR = join(process.cwd(), 'build', 'static', 'examples');
|
|
const EXAMPLES_DIR = join(process.cwd(), 'examples');
|
|
const EXAMPLES_GLOB = '**/*.ligo';
|
|
const EXAMPLES_LIST_FILE = 'list';
|
|
|
|
fs.mkdirSync(EXAMPLES_DEST_DIR, { recursive: true });
|
|
|
|
const files = await findFiles(EXAMPLES_GLOB, EXAMPLES_DIR);
|
|
const examples = await processExamples(
|
|
EXAMPLES_DIR,
|
|
files,
|
|
EXAMPLES_DEST_DIR
|
|
);
|
|
|
|
await writeFile(join(EXAMPLES_DEST_DIR, EXAMPLES_LIST_FILE), examples);
|
|
}
|
|
|
|
main();
|