|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const mkdirp = require('mkdirp'); |
| 6 | +const path = require('path'); |
| 7 | +const temp = require('temp'); |
| 8 | + |
| 9 | +function renameFileTo(oldPath, newFilename) { |
| 10 | + const projectPath = path.dirname(oldPath); |
| 11 | + const newPath = path.join(projectPath, newFilename); |
| 12 | + mkdirp.sync(path.dirname(newPath)); |
| 13 | + fs.renameSync(oldPath, newPath); |
| 14 | + return newPath; |
| 15 | +} |
| 16 | + |
| 17 | +function createTempFileWith(content, filename, extension) { |
| 18 | + const info = temp.openSync({ suffix: extension }); |
| 19 | + let filePath = info.path; |
| 20 | + fs.writeSync(info.fd, content); |
| 21 | + fs.closeSync(info.fd); |
| 22 | + if (filename) { |
| 23 | + filePath = renameFileTo(filePath, filename); |
| 24 | + } |
| 25 | + return filePath; |
| 26 | +} |
| 27 | + |
| 28 | +// Test transform files need a js extension to work with @babel/register |
| 29 | +// .ts or .tsx work as well |
| 30 | +function createTransformWith(content, ext = '.js') { |
| 31 | + return createTempFileWith( |
| 32 | + 'module.exports = function(fileInfo, api, options) { ' + content + ' }', |
| 33 | + undefined, |
| 34 | + ext, |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function getFileContent(filePath) { |
| 39 | + return fs.readFileSync(filePath).toString(); |
| 40 | +} |
| 41 | + |
| 42 | +describe('Worker API', () => { |
| 43 | + it('transforms files', done => { |
| 44 | + const worker = require('./Worker'); |
| 45 | + const transformPath = createTransformWith( |
| 46 | + 'return fileInfo.source + " changed";', |
| 47 | + ); |
| 48 | + const sourcePath = createTempFileWith('foo'); |
| 49 | + const emitter = worker([transformPath]); |
| 50 | + |
| 51 | + emitter.send({ files: [sourcePath] }); |
| 52 | + emitter.once('message', data => { |
| 53 | + expect(data.status).toBe('ok'); |
| 54 | + expect(data.msg).toBe(sourcePath); |
| 55 | + expect(getFileContent(sourcePath)).toBe('foo changed'); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('transforms files with tranformId as extension', done => { |
| 61 | + const worker = require('./Worker'); |
| 62 | + const configPath = createTempFileWith( |
| 63 | + ` |
| 64 | + const transfomer = (fileInfo) => fileInfo.source + " changed"; |
| 65 | + module.exports = { transforms: { "1.0.0": transfomer } }; |
| 66 | + `, |
| 67 | + 'codeshift1.config.js', |
| 68 | + '.js', |
| 69 | + ); |
| 70 | + const sourcePath = createTempFileWith('foo'); |
| 71 | + const emitter = worker([configPath + '@1.0.0']); |
| 72 | + |
| 73 | + emitter.send({ files: [sourcePath] }); |
| 74 | + emitter.once('message', data => { |
| 75 | + expect(data.status).toBe('ok'); |
| 76 | + expect(data.msg).toBe(sourcePath); |
| 77 | + expect(getFileContent(sourcePath)).toBe('foo changed'); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('transforms files with presetId as extension', done => { |
| 83 | + const worker = require('./Worker'); |
| 84 | + const configPath = createTempFileWith( |
| 85 | + ` |
| 86 | + const transfomer = (fileInfo) => fileInfo.source + " changed"; |
| 87 | + module.exports = { presets: { "my-preset": transfomer } }; |
| 88 | + `, |
| 89 | + 'codeshift2.config.js', |
| 90 | + '.js', |
| 91 | + ); |
| 92 | + const sourcePath = createTempFileWith('foo'); |
| 93 | + const emitter = worker([configPath + '#my-preset']); |
| 94 | + |
| 95 | + emitter.send({ files: [sourcePath] }); |
| 96 | + emitter.once('message', data => { |
| 97 | + expect(data.status).toBe('ok'); |
| 98 | + expect(data.msg).toBe(sourcePath); |
| 99 | + expect(getFileContent(sourcePath)).toBe('foo changed'); |
| 100 | + done(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('passes j as argument', done => { |
| 105 | + const worker = require('./Worker'); |
| 106 | + const transformPath = createTempFileWith( |
| 107 | + `module.exports = function (file, api) { |
| 108 | + return api.j(file.source).toSource() + ' changed'; |
| 109 | + }`, |
| 110 | + ); |
| 111 | + const sourcePath = createTempFileWith('const x = 10;'); |
| 112 | + |
| 113 | + const emitter = worker([transformPath]); |
| 114 | + emitter.send({ files: [sourcePath] }); |
| 115 | + emitter.once('message', data => { |
| 116 | + expect(data.status).toBe('ok'); |
| 117 | + expect(getFileContent(sourcePath)).toBe('const x = 10;' + ' changed'); |
| 118 | + done(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('custom parser', () => { |
| 123 | + function getTransformForParser(parser) { |
| 124 | + return createTempFileWith( |
| 125 | + `function transform(fileInfo, api) { |
| 126 | + api.jscodeshift(fileInfo.source); |
| 127 | + return "changed"; |
| 128 | + } |
| 129 | + ${parser ? `transform.parser = '${parser}';` : ''} |
| 130 | + module.exports = transform; |
| 131 | + `, |
| 132 | + ); |
| 133 | + } |
| 134 | + function getSourceFile() { |
| 135 | + // This code cannot be parsed by Babel v5 |
| 136 | + return createTempFileWith('const x = (a: Object, b: string): void => {}'); |
| 137 | + } |
| 138 | + |
| 139 | + it('errors if new flow type code is parsed with babel v5', done => { |
| 140 | + const worker = require('./Worker'); |
| 141 | + const transformPath = createTransformWith( |
| 142 | + 'api.jscodeshift(fileInfo.source); return "changed";', |
| 143 | + ); |
| 144 | + const sourcePath = getSourceFile(); |
| 145 | + const emitter = worker([transformPath]); |
| 146 | + |
| 147 | + emitter.send({ files: [sourcePath] }); |
| 148 | + emitter.once('message', data => { |
| 149 | + expect(data.status).toBe('error'); |
| 150 | + expect(data.msg).toMatch('SyntaxError'); |
| 151 | + done(); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + ['flow', 'babylon'].forEach(parser => { |
| 156 | + it(`uses ${parser} if configured as such`, done => { |
| 157 | + const worker = require('./Worker'); |
| 158 | + const transformPath = getTransformForParser(parser); |
| 159 | + const sourcePath = getSourceFile(); |
| 160 | + const emitter = worker([transformPath]); |
| 161 | + |
| 162 | + emitter.send({ files: [sourcePath] }); |
| 163 | + emitter.once('message', data => { |
| 164 | + expect(data.status).toBe('ok'); |
| 165 | + expect(getFileContent(sourcePath)).toBe('changed'); |
| 166 | + done(); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + ['babylon', 'flow', 'tsx'].forEach(parser => { |
| 172 | + it(`can parse JSX with ${parser}`, done => { |
| 173 | + const worker = require('./Worker'); |
| 174 | + const transformPath = getTransformForParser(parser); |
| 175 | + const sourcePath = createTempFileWith( |
| 176 | + 'var component = <div>{foobar}</div>;', |
| 177 | + ); |
| 178 | + const emitter = worker([transformPath]); |
| 179 | + |
| 180 | + emitter.send({ files: [sourcePath] }); |
| 181 | + emitter.once('message', data => { |
| 182 | + expect(data.status).toBe('ok'); |
| 183 | + expect(getFileContent(sourcePath)).toBe('changed'); |
| 184 | + done(); |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + it('can parse enums with flow', done => { |
| 190 | + const worker = require('./Worker'); |
| 191 | + const transformPath = getTransformForParser('flow'); |
| 192 | + const sourcePath = createTempFileWith('enum E {A, B}'); |
| 193 | + const emitter = worker([transformPath]); |
| 194 | + |
| 195 | + emitter.send({ files: [sourcePath] }); |
| 196 | + emitter.once('message', data => { |
| 197 | + expect(data.status).toBe('ok'); |
| 198 | + expect(getFileContent(sourcePath)).toBe('changed'); |
| 199 | + done(); |
| 200 | + }); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments