Skip to content

Commit

Permalink
First pass at respecting .forceignore
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuckJonas committed Oct 16, 2019
1 parent ca150c8 commit 9225647
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ EXAMPLES
$ sfdx force:source:clean -x manifest/package.xml --noprompt
```

_See code: [lib/commands/force/source/clean.js](https://github.com/ChuckJonas/force-source-clean/blob/v0.0.2/lib/commands/force/source/clean.js)_
_See code: [lib/commands/force/source/clean.js](https://github.com/ChuckJonas/force-source-clean/blob/v0.0.3/lib/commands/force/source/clean.js)_
<!-- commandsstop -->


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@salesforce/command": "^1.4.1",
"@salesforce/core": "^1.3.2",
"chalk": "^2.4.2",
"ignore": "^5.1.4",
"recursive-copy": "^2.0.10",
"rimraf": "^3.0.0",
"tslib": "^1"
Expand Down
39 changes: 29 additions & 10 deletions src/commands/force/source/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AnyJson } from '@salesforce/ts-types';
import * as chalk from 'chalk';
import { spawn, SpawnOptions } from 'child_process';
import * as fs from 'fs';
import ignore, { Ignore } from 'ignore';
import * as path from 'path';
import * as copy from 'recursive-copy';
import * as rimraf from 'rimraf';
Expand Down Expand Up @@ -63,11 +64,14 @@ export default class Org extends SfdxCommand {
});
});

const ignore = await getIgnore(this.project.getPath());
for (const sourcePath of sourcePaths) {

// backup
await copy(sourcePath, path.join(tempDir, sourcePath));
// mark
markContents(sourcePath);
markContents(sourcePath, ignore);

}

try {
Expand All @@ -84,7 +88,7 @@ export default class Org extends SfdxCommand {
this.ux.log('Restoring source from backup');
// restore
for (const sourcePath of sourcePaths) {
await copy(path.join(tempDir, sourcePath), sourcePath, {overwrite: true});
await copy(path.join(tempDir, sourcePath), sourcePath, { overwrite: true });
}
return;
}
Expand All @@ -98,6 +102,17 @@ export default class Org extends SfdxCommand {
}
}

async function getIgnore(projectRoot: string) {
const ig = ignore();
const ignorePath = path.join(projectRoot, '.forceignore');
if (fs.existsSync(ignorePath)) {
const file = await (await fs.promises.readFile(ignorePath)).toString();
ig.add(file);
}

return ig;
}

interface SpawnPromiseArgs {
cmd: string;
args: string[];
Expand Down Expand Up @@ -136,22 +151,26 @@ function spawnPromise({ cmd, args, options, onStdOut, onStdErr }: SpawnPromiseAr
});
}

function markContents(targetDir: string) {
function markContents(targetDir: string, ignore: Ignore) {
console.log(targetDir);
fs.readdir(targetDir, (err, files) => {
files.forEach((file, index) => {
fs.stat(path.join(targetDir, file), (err, stat) => {
if (err) {
return console.error(err);
}

const filePath = path.join(targetDir, file);
if (stat.isDirectory()) {
markContents(path.resolve(targetDir, file));
markContents(filePath, ignore);
} else {
fs.writeFile(path.join(targetDir, file), FILE_MARKER_CONTENTS, (err) => {
if (err) {
console.log(err);
}
});

if (!ignore.ignores(filePath)) {
fs.writeFile(filePath, FILE_MARKER_CONTENTS, (err) => {
if (err) {
console.log(err);
}
});
}
}
});
});
Expand Down

0 comments on commit 9225647

Please sign in to comment.