diff --git a/.travis.yml b/.travis.yml index 665b33716..751243fbe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ script: - npm run packager - npm run tester - npm run extractor +- npm run vscoder - npm run glossary:keymaker - npm run webber - npm run builder diff --git a/package.json b/package.json index 10736c25b..ddd6ee780 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "webber": "node ./scripts/web.js", "tester": "node ./scripts/tdd.js", "extractor": "node ./scripts/extract.js", + "vscoder": "node ./scripts/vscodegen.js", "packager": "node ./scripts/module.js", "localizer": "node ./scripts/localize.js", "test": "jest --verbose" diff --git a/scripts/extract.js b/scripts/extract.js index aab30cda0..0f6514fc0 100644 --- a/scripts/extract.js +++ b/scripts/extract.js @@ -90,5 +90,5 @@ let listingData = { fs.writeFileSync(path.join(OUTPUT_PATH, 'snippets.json'), JSON.stringify(completeData, null, 2)); fs.writeFileSync(path.join(OUTPUT_PATH, 'snippetList.json'), JSON.stringify(listingData, null, 2)); // Display messages and time -console.log(`${chalk.green('SUCCESS!')} snippets.json and snippetsArchive.json files generated!`); +console.log(`${chalk.green('SUCCESS!')} snippets.json and snippetList.json files generated!`); console.timeEnd('Extractor'); diff --git a/scripts/util.js b/scripts/util.js index cf25b1ad9..f3808a8dc 100644 --- a/scripts/util.js +++ b/scripts/util.js @@ -131,7 +131,7 @@ const getCodeBlocks = str => { results = results.map(v => v.replace(/```js([\s\S]*?)```/g, '$1').trim()); return { es6: results[0], - es5: babel.transformSync(results[0], { presets: ['@babel/preset-env'] }).code, + es5: babel.transformSync(results[0], { presets: ['@babel/preset-env'] }).code.replace('"use strict";\n\n',''), example: results[1] } }; diff --git a/scripts/vscodegen.js b/scripts/vscodegen.js new file mode 100644 index 000000000..3a89490cc --- /dev/null +++ b/scripts/vscodegen.js @@ -0,0 +1,33 @@ +/* + This is the VSCode generator script that generates the vscode_snippets/snippets.json file. + Run using `npm run vscoder`. +*/ +// Load modules +const fs = require('fs-extra'); +const path = require('path'); +const chalk = require('chalk'); +let snippetsData = require('../snippet_data/snippets.json'); +// Paths +const OUTPUT_PATH = './vscode_snippets'; +console.time('VSCoder'); +// Read and format data +let vscodeData = snippetsData.data.filter(v => !v.meta.archived ).reduce((acc,v) => { + acc[v.id] = { + prefix: `30s_${v.id}`, + body: v.attributes.codeBlocks.es6.replace(/\r/g,'').split('\n'), + description: v.attributes.text.slice(0, v.attributes.text.indexOf('\r\n\r\n')) + }; + return acc; +}, {}); +// Write data +fs.writeFileSync( + path.join(OUTPUT_PATH, 'snippets.json'), + JSON.stringify(vscodeData, null, 2) +); +// Display messages and time +console.log( + `${chalk.green( + 'SUCCESS!' + )} vscode_snippets/snippets.json file generated!` +); +console.timeEnd('VSCoder'); \ No newline at end of file diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index eb4bbef82..1941e4704 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -897,7 +897,7 @@ "archived": false }, "meta": { - "hash": "91e05fa632509c91fad4770339e0f8a4ffc5d0c3a90694f2fd59af2321cac9fb" + "hash": "9bbacd07c7a1d66b521b98d192f21964ab2cc33a6a62e18314d9dae5ec51a270" } }, { @@ -1118,7 +1118,7 @@ "archived": false }, "meta": { - "hash": "683bb22450b09dc717ef83b36ffcc6729a48891fcc47bab587b389f232597563" + "hash": "50b2980119026b898646420c2dc3cf2f394f5162f1a336cf64bc8d0244e54f26" } }, { @@ -3289,7 +3289,7 @@ "archived": false }, "meta": { - "hash": "12276cbe7dde6659e4ab67ed3fbe81ac57d1e7766b41dc879f946f226b7b76ab" + "hash": "6a7d303a9761c57b32f977b8068de4fd9bd9ea72167df268220d13250d1483e5" } }, { @@ -3661,7 +3661,7 @@ "archived": false }, "meta": { - "hash": "5aac190c69a2a7d42dd4f17caabe760270ac8c82b8b54b760d3d6b1de86362d7" + "hash": "7a8643d6d71023c773ccb63efe25e4c9fe0bdd976a8a8f76f6f6a0abb928eaf1" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 787b4a287..dd8fc2ab5 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -8,7 +8,7 @@ "text": "Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise.\r\n\r\nUse `Array.prototype.every()` to test if all elements in the collection return `true` based on `fn`.\r\nOmit the second argument, `fn`, to use `Boolean` as a default.", "codeBlocks": { "es6": "const all = (arr, fn = Boolean) => arr.every(fn);", - "es5": "\"use strict\";\n\nvar all = function all(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.every(fn);\n};", + "es5": "var all = function all(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.every(fn);\n};", "example": "all([4, 2, 3], x => x > 1); // true\r\nall([1, 2, 3]); // true" }, "tags": [ @@ -30,7 +30,7 @@ "text": "Check if all elements in an array are equal.\r\n\r\nUse `Array.prototype.every()` to check if all the elements of the array are the same as the first one.", "codeBlocks": { "es6": "const allEqual = arr => arr.every(val => val === arr[0]);", - "es5": "\"use strict\";\n\nvar allEqual = function allEqual(arr) {\n return arr.every(function (val) {\n return val === arr[0];\n });\n};", + "es5": "var allEqual = function allEqual(arr) {\n return arr.every(function (val) {\n return val === arr[0];\n });\n};", "example": "allEqual([1, 2, 3, 4, 5, 6]); // false\r\nallEqual([1, 1, 1, 1]); // true" }, "tags": [ @@ -52,7 +52,7 @@ "text": "Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise.\r\n\r\nUse `Array.prototype.some()` to test if any elements in the collection return `true` based on `fn`.\r\nOmit the second argument, `fn`, to use `Boolean` as a default.", "codeBlocks": { "es6": "const any = (arr, fn = Boolean) => arr.some(fn);", - "es5": "\"use strict\";\n\nvar any = function any(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.some(fn);\n};", + "es5": "var any = function any(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.some(fn);\n};", "example": "any([0, 1, 2, 0], x => x >= 2); // true\r\nany([0, 0, 1, 0]); // true" }, "tags": [ @@ -74,7 +74,7 @@ "text": "Checks if two numbers are approximately equal to each other.\r\n\r\nUse `Math.abs()` to compare the absolute difference of the two values to `epsilon`.\r\nOmit the third parameter, `epsilon`, to use a default value of `0.001`.", "codeBlocks": { "es6": "const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;", - "es5": "\"use strict\";\n\nvar approximatelyEqual = function approximatelyEqual(v1, v2) {\n var epsilon = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.001;\n return Math.abs(v1 - v2) < epsilon;\n};", + "es5": "var approximatelyEqual = function approximatelyEqual(v1, v2) {\n var epsilon = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.001;\n return Math.abs(v1 - v2) < epsilon;\n};", "example": "approximatelyEqual(Math.PI / 2.0, 1.5708); // true" }, "tags": [ @@ -95,7 +95,7 @@ "text": "Converts a 2D array to a comma-separated values (CSV) string.\r\n\r\nUse `Array.prototype.map()` and `Array.prototype.join(delimiter)` to combine individual 1D arrays (rows) into strings.\r\nUse `Array.prototype.join('\\n')` to combine all rows into a CSV string, separating each row with a newline.\r\nOmit the second argument, `delimiter`, to use a default delimiter of `,`.", "codeBlocks": { "es6": "const arrayToCSV = (arr, delimiter = ',') =>\r\n arr.map(v => v.map(x => `\"${x}\"`).join(delimiter)).join('\\n');", - "es5": "\"use strict\";\n\nvar arrayToCSV = function arrayToCSV(arr) {\n var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';\n return arr.map(function (v) {\n return v.map(function (x) {\n return \"\\\"\".concat(x, \"\\\"\");\n }).join(delimiter);\n }).join('\\n');\n};", + "es5": "var arrayToCSV = function arrayToCSV(arr) {\n var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';\n return arr.map(function (v) {\n return v.map(function (x) {\n return \"\\\"\".concat(x, \"\\\"\");\n }).join(delimiter);\n }).join('\\n');\n};", "example": "arrayToCSV([['a', 'b'], ['c', 'd']]); // '\"a\",\"b\"\\n\"c\",\"d\"'\r\narrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '\"a\";\"b\"\\n\"c\";\"d\"'" }, "tags": [ @@ -118,7 +118,7 @@ "text": "Converts the given array elements into `