Skip to content

Commit

Permalink
fix: change "clean" to remove all types of .original files (#117)
Browse files Browse the repository at this point in the history
This is a little unsafe in that it's more likely to get false positives, but
should still be safe since people should be running this within a git repo and
because it skips node_modules.
  • Loading branch information
alangpierce authored May 7, 2017
1 parent 60194b1 commit 21c4f8c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ bulk-decaffeinate supports a number of commands:
is in the hash fragment and not a regular query param, your code is never sent
to the server.
* `convert` actually converts the files from CofeeScript to JavaScript.
* `clean` deletes all .original.coffee files in the current directory or any of
its subdirectories.
* `clean` deletes all files with ".original" in the name in the current
directory or any of its subdirectories.
* `land` packages multiple commits into a merge commit based on an remote branch
(`origin/master` by default). Splitting the decaffeinate work into separate
commits allows git to properly track file history, but it can create added
Expand Down
8 changes: 5 additions & 3 deletions src/clean.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { unlink } from 'mz/fs';
import { basename } from 'path';

import getFilesUnderPath from './util/getFilesUnderPath';

export default async function clean() {
let filesToDelete = await getFilesUnderPath('.', p => p.endsWith('.original.coffee'));
let filesToDelete = await getFilesUnderPath('.', p => basename(p).includes('.original'));
if (filesToDelete.length === 0) {
console.log('No .original.coffee files were found.');
console.log('No .original files were found.');
return;
}
for (let path of filesToDelete) {
console.log(`Deleting ${path}`);
await unlink(path);
}
console.log('Done deleting .original.coffee files.');
console.log('Done deleting .original files.');
}
19 changes: 9 additions & 10 deletions test/convert-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-env mocha */
import assert from 'assert';
import { exec } from 'mz/child_process';
import { exists, readFile, writeFile } from 'mz/fs';
import { readFile, writeFile } from 'mz/fs';

import {
assertExists,
assertNotExists,
assertFileContents,
assertIncludes,
initGitRepo,
Expand Down Expand Up @@ -315,18 +316,16 @@ console.log(x);
});

it('generates backup files that are removed by clean', async function() {
await runWithTemplateDir('simple-success', async function() {
await runWithTemplateDir('backup-files', async function() {
await initGitRepo();
await runCli('convert');
assert(
await exists('./A.original.coffee'),
'Expected a backup file to be created.'
);
await assertExists('./A.original.coffee');
await assertExists('./B.original.coffee.md');
await assertExists('./C.original');
await runCli('clean');
assert(
!await exists('./A.original.coffee'),
'Expected the "clean" command to get rid of the backup file.'
);
await assertNotExists('./A.original.coffee');
await assertNotExists('./B.original.coffee.md');
await assertNotExists('./C.original');
});
});

Expand Down
1 change: 1 addition & 0 deletions test/examples/backup-files/A.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log 'A'
3 changes: 3 additions & 0 deletions test/examples/backup-files/B.coffee.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Testing

console.log 'B'
3 changes: 3 additions & 0 deletions test/examples/backup-files/C
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env coffee

console.log 'C'
4 changes: 4 additions & 0 deletions test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export async function assertExists(path) {
assert(await exists(path), `Expected ${path} to exist.`);
}

export async function assertNotExists(path) {
assert(!await exists(path), `Expected ${path} to not exist.`);
}

export async function assertFileContents(path, expectedContents) {
let contents = (await readFile(path)).toString();
assert.equal(contents, expectedContents);
Expand Down

0 comments on commit 21c4f8c

Please sign in to comment.