@vonage/client-sdk
Version:
The Client SDK is intended to provide a ready solution for developers to build Programmable Conversation applications across multiple Channels including: Messages, Voice, SIP, websockets, and App.
107 lines (91 loc) • 3.28 kB
JavaScript
console.log('============== EXAMPLE SNIPPETS EXTRACTOR ==============');
// the project root used for relative IO paths (set by commandline)
const project_root = process.argv[2];
const input_dir = process.argv[3] || 'examples';
const output_dir = process.argv[4] || 'examples/snippets';
if (!project_root) {
console.error('Error: project_root not provided. Exiting...');
process.exit(1);
}
console.log('args: ', process.argv.slice(2));
console.log('project_root: ', project_root);
console.log('input_dir: ', input_dir);
console.log('output_dir: ', output_dir);
//output_file_name_template = '{{name}}.txt' # a mustache template for the output file name
// Code block indicators
const start_flag = 'example:';
const end_flag = 'endExample';
// straightforward replacements
//[snippet.replacements]
// "self." = ""
const comments_by_format = {
html: ['<!--', '-->'],
js: ['//', ''],
ts: ['//', '']
};
import {
readdirSync,
readFileSync,
existsSync,
mkdirSync,
writeFileSync
} from 'fs';
const input_path = `${project_root}/${input_dir}`;
const examples = [];
let current_example = 0;
const files = readdirSync(input_path);
console.log('Input files: ', files);
const camalize = (str) =>
str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
const getFileName = (str) =>
str.startsWith('snippet')
? `snippet_${camalize(str.replace('snippet', ''))}.txt`
: `${str.replace(/ /g, '_')}.txt`;
files.forEach((file) => {
const format = file.split('.').pop();
const comments = comments_by_format[format];
if (!comments) return;
const comment_prefix = comments[0];
const comment_suffix = comments[1];
let isExampleCode = false;
let indentationLevel = 0;
const allFileContents = readFileSync(`${input_path}/${file}`, 'utf-8');
allFileContents.split(/\r?\n/).forEach((line) => {
const lineTrim = line.trim();
const isComment =
lineTrim.startsWith(comment_prefix) && lineTrim.endsWith(comment_suffix);
const isExampleStart = isComment && lineTrim.includes(start_flag);
const isExampleFinished = isComment && lineTrim.includes(end_flag);
if (isExampleStart) {
isExampleCode = true;
let title = lineTrim.split(start_flag)[1];
if (comment_suffix) {
title = title.split(comment_suffix)[0];
}
examples[current_example] = {
title: title.trim(),
format: format,
file_name: getFileName(title.trim().toLowerCase()),
content: ``
};
indentationLevel = line.search(comment_prefix);
} else if (isExampleFinished) {
isExampleCode = false;
current_example = current_example + 1;
indentationLevel = 0;
} else if (isExampleCode) {
const adjustedLine = line.slice(indentationLevel);
examples[current_example].content += adjustedLine + `\n`;
}
});
});
const output_path = `${project_root}/${output_dir}`;
if (!existsSync(output_path)) {
mkdirSync(output_path, { recursive: true });
}
console.log(`${examples.length} example snippets found`);
examples.forEach((res) => {
const { title, file_name, content, format } = res;
writeFileSync(`${output_path}/${file_name}`, content.trim(), 'utf-8');
console.log(`${title} (${format.toUpperCase()})`, ' -> ', file_name);
});