Created
October 13, 2015 02:55
-
-
Save nkbt/0d531edd99c1ced81a69 to your computer and use it in GitHub Desktop.
Generate empty Jasmine specs for React components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const glob = require('glob'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const mkdirp = require('mkdirp'); | |
const args = global.process.argv.slice(2); | |
const sourcesRoot = args.shift() || path.join(__dirname, '..', 'src'); | |
const specsRoot = args.shift() || path.join(__dirname, '..', 'spec'); | |
const sources = glob.sync('/**/*.js', {root: sourcesRoot}) | |
.map(src => path.relative(sourcesRoot, src)); | |
const specs = glob.sync('/**/*_spec.js', {root: specsRoot}) | |
.map(src => path.relative(specsRoot, src)); | |
const missingSpecs = sources | |
.map(src => src.replace(/\.js$/, '_spec.js')) | |
.filter(src => | |
src.indexOf('/') !== -1 && src.indexOf('.jss') === -1 && specs.indexOf(src) === -1); | |
const extraSpecs = specs | |
.map(src => src.replace(/_spec\.js$/, '.js')) | |
.filter(src => sources.indexOf(src) === -1); | |
const files = missingSpecs.map(spec => { | |
const ClassPath = spec.replace(/_spec\.js/, ''); | |
const ClassName = path.basename(ClassPath); | |
const relativePath = path.relative(path.dirname(path.join(specsRoot, spec)), | |
path.join(sourcesRoot, ClassPath)); | |
return `import ${ClassName} from '${relativePath}'; | |
describe('${ClassName}', () => { | |
it('Should exist', () => { | |
expect(${ClassName}).toBeTruthy(); | |
}); | |
}); | |
`; | |
}); | |
if (extraSpecs.length) { | |
console.log(`Extra specs found: \n${extraSpecs.map(spec => | |
`./spec/${spec.replace(/\.js$/, '_spec.js')}`).join(' ')}`); | |
} else { | |
console.log('No extra specs found'); | |
} | |
console.log('generate-specs.js:48 missing specs', missingSpecs); | |
missingSpecs.forEach((spec, i) => | |
mkdirp(path.dirname(path.join(specsRoot, spec)), e => | |
e ? console.error(`Cannot create dir ${path.dirname(path.join(specsRoot, spec))}: ${e}`) : | |
fs.writeFile(path.join(specsRoot, spec), files[i], 'utf8', e1 => | |
e1 ? console.error(`Cannot write ${spec}: ${e1}`) : console.log(`Created ${spec}`)))); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment