diff --git a/package-lock.json b/package-lock.json index 07f08ceb1..2cd489a62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1421,6 +1421,12 @@ "esutils": "2.0.2" } }, + "duplexer": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", + "dev": true + }, "ecc-jsbn": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz", @@ -3092,6 +3098,12 @@ "error-ex": "1.3.1" } }, + "parse-ms": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/parse-ms/-/parse-ms-1.0.1.tgz", + "integrity": "sha1-VjRtR0nXjyNDDKDHE4UK75GqNh0=", + "dev": true + }, "path-exists": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", @@ -3190,6 +3202,12 @@ "find-up": "1.1.2" } }, + "plur": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/plur/-/plur-1.0.0.tgz", + "integrity": "sha1-24XGgU9eXlo7Se/CjWBP7GKXUVY=", + "dev": true + }, "pluralize": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-1.2.1.tgz", @@ -3235,6 +3253,12 @@ "resolved": "https://registry.npmjs.org/qs/-/qs-6.3.2.tgz", "integrity": "sha1-51vV9uJoEioqDgvaYwslUMFmUCw=" }, + "re-emitter": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/re-emitter/-/re-emitter-1.1.3.tgz", + "integrity": "sha1-+p4xn/3u6zWycpbvDz03TawvUqc=", + "dev": true + }, "read-pkg": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-1.1.0.tgz", @@ -3387,6 +3411,12 @@ "resolved": "https://registry.npmjs.org/relateurl/-/relateurl-0.2.7.tgz", "integrity": "sha1-VNvzd+UUQKypCkzSdGANP/LYiKk=" }, + "repeat-string": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", + "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=", + "dev": true + }, "repeating": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/repeating/-/repeating-2.0.1.tgz", @@ -3655,6 +3685,15 @@ "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz", "integrity": "sha1-yd96NCRZSt5r0RkA1ZZpbcBrrFc=" }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "dev": true, + "requires": { + "through": "2.3.8" + } + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -3831,6 +3870,55 @@ } } }, + "tap-out": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/tap-out/-/tap-out-1.4.2.tgz", + "integrity": "sha1-yQfsG/lAURHQiCY+kvVgi4jLs3o=", + "dev": true, + "requires": { + "re-emitter": "1.1.3", + "readable-stream": "2.3.3", + "split": "1.0.1", + "trim": "0.0.1" + } + }, + "tap-spec": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tap-spec/-/tap-spec-4.1.1.tgz", + "integrity": "sha1-4unyb1IIIysfViKIyXYk1YqI8Fo=", + "dev": true, + "requires": { + "chalk": "1.1.3", + "duplexer": "0.1.1", + "figures": "1.7.0", + "lodash": "3.10.1", + "pretty-ms": "2.1.0", + "repeat-string": "1.6.1", + "tap-out": "1.4.2", + "through2": "2.0.3" + }, + "dependencies": { + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "dev": true, + "requires": { + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" + } + }, + "lodash": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz", + "integrity": "sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y=", + "dev": true + } + } + }, "tape": { "version": "4.8.0", "resolved": "https://registry.npmjs.org/tape/-/tape-4.8.0.tgz", @@ -3894,6 +3982,12 @@ "punycode": "1.4.1" } }, + "trim": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/trim/-/trim-0.0.1.tgz", + "integrity": "sha1-WFhUf2spB1fulczMZm+1AITEYN0=", + "dev": true + }, "trim-newlines": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-1.0.0.tgz", diff --git a/package.json b/package.json index 0d0c380c0..1d58fac2b 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "rollup-plugin-babel": "^3.0.3", "rollup-plugin-babel-minify": "^3.1.2", "semistandard": "^11.0.0", - "tape": "^4.8.0" + "tape": "^4.8.0", + "tap-spec": "^4.1.1" }, "name": "30-seconds-of-code", "description": "A collection of useful JavaScript snippets.", @@ -24,7 +25,8 @@ "tagger": "node ./scripts/tag.js", "webber": "node ./scripts/web.js", "tdd": "node ./scripts/tdd.js", - "module": "node ./scripts/module.js" + "module": "node ./scripts/module.js", + "test": "tape test/**/*.test.js | tap-spec" }, "repository": { "type": "git", diff --git a/scripts/tdd.js b/scripts/tdd.js index d67396d7b..92cadd062 100644 --- a/scripts/tdd.js +++ b/scripts/tdd.js @@ -1,50 +1,84 @@ -const fs = require('fs-extra'); +/* + This is the tdd script that creates & updates your TDD environment . + Run using `npm run tagger`. +*/ +// Load modules +const fs = require('fs-extra'); +const chalk = require('chalk'); +// Load helper functions (these are from existing snippets in 30 seconds of code!) +const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env; +if(isTravisCI() && process.env['TRAVIS_EVENT_TYPE'] !== 'cron' && process.env['TRAVIS_EVENT_TYPE'] !== 'api') { + console.log(`${chalk.green('NOBUILD')} Testing terminated, not a cron job or a custom build!`); + process.exit(0); +} +// Declare paths const SNIPPETS_PATH = './snippets'; const TEST_PATH = './test'; +// Array of snippet names const snippetFiles = fs.readdirSync(SNIPPETS_PATH, 'utf8').map(fileName => fileName.slice(0, -3)); -fs.removeSync(TEST_PATH); +// Current Snippet that depend on node_modules +const errSnippets = ['JSONToFile', 'readFileLines', 'UUIDGeneratorNode']; snippetFiles + .filter(fileName => !errSnippets.includes(fileName)) .map(fileName => { + // Check if fileName for snippet exist in test/ dir, if doesnt create fs.ensureDirSync(`${TEST_PATH}/${fileName}`); + + // return fileName for later use return fileName; }) .map(fileName => { + // Grab snippetData const fileData = fs.readFileSync(`${SNIPPETS_PATH}/${fileName}.md`, 'utf8'); + // Grab snippet Code blocks const fileCode = fileData.slice(fileData.indexOf('```js'), fileData.lastIndexOf('```') + 3); + // Split code based on code markers const blockMarkers = fileCode .split('\n') .map((line, lineIndex) => (line.slice(0, 3) === '```' ? lineIndex : '//CLEAR//')) .filter(x => !(x === '//CLEAR//')); + // Grab snippet function based on code markers const fileFunction = fileCode .split('\n') .map(line => line.trim()) .filter((_, i) => blockMarkers[0] < i && i < blockMarkers[1]); + // Grab snippet example based on code markers const fileExample = fileCode .split('\n') .map(line => line.trim()) .filter((_, i) => blockMarkers[2] < i && i < blockMarkers[3]); - const exportFile = `module.exports = ${fileFunction.join('\n').slice(17)}`; + // Export template for snippetName.js which takes into account snippet name.length when generating snippetName.js file + const exportFile = `module.exports = ${fileFunction.join('\n').slice(9 + fileName.length)}`; + + // Export template for snippetName.test.js which generates a example test & other information const exportTest = [ `const test = require('tape');`, `const ${fileName} = require('./${fileName}.js');`, - `test('Testing ${fileName}', (t) => {`, - `//For more information on all the methods supported by tape\n//Please go to https://github.com/substack/tape`, - `//t.deepEqual(${fileName}(args..), 'Expected');`, - `//t.equal(${fileName}(args..), 'Expected');`, - `//t.false(${fileName}(args..), 'Expected');`, - `//t.true(${fileName}(args..), 'Expected');`, - `//t.throws(${fileName}(args..), 'Expected');`, - `t.end();`, + `\ntest('Testing ${fileName}', (t) => {`, + `\t//For more information on all the methods supported by tape\n\t//Please go to https://github.com/substack/tape`, + `\tt.true(typeof ${fileName} === 'function', '${fileName} is a Function');`, + `\t//t.deepEqual(${fileName}(args..), 'Expected');`, + `\t//t.equal(${fileName}(args..), 'Expected');`, + `\t//t.false(${fileName}(args..), 'Expected');`, + `\t//t.throws(${fileName}(args..), 'Expected');`, + `\tt.end();`, `});` ].join('\n'); + // Write/Update exportFile which is snippetName.js in respective dir fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.js`, exportFile); - fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`, exportTest); + if ( !fs.existsSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`) ) { + // if snippetName.test.js doesn't exist inrespective dir exportTest + fs.writeFileSync(`${TEST_PATH}/${fileName}/${fileName}.test.js`, exportTest); + } + + // return fileName for later use return fileName; }); +