Skip to content

Commit

Permalink
Add bin links
Browse files Browse the repository at this point in the history
  • Loading branch information
krisselden committed Feb 23, 2022
1 parent e75e373 commit f8b97c7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 86 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ project.files; // => read or write the set of files further
// if you don't set this, a new temp dir will be made for you when you writeSync()
project.baseDir = 'some/root/';

project.writeSync();
await project.write();

// after writeSync(), you can read project.baseDir even if you didn't set it
expect(fs.existsSync(join(project.baseDir, 'index.js'))).to.eql(true);
Expand Down Expand Up @@ -57,7 +57,7 @@ let a = project.addDependency('a');
let b = a.addDependency('b');
let c = b.addDependency('c');

project.writeSync();
await project.write();
```

Which produces:
Expand Down Expand Up @@ -91,7 +91,7 @@ project.linkDependency('c', { baseDir: '/example' });
// this will follow node resolution rules to lookup "my-aliased-name" from "../elsewhere"
project.linkDependency('d', { baseDir: '/example', resolveName: 'my-aliased-name' });

project.writeSync();
await project.write();
```

Produces:
Expand All @@ -111,7 +111,7 @@ dependencies instead of copying them in as Projects:
```js
let project = Project.fromDir('./sample-project', { linkDeps: true });
project.files['extra.js'] = '// stuff';
project.write();
await project.write();
```

This will generate a new copy of sample-project, with symlinks to all its
Expand Down
24 changes: 21 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs = require('fs-extra');
import path = require('path');
import resolvePackagePath = require('resolve-package-path');
import CacheGroup = require('resolve-package-path/lib/cache-group');
import binLinks = require('bin-links');
import { PackageJson as BasePackageJson } from 'type-fest';
import { entries } from 'walk-sync';

Expand Down Expand Up @@ -223,7 +224,7 @@ export class Project {
get baseDir() {
if (!this._baseDir) {
throw new Error(
`this project has no baseDir yet. Either set one manually or call writeSync to have one chosen for you`
`this project has no baseDir yet. Either set one manually or call write to have one chosen for you`
);
}
return this._baseDir;
Expand Down Expand Up @@ -253,23 +254,40 @@ export class Project {
this.pkg.version = value;
}

async write() {
this.writeProject();
await this.binLinks();
}

/**
* @deprecated please use `await project.write()` instead.
*/
writeSync() {
console.warn('this method is deprecated, please use write instead');
this.writeProject();
}

private writeProject() {
this.autoBaseDir();
fixturify.writeSync(this.baseDir, this.files);
fs.outputJSONSync(path.join(this.baseDir, 'package.json'), this.pkgJSONWithDeps(), { spaces: 2 });
for (let dep of this.dependencyProjects()) {
dep.baseDir = path.join(this.baseDir, 'node_modules', dep.name);
dep.writeSync();
dep.writeProject();
}
for (let dep of this.devDependencyProjects()) {
dep.baseDir = path.join(this.baseDir, 'node_modules', dep.name);
dep.writeSync();
dep.writeProject();
}
for (let [name, { dir: target }] of this.dependencyLinks) {
this.writeLinkedPackage(name, target);
}
}

private async binLinks() {
await binLinks({ pkg: this.pkg, path: this.baseDir, top: false, global: false });
}

private writeLinkedPackage(name: string, target: string) {
let targetPkg = fs.readJsonSync(`${target}/package.json`);
let peers = new Set(Object.keys(targetPkg.peerDependencies ?? {}));
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"author": "Stefan Penner <stefan.penner@gmail.com>",
"license": "MIT",
"dependencies": {
"bin-links": "^3.0.0",
"fixturify": "^2.1.1",
"resolve-package-path": "^3.1.0",
"tmp": "^0.0.33",
"walk-sync": "^3.0.0",
"type-fest": "^2.3.2"
"type-fest": "^2.3.2",
"walk-sync": "^3.0.0"
},
"devDependencies": {
"@types/chai": "^4.2.18",
Expand Down
Loading

0 comments on commit f8b97c7

Please sign in to comment.