Skip to content

Commit

Permalink
Merge pull request #122 from claytonrcarter/add-link-force
Browse files Browse the repository at this point in the history
feat(link): add --force flag
  • Loading branch information
DeeDeeG authored Feb 1, 2024
2 parents a570e91 + 2f8abfe commit 241d794
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
51 changes: 51 additions & 0 deletions spec/link-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,57 @@ describe('apm link/unlink', () => {
});
});

describe('when linking a path that already exists', () => {
it('logs an error and exits', () => {
const atomHome = temp.mkdirSync('apm-home-dir-');
process.env.ATOM_HOME = atomHome;
const packageToLink = temp.mkdirSync('a-package-');

const existingPackageDir = path.join(atomHome, 'packages', path.basename(packageToLink));
fs.mkdirSync(existingPackageDir, {recursive: true});
fs.writeFileSync(path.join(existingPackageDir, 'foo.txt'), '');

fs.writeFileSync(path.join(packageToLink, 'bar.txt'), '');
process.chdir(packageToLink);
const callback = jasmine.createSpy('callback');

apm.run(['link'], callback);
waitsFor('command to complete', () => callback.callCount > 0);

runs(() => {
expect(console.error.mostRecentCall.args[0].length).toBeGreaterThan(0);
expect(callback.mostRecentCall.args[0]).not.toBeUndefined();

expect(fs.existsSync(path.join(existingPackageDir, 'foo.txt'))).toBeTruthy();
expect(fs.existsSync(path.join(existingPackageDir, 'bar.txt'))).toBeFalsy();
});
});

it('overwrites the path if the --force flag is passed', () => {
const atomHome = temp.mkdirSync('apm-home-dir-');
process.env.ATOM_HOME = atomHome;
const packageToLink = temp.mkdirSync('a-package-');

const existingPackageDir = path.join(atomHome, 'packages', path.basename(packageToLink));
fs.mkdirSync(existingPackageDir, {recursive: true});
fs.writeFileSync(path.join(existingPackageDir, 'foo.txt'), '');

fs.writeFileSync(path.join(packageToLink, 'bar.txt'), '');
process.chdir(packageToLink);
const callback = jasmine.createSpy('callback');

apm.run(['link', '--force'], callback);
waitsFor('command to complete', () => callback.callCount > 0);

runs(() => {
expect(fs.existsSync(existingPackageDir)).toBeTruthy();
expect(fs.realpathSync(existingPackageDir)).toBe(fs.realpathSync(packageToLink));
expect(fs.existsSync(path.join(existingPackageDir, 'foo.txt'))).toBeFalsy();
expect(fs.existsSync(path.join(existingPackageDir, 'bar.txt'))).toBeTruthy();
});
});
});

describe('when the hard flag is true', () => {
it('unlinks the package from both $ATOM_HOME/packages and $ATOM_HOME/dev/packages', () => {
const atomHome = temp.mkdirSync('apm-home-dir-');
Expand Down
4 changes: 4 additions & 0 deletions src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Run \`ppm links\` to view all the currently linked packages.\
`
);
options.alias('h', 'help').describe('help', 'Print this usage message');
options.alias('f', 'force').boolean('force').describe('force', 'Remove the target path before linking');
return options.alias('d', 'dev').boolean('dev').describe('dev', 'Link to ~/.pulsar/dev/packages');
}

Expand All @@ -50,6 +51,9 @@ Run \`ppm links\` to view all the currently linked packages.\

try {
if (fs.isSymbolicLinkSync(targetPath)) { fs.unlinkSync(targetPath); }
else if (options.argv.force && fs.existsSync(targetPath)) {
fs.rmSync(targetPath, { recursive: true, force: true });
}
fs.makeTreeSync(path.dirname(targetPath));
fs.symlinkSync(linkPath, targetPath, 'junction');
console.log(`${targetPath} -> ${linkPath}`);
Expand Down

0 comments on commit 241d794

Please sign in to comment.