Skip to content

Commit

Permalink
feat(rule): move globbed files to a relative file path
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Jul 7, 2019
1 parent 9751215 commit bdb44ca
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
100 changes: 97 additions & 3 deletions src/move-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ describe('when moving a glob pattern of multiple files', () => {
});
});

describe('when target is a relative path', () => {
describe('when target is a relative path to a directory', () => {
const target = './nested';
const changes = [
{
filePath: ['/fake/dir/file-a.js', '/fake/dir/nested/file-a.js'],
Expand Down Expand Up @@ -336,14 +337,107 @@ describe('when moving a glob pattern of multiple files', () => {
mock.restore();
});

it('moves each file to the target relative to itself', (done) => {
it('moves each file to the target directory relative to itself', (done) => {
ruleTester.run('move-files', rule, {
valid: [],
invalid: changes.map(({ contents, filePath }) => ({
code: contents[0],
errors: [{ message: `${filePath[0]} has moved to ${filePath[1]}` }],
filename: filePath[0],
options: [{ files: { [source]: './nested' } }],
options: [{ files: { [source]: target } }],
output: contents[1]
}))
});

process.nextTick(() => {
// ESLint's RuleTester does not write to Disk, but we can assert that:
// 1. The File in its old location had its imports updated (via the
// `output` property above).
// 2. A file was written in the new location containing the *old*
// contents, in reality this would be the new contents with the
// updated imports.
changes.forEach(({ contents, filePath }) => {
expect(existsSync(filePath[0])).toEqual(false);
expect(readTextFileSync(filePath[1])).toEqual(contents[0]);
});
done();
});
});
});

describe('when target is a relative path to a file', () => {
const target = './nested/new-file.js';
const changes = [
{
filePath: ['/fake/dir/file-a.js', '/fake/dir/nested/new-file.js'],
contents: [
`
import { b } from './b/file-b';
import { c } from './b/c/file-c';
export const a = 1;
`,
`
import { b } from '../b/nested/new-file';
import { c } from '../b/c/nested/new-file';
export const a = 1;
`
]
},
{
filePath: ['/fake/dir/b/file-b.js', '/fake/dir/b/nested/new-file.js'],
contents: [
`
import { a } from '../file-a';
import { c } from './c/file-c';
export const b = 2;
`,
`
import { a } from '../../nested/new-file';
import { c } from '../c/nested/new-file';
export const b = 2;
`
]
},
{
filePath: [
'/fake/dir/b/c/file-c.js',
'/fake/dir/b/c/nested/new-file.js'
],
contents: [
`
import { a } from '../../file-a';
import { b } from '../file-b';
export const c = 3;
`,
`
import { a } from '../../../nested/new-file';
import { b } from '../../nested/new-file';
export const c = 3;
`
]
}
];

beforeEach(() => {
mock({
[changes[0].filePath[0]]: changes[0].contents[0],
[changes[1].filePath[0]]: changes[1].contents[0],
[changes[2].filePath[0]]: changes[2].contents[0]
});
});

afterEach(() => {
mock.restore();
});

it('moves each file to the target location relative to itself', (done) => {
ruleTester.run('move-files', rule, {
valid: [],
invalid: changes.map(({ contents, filePath }) => ({
code: contents[0],
errors: [{ message: `${filePath[0]} has moved to ${filePath[1]}` }],
filename: filePath[0],
options: [{ files: { [source]: target } }],
output: contents[1]
}))
});
Expand Down
12 changes: 6 additions & 6 deletions src/move-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ const rule: Rule.RuleModule = {
const sourceIsGlob = glob.hasMagic(source);
const targetIsGlob = glob.hasMagic(target);
const targetIsRelativePath = !targetIsGlob && target.startsWith('.');
const targetIsDirectoryLike = !basename(target).includes('.');

if (sourceIsGlob && targetIsGlob) {
throw new Error(ERROR_MULTIPLE_TARGETS(source, target));
}

if (sourceIsGlob && targetIsRelativePath) {
return glob.sync(source).forEach((sourceFile) => {
files[sourceFile] = join(
resolve(dirname(sourceFile), target),
basename(sourceFile)
);
files[sourceFile] = targetIsDirectoryLike
? join(resolve(dirname(sourceFile), target), basename(sourceFile))
: resolve(dirname(sourceFile), target);
});
}

Expand All @@ -81,8 +81,8 @@ const rule: Rule.RuleModule = {
const withoutFileExtension = (filePath: string) =>
filePath.replace(/\.[^.]+$/, '');

const getNewModuleId = (xx: string) =>
withLeadingDot(withoutFileExtension(xx));
const getNewModuleId = (filePath: string) =>
withLeadingDot(withoutFileExtension(filePath));

const withFileExtension = (filePath: string) => {
try {
Expand Down

0 comments on commit bdb44ca

Please sign in to comment.