From f89d5604aaf66a2c2a32b454d822da1187c5d6f6 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 06:11:41 +1100 Subject: [PATCH 1/9] Lint examples + use prettier --- scripts/lint-script.js | 118 +++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index d67980f17..cce751da9 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -3,51 +3,87 @@ Run using `npm run linter`. You might have to run `npm i -g semistandard` for this script to run properly. */ -// Load modules -const fs = require('fs-extra'), cp = require('child_process'), path = require('path'), chalk = require('chalk'); -// Set variables for paths -var snippetsPath = './snippets'; -// Read files, lint each one individually and update +const fs = require('fs-extra'); +const cp = require('child_process'); +const path = require('path'); +const chalk = require('chalk'); +const prettier = require('prettier'); + +const SNIPPETS_PATH = './snippets'; +const TEMP_PATH = './temp'; + +const codeRE = /```js([\s\S]*?)```/g; + +console.time('lint'); + try { - let snippetFilenames = fs.readdirSync(snippetsPath); - let jobCounter = 0; - snippetFilenames.sort((a, b) => { - a = a.toLowerCase(); - b = b.toLowerCase(); - if (a < b) return -1; - if (a > b) return 1; - return 0; - }); - // Read each file, get its code, write it to a temporary file, pass it through - // semistandard, get the output from the file, update the original file. - for(let snippet of snippetFilenames){ - // Start a timer for the file - console.time(`Linter (${snippet})`); - // Synchronously read data from the snippet, get the code, write it to a temporary file - let snippetData = fs.readFileSync(path.join(snippetsPath,snippet),'utf8'); - let codeStart = snippetData.indexOf('```js'), codeEnd = snippetData.search(/```[\n\r\s]+```js/g); - let originalCode = snippetData.slice(codeStart+5,codeEnd); - while(jobCounter >= 20){ - setTimeout(()=>{},1000); + const snippets = fs.readdirSync(SNIPPETS_PATH) + .sort((a, b) => a.toLowerCase() - b.toLowerCase()) + // turn it into an object so we can add data to it to be used in a different scope + .map(name => ({ name })); + + if (!fs.existsSync(TEMP_PATH)) { + fs.mkdirSync(TEMP_PATH); + } + + for (const snippet of snippets) { + snippet.data = fs.readFileSync(path.join(SNIPPETS_PATH, snippet.name), 'utf8'); + snippet.tempNames = []; + snippet.code = []; + + let match = codeRE.exec(snippet.data); + // make a counter so we write the definition + example code blocks to a different file + let counter = 0; + while (match) { + snippet.code.push(match[1]); // capture group + snippet.tempNames.push(snippet.name.replace('.md', counter)); + match = codeRE.exec(snippet.data); + counter++; } - const tempSnippet = snippet.replace('.md', ''); - fs.writeFileSync(`${tempSnippet}.temp.js`,`${originalCode}`); - // Run semistandard asynchronously (only way this manages to run), get linted code - // and write back to the original snippet file. Remove temporary file - cp.exec(`semistandard "${tempSnippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => { - jobCounter += 1; - let lintedCode = fs.readFileSync(`${tempSnippet}.temp.js`,'utf8'); - fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, codeStart+5)+lintedCode+snippetData.slice(codeEnd)}`); - fs.unlink(`${tempSnippet}.temp.js`); - // Log a success message - console.log(`${chalk.green('SUCCESS!')} Linted snippet: ${snippet}`); - // Log the time taken for the file - console.timeEnd(`Linter (${snippet})`); - jobCounter -= 1; + + snippet.code.forEach((str, i) => { + fs.writeFileSync(`${TEMP_PATH}/${snippet.tempNames[i]}.js`, `${snippet.code[i]}`); }); } -} -catch (err){ // Handle errors (hopefully not!) + + cp.exec(`semistandard "${TEMP_PATH}" --fix`, {}, (err, stdout, stderr) => { + // Loop through each snippet now that semistandard has done its job, + // run prettier and write to the files + for (const snippet of snippets) { + // an array to store each linted code block (definition + example) + const lintedCode = []; + + for (const tempName of snippet.tempNames) { + let data = fs.readFileSync(`${TEMP_PATH}/${tempName}.js`,'utf8'); + + // prettier sometimes throws an error.. + try { + data = prettier.format(data, { + printWidth: 100, + singleQuote: true + }); + } catch (e) {} + + lintedCode.push(data); + + fs.unlink(`${TEMP_PATH}/${tempName}.js`); + } + + // We replace each ```js ``` code block with the newly linted code + let index = -1; + snippet.data = snippet.data.replace(codeRE, () => { + index++; + return '```js\n' + lintedCode[index] + '```'; + }); + + fs.writeFileSync(path.join(SNIPPETS_PATH, snippet.name), snippet.data); + } + + fs.removeSync(TEMP_PATH); + + console.timeEnd('lint'); + }); +} catch (err) { console.log(`${chalk.red('ERROR!')} During linting: ${err}`); process.exit(1); } From 1d85b94c48edb8b38de364d7c4250606888e0d0b Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 06:13:34 +1100 Subject: [PATCH 2/9] Add packages --- package-lock.json | 795 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 796 insertions(+) diff --git a/package-lock.json b/package-lock.json index 82dcfac8a..43fd1243b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -561,6 +561,7 @@ "requires": { "anymatch": "1.3.2", "async-each": "1.0.1", + "fsevents": "1.1.3", "glob-parent": "2.0.0", "inherits": "2.0.3", "is-binary-path": "1.0.1", @@ -1585,6 +1586,795 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "fsevents": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.1.3.tgz", + "integrity": "sha512-WIr7iDkdmdbxu/Gh6eKEZJL6KPE74/5MEsf2whTOFNxbIoIixogroLdKYqB6FDav4Wavh/lZdzzd3b2KxIXC5Q==", + "optional": true, + "requires": { + "nan": "2.8.0", + "node-pre-gyp": "0.6.39" + }, + "dependencies": { + "abbrev": { + "version": "1.1.0", + "bundled": true, + "optional": true + }, + "ajv": { + "version": "4.11.8", + "bundled": true, + "optional": true, + "requires": { + "co": "4.6.0", + "json-stable-stringify": "1.0.1" + } + }, + "ansi-regex": { + "version": "2.1.1", + "bundled": true + }, + "aproba": { + "version": "1.1.1", + "bundled": true, + "optional": true + }, + "are-we-there-yet": { + "version": "1.1.4", + "bundled": true, + "optional": true, + "requires": { + "delegates": "1.0.0", + "readable-stream": "2.2.9" + } + }, + "asn1": { + "version": "0.2.3", + "bundled": true, + "optional": true + }, + "assert-plus": { + "version": "0.2.0", + "bundled": true, + "optional": true + }, + "asynckit": { + "version": "0.4.0", + "bundled": true, + "optional": true + }, + "aws-sign2": { + "version": "0.6.0", + "bundled": true, + "optional": true + }, + "aws4": { + "version": "1.6.0", + "bundled": true, + "optional": true + }, + "balanced-match": { + "version": "0.4.2", + "bundled": true + }, + "bcrypt-pbkdf": { + "version": "1.0.1", + "bundled": true, + "optional": true, + "requires": { + "tweetnacl": "0.14.5" + } + }, + "block-stream": { + "version": "0.0.9", + "bundled": true, + "requires": { + "inherits": "2.0.3" + } + }, + "boom": { + "version": "2.10.1", + "bundled": true, + "requires": { + "hoek": "2.16.3" + } + }, + "brace-expansion": { + "version": "1.1.7", + "bundled": true, + "requires": { + "balanced-match": "0.4.2", + "concat-map": "0.0.1" + } + }, + "buffer-shims": { + "version": "1.0.0", + "bundled": true + }, + "caseless": { + "version": "0.12.0", + "bundled": true, + "optional": true + }, + "co": { + "version": "4.6.0", + "bundled": true, + "optional": true + }, + "code-point-at": { + "version": "1.1.0", + "bundled": true + }, + "combined-stream": { + "version": "1.0.5", + "bundled": true, + "requires": { + "delayed-stream": "1.0.0" + } + }, + "concat-map": { + "version": "0.0.1", + "bundled": true + }, + "console-control-strings": { + "version": "1.1.0", + "bundled": true + }, + "core-util-is": { + "version": "1.0.2", + "bundled": true + }, + "cryptiles": { + "version": "2.0.5", + "bundled": true, + "requires": { + "boom": "2.10.1" + } + }, + "dashdash": { + "version": "1.14.1", + "bundled": true, + "optional": true, + "requires": { + "assert-plus": "1.0.0" + }, + "dependencies": { + "assert-plus": { + "version": "1.0.0", + "bundled": true, + "optional": true + } + } + }, + "debug": { + "version": "2.6.8", + "bundled": true, + "optional": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-extend": { + "version": "0.4.2", + "bundled": true, + "optional": true + }, + "delayed-stream": { + "version": "1.0.0", + "bundled": true + }, + "delegates": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "detect-libc": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "ecc-jsbn": { + "version": "0.1.1", + "bundled": true, + "optional": true, + "requires": { + "jsbn": "0.1.1" + } + }, + "extend": { + "version": "3.0.1", + "bundled": true, + "optional": true + }, + "extsprintf": { + "version": "1.0.2", + "bundled": true + }, + "forever-agent": { + "version": "0.6.1", + "bundled": true, + "optional": true + }, + "form-data": { + "version": "2.1.4", + "bundled": true, + "optional": true, + "requires": { + "asynckit": "0.4.0", + "combined-stream": "1.0.5", + "mime-types": "2.1.15" + } + }, + "fs.realpath": { + "version": "1.0.0", + "bundled": true + }, + "fstream": { + "version": "1.0.11", + "bundled": true, + "requires": { + "graceful-fs": "4.1.11", + "inherits": "2.0.3", + "mkdirp": "0.5.1", + "rimraf": "2.6.1" + } + }, + "fstream-ignore": { + "version": "1.0.5", + "bundled": true, + "optional": true, + "requires": { + "fstream": "1.0.11", + "inherits": "2.0.3", + "minimatch": "3.0.4" + } + }, + "gauge": { + "version": "2.7.4", + "bundled": true, + "optional": true, + "requires": { + "aproba": "1.1.1", + "console-control-strings": "1.1.0", + "has-unicode": "2.0.1", + "object-assign": "4.1.1", + "signal-exit": "3.0.2", + "string-width": "1.0.2", + "strip-ansi": "3.0.1", + "wide-align": "1.1.2" + } + }, + "getpass": { + "version": "0.1.7", + "bundled": true, + "optional": true, + "requires": { + "assert-plus": "1.0.0" + }, + "dependencies": { + "assert-plus": { + "version": "1.0.0", + "bundled": true, + "optional": true + } + } + }, + "glob": { + "version": "7.1.2", + "bundled": true, + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "graceful-fs": { + "version": "4.1.11", + "bundled": true + }, + "har-schema": { + "version": "1.0.5", + "bundled": true, + "optional": true + }, + "har-validator": { + "version": "4.2.1", + "bundled": true, + "optional": true, + "requires": { + "ajv": "4.11.8", + "har-schema": "1.0.5" + } + }, + "has-unicode": { + "version": "2.0.1", + "bundled": true, + "optional": true + }, + "hawk": { + "version": "3.1.3", + "bundled": true, + "requires": { + "boom": "2.10.1", + "cryptiles": "2.0.5", + "hoek": "2.16.3", + "sntp": "1.0.9" + } + }, + "hoek": { + "version": "2.16.3", + "bundled": true + }, + "http-signature": { + "version": "1.1.1", + "bundled": true, + "optional": true, + "requires": { + "assert-plus": "0.2.0", + "jsprim": "1.4.0", + "sshpk": "1.13.0" + } + }, + "inflight": { + "version": "1.0.6", + "bundled": true, + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "bundled": true + }, + "ini": { + "version": "1.3.4", + "bundled": true, + "optional": true + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "bundled": true, + "requires": { + "number-is-nan": "1.0.1" + } + }, + "is-typedarray": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "isarray": { + "version": "1.0.0", + "bundled": true + }, + "isstream": { + "version": "0.1.2", + "bundled": true, + "optional": true + }, + "jodid25519": { + "version": "1.0.2", + "bundled": true, + "optional": true, + "requires": { + "jsbn": "0.1.1" + } + }, + "jsbn": { + "version": "0.1.1", + "bundled": true, + "optional": true + }, + "json-schema": { + "version": "0.2.3", + "bundled": true, + "optional": true + }, + "json-stable-stringify": { + "version": "1.0.1", + "bundled": true, + "optional": true, + "requires": { + "jsonify": "0.0.0" + } + }, + "json-stringify-safe": { + "version": "5.0.1", + "bundled": true, + "optional": true + }, + "jsonify": { + "version": "0.0.0", + "bundled": true, + "optional": true + }, + "jsprim": { + "version": "1.4.0", + "bundled": true, + "optional": true, + "requires": { + "assert-plus": "1.0.0", + "extsprintf": "1.0.2", + "json-schema": "0.2.3", + "verror": "1.3.6" + }, + "dependencies": { + "assert-plus": { + "version": "1.0.0", + "bundled": true, + "optional": true + } + } + }, + "mime-db": { + "version": "1.27.0", + "bundled": true + }, + "mime-types": { + "version": "2.1.15", + "bundled": true, + "requires": { + "mime-db": "1.27.0" + } + }, + "minimatch": { + "version": "3.0.4", + "bundled": true, + "requires": { + "brace-expansion": "1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "bundled": true + }, + "mkdirp": { + "version": "0.5.1", + "bundled": true, + "requires": { + "minimist": "0.0.8" + } + }, + "ms": { + "version": "2.0.0", + "bundled": true, + "optional": true + }, + "node-pre-gyp": { + "version": "0.6.39", + "bundled": true, + "optional": true, + "requires": { + "detect-libc": "1.0.2", + "hawk": "3.1.3", + "mkdirp": "0.5.1", + "nopt": "4.0.1", + "npmlog": "4.1.0", + "rc": "1.2.1", + "request": "2.81.0", + "rimraf": "2.6.1", + "semver": "5.3.0", + "tar": "2.2.1", + "tar-pack": "3.4.0" + } + }, + "nopt": { + "version": "4.0.1", + "bundled": true, + "optional": true, + "requires": { + "abbrev": "1.1.0", + "osenv": "0.1.4" + } + }, + "npmlog": { + "version": "4.1.0", + "bundled": true, + "optional": true, + "requires": { + "are-we-there-yet": "1.1.4", + "console-control-strings": "1.1.0", + "gauge": "2.7.4", + "set-blocking": "2.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "bundled": true + }, + "oauth-sign": { + "version": "0.8.2", + "bundled": true, + "optional": true + }, + "object-assign": { + "version": "4.1.1", + "bundled": true, + "optional": true + }, + "once": { + "version": "1.4.0", + "bundled": true, + "requires": { + "wrappy": "1.0.2" + } + }, + "os-homedir": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "os-tmpdir": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "osenv": { + "version": "0.1.4", + "bundled": true, + "optional": true, + "requires": { + "os-homedir": "1.0.2", + "os-tmpdir": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "bundled": true + }, + "performance-now": { + "version": "0.2.0", + "bundled": true, + "optional": true + }, + "process-nextick-args": { + "version": "1.0.7", + "bundled": true + }, + "punycode": { + "version": "1.4.1", + "bundled": true, + "optional": true + }, + "qs": { + "version": "6.4.0", + "bundled": true, + "optional": true + }, + "rc": { + "version": "1.2.1", + "bundled": true, + "optional": true, + "requires": { + "deep-extend": "0.4.2", + "ini": "1.3.4", + "minimist": "1.2.0", + "strip-json-comments": "2.0.1" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "bundled": true, + "optional": true + } + } + }, + "readable-stream": { + "version": "2.2.9", + "bundled": true, + "requires": { + "buffer-shims": "1.0.0", + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "1.0.0", + "process-nextick-args": "1.0.7", + "string_decoder": "1.0.1", + "util-deprecate": "1.0.2" + } + }, + "request": { + "version": "2.81.0", + "bundled": true, + "optional": true, + "requires": { + "aws-sign2": "0.6.0", + "aws4": "1.6.0", + "caseless": "0.12.0", + "combined-stream": "1.0.5", + "extend": "3.0.1", + "forever-agent": "0.6.1", + "form-data": "2.1.4", + "har-validator": "4.2.1", + "hawk": "3.1.3", + "http-signature": "1.1.1", + "is-typedarray": "1.0.0", + "isstream": "0.1.2", + "json-stringify-safe": "5.0.1", + "mime-types": "2.1.15", + "oauth-sign": "0.8.2", + "performance-now": "0.2.0", + "qs": "6.4.0", + "safe-buffer": "5.0.1", + "stringstream": "0.0.5", + "tough-cookie": "2.3.2", + "tunnel-agent": "0.6.0", + "uuid": "3.0.1" + } + }, + "rimraf": { + "version": "2.6.1", + "bundled": true, + "requires": { + "glob": "7.1.2" + } + }, + "safe-buffer": { + "version": "5.0.1", + "bundled": true + }, + "semver": { + "version": "5.3.0", + "bundled": true, + "optional": true + }, + "set-blocking": { + "version": "2.0.0", + "bundled": true, + "optional": true + }, + "signal-exit": { + "version": "3.0.2", + "bundled": true, + "optional": true + }, + "sntp": { + "version": "1.0.9", + "bundled": true, + "requires": { + "hoek": "2.16.3" + } + }, + "sshpk": { + "version": "1.13.0", + "bundled": true, + "optional": true, + "requires": { + "asn1": "0.2.3", + "assert-plus": "1.0.0", + "bcrypt-pbkdf": "1.0.1", + "dashdash": "1.14.1", + "ecc-jsbn": "0.1.1", + "getpass": "0.1.7", + "jodid25519": "1.0.2", + "jsbn": "0.1.1", + "tweetnacl": "0.14.5" + }, + "dependencies": { + "assert-plus": { + "version": "1.0.0", + "bundled": true, + "optional": true + } + } + }, + "string-width": { + "version": "1.0.2", + "bundled": true, + "requires": { + "code-point-at": "1.1.0", + "is-fullwidth-code-point": "1.0.0", + "strip-ansi": "3.0.1" + } + }, + "string_decoder": { + "version": "1.0.1", + "bundled": true, + "requires": { + "safe-buffer": "5.0.1" + } + }, + "stringstream": { + "version": "0.0.5", + "bundled": true, + "optional": true + }, + "strip-ansi": { + "version": "3.0.1", + "bundled": true, + "requires": { + "ansi-regex": "2.1.1" + } + }, + "strip-json-comments": { + "version": "2.0.1", + "bundled": true, + "optional": true + }, + "tar": { + "version": "2.2.1", + "bundled": true, + "requires": { + "block-stream": "0.0.9", + "fstream": "1.0.11", + "inherits": "2.0.3" + } + }, + "tar-pack": { + "version": "3.4.0", + "bundled": true, + "optional": true, + "requires": { + "debug": "2.6.8", + "fstream": "1.0.11", + "fstream-ignore": "1.0.5", + "once": "1.4.0", + "readable-stream": "2.2.9", + "rimraf": "2.6.1", + "tar": "2.2.1", + "uid-number": "0.0.6" + } + }, + "tough-cookie": { + "version": "2.3.2", + "bundled": true, + "optional": true, + "requires": { + "punycode": "1.4.1" + } + }, + "tunnel-agent": { + "version": "0.6.0", + "bundled": true, + "optional": true, + "requires": { + "safe-buffer": "5.0.1" + } + }, + "tweetnacl": { + "version": "0.14.5", + "bundled": true, + "optional": true + }, + "uid-number": { + "version": "0.0.6", + "bundled": true, + "optional": true + }, + "util-deprecate": { + "version": "1.0.2", + "bundled": true + }, + "uuid": { + "version": "3.0.1", + "bundled": true, + "optional": true + }, + "verror": { + "version": "1.3.6", + "bundled": true, + "optional": true, + "requires": { + "extsprintf": "1.0.2" + } + }, + "wide-align": { + "version": "1.1.2", + "bundled": true, + "optional": true, + "requires": { + "string-width": "1.0.2" + } + }, + "wrappy": { + "version": "1.0.2", + "bundled": true + } + } + }, "fstream": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.11.tgz", @@ -3420,6 +4210,11 @@ "resolved": "https://registry.npmjs.org/preserve/-/preserve-0.2.0.tgz", "integrity": "sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=" }, + "prettier": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.9.2.tgz", + "integrity": "sha512-piXx9N2WT8hWb7PBbX1glAuJVIkEyUV9F5fMXFINpZ0x3otVOFKKeGmeuiclFJlP/UrgTckyV606VjH2rNK4bw==" + }, "process-nextick-args": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", diff --git a/package.json b/package.json index 09024b301..f48b9768b 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "markdown-it": "^8.4.0", "node-sass": "^4.7.2", "nodemon": "^1.12.1", + "prettier": "^1.9.2", "semistandard": "^11.0.0", "tagger": "^0.1.2", "webber": "0.0.1" From 2d18910922af58740be0b1b959a02a3d359c18e9 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 06:30:00 +1100 Subject: [PATCH 3/9] Optimize a bit --- scripts/lint-script.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index cce751da9..87b705904 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -54,8 +54,7 @@ try { const lintedCode = []; for (const tempName of snippet.tempNames) { - let data = fs.readFileSync(`${TEMP_PATH}/${tempName}.js`,'utf8'); - + let data = fs.readFileSync(`${TEMP_PATH}/${tempName}.js`, 'utf8'); // prettier sometimes throws an error.. try { data = prettier.format(data, { @@ -63,24 +62,19 @@ try { singleQuote: true }); } catch (e) {} - + lintedCode.push(data); - fs.unlink(`${TEMP_PATH}/${tempName}.js`); } // We replace each ```js ``` code block with the newly linted code - let index = -1; - snippet.data = snippet.data.replace(codeRE, () => { - index++; - return '```js\n' + lintedCode[index] + '```'; - }); + let index = 0; + snippet.data = snippet.data.replace(codeRE, () => '```js\n' + lintedCode[index++] + '```') fs.writeFileSync(path.join(SNIPPETS_PATH, snippet.name), snippet.data); } fs.removeSync(TEMP_PATH); - console.timeEnd('lint'); }); } catch (err) { From 57ad72514e3b72e60505ca6103e2c671571b2c97 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 06:32:58 +1100 Subject: [PATCH 4/9] more fixes --- scripts/lint-script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index 87b705904..824aa2b49 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -42,7 +42,7 @@ try { } snippet.code.forEach((str, i) => { - fs.writeFileSync(`${TEMP_PATH}/${snippet.tempNames[i]}.js`, `${snippet.code[i]}`); + fs.writeFileSync(`${TEMP_PATH}/${snippet.tempNames[i]}.js`, str); }); } @@ -62,14 +62,14 @@ try { singleQuote: true }); } catch (e) {} - + lintedCode.push(data); fs.unlink(`${TEMP_PATH}/${tempName}.js`); } // We replace each ```js ``` code block with the newly linted code let index = 0; - snippet.data = snippet.data.replace(codeRE, () => '```js\n' + lintedCode[index++] + '```') + snippet.data = snippet.data.replace(codeRE, () => '```js\n' + lintedCode[index++] + '```'); fs.writeFileSync(path.join(SNIPPETS_PATH, snippet.name), snippet.data); } From 7654022f012ae1c6fb9b47a6627f317cd396ff1d Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 15:23:02 +1100 Subject: [PATCH 5/9] Account for spaces between opening block and js --- scripts/lint-script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index 824aa2b49..a42cd29a3 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -12,7 +12,7 @@ const prettier = require('prettier'); const SNIPPETS_PATH = './snippets'; const TEMP_PATH = './temp'; -const codeRE = /```js([\s\S]*?)```/g; +const codeRE = /```\s*js([\s\S]*?)```/g; console.time('lint'); From c68cb5e74815e1e841263140d7ee3e35e70883eb Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 17:59:47 +1100 Subject: [PATCH 6/9] Use prettier in the cmd --- scripts/lint-script.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index a42cd29a3..1840654b5 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -7,7 +7,6 @@ const fs = require('fs-extra'); const cp = require('child_process'); const path = require('path'); const chalk = require('chalk'); -const prettier = require('prettier'); const SNIPPETS_PATH = './snippets'; const TEMP_PATH = './temp'; @@ -46,7 +45,10 @@ try { }); } - cp.exec(`semistandard "${TEMP_PATH}" --fix`, {}, (err, stdout, stderr) => { + const cmd = `semistandard "${TEMP_PATH}" --fix & ` + + `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write` + + cp.exec(cmd, {}, (err, stdout, stderr) => { // Loop through each snippet now that semistandard has done its job, // run prettier and write to the files for (const snippet of snippets) { @@ -54,16 +56,7 @@ try { const lintedCode = []; for (const tempName of snippet.tempNames) { - let data = fs.readFileSync(`${TEMP_PATH}/${tempName}.js`, 'utf8'); - // prettier sometimes throws an error.. - try { - data = prettier.format(data, { - printWidth: 100, - singleQuote: true - }); - } catch (e) {} - - lintedCode.push(data); + lintedCode.push(fs.readFileSync(`${TEMP_PATH}/${tempName}.js`, 'utf8')); fs.unlink(`${TEMP_PATH}/${tempName}.js`); } From f42d37e8ac3936cf5ec9c95852708bd2d916512f Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 18:24:25 +1100 Subject: [PATCH 7/9] fix comments --- scripts/lint-script.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index 1840654b5..01e7a5ca9 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -35,9 +35,8 @@ try { let counter = 0; while (match) { snippet.code.push(match[1]); // capture group - snippet.tempNames.push(snippet.name.replace('.md', counter)); + snippet.tempNames.push(snippet.name.replace('.md', counter++)); match = codeRE.exec(snippet.data); - counter++; } snippet.code.forEach((str, i) => { @@ -46,11 +45,10 @@ try { } const cmd = `semistandard "${TEMP_PATH}" --fix & ` + - `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write` + `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write`; cp.exec(cmd, {}, (err, stdout, stderr) => { - // Loop through each snippet now that semistandard has done its job, - // run prettier and write to the files + // Loop through each snippet now that semistandard and prettier did their job, for (const snippet of snippets) { // an array to store each linted code block (definition + example) const lintedCode = []; From ac852cc0b63d89ea5c74fc43ef43f292c8e190df Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 28 Dec 2017 18:25:43 +1100 Subject: [PATCH 8/9] more comment fixes --- scripts/lint-script.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index 01e7a5ca9..b3f8996e2 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -1,7 +1,9 @@ /* This is the linter script that lints snippets. Run using `npm run linter`. - You might have to run `npm i -g semistandard` for this script to run properly. + You might have to run + `npm i -g semistandard && npm i -g prettier` + for this script to run properly. */ const fs = require('fs-extra'); const cp = require('child_process'); @@ -31,7 +33,7 @@ try { snippet.code = []; let match = codeRE.exec(snippet.data); - // make a counter so we write the definition + example code blocks to a different file + // make a counter so we write the definition + example code blocks to different files let counter = 0; while (match) { snippet.code.push(match[1]); // capture group @@ -48,7 +50,7 @@ try { `prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write`; cp.exec(cmd, {}, (err, stdout, stderr) => { - // Loop through each snippet now that semistandard and prettier did their job, + // Loop through each snippet now that semistandard and prettier did their job for (const snippet of snippets) { // an array to store each linted code block (definition + example) const lintedCode = []; From 67ad54f36e9048e35567346471019bd9dc09b263 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 28 Dec 2017 10:22:01 +0200 Subject: [PATCH 9/9] Update lint-script.js --- scripts/lint-script.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/lint-script.js b/scripts/lint-script.js index b3f8996e2..ba66f36a8 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -15,7 +15,7 @@ const TEMP_PATH = './temp'; const codeRE = /```\s*js([\s\S]*?)```/g; -console.time('lint'); +console.time('Linter'); try { const snippets = fs.readdirSync(SNIPPETS_PATH) @@ -68,7 +68,8 @@ try { } fs.removeSync(TEMP_PATH); - console.timeEnd('lint'); + console.log(`${chalk.green('SUCCESS!')} Snippet files linted!`); + console.timeEnd('Linter'); }); } catch (err) { console.log(`${chalk.red('ERROR!')} During linting: ${err}`);